Skip to content

Commit 8301057

Browse files
authored
Merge pull request #2 from base-org/s3-iam-support
Add support for IAM access to S3
2 parents a4cb757 + fb49b65 commit 8301057

File tree

5 files changed

+60
-24
lines changed

5 files changed

+60
-24
lines changed

api/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ test:
1010
.PHONY: \
1111
blob-api \
1212
clean \
13-
test
13+
test

common/flags/config.go

+39-17
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,44 @@ import (
88
)
99

1010
type DataStorage string
11+
type S3CredentialType string
1112

1213
const (
13-
DataStorageUnknown DataStorage = "unknown"
14-
DataStorageS3 DataStorage = "s3"
15-
DataStorageFile DataStorage = "file"
14+
DataStorageUnknown DataStorage = "unknown"
15+
DataStorageS3 DataStorage = "s3"
16+
DataStorageFile DataStorage = "file"
17+
S3CredentialUnknown S3CredentialType = "unknown"
18+
S3CredentialStatic S3CredentialType = "static"
19+
S3CredentialIAM S3CredentialType = "iam"
1620
)
1721

1822
type S3Config struct {
19-
Endpoint string
20-
AccessKey string
21-
SecretAccessKey string
22-
UseHttps bool
23-
Bucket string
23+
Endpoint string
24+
UseHttps bool
25+
Bucket string
26+
27+
S3CredentialType S3CredentialType
28+
AccessKey string
29+
SecretAccessKey string
2430
}
2531

2632
func (c S3Config) check() error {
2733
if c.Endpoint == "" {
2834
return errors.New("s3 endpoint must be set")
2935
}
3036

31-
if c.AccessKey == "" {
32-
return errors.New("s3 access key must be set")
37+
if c.S3CredentialType == S3CredentialUnknown {
38+
return errors.New("s3 credential type must be set")
3339
}
3440

35-
if c.SecretAccessKey == "" {
36-
return errors.New("s3 secret access key must be set")
41+
if c.S3CredentialType == S3CredentialStatic {
42+
if c.AccessKey == "" {
43+
return errors.New("s3 access key must be set")
44+
}
45+
46+
if c.SecretAccessKey == "" {
47+
return errors.New("s3 secret access key must be set")
48+
}
3749
}
3850

3951
if c.Bucket == "" {
@@ -85,12 +97,22 @@ func toDataStorage(s string) DataStorage {
8597

8698
func readS3Config(ctx *cli.Context) S3Config {
8799
return S3Config{
88-
Endpoint: ctx.String(S3EndpointFlagName),
89-
AccessKey: ctx.String(S3AccessKeyFlagName),
90-
SecretAccessKey: ctx.String(S3SecretAccessKeyFlagName),
91-
UseHttps: ctx.Bool(S3EndpointHttpsFlagName),
92-
Bucket: ctx.String(S3BucketFlagName),
100+
Endpoint: ctx.String(S3EndpointFlagName),
101+
AccessKey: ctx.String(S3AccessKeyFlagName),
102+
SecretAccessKey: ctx.String(S3SecretAccessKeyFlagName),
103+
UseHttps: ctx.Bool(S3EndpointHttpsFlagName),
104+
Bucket: ctx.String(S3BucketFlagName),
105+
S3CredentialType: toS3CredentialType(ctx.String(S3CredentialTypeFlagName)),
106+
}
107+
}
108+
109+
func toS3CredentialType(s string) S3CredentialType {
110+
if s == string(S3CredentialStatic) {
111+
return S3CredentialStatic
112+
} else if s == string(S3CredentialIAM) {
113+
return S3CredentialIAM
93114
}
115+
return S3CredentialUnknown
94116
}
95117

96118
func (c BeaconConfig) Check() error {

common/flags/flags.go

+6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const (
99
BeaconHttpFlagName = "l1-beacon-http"
1010
BeaconHttpClientTimeoutFlagName = "l1-beacon-client-timeout"
1111
DataStoreFlagName = "data-store"
12+
S3CredentialTypeFlagName = "s3-credential-type"
1213
S3EndpointFlagName = "s3-endpoint"
1314
S3EndpointHttpsFlagName = "s3-endpoint-https"
1415
S3AccessKeyFlagName = "s3-access-key"
@@ -34,6 +35,11 @@ func CLIFlags(envPrefix string) []cli.Flag {
3435
},
3536
// Optional Flags
3637
// S3 Data Store Flags
38+
&cli.StringFlag{
39+
Name: S3CredentialTypeFlagName,
40+
Usage: "The way to authenticate to S3, options are [iam, static]",
41+
EnvVars: opservice.PrefixEnvVar(envPrefix, "S3_CREDENTIAL_TYPE"),
42+
},
3743
&cli.StringFlag{
3844
Name: S3EndpointFlagName,
3945
Usage: "The URL for the S3 bucket (without the scheme http or https specified)",

common/storage/s3.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,15 @@ type S3Storage struct {
1919
}
2020

2121
func NewS3Storage(cfg flags.S3Config, l log.Logger) (*S3Storage, error) {
22+
var c *credentials.Credentials
23+
if cfg.S3CredentialType == flags.S3CredentialStatic {
24+
c = credentials.NewStaticV4(cfg.AccessKey, cfg.SecretAccessKey, "")
25+
} else {
26+
c = credentials.NewIAM("")
27+
}
28+
2229
client, err := minio.New(cfg.Endpoint, &minio.Options{
23-
Creds: credentials.NewStaticV4(cfg.AccessKey, cfg.SecretAccessKey, ""),
30+
Creds: c,
2431
Secure: cfg.UseHttps,
2532
})
2633

common/storage/s3_test.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@ func setupS3(t *testing.T) *S3Storage {
2424
l := testlog.Logger(t, log.LvlInfo)
2525

2626
s3, err := NewS3Storage(flags.S3Config{
27-
Endpoint: "localhost:9000",
28-
AccessKey: "admin",
29-
SecretAccessKey: "password",
30-
UseHttps: false,
31-
Bucket: "blobs",
27+
Endpoint: "localhost:9000",
28+
AccessKey: "admin",
29+
SecretAccessKey: "password",
30+
UseHttps: false,
31+
Bucket: "blobs",
32+
S3CredentialType: flags.S3CredentialStatic,
3233
}, l)
3334

3435
require.NoError(t, err)

0 commit comments

Comments
 (0)