55 "strings"
66 "time"
77
8- "github.com/armon/go-metrics"
98 "github.com/aws/aws-sdk-go-v2/aws"
109 "github.com/aws/aws-sdk-go-v2/aws/external"
1110 "github.com/aws/aws-sdk-go-v2/service/iam"
@@ -37,22 +36,20 @@ func ConfigureAWS() {
3736 stsService = sts .New (cfg )
3837}
3938
40- func readRoleFromAWS (role string , labels []metrics. Label ) (* iam.Role , []metrics. Label , error ) {
41- logWithLabels ( labels ) .Infof ("Looking for IAM role for %s" , role )
39+ func readRoleFromAWS (role string , request * Request ) (* iam.Role , error ) {
40+ request . log .Infof ("Looking for IAM role for %s" , role )
4241
4342 roleObject := & iam.Role {}
44-
4543 if roleObject , ok := roleCache .Get (role ); ok {
46- labels = append (labels , metrics.Label {Name : "read_role_from_aws_cache" , Value : "hit" })
47-
48- logWithLabels (labels ).Infof ("Found IAM role %s in cache" , role )
49- return roleObject .(* iam.Role ), labels , nil
44+ request .setLabel ("read_role_from_aws_cache" , "hit" )
45+ request .log .Infof ("Found IAM role %s in cache" , role )
46+ return roleObject .(* iam.Role ), nil
5047 }
5148
52- labels = append ( labels , metrics. Label { Name : "read_role_from_aws_cache" , Value : "miss" } )
49+ request . setLabel ( "read_role_from_aws_cache" , "miss" )
5350
5451 if strings .Contains (role , "@" ) { // IAM_ROLE=my-role@012345678910
55- logWithLabels ( labels ) .Infof ("Constructing IAM role info for %s manually" , role )
52+ request . log .Infof ("Constructing IAM role info for %s manually" , role )
5653 chunks := strings .SplitN (role , "@" , 2 )
5754 nameChunks := strings .Split (chunks [0 ], "/" )
5855
@@ -61,7 +58,7 @@ func readRoleFromAWS(role string, labels []metrics.Label) (*iam.Role, []metrics.
6158 RoleName : aws .String (nameChunks [len (nameChunks )- 1 ]),
6259 }
6360 } else if strings .HasPrefix (role , "arn:aws:iam" ) { // IAM_ROLE=arn:aws:iam::012345678910:role/my-role
64- logWithLabels ( labels ) .Infof ("Using IAM role ARN as is for %s" , role )
61+ request . log .Infof ("Using IAM role ARN as is for %s" , role )
6562
6663 chunks := strings .SplitN (role , ":role/" , 2 )
6764 nameChunks := strings .Split (chunks [1 ], "/" )
@@ -71,50 +68,46 @@ func readRoleFromAWS(role string, labels []metrics.Label) (*iam.Role, []metrics.
7168 RoleName : aws .String (nameChunks [len (nameChunks )- 1 ]),
7269 }
7370 } else { // IAM_ROLE=my-role
74- logWithLabels ( labels ) .Infof ("Requesting IAM role info for %s from AWS" , role )
71+ request . log .Infof ("Requesting IAM role info for %s from AWS" , role )
7572 req := iamService .GetRoleRequest (& iam.GetRoleInput {
7673 RoleName : aws .String (role ),
7774 })
7875
7976 resp , err := req .Send ()
8077 if err != nil {
81- return nil , labels , err
78+ return nil , err
8279 }
8380
8481 roleObject = resp .Role
8582 }
8683
8784 roleCache .Set (role , roleObject , cache .DefaultExpiration )
88- return roleObject , labels , nil
85+ return roleObject , nil
8986}
9087
91- func assumeRoleFromAWS (arn string , labels []metrics. Label ) (* sts.AssumeRoleOutput , []metrics. Label , error ) {
92- logWithLabels ( labels ) .Infof ("Looking for STS Assume Role for %s" , arn )
88+ func assumeRoleFromAWS (arn string , request * Request ) (* sts.AssumeRoleOutput , error ) {
89+ request . log .Infof ("Looking for STS Assume Role for %s" , arn )
9390
9491 if assumedRole , ok := permissionCache .Get (arn ); ok {
95- labels = append (labels , metrics.Label {Name : "assume_role_from_aws_cache" , Value : "hit" })
96-
97- logWithLabels (labels ).Infof ("Found STS Assume Role %s in cache" , arn )
98- return assumedRole .(* sts.AssumeRoleOutput ), labels , nil
92+ request .setLabel ("assume_role_from_aws_cache" , "hit" )
93+ request .log .Infof ("Found STS Assume Role %s in cache" , arn )
94+ return assumedRole .(* sts.AssumeRoleOutput ), nil
9995 }
100- labels = append (labels , metrics.Label {Name : "assume_role_from_aws_cache" , Value : "miss" })
10196
102- logWithLabels (labels ).Infof ("Requesting STS Assume Role info for %s from AWS" , arn )
97+ request .setLabel ("assume_role_from_aws_cache" , "miss" )
98+ request .log .Infof ("Requesting STS Assume Role info for %s from AWS" , arn )
10399 req := stsService .AssumeRoleRequest (& sts.AssumeRoleInput {
104100 RoleArn : aws .String (arn ),
105101 RoleSessionName : aws .String ("go-metadataproxy" ),
106102 })
107103
108104 assumedRole , err := req .Send ()
109105 if err != nil {
110- return nil , labels , err
106+ return nil , err
111107 }
112108
113109 ttl := assumedRole .Credentials .Expiration .Sub (time .Now ()) - 1 * time .Minute
114-
115- logWithLabels (labels ).Infof ("Will cache STS Assumed Role info for %s in %s" , arn , ttl .String ())
116-
110+ request .log .Infof ("Will cache STS Assumed Role info for %s in %s" , arn , ttl .String ())
117111 permissionCache .Set (arn , assumedRole , ttl )
118-
119- return assumedRole , labels , nil
112+ return assumedRole , nil
120113}
0 commit comments