-
Notifications
You must be signed in to change notification settings - Fork 16
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
Ds 5114 update the aws secrets provider to aws sdk v2 #76
Open
arivankar-px
wants to merge
17
commits into
master
Choose a base branch
from
DS-5114-Update-the-AWS-secrets-provider-to-AWS-SDK-V2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
087660a
DS-5114-Draft changes
arivankar-px e1853a0
DS-5114-Draft changes
arivankar-px 795621a
DS-5114-Draft changes
arivankar-px 6d3bf6a
Revert "Update minikube setup versions (#74)"
arivankar-px b29a619
Revert "Revert "Update minikube setup versions (#74)""
arivankar-px 0304ae7
Updated code to support AWS SDK V2
arivankar-px 78bf645
Changed retention period to 7 days for integration test
arivankar-px e0fa474
Add a delay to allow time for deletion to propagate
arivankar-px cd3f98a
Add a delay to allow time for deletion to propagate
arivankar-px c073ad0
Add a delay to allow time for deletion to propagate
arivankar-px bd0945e
Add a delay to allow time for deletion to propagate
arivankar-px 61157c5
reduce sleep time
arivankar-px 1762ab5
reduce sleep time
arivankar-px 5f953e7
Changed error handling by using secretmanager error handler instead o…
arivankar-px 4c53935
Made changes to the struct
arivankar-px 6a1cf46
Made changes based on review comments
arivankar-px d367c77
Removed keyContext value
arivankar-px 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
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
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 |
---|---|---|
@@ -1,69 +1,75 @@ | ||
package credentials | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"context" | ||
"time" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/credentials" | ||
"github.com/aws/aws-sdk-go/aws/credentials/ec2rolecreds" | ||
"github.com/aws/aws-sdk-go/aws/ec2metadata" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/aws/transport/http" | ||
"github.com/aws/aws-sdk-go-v2/config" | ||
"github.com/aws/aws-sdk-go-v2/credentials" | ||
"github.com/aws/aws-sdk-go-v2/credentials/ec2rolecreds" | ||
"github.com/aws/aws-sdk-go-v2/feature/ec2/imds" | ||
) | ||
|
||
type AWSCredentials interface { | ||
Get() (*credentials.Credentials, error) | ||
Get() (*aws.Credentials, error) | ||
GetCredentialsProvider() (aws.CredentialsProvider, error) | ||
} | ||
|
||
type awsCred struct { | ||
creds *credentials.Credentials | ||
creds *aws.Credentials | ||
credsprovider aws.CredentialsProvider | ||
} | ||
|
||
func NewAWSCredentials(id, secret, token string, runningOnEc2 bool) (AWSCredentials, error) { | ||
var creds *credentials.Credentials | ||
sess, err := session.NewSession() | ||
if err != nil { | ||
return nil, fmt.Errorf("error creating new aws credentials: %w", err) | ||
} | ||
var creds aws.Credentials | ||
var credsprovider aws.CredentialsProvider | ||
var ctx context.Context | ||
if id != "" && secret != "" { | ||
creds = credentials.NewStaticCredentials(id, secret, token) | ||
if _, err := creds.Get(); err != nil { | ||
cfg, err := config.LoadDefaultConfig(ctx, config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(id, secret, token))) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} else if sess.Config.Credentials != nil { | ||
// sess config loads credential automatically from environment variable | ||
// this is used to prioritize loading aws web identity token whenever it's specified. | ||
creds = sess.Config.Credentials | ||
} else { | ||
providers := []credentials.Provider{ | ||
&credentials.EnvProvider{}, | ||
|
||
creds, err = cfg.Credentials.Retrieve(context.Background()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if runningOnEc2 { | ||
client := http.Client{Timeout: time.Second * 10} | ||
ec2RoleProvider := &ec2rolecreds.EC2RoleProvider{ | ||
Client: ec2metadata.New(sess, &aws.Config{ | ||
HTTPClient: &client, | ||
}), | ||
} | ||
providers = append(providers, ec2RoleProvider) | ||
|
||
} else if runningOnEc2 { | ||
|
||
ec2Provider := ec2rolecreds.New(func(o *ec2rolecreds.Options) { | ||
o.Client = imds.New(imds.Options{ | ||
HTTPClient: http.NewBuildableClient().WithTimeout(10 * time.Second), | ||
}) | ||
}) | ||
|
||
cfg, err := config.LoadDefaultConfig(context.TODO(), | ||
config.WithCredentialsProvider(ec2Provider), | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
providers = append(providers, &credentials.SharedCredentialsProvider{}) | ||
creds = credentials.NewChainCredentials(providers) | ||
if _, err := creds.Get(); err != nil { | ||
|
||
creds, err = cfg.Credentials.Retrieve(context.Background()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
return &awsCred{creds}, nil | ||
return &awsCred{&creds, credsprovider}, nil | ||
} | ||
|
||
func (a *awsCred) Get() (*credentials.Credentials, error) { | ||
if a.creds.IsExpired() { | ||
arivankar-px marked this conversation as resolved.
Show resolved
Hide resolved
|
||
func (a *awsCred) Get() (*aws.Credentials, error) { | ||
if a.creds.Expired() { | ||
// Refresh the credentials | ||
_, err := a.creds.Get() | ||
if err != nil { | ||
if _, err := a.credsprovider.Retrieve(context.TODO()); err != nil { | ||
return nil, err | ||
} | ||
} | ||
return a.creds, nil | ||
} | ||
|
||
func (a *awsCred) GetCredentialsProvider() (aws.CredentialsProvider, error) { | ||
return a.credsprovider, nil | ||
} |
Oops, something went wrong.
Oops, something went wrong.
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.
Curious, does AWS not allow chaining of providers anymore? Previously we were able to add EnvProvider and RoleProvider together. With this change we won't since each provider is in its own if condition.