Skip to content

Commit

Permalink
adds a new flag s3.use-iam to enable minio client to use aws iam role
Browse files Browse the repository at this point in the history
  • Loading branch information
zexuanhuang committed Jun 27, 2018
1 parent 6cc17fe commit fe0d268
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion pkg/objstore/s3/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/improbable-eng/thanos/pkg/objstore"
"github.com/minio/minio-go"
"github.com/minio/minio-go/pkg/encrypt"
"github.com/minio/minio-go/pkg/credentials"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/version"
Expand Down Expand Up @@ -52,6 +53,7 @@ type Config struct {
Insecure bool
SignatureV2 bool
SSEEnprytion bool
UseIAM bool
}

// RegisterS3Params registers the s3 flags and returns an initialized Config struct.
Expand All @@ -78,11 +80,18 @@ func RegisterS3Params(cmd *kingpin.CmdClause) *Config {
cmd.Flag("s3.encrypt-sse", "Whether to use Server Side Encryption").
Default("false").Envar("S3_SSE_ENCRYPTION").BoolVar(&s3config.SSEEnprytion)

cmd.Flag("s3.use-iam", "Whether to use aws iam role").
Default("false").Envar("USE_IAM").BoolVar(&s3config.UseIAM)

return &s3config
}

// Validate checks to see if mandatory s3 config options are set.
func (conf *Config) Validate() error {
if conf.UseIAM {
return nil
}

if conf.Bucket == "" ||
conf.Endpoint == "" ||
conf.AccessKey == "" ||
Expand All @@ -94,6 +103,10 @@ func (conf *Config) Validate() error {

// ValidateForTests checks to see if mandatory s3 config options for tests are set.
func (conf *Config) ValidateForTests() error {
if conf.UseIAM {
return nil
}

if conf.Endpoint == "" ||
conf.AccessKey == "" ||
conf.SecretKey == "" {
Expand All @@ -111,7 +124,15 @@ func NewBucket(conf *Config, reg prometheus.Registerer, component string) (*Buck
f = minio.NewV4
}

client, err := f(conf.Endpoint, conf.AccessKey, conf.SecretKey, !conf.Insecure)
var client *minio.Client
var err error
if conf.UseIAM {
iam := credentials.NewIAM("")
client, err = minio.NewWithCredentials("s3.amazonaws.com", iam, true, "")
} else {
client, err = f(conf.Endpoint, conf.AccessKey, conf.SecretKey, !conf.Insecure)
}

if err != nil {
return nil, errors.Wrap(err, "initialize s3 client")
}
Expand Down

0 comments on commit fe0d268

Please sign in to comment.