-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
dynamic aws credentials support #2135
Closed
kush-patel-hs
wants to merge
3
commits into
thanos-io:master
from
kush-patel-hs:dynamic-aws-credentials-support
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ import ( | |
"net" | ||
"net/http" | ||
"os" | ||
"path/filepath" | ||
"runtime" | ||
"strconv" | ||
"strings" | ||
|
@@ -23,11 +24,13 @@ import ( | |
"github.com/minio/minio-go/v6" | ||
"github.com/minio/minio-go/v6/pkg/credentials" | ||
"github.com/minio/minio-go/v6/pkg/encrypt" | ||
"github.com/mitchellh/go-homedir" | ||
"github.com/pkg/errors" | ||
"github.com/prometheus/common/model" | ||
"github.com/prometheus/common/version" | ||
"github.com/thanos-io/thanos/pkg/objstore" | ||
"github.com/thanos-io/thanos/pkg/runutil" | ||
"gopkg.in/fsnotify.v1" | ||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
|
@@ -105,7 +108,7 @@ func NewBucket(logger log.Logger, conf []byte, component string) (*Bucket, error | |
|
||
// NewBucketWithConfig returns a new Bucket using the provided s3 config values. | ||
func NewBucketWithConfig(logger log.Logger, config Config, component string) (*Bucket, error) { | ||
var chain []credentials.Provider | ||
var creds *credentials.Credentials | ||
|
||
if err := validate(config); err != nil { | ||
return nil, err | ||
|
@@ -117,15 +120,16 @@ func NewBucketWithConfig(logger log.Logger, config Config, component string) (*B | |
signature = credentials.SignatureV2 | ||
} | ||
|
||
chain = []credentials.Provider{&credentials.Static{ | ||
chain := []credentials.Provider{&credentials.Static{ | ||
Value: credentials.Value{ | ||
AccessKeyID: config.AccessKey, | ||
SecretAccessKey: config.SecretKey, | ||
SignerType: signature, | ||
}, | ||
}} | ||
creds = credentials.NewChainCredentials(chain) | ||
} else { | ||
chain = []credentials.Provider{ | ||
chain := []credentials.Provider{ | ||
&credentials.EnvAWS{}, | ||
&credentials.FileAWSCredentials{}, | ||
&credentials.IAM{ | ||
|
@@ -134,9 +138,22 @@ func NewBucketWithConfig(logger log.Logger, config Config, component string) (*B | |
}, | ||
}, | ||
} | ||
creds = credentials.NewChainCredentials(chain) | ||
|
||
// We will watch for credential changes for non-static credentials | ||
filename := os.Getenv("AWS_SHARED_CREDENTIALS_FILE") | ||
if filename == "" { | ||
homeDir, err := homedir.Dir() | ||
if err == nil { | ||
filename = filepath.Join(homeDir, ".aws", "credentials") | ||
} | ||
} | ||
if err := startCredentialExpirer(creds, filename); err != nil { | ||
logger.Log("msg", "failed to start credential expirer", err) | ||
} | ||
} | ||
|
||
client, err := minio.NewWithCredentials(config.Endpoint, credentials.NewChainCredentials(chain), !config.Insecure, config.Region) | ||
client, err := minio.NewWithCredentials(config.Endpoint, creds, !config.Insecure, config.Region) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "initialize s3 client") | ||
} | ||
|
@@ -192,6 +209,46 @@ func (b *Bucket) Name() string { | |
return b.name | ||
} | ||
|
||
// startCredentialExpirer will start a watch of the file directory of AWS credentials and | ||
// if it is updated it will force expire credentials so they are fetched again next get. | ||
func startCredentialExpirer(creds *credentials.Credentials, filePath string) error { | ||
// Will not start watch for empty filePath | ||
if filePath == "" { | ||
return nil | ||
} | ||
|
||
watcher, err := fsnotify.NewWatcher() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Launch the watch which will expire the credentials when fired | ||
go func() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to kill this go routine when thanos quit or dies. Is there a standard way thanos does this atm for other go routines? |
||
defer watcher.Close() | ||
for { | ||
select { | ||
case _, ok := <-watcher.Events: | ||
if !ok { | ||
return | ||
} | ||
creds.Expire() | ||
case _, ok := <-watcher.Errors: | ||
if !ok { | ||
return | ||
} | ||
} | ||
} | ||
}() | ||
|
||
// We want to watch the parent because when files are rewritten watch will die | ||
err = watcher.Add(filepath.Dir(filePath)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// validate checks to see the config options are set. | ||
func validate(conf Config) error { | ||
if conf.Endpoint == "" { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should probably also get filename from config if we can. Then provide that to FileAWSCredentials. I really hate having to duplicate the FileAWSCredentials logic to determine filename here (I guess we would have to anyways if there's no filename provided in config).