mirror of
https://github.com/vmware-tanzu/velero.git
synced 2026-09-19 14:34:17 +00:00
fix: load AWS config and assume role
Signed-off-by: Luis Davim <dluis@vmware.com>
This commit is contained in:
@@ -33,8 +33,13 @@ import (
|
||||
const (
|
||||
// AWS specific environment variable
|
||||
awsProfileEnvVar = "AWS_PROFILE"
|
||||
awsRoleEnvVar = "AWS_ROLE_ARN"
|
||||
awsKeyIDEnvVar = "AWS_ACCESS_KEY_ID"
|
||||
awsSecretKeyEnvVar = "AWS_SECRET_ACCESS_KEY"
|
||||
awsSessTokenEnvVar = "AWS_SESSION_TOKEN"
|
||||
awsProfileKey = "profile"
|
||||
awsCredentialsFileEnvVar = "AWS_SHARED_CREDENTIALS_FILE"
|
||||
awsConfigFileEnvVar = "AWS_CONFIG_FILE"
|
||||
)
|
||||
|
||||
// GetS3ResticEnvVars gets the environment variables that restic
|
||||
@@ -51,32 +56,46 @@ func GetS3ResticEnvVars(config map[string]string) (map[string]string, error) {
|
||||
result[awsProfileEnvVar] = profile
|
||||
}
|
||||
|
||||
// GetS3ResticEnvVars reads the AWS config, from files and envs
|
||||
// if needed assumes the role and returns the session credentials
|
||||
// setting these variables emulates what would happen for example when using kube2iam
|
||||
if creds, err := GetS3Credentials(config); err == nil && creds != nil {
|
||||
result[awsKeyIDEnvVar] = creds.AccessKeyID
|
||||
result[awsSecretKeyEnvVar] = creds.SecretAccessKey
|
||||
result[awsSessTokenEnvVar] = creds.SessionToken
|
||||
result[awsCredentialsFileEnvVar] = ""
|
||||
result[awsProfileEnvVar] = ""
|
||||
result[awsConfigFileEnvVar] = ""
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// GetS3Credentials gets the S3 credential values according to the information
|
||||
// of the provided config or the system's environment variables
|
||||
func GetS3Credentials(config map[string]string) (*credentials.Value, error) {
|
||||
if len(os.Getenv("AWS_ROLE_ARN")) > 0 {
|
||||
if os.Getenv(awsRoleEnvVar) != "" {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
opts := session.Options{}
|
||||
credentialsFile := config[CredentialsFileKey]
|
||||
if credentialsFile == "" {
|
||||
credentialsFile = os.Getenv("AWS_SHARED_CREDENTIALS_FILE")
|
||||
}
|
||||
|
||||
if credentialsFile == "" {
|
||||
return nil, errors.New("missing credential file")
|
||||
if credentialsFile != "" {
|
||||
opts.SharedConfigFiles = append(opts.SharedConfigFiles, credentialsFile)
|
||||
opts.SharedConfigState = session.SharedConfigEnable
|
||||
}
|
||||
|
||||
creds := credentials.NewSharedCredentials(credentialsFile, config[awsProfileKey])
|
||||
credValue, err := creds.Get()
|
||||
sess, err := session.NewSessionWithOptions(opts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &credValue, nil
|
||||
creds, err := sess.Config.Credentials.Get()
|
||||
|
||||
return &creds, err
|
||||
}
|
||||
|
||||
// GetAWSBucketRegion returns the AWS region that a bucket is in, or an error
|
||||
|
||||
Reference in New Issue
Block a user