-
Notifications
You must be signed in to change notification settings - Fork 19
/
main.go
451 lines (373 loc) · 11.7 KB
/
main.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package main
import (
"bufio"
"bytes"
"context"
"crypto/rand"
"fmt"
"net"
"os"
"path/filepath"
"strings"
"time"
"github.com/minio/cli"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/zeebo/errs"
"go.uber.org/zap"
"storj.io/common/base58"
"storj.io/common/cfgstruct"
"storj.io/common/fpath"
"storj.io/common/process"
"storj.io/common/process/eventkitbq"
"storj.io/common/version"
"storj.io/gateway/internal/wizard"
"storj.io/gateway/miniogw"
minio "storj.io/minio/cmd"
"storj.io/uplink"
)
// GatewayFlags configuration flags.
type GatewayFlags struct {
NonInteractive bool `help:"disable interactive mode" default:"false" setup:"true"`
SatelliteAddress string `help:"satellite address (<nodeid>@<address>:<port>)" default:"" setup:"true"`
APIKey string `help:"API key" default:"" setup:"true"`
Passphrase string `help:"encryption passphrase" default:"" setup:"true"`
Server miniogw.ServerConfig
Minio miniogw.MinioConfig
S3 miniogw.S3CompatibilityConfig
Config
Website bool `help:"serve content as a static website" default:"false" basic-help:"true"`
}
var (
gatewayUserAgent = "Gateway-ST/" + version.Build.Version.String()
// Error is the default gateway setup errs class.
Error = errs.Class("gateway setup")
// ConfigError is a class of errors relating to config validation.
ConfigError = errs.Class("gateway configuration")
// rootCmd represents the base gateway command when called without any subcommands.
rootCmd = &cobra.Command{
Use: "gateway",
Short: "Single-tenant, S3-compatible gateway to Storj DCS",
Args: cobra.OnlyValidArgs,
}
setupCmd = &cobra.Command{
Use: "setup",
Short: "Create a gateway config file",
RunE: cmdSetup,
Annotations: map[string]string{"type": "setup"},
}
runCmd = &cobra.Command{
Use: "run",
Short: "Run the S3 gateway",
RunE: cmdRun,
}
setupCfg GatewayFlags
runCfg GatewayFlags
confDir string
)
func init() {
defaultConfDir := fpath.ApplicationDir("storj", "gateway")
cfgstruct.SetupFlag(zap.L(), rootCmd, &confDir, "config-dir", defaultConfDir, "main directory for gateway configuration")
defaults := cfgstruct.DefaultsFlag(rootCmd)
rootCmd.AddCommand(runCmd)
rootCmd.AddCommand(setupCmd)
process.Bind(runCmd, &runCfg, defaults, cfgstruct.ConfDir(confDir))
process.Bind(setupCmd, &setupCfg, defaults, cfgstruct.ConfDir(confDir), cfgstruct.SetupMode())
rootCmd.PersistentFlags().BoolVar(new(bool), "advanced", false, "if used in with -h, print advanced flags help")
cfgstruct.SetBoolAnnotation(rootCmd.PersistentFlags(), "advanced", cfgstruct.BasicHelpAnnotationName, true)
cfgstruct.SetBoolAnnotation(rootCmd.PersistentFlags(), "config-dir", cfgstruct.BasicHelpAnnotationName, true)
setUsageFunc(rootCmd)
}
func cmdSetup(cmd *cobra.Command, args []string) (err error) {
setupDir, err := filepath.Abs(confDir)
if err != nil {
return Error.Wrap(err)
}
valid, _ := fpath.IsValidSetupDir(setupDir)
if !valid {
return Error.New("gateway configuration already exists (%v)", setupDir)
}
err = os.MkdirAll(setupDir, 0700)
if err != nil {
return Error.Wrap(err)
}
overrides := map[string]interface{}{}
accessKeyFlag := cmd.Flag("minio.access-key")
if !accessKeyFlag.Changed {
accessKey, err := generateKey()
if err != nil {
return err
}
overrides[accessKeyFlag.Name] = accessKey
}
secretKeyFlag := cmd.Flag("minio.secret-key")
if !secretKeyFlag.Changed {
secretKey, err := generateKey()
if err != nil {
return err
}
overrides[secretKeyFlag.Name] = secretKey
}
if setupCfg.NonInteractive {
return setupCfg.nonInteractive(cmd, setupDir, overrides)
}
return setupCfg.interactive(cmd, setupDir, overrides)
}
func cmdRun(cmd *cobra.Command, args []string) (err error) {
address := runCfg.Server.Address
host, port, err := net.SplitHostPort(address)
if err != nil {
return err
}
if host == "" {
address = net.JoinHostPort("127.0.0.1", port)
}
ctx, _ := process.Ctx(cmd)
if err := process.InitMetrics(ctx, zap.L(), nil, "", eventkitbq.BQDestination); err != nil {
zap.S().Warn("Failed to initialize telemetry batcher: ", err)
}
zap.S().Infof("Starting Storj DCS S3 Gateway\n\n")
zap.S().Infof("Endpoint: %s\n", address)
zap.S().Infof("Access key: %s\n", runCfg.Minio.AccessKey)
zap.S().Infof("Secret key: %s\n", runCfg.Minio.SecretKey)
err = checkCfg(ctx)
if err != nil {
return err
}
return runCfg.Run(ctx)
}
func generateKey() (key string, err error) {
var buf [20]byte
_, err = rand.Read(buf[:])
if err != nil {
return "", Error.Wrap(err)
}
return base58.Encode(buf[:]), nil
}
func checkCfg(ctx context.Context) (err error) {
access, err := runCfg.GetAccess()
if err != nil {
return ConfigError.New("failed parsing access config: %w", err)
}
config := runCfg.newUplinkConfig(ctx)
project, err := config.OpenProject(ctx, access)
if err != nil {
return ConfigError.New("failed to open project: %w", err)
}
defer func() { err = errs.Combine(err, project.Close()) }()
buckets := project.ListBuckets(ctx, nil)
_ = buckets.Next()
if buckets.Err() != nil {
return ConfigError.New("failed to contact Satellite: %w", buckets.Err())
}
return nil
}
// Run starts a Minio Gateway given proper config.
func (flags GatewayFlags) Run(ctx context.Context) (err error) {
err = minio.RegisterGatewayCommand(cli.Command{
Name: "storj",
Usage: "Storj",
Action: func(cliCtx *cli.Context) error {
return flags.action(ctx, cliCtx)
},
HideHelpCommand: true,
})
if err != nil {
return err
}
// TODO(jt): Surely there is a better way. This is so upsetting
err = os.Setenv("MINIO_ACCESS_KEY", flags.Minio.AccessKey)
if err != nil {
return err
}
err = os.Setenv("MINIO_SECRET_KEY", flags.Minio.SecretKey)
if err != nil {
return err
}
minio.Main([]string{"storj", "gateway", "storj",
"--address", flags.Server.Address, "--config-dir", flags.Minio.Dir, "--quiet",
"--compat"})
return errs.New("unexpected minio exit")
}
func (flags GatewayFlags) action(ctx context.Context, cliCtx *cli.Context) (err error) {
access, err := flags.GetAccess()
if err != nil {
return Error.Wrap(err)
}
config := flags.newUplinkConfig(ctx)
gw, err := flags.NewGateway(ctx)
if err != nil {
return err
}
minio.StartGateway(cliCtx, miniogw.NewSingleTenantGateway(zap.L(), access, config, gw, flags.Website))
return errs.New("unexpected minio exit")
}
// NewGateway creates a new minio Gateway.
func (flags GatewayFlags) NewGateway(ctx context.Context) (gw minio.Gateway, err error) {
return miniogw.NewStorjGateway(flags.S3), nil
}
func (flags *GatewayFlags) newUplinkConfig(ctx context.Context) uplink.Config {
// Transform the gateway config flags to the uplink config object
config := uplink.Config{}
config.UserAgent = gatewayUserAgent
if flags.Client.AdditionalUserAgent != "" {
config.UserAgent = flags.Client.AdditionalUserAgent + " " + config.UserAgent
}
if flags.Client.UserAgent != "" {
config.UserAgent = flags.Client.UserAgent + " " + config.UserAgent
}
return config
}
// interactive creates the configuration of the gateway interactively.
func (flags GatewayFlags) interactive(cmd *cobra.Command, setupDir string, overrides map[string]interface{}) error {
ctx, _ := process.Ctx(cmd)
satelliteAddress, err := wizard.PromptForSatellite(cmd)
if err != nil {
return Error.Wrap(err)
}
apiKey, err := wizard.PromptForAPIKey()
if err != nil {
return Error.Wrap(err)
}
passphrase, err := wizard.PromptForEncryptionPassphrase()
if err != nil {
return Error.Wrap(err)
}
access, err := uplink.RequestAccessWithPassphrase(ctx, satelliteAddress, apiKey, passphrase)
if err != nil {
return Error.Wrap(err)
}
accessData, err := access.Serialize()
if err != nil {
return Error.Wrap(err)
}
overrides["access"] = accessData
tracingEnabled, err := wizard.PromptForTracing()
if err != nil {
return Error.Wrap(err)
}
if tracingEnabled {
overrides["tracing.enabled"] = true
overrides["tracing.sample"] = 0.1
overrides["tracing.interval"] = 30 * time.Second
}
err = process.SaveConfig(cmd, filepath.Join(setupDir, "config.yaml"),
process.SaveConfigWithOverrides(overrides),
process.SaveConfigRemovingDeprecated())
if err != nil {
return Error.Wrap(err)
}
fmt.Println(`
Your S3 Gateway is configured and ready to use!
Some things to try next:
* See https://docs.storj.io/api-reference/s3-gateway for some example commands`)
return nil
}
// nonInteractive creates the configuration of the gateway non-interactively.
func (flags GatewayFlags) nonInteractive(cmd *cobra.Command, setupDir string, overrides map[string]interface{}) (err error) {
ctx, _ := process.Ctx(cmd)
var access *uplink.Access
accessString := setupCfg.Access
if accessString != "" {
access, err = uplink.ParseAccess(accessString)
} else if setupCfg.SatelliteAddress != "" && setupCfg.APIKey != "" && setupCfg.Passphrase != "" {
satellite := setupCfg.SatelliteAddress
if fullAddress, ok := wizard.SatellitesURL[satellite]; ok {
satellite = fullAddress
}
access, err = uplink.RequestAccessWithPassphrase(ctx, satellite, setupCfg.APIKey, setupCfg.Passphrase)
} else {
err = errs.New("non-interactive setup requires '--access' flag or all '--satellite-address', '--api-key', '--passphrase' flags")
}
if err != nil {
return err
}
accessData, err := access.Serialize()
if err != nil {
return err
}
overrides["access"] = accessData
return Error.Wrap(process.SaveConfig(cmd, filepath.Join(setupDir, "config.yaml"),
process.SaveConfigWithOverrides(overrides),
process.SaveConfigRemovingDeprecated()))
}
/*
`setUsageFunc` is a bit unconventional but cobra didn't leave much room for
extensibility here. `cmd.SetUsageTemplate` is fairly useless for our case without
the ability to add to the template's function map (see: https://golang.org/pkg/text/template/#hdr-Functions).
Because we can't alter what `cmd.Usage` generates, we have to edit it afterwards.
In order to hook this function *and* get the usage string, we have to juggle the
`cmd.usageFunc` between our hook and `nil`, so that we can get the usage string
from the default usage func.
*/
func setUsageFunc(cmd *cobra.Command) {
if findBoolFlagEarly("advanced") {
return
}
reset := func() (set func()) {
original := cmd.UsageFunc()
cmd.SetUsageFunc(nil)
return func() {
cmd.SetUsageFunc(original)
}
}
cmd.SetUsageFunc(func(cmd *cobra.Command) error {
set := reset()
usageStr := cmd.UsageString()
defer set()
usageScanner := bufio.NewScanner(bytes.NewBufferString(usageStr))
var basicFlags []string
cmd.Flags().VisitAll(func(flag *pflag.Flag) {
basic, ok := flag.Annotations[cfgstruct.BasicHelpAnnotationName]
if ok && len(basic) == 1 && basic[0] == "true" {
basicFlags = append(basicFlags, flag.Name)
}
})
for usageScanner.Scan() {
line := usageScanner.Text()
trimmedLine := strings.TrimSpace(line)
var flagName string
if _, err := fmt.Sscanf(trimmedLine, "--%s", &flagName); err != nil {
fmt.Println(line)
continue
}
// TODO: properly filter flags with short names
if !strings.HasPrefix(trimmedLine, "--") {
fmt.Println(line)
}
for _, basicFlag := range basicFlags {
if basicFlag == flagName {
fmt.Println(line)
}
}
}
return nil
})
}
func findBoolFlagEarly(flagName string) bool {
for i, arg := range os.Args {
arg := arg
argHasPrefix := func(format string, args ...interface{}) bool {
return strings.HasPrefix(arg, fmt.Sprintf(format, args...))
}
if !argHasPrefix("--%s", flagName) {
continue
}
// NB: covers `--<flagName> false` usage
if i+1 != len(os.Args) {
next := os.Args[i+1]
if next == "false" {
return false
}
}
if !argHasPrefix("--%s=false", flagName) {
return true
}
}
return false
}
func main() {
process.Exec(rootCmd)
}