Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 32 additions & 6 deletions pkg/awsauth/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,16 @@ import (
)

const (
// awsTempCredsAccessKey and awsTempCredsSecretKey are the files containing the
awsTempCredsAccessKey = "/tmp/aws.credentials/access-key-id"
awsTempCredsSecretKey = "/tmp/aws.credentials/secret-access-key"
profileName = "assume_role_credentials"
// oldAwsTempCredsAccessKey and oldAwsTempCredsSecretKey are the files containing the keys for the old Grafana Assume Role implementation
oldAwsTempCredsAccessKey = "/tmp/aws.credentials/access-key-id"
oldAwsTempCredsSecretKey = "/tmp/aws.credentials/secret-access-key"

profileName = "assume_role_credentials"
)

// grafanaAssumeRoleKeysFolders are the folders for the keys for the Grafana Assume Role implementation
var grafanaAssumeRoleKeysFolders = []string{"aws-temp-credentials-1", "aws-temp-credentials-2"}

// Settings carries configuration for authenticating with AWS
type Settings struct {
AuthType AuthType
Expand Down Expand Up @@ -128,8 +132,30 @@ func (s Settings) WithSharedCredentials() LoadOptionsFunc {

// WithGrafanaAssumeRole returns a LoadOptionsFunc to initialize config for Grafana Assume Role
func (s Settings) WithGrafanaAssumeRole(ctx context.Context, client AWSAPIClient) LoadOptionsFunc {
accessKey, keyErr := os.ReadFile(awsTempCredsAccessKey)
secretKey, secretErr := os.ReadFile(awsTempCredsSecretKey)
// Iterate over the key folders until we get an active key
for _, folder := range grafanaAssumeRoleKeysFolders {
accessKeyFile := fmt.Sprintf("/tmp/%s/access-key-id", folder)
secretKeyFile := fmt.Sprintf("/tmp/%s/secret-access-key", folder)

accessKey, keyErr := os.ReadFile(accessKeyFile)
secretKey, secretErr := os.ReadFile(secretKeyFile)
if keyErr != nil || secretErr != nil {
continue
}

creds := client.NewStaticCredentialsProvider(string(accessKey), string(secretKey), "")
_, err := creds.Retrieve(ctx)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately this doesn't actually validate the credentials - you don't get an error regardless of what strings you pass in. We could validate them here by using GetCallerIdentity or the like, but that takes a nontrivial amount of time so we probably don't want to do it every time.

One approach would be to set a global to aws-temp-credentials-1 and use that when choosing keys, then in WithAssumeRole if we get an error indicating the credentials are invalid (have to check what that looks like) we switch the global to aws-temp-credentials-2 and retry (once).

// if the credentials are valid, return them. Otherwise try the next credentials folder
if err == nil {
return func(opts *config.LoadOptions) error {
opts.Credentials = creds
return nil
}
}
}

accessKey, keyErr := os.ReadFile(oldAwsTempCredsAccessKey)
secretKey, secretErr := os.ReadFile(oldAwsTempCredsSecretKey)
if keyErr == nil && secretErr == nil {
return func(opts *config.LoadOptions) error {
opts.Credentials = client.NewStaticCredentialsProvider(string(accessKey), string(secretKey), "")
Expand Down