Skip to content

Commit 2e11186

Browse files
committed
Add CORS options & bump up docker's base image
1 parent 0c4cd39 commit 2e11186

File tree

9 files changed

+55
-16
lines changed

9 files changed

+55
-16
lines changed

README-ja.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ BASIC_AUTH_USER | Basic 認証をかけるなら、その `ユーザ
2727
BASIC_AUTH_PASS | Basic 認証をかけるなら、その `パスワード` | | -
2828
SSL_CERT_PATH | TLS を有効にしたいなら、その `cert.pem` へのパス | | -
2929
SSL_KEY_PATH | TLS を有効にしたいなら、その `key.pem` へのパス | | -
30+
CORS_ALLOW_ORIGIN | CORS を有効にしたいなら、リソースへのアクセスを許可する URI | | -
31+
CORS_ALLOW_METHODS | CORS を有効にしたいなら、許可する [HTTP request methods](https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html)のカンマ区切りのリスト | | -
32+
CORS_ALLOW_HEADERS | CORS を有効にしたいなら、サポートするヘッダーのカンマ区切りのリスト | | -
33+
CORS_MAX_AGE | CORS における preflight リクエスト結果のキャッシュ上限時間(秒) | | 600
3034
APP_PORT | このサービスが待機する `ポート番号` | | 80
3135
ACCESS_LOG | 標準出力へアクセスログを送る | | false
3236
STRIP_PATH | 指定した Prefix を S3 のパスから削除 | | -
@@ -43,6 +47,10 @@ STRIP_PATH | 指定した Prefix を S3 のパスから削除
4347

4448
`docker run -d -p 8080:80 -e AWS_REGION -e AWS_S3_BUCKET -e SSL_CERT_PATH -e SSL_KEY_PATH pottava/s3-proxy`
4549

50+
* CORS を有効にしたいなら:
51+
52+
`docker run -d -p 8080:80 -e PROXY_URL -e CORS_ALLOW_ORIGIN -e CORS_ALLOW_METHODS -e CORS_ALLOW_HEADERS -e CORS_MAX_AGE pottava/s3-proxy`
53+
4654
* docker-compose.yml として使うなら:
4755

4856
```

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ BASIC_AUTH_USER | User for basic authentication. |
3232
BASIC_AUTH_PASS | Password for basic authentication. | | -
3333
SSL_CERT_PATH | TLS: cert.pem file path. | | -
3434
SSL_KEY_PATH | TLS: key.pem file path. | | -
35+
CORS_ALLOW_ORIGIN | CORS: a URI that may access the resource. | | -
36+
CORS_ALLOW_METHODS | CORS: Comma-delimited list of the allowed [HTTP request methods](https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html). | | -
37+
CORS_ALLOW_HEADERS | CORS: Comma-delimited list of the supported request headers. | | -
38+
CORS_MAX_AGE | CORS: Maximum number of seconds the results of a preflight request can be cached. | | 600
3539
APP_PORT | The port number to be assigned for listening. | | 80
3640
ACCESS_LOG | Send access logs to /dev/stdout. | | false
3741
STRIP_PATH | Strip path prefix. | | -
@@ -48,6 +52,10 @@ STRIP_PATH | Strip path prefix. |
4852

4953
`docker run -d -p 8080:80 -e AWS_REGION -e AWS_S3_BUCKET -e SSL_CERT_PATH -e SSL_KEY_PATH pottava/s3-proxy`
5054

55+
* with CORS:
56+
57+
`docker run -d -p 8080:80 -e PROXY_URL -e CORS_ALLOW_ORIGIN -e CORS_ALLOW_METHODS -e CORS_ALLOW_HEADERS -e CORS_MAX_AGE pottava/s3-proxy`
58+
5159
* with docker-compose.yml:
5260

5361
```

circle.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ dependencies:
1010
pre:
1111
# Prepare directories.
1212
- mkdir -p ~/cache "$PRIVATE_GOPATH/src/$IMPORT_PATH"
13-
# Install go 1.7
13+
# Install go 1.8
1414
- sudo rm -rf /usr/local/go
15-
- cd ~/cache && if [ ! -e go1.7.5.linux-amd64.tar.gz ]; then wget https://storage.googleapis.com/golang/go1.7.5.linux-amd64.tar.gz; fi
16-
- cd ~/cache && sudo tar -C /usr/local -xzf go1.7.5.linux-amd64.tar.gz
15+
- cd ~/cache && if [ ! -e go1.8.linux-amd64.tar.gz ]; then wget https://storage.googleapis.com/golang/go1.8.linux-amd64.tar.gz; fi
16+
- cd ~/cache && sudo tar -C /usr/local -xzf go1.8.linux-amd64.tar.gz
1717
- go version
1818
# Install test utils
19-
- go get github.com/golang/dep
19+
- go get github.com/golang/dep/...
2020
- go get github.com/golang/lint/golint
2121
post:
2222
- >

dev/Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
FROM pottava/golang:1.8-builder
2+
RUN apk add --no-cache ca-certificates

dev/docker-compose-gox.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# BUILD_VERSION=v0.1.0 BUILD_DATE=`date +%FT%T%z` docker-compose -f development/docker-compose-gox.yml run --rm build
1+
# BUILD_VERSION=v0.1.0 BUILD_DATE=`date +%FT%T%z` docker-compose -f dev/docker-compose-gox.yml run --rm build
22

33
build:
44
image: pottava/gox:go1.8

docker-compose.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ version: "2"
22

33
services:
44
app:
5-
image: pottava/golang:1.8
5+
build: dev/
6+
image: aws-s3-proxy:dev
67
command: go run /go/src/github.com/pottava/aws-s3-proxy/main.go
78
ports:
89
- 8080:80
@@ -21,5 +22,9 @@ services:
2122
- BASIC_AUTH_PASS
2223
- SSL_CERT_PATH
2324
- SSL_KEY_PATH
25+
- CORS_ALLOW_ORIGIN
26+
- CORS_ALLOW_METHODS
27+
- CORS_ALLOW_HEADERS
28+
- CORS_MAX_AGE
2429
- ACCESS_LOG=true
2530
container_name: app

lock.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
{
55
"name": "github.com/aws/aws-sdk-go",
66
"branch": "master",
7-
"revision": "830984cdef47f8ac0d4883012367bf75e5ccac2c",
7+
"revision": "e43e7ed87a3584fd820402855e7ff990fb10239f",
88
"packages": [
99
"aws",
1010
"aws/session",

main.go

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ type config struct {
3131
sslCert string // SSL_CERT_PATH
3232
sslKey string // SSL_KEY_PATH
3333
stripPath string // STRIP_PATH
34+
corsAllowOrigin string // CORS_ALLOW_ORIGIN
35+
corsAllowMethods string // CORS_ALLOW_METHODS
36+
corsAllowHeaders string // CORS_ALLOW_HEADERS
37+
corsMaxAge int64 // CORS_MAX_AGE
3438
}
3539

3640
type symlink struct {
@@ -66,12 +70,6 @@ func main() {
6670
}
6771

6872
func configFromEnvironmentVariables() *config {
69-
if len(os.Getenv("AWS_ACCESS_KEY_ID")) == 0 {
70-
log.Print("Not defined environment variable: AWS_ACCESS_KEY_ID")
71-
}
72-
if len(os.Getenv("AWS_SECRET_ACCESS_KEY")) == 0 {
73-
log.Print("Not defined environment variable: AWS_SECRET_ACCESS_KEY")
74-
}
7573
if len(os.Getenv("AWS_S3_BUCKET")) == 0 {
7674
log.Fatal("Missing required environment variable: AWS_S3_BUCKET")
7775
}
@@ -87,6 +85,10 @@ func configFromEnvironmentVariables() *config {
8785
if b, err := strconv.ParseBool(os.Getenv("ACCESS_LOG")); err == nil {
8886
accessLog = b
8987
}
88+
corsMaxAge := int64(600)
89+
if i, err := strconv.ParseInt(os.Getenv("CORS_MAX_AGE"), 10, 64); err == nil {
90+
corsMaxAge = i
91+
}
9092
conf := &config{
9193
awsRegion: region,
9294
s3Bucket: os.Getenv("AWS_S3_BUCKET"),
@@ -100,6 +102,10 @@ func configFromEnvironmentVariables() *config {
100102
sslCert: os.Getenv("SSL_CERT_PATH"),
101103
sslKey: os.Getenv("SSL_KEY_PATH"),
102104
stripPath: os.Getenv("STRIP_PATH"),
105+
corsAllowOrigin: os.Getenv("CORS_ALLOW_ORIGIN"),
106+
corsAllowMethods: os.Getenv("CORS_ALLOW_METHODS"),
107+
corsAllowHeaders: os.Getenv("CORS_ALLOW_HEADERS"),
108+
corsMaxAge: corsMaxAge,
103109
}
104110
// Proxy
105111
log.Printf("[config] Proxy to %v", conf.s3Bucket)
@@ -113,6 +119,10 @@ func configFromEnvironmentVariables() *config {
113119
if (len(conf.basicAuthUser) > 0) && (len(conf.basicAuthPass) > 0) {
114120
log.Printf("[config] Basic authentication: %s", conf.basicAuthUser)
115121
}
122+
// CORS
123+
if (len(conf.corsAllowOrigin) > 0) && (conf.corsMaxAge > 0) {
124+
log.Printf("[config] CORS enabled: %s", conf.corsAllowOrigin)
125+
}
116126
return conf
117127
}
118128

@@ -128,6 +138,12 @@ func (r *custom) WriteHeader(status int) {
128138

129139
func wrapper(f func(w http.ResponseWriter, r *http.Request)) http.Handler {
130140
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
141+
if (len(c.corsAllowOrigin) > 0) && (len(c.corsAllowMethods) > 0) && (len(c.corsAllowHeaders) > 0) && (c.corsMaxAge > 0) {
142+
w.Header().Set("Access-Control-Allow-Origin", c.corsAllowOrigin)
143+
w.Header().Set("Access-Control-Allow-Methods", c.corsAllowMethods)
144+
w.Header().Set("Access-Control-Allow-Headers", c.corsAllowHeaders)
145+
w.Header().Set("Access-Control-Max-Age", strconv.FormatInt(c.corsMaxAge, 10))
146+
}
131147
if (len(c.basicAuthUser) > 0) && (len(c.basicAuthPass) > 0) && !auth(r) {
132148
w.Header().Set("WWW-Authenticate", `Basic realm="REALM"`)
133149
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)

prod/Dockerfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ RUN apk add --no-cache ca-certificates
1212
RUN apk --no-cache add --virtual build-dependencies bash gcc musl-dev openssl go git \
1313

1414
# Install go 1.8
15-
&& GOLANG_VERSION=1.8rc3 \
15+
&& GOLANG_VERSION=1.8 \
1616
&& GOLANG_SRC_URL=https://golang.org/dl/go$GOLANG_VERSION.src.tar.gz \
17-
&& GOLANG_SRC_SHA256=38b1c1738f111f7bccdd372efca2aa98a7bad1ca2cb21767ba69f34ae007499c \
17+
&& GOLANG_SRC_SHA256=406865f587b44be7092f206d73fc1de252600b79b3cacc587b74b5ef5c623596 \
1818
&& export GOROOT_BOOTSTRAP="$(go env GOROOT)" \
1919
&& wget -q "$GOLANG_SRC_URL" -O golang.tar.gz \
2020
&& echo "$GOLANG_SRC_SHA256 golang.tar.gz" | sha256sum -c - \
@@ -32,7 +32,7 @@ RUN apk --no-cache add --virtual build-dependencies bash gcc musl-dev openssl go
3232
&& mv /go/bin/aws-s3-proxy /usr/bin \
3333

3434
# Clean up
35-
&& apk del --purge build-dependencies \
35+
&& apk del --purge -r build-dependencies \
3636
&& rm -rf /usr/local/go /usr/lib/go /go /golang.tar.gz /*.patch
3737

3838
EXPOSE 80

0 commit comments

Comments
 (0)