Skip to content

Commit f974270

Browse files
author
Kai-Hsin Liu
committed
Revert "Revert "Remove singleton of AWSConfig. Use singleton at Credentials level""
This reverts commit dbd6977.
1 parent 61c6a76 commit f974270

File tree

2 files changed

+24
-24
lines changed

2 files changed

+24
-24
lines changed

agent/managedInstances/rolecreds/role_provider.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ package rolecreds
1111

1212
import (
1313
"fmt"
14+
"sync"
1415
"time"
1516

1617
"github.com/aws/amazon-ssm-agent/agent/ssm/rsaauth"
@@ -49,13 +50,25 @@ type managedInstancesRoleProvider struct {
4950
}
5051

5152
var (
52-
emptyCredential = credentials.Value{ProviderName: ProviderName}
53+
emptyCredential = credentials.Value{ProviderName: ProviderName}
54+
credentialsSingleton *credentials.Credentials
55+
lock sync.RWMutex
5356
)
5457

55-
// NewCredentials returns a pointer to a new Credentials object wrapping
56-
// the managedInstancesRoleProvider. Takes a ConfigProvider to create a EC2Metadata client.
57-
// The ConfigProvider is satisfied by the session.Session type.
58-
func NewCredentials(options ...func(*managedInstancesRoleProvider)) *credentials.Credentials {
58+
// ManagedInstanceCredentialsInstance returns a singleton instance of
59+
// Crednetials which provides credentials of a managed instance.
60+
func ManagedInstanceCredentialsInstance() *credentials.Credentials {
61+
lock.Lock()
62+
defer lock.Unlock()
63+
if credentialsSingleton == nil {
64+
credentialsSingleton = newManagedInstanceCredentials()
65+
}
66+
return credentialsSingleton
67+
}
68+
69+
// newManagedInstanceCredentials returns a pointer to a new Credentials object wrapping
70+
// the managedInstancesRoleProvider.
71+
func newManagedInstanceCredentials() *credentials.Credentials {
5972
instanceID := managedInstance.InstanceID()
6073
region := managedInstance.Region()
6174
privateKey := managedInstance.PrivateKey()
@@ -64,10 +77,6 @@ func NewCredentials(options ...func(*managedInstancesRoleProvider)) *credentials
6477
ExpiryWindow: EarlyExpiryTimeWindow,
6578
}
6679

67-
for _, option := range options {
68-
option(p)
69-
}
70-
7180
return credentials.NewCredentials(p)
7281
}
7382

agent/sdkutil/awsconfig.go

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,10 @@ import (
2626
"github.com/aws/aws-sdk-go/aws"
2727
)
2828

29-
var config *aws.Config
30-
31-
func init() {
32-
config = getAwsConfig()
33-
}
34-
35-
// AwsConfig returns the default aws.Config object while the appropriate credentials
36-
func AwsConfig() *aws.Config {
37-
return config
38-
}
39-
40-
// getAwsConfig : Default AWS config populates with default region and credentials.
41-
// Callers should override returned config properties with any values they want for service specific overrides.
42-
func getAwsConfig() (awsConfig *aws.Config) {
29+
// AwsConfig returns the default aws.Config object while the appropriate
30+
// credentials. Callers should override returned config properties with any
31+
// values they want for service specific overrides.
32+
func AwsConfig() (awsConfig *aws.Config) {
4333
// create default config
4434
awsConfig = &aws.Config{
4535
Retryer: newRetryer(),
@@ -54,7 +44,8 @@ func getAwsConfig() (awsConfig *aws.Config) {
5444

5545
// load managed credentials if applicable
5646
if isManaged, err := registration.HasManagedInstancesCredentials(); isManaged && err == nil {
57-
awsConfig.Credentials = rolecreds.NewCredentials()
47+
awsConfig.Credentials =
48+
rolecreds.ManagedInstanceCredentialsInstance()
5849
return
5950
}
6051

0 commit comments

Comments
 (0)