forked from moriyoshi/s3-sftp-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
213 lines (183 loc) · 6.41 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
204
205
206
207
208
209
210
211
212
213
package main
import (
"fmt"
"io/ioutil"
"net/url"
"os"
"github.com/BurntSushi/toml"
"github.com/pkg/errors"
)
var (
minReaderLookbackBufferSize = 1048576
minReaderMinChunkSize = 262144
minListerLookbackBufferSize = 100
vTrue = true
)
type URL struct {
*url.URL
}
func (u *URL) UnmarshalText(text []byte) (err error) {
u.URL, err = url.Parse(string(text))
return
}
type AWSCredentialsConfig struct {
AWSAccessKeyID string `toml:"aws_access_key_id"`
AWSSecretAccessKey string `toml:"aws_secret_access_key"`
}
type S3BucketConfig struct {
Profile string `toml:"profile"`
Credentials *AWSCredentialsConfig `toml:"credentials"`
Region string `toml:"region"`
Bucket string `toml:"bucket"`
KeyPrefix string `toml:"key_prefix"`
BucketUrl *URL `toml:"bucket_url"`
Auth string `toml:"auth"`
MaxObjectSize *int64 `toml:"max_object_size"`
Readable *bool `toml:"readble"`
Writable *bool `toml:"writable"`
Listable *bool `toml:"listable"`
ServerSideEncryption ServerSideEncryptionType `toml:"server_side_encryption"`
SSECustomerKey string `toml:"sse_customer_key"`
SSEKMSKeyId string `toml:"sse_kms_key_id"`
KeyboardInteractiveAuthEnabled bool `toml:"keyboard_interactive_auth"`
}
type AuthUser struct {
Password string `toml:"password"`
PublicKeys string `toml:"public_keys"`
PublicKeyFile string `toml:"public_key_file"`
}
type AuthConfig struct {
Type string `toml:"type"`
UserDBFile string `toml:"user_db_file"`
Users map[string]AuthUser `toml:"users"`
}
type S3SFTPProxyConfig struct {
Bind string `toml:"bind"`
HostKeyFile string `toml:"host_key_file"`
Banner string `toml:"banner"`
ReaderLookbackBufferSize *int `toml:"reader_lookback_buffer_size"`
ReaderMinChunkSize *int `toml:"reader_min_chunk_size"`
ListerLookbackBufferSize *int `toml:"lister_lookback_buffer_size"`
Buckets map[string]*S3BucketConfig `toml:"buckets"`
AuthConfigs map[string]*AuthConfig `toml:"auth"`
}
func validateAndFixupBucketConfig(bCfg *S3BucketConfig) error {
if bCfg.Profile != "" {
if bCfg.Credentials != nil {
return fmt.Errorf("no credentials may be specified if profile is given")
}
}
if bCfg.BucketUrl != nil {
if bCfg.Bucket != "" {
return fmt.Errorf("bucket may not be specified if bucket_url is given")
}
if bCfg.KeyPrefix != "" {
return fmt.Errorf("root path may not be specified if bucket_url is given")
}
if bCfg.BucketUrl.Host == "" {
return fmt.Errorf("bucket name is empty")
}
if bCfg.BucketUrl.Scheme != "s3" {
return fmt.Errorf("bucket URL scheme must be \"s3\"")
}
bCfg.Bucket = bCfg.BucketUrl.Host
bCfg.KeyPrefix = bCfg.BucketUrl.Path
} else {
if bCfg.Bucket == "" {
return fmt.Errorf("bucket name is empty")
}
}
if bCfg.Auth == "" {
return fmt.Errorf("auth is not specified")
}
if bCfg.Readable == nil {
bCfg.Readable = &vTrue
}
if bCfg.Writable == nil {
bCfg.Writable = &vTrue
}
if bCfg.Listable == nil {
bCfg.Listable = &vTrue
}
return nil
}
func validateAndFixupAuthConfigInplace(aCfg *AuthConfig) error {
if aCfg.UserDBFile != "" {
return fmt.Errorf(`user_db_file may not be specified when auth type is "inplace"`)
}
if aCfg.Users == nil || len(aCfg.Users) == 0 {
fmt.Printf("%#v\n", aCfg.Users)
return fmt.Errorf(`no "users" present`)
}
return nil
}
func validateAndFixupAuthConfig(aCfg *AuthConfig) error {
switch aCfg.Type {
case "inplace":
return validateAndFixupAuthConfigInplace(aCfg)
default:
return fmt.Errorf("unknown auth type: %s", aCfg.Type)
}
}
func ReadConfig(tomlStr string) (*S3SFTPProxyConfig, error) {
cfg := &S3SFTPProxyConfig{
Buckets: map[string]*S3BucketConfig{},
AuthConfigs: map[string]*AuthConfig{},
}
_, err := toml.Decode(tomlStr, cfg)
if err != nil {
return nil, err
}
if len(cfg.Buckets) == 0 {
return nil, fmt.Errorf("no bucket configs are present")
}
if len(cfg.AuthConfigs) == 0 {
return nil, fmt.Errorf("no auth configs are present")
}
if cfg.HostKeyFile == "" {
return nil, fmt.Errorf("no host key file is specified")
}
if len(cfg.Banner) > 0 && cfg.Banner[len(cfg.Banner)-1] != '\n' {
cfg.Banner += "\n"
}
if cfg.ReaderLookbackBufferSize == nil {
cfg.ReaderLookbackBufferSize = &minReaderLookbackBufferSize
} else if *cfg.ReaderLookbackBufferSize < minReaderLookbackBufferSize {
return nil, fmt.Errorf("reader_lookback_buffer_size must be equal to or greater than %d", minReaderMinChunkSize)
}
if cfg.ReaderMinChunkSize == nil {
cfg.ReaderMinChunkSize = &minReaderMinChunkSize
} else if *cfg.ReaderMinChunkSize < minReaderMinChunkSize {
return nil, fmt.Errorf("reader_min_chunk_size must be equal to or greater than %d", minReaderMinChunkSize)
}
if cfg.ListerLookbackBufferSize == nil {
cfg.ListerLookbackBufferSize = &minListerLookbackBufferSize
} else if *cfg.ListerLookbackBufferSize < minListerLookbackBufferSize {
return nil, fmt.Errorf("lister_lookback_buffer_size must be equal to or greater than %d", minListerLookbackBufferSize)
}
for name, bCfg := range cfg.Buckets {
err := validateAndFixupBucketConfig(bCfg)
if err != nil {
return nil, errors.Wrapf(err, `bucket config "%s"`, name)
}
}
for name, aCfg := range cfg.AuthConfigs {
err := validateAndFixupAuthConfig(aCfg)
if err != nil {
return nil, errors.Wrapf(err, `auth config "%s"`, name)
}
}
return cfg, err
}
func ReadConfigFromFile(tomlFile string) (*S3SFTPProxyConfig, error) {
tomlBytes, err := ioutil.ReadFile(tomlFile)
if err != nil {
return nil, errors.Wrapf(err, "failed to open %s", tomlFile)
}
tomlBytes = []byte(os.ExpandEnv(string(tomlBytes)))
cfg, err := ReadConfig(string(tomlBytes))
if err != nil {
return nil, errors.Wrapf(err, "failed to parse %s", tomlFile)
}
return cfg, nil
}