@@ -38,7 +38,7 @@ func New() *AWSFed {
3838 }
3939}
4040
41- func (a * AWSFed ) AWSCreds (
41+ func (a * AWSFed ) awsCredsWithLocalCache (
4242 ctx context.Context ,
4343 tok * session.Token ,
4444) (* types.Credentials , error ) {
@@ -50,17 +50,38 @@ func (a *AWSFed) AWSCreds(
5050 }
5151 }
5252
53+ outputCreds , err := a .awsCreds (ctx , tok .IDToken )
54+ if err != nil {
55+ return nil , err
56+ }
57+
58+ if creds , err := json .Marshal (outputCreds ); err != nil {
59+ return nil , err
60+ } else if err := cache .SetT (
61+ cacheKey (tok ),
62+ creds ,
63+ * outputCreds .Expiration ,
64+ ); err != nil {
65+ return nil , err
66+ }
67+
68+ return outputCreds , nil
69+ }
70+
71+ // awsCreds behaves similar to AWSCredsWithLocalCache but it takes a JWT from input
72+ // rather than reading from a file or cache. This is to allow web services use
73+ // this package without having to write every user's JWT in a cache or a file.
74+ func (a * AWSFed ) awsCreds (
75+ ctx context.Context ,
76+ idToken string ,
77+ ) (* types.Credentials , error ) {
78+
5379 svc := cognitoidentity .New (cognitoidentity.Options {
5480 Region : a .Region ,
5581 })
5682
57- logins := map [string ]string {}
58- if tok .IDClaims () == nil {
59- // skip
60- } else if tok .IDClaims ().Issuer == fmt .Sprintf ("https://%s/" , a .LegacyProvider ) {
61- logins [a .LegacyProvider ] = tok .IDToken
62- } else {
63- logins [a .Provider ] = tok .IDToken
83+ logins := map [string ]string {
84+ a .Provider : idToken ,
6485 }
6586
6687 getIdoutput , err := svc .GetId (
@@ -86,16 +107,6 @@ func (a *AWSFed) AWSCreds(
86107 return nil , err
87108 }
88109
89- if creds , err := json .Marshal (output .Credentials ); err != nil {
90- return nil , err
91- } else if err := cache .SetT (
92- cacheKey (tok ),
93- creds ,
94- * output .Credentials .Expiration ,
95- ); err != nil {
96- return nil , err
97- }
98-
99110 return output .Credentials , nil
100111}
101112
@@ -110,15 +121,22 @@ func cacheKey(t *session.Token) string {
110121 return fmt .Sprintf ("%s-%s" , cacheKeyPrefix , id )
111122}
112123
113- func GenSSMConfigForUser (
124+ func GenSSMConfigFromToken (
114125 ctx context.Context ,
115126 tok * session.Token ,
127+ useCache bool ,
116128) (* envsec.SSMConfig , error ) {
117129 if tok == nil {
118130 return & envsec.SSMConfig {}, nil
119131 }
120132 fed := New ()
121- creds , err := fed .AWSCreds (ctx , tok )
133+ var creds * types.Credentials
134+ var err error
135+ if useCache {
136+ creds , err = fed .awsCredsWithLocalCache (ctx , tok )
137+ } else {
138+ creds , err = fed .awsCreds (ctx , tok .IDToken )
139+ }
122140 if err != nil {
123141 return nil , errors .WithStack (err )
124142 }
0 commit comments