-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathconfig.go
203 lines (162 loc) · 4.17 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package main
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"os"
"strings"
"github.com/gorilla/handlers"
)
type sitesCfg []Site
const (
kConfigName = "S3PROXY_CONFIG"
kAWSKeyName = "S3PROXY_AWS_KEY"
kAWSSecretName = "S3PROXY_AWS_SECRET"
kAWSRegionName = "S3PROXY_AWS_REGION"
kAWSBucketName = "S3PROXY_AWS_BUCKET"
kUsersName = "S3PROXY_USERS"
kCORSKeyName = "S3PROXY_OPTION_CORS"
kGzipKeyName = "S3PROXY_OPTION_GZIP"
kWebsiteKeyName = "S3PROXY_OPTION_WEBSITE"
kPrefixKeyName = "S3PROXY_OPTION_PREFIX"
kForceSSLKeyName = "S3PROXY_OPTION_FORCE_SSL"
kProxiedKeyName = "S3PROXY_OPTION_PROXIED"
)
func ConfiguredProxyHandler() (http.Handler, error) {
_, ok := os.LookupEnv(kConfigName)
if ok {
return createMulti()
} else {
return createSingle()
}
}
func createMulti() (http.Handler, error) {
var cfg sitesCfg
cfgJson := os.Getenv(kConfigName)
err := json.Unmarshal([]byte(cfgJson), &cfg)
if err != nil {
return nil, err
}
if len(cfg) == 0 {
return nil, errors.New("Must specify one or more configurations")
}
handler := NewHostDispatchingHandler()
for i, site := range cfg {
err = site.validateWithHost()
if err != nil {
msg := fmt.Sprintf("%v in configuration at position %d", err, i)
return nil, errors.New(msg)
}
handler.HandleHost(site.Host, createSiteHandler(site))
}
return handler, nil
}
func createSingle() (http.Handler, error) {
users, err := parseUsers(os.Getenv(kUsersName))
if err != nil {
return nil, err
}
opts := Options{
CORS: os.Getenv(kCORSKeyName) == "true",
Gzip: os.Getenv(kGzipKeyName) == "true",
Website: os.Getenv(kWebsiteKeyName) == "true",
Prefix: os.Getenv(kPrefixKeyName),
ForceSSL: os.Getenv(kForceSSLKeyName) == "true",
Proxied: os.Getenv(kProxiedKeyName) == "true",
}
s := Site{
AWSKey: os.Getenv(kAWSKeyName),
AWSSecret: os.Getenv(kAWSSecretName),
AWSRegion: os.Getenv(kAWSRegionName),
AWSBucket: os.Getenv(kAWSBucketName),
Users: users,
Options: opts,
}
err = s.validate()
if err != nil {
return nil, err
} else {
return createSiteHandler(s), nil
}
}
func createSiteHandler(s Site) http.Handler {
var handler http.Handler
proxy := NewS3Proxy(s.AWSKey, s.AWSSecret, s.AWSRegion, s.AWSBucket)
handler = NewProxyHandler(proxy, s.Options.Prefix)
if s.Options.Website {
cfg, err := proxy.GetWebsiteConfig()
if err != nil {
fmt.Printf("warning: site for bucket %s configured with "+
"website option but received error when retrieving "+
"website config\n\t%v", s.AWSBucket, err)
} else {
handler = NewWebsiteHandler(handler, cfg)
}
}
if s.Options.CORS {
handler = corsHandler(handler)
}
if s.Options.Gzip {
handler = handlers.CompressHandler(handler)
}
if len(s.Users) > 0 {
handler = NewBasicAuthHandler(s.Users, handler)
} else {
fmt.Printf("warning: site for bucket %s has no configured users\n", s.AWSBucket)
}
if s.Options.ForceSSL {
handler = NewSSLRedirectHandler(handler)
}
if s.Options.Proxied {
handler = handlers.ProxyHeaders(handler)
}
return handler
}
func corsHandler(next http.Handler) http.Handler {
return handlers.CORS(
handlers.AllowedHeaders([]string{"*"}),
handlers.AllowedOrigins([]string{"*"}),
handlers.AllowedMethods([]string{"HEAD", "GET", "OPTIONS"}),
)(next)
}
func parseUsers(us string) ([]User, error) {
if us == "" {
return []User{}, nil
}
pairs := strings.Split(us, ",")
users := make([]User, len(pairs))
for i, p := range pairs {
parts := strings.Split(p, ":")
if len(parts) != 2 {
msg := fmt.Sprintf("Failed to parse user %s at position %d", p, i)
return nil, errors.New(msg)
}
users[i] = User{
Name: parts[0],
Password: parts[1],
}
}
return users, nil
}
func (s Site) validateWithHost() error {
if s.Host == "" {
return errors.New("Host not specified")
}
return s.validate()
}
func (s Site) validate() error {
if s.AWSKey == "" {
return errors.New("AWS Key not specified")
}
if s.AWSSecret == "" {
return errors.New("AWS Secret not specified")
}
if s.AWSRegion == "" {
return errors.New("AWS Region not specified")
}
if s.AWSBucket == "" {
return errors.New("AWS Bucket not specified")
}
return nil
}