@@ -120,6 +120,14 @@ public class DefaultCredentialProviderTests
120120 ""format"": {
121121 ""type"": ""json"",
122122 ""subject_token_field_name"": ""access_token""}}}" ;
123+ private const string DummyFileSourcedExternalAccountCredentialFileContents = @"{
124+ ""type"": ""external_account"",
125+ ""audience"": ""//iam.googleapis.com/projects/$PROJECT_NUMBER/locations/global/workloadIdentityPools/$POOL_ID/providers/$PROVIDER_ID"",
126+ ""subject_token_type"": ""urn:ietf:params:oauth:token-type:saml2"",
127+ ""token_url"": ""https://sts.googleapis.com/v1/token"",
128+ ""credential_source"": {
129+ ""file"": ""/var/run/saml/assertion/token""
130+ }}" ;
123131 private const string DummyUrlSourcedImpersonatedExternalAccountCredentialFileContents = @"{
124132""type"": ""external_account"",
125133""audience"": ""//iam.googleapis.com/projects/$PROJECT_NUMBER/locations/global/workloadIdentityPools/$POOL_ID/providers/$PROVIDER_ID"",
@@ -134,6 +142,15 @@ public class DefaultCredentialProviderTests
134142 ""format"": {
135143 ""type"": ""json"",
136144 ""subject_token_field_name"": ""access_token""}}}" ;
145+ private const string DummyFileSourcedImpersonatedExternalAccountCredentialFileContents = @"{
146+ ""type"": ""external_account"",
147+ ""audience"": ""//iam.googleapis.com/projects/$PROJECT_NUMBER/locations/global/workloadIdentityPools/$POOL_ID/providers/$PROVIDER_ID"",
148+ ""subject_token_type"": ""urn:ietf:params:oauth:token-type:saml2"",
149+ ""service_account_impersonation_url"": ""https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/$EMAIL:generateAccessToken"",
150+ ""token_url"": ""https://sts.googleapis.com/v1/token"",
151+ ""credential_source"": {
152+ ""file"": ""/var/run/saml/assertion/token""
153+ }}" ;
137154 private const string DummyUrlSourcedWorkforceExternalAccountCredentialFileContents = @"{
138155""type"":""external_account"",
139156""audience"":""//iam.googleapis.com/locations/global/workforcePools/pool/providers/oidc-google"",
@@ -148,6 +165,15 @@ public class DefaultCredentialProviderTests
148165 ""format"": {
149166 ""type"": ""json"",
150167 ""subject_token_field_name"": ""access_token""}}}" ;
168+ private const string DummyFileSourcedWorkforceExternalAccountCredentialFileContents = @"{
169+ ""type"": ""external_account"",
170+ ""audience"": ""//iam.googleapis.com/projects/$PROJECT_NUMBER/locations/global/workloadIdentityPools/$POOL_ID/providers/$PROVIDER_ID"",
171+ ""subject_token_type"": ""urn:ietf:params:oauth:token-type:saml2"",
172+ ""token_url"": ""https://sts.googleapis.com/v1/token"",
173+ ""workforce_pool_user_project"": ""user_project"",
174+ ""credential_source"": {
175+ ""file"": ""/var/run/saml/assertion/token""
176+ }}" ;
151177
152178 public DefaultCredentialProviderTests ( )
153179 {
@@ -254,45 +280,70 @@ public async Task GetDefaultCredential_ExternalAccountCredential_NoCredentialSou
254280 await Assert . ThrowsAsync < InvalidOperationException > ( ( ) => credentialProvider . GetDefaultCredentialAsync ( ) ) ;
255281 }
256282
257- [ Fact ]
258- public async Task GetDefaultCredential_UrlSourcedExternalAccountCredential ( )
283+ public static TheoryData < string , Type > ExternalAccountCredentialTestData => new TheoryData < string , Type >
284+ {
285+ { DummyUrlSourcedExternalAccountCredentialFileContents , typeof ( UrlSourcedExternalAccountCredential ) } ,
286+ { DummyFileSourcedExternalAccountCredentialFileContents , typeof ( FileSourcedExternalAccountCredential ) } ,
287+ } ;
288+
289+ [ Theory ]
290+ [ MemberData ( nameof ( ExternalAccountCredentialTestData ) ) ]
291+ public async Task GetDefaultCredential_ExternalAccountCredential ( string credentialFileContent , Type expectedCredentialType )
259292 {
260293 // Setup fake environment variables and credential file contents.
261294 var credentialFilepath = "TempFilePath.json" ;
262295 credentialProvider . SetEnvironmentVariable ( CredentialEnvironmentVariable , credentialFilepath ) ;
263- credentialProvider . SetFileContents ( credentialFilepath , DummyUrlSourcedExternalAccountCredentialFileContents ) ;
296+ credentialProvider . SetFileContents ( credentialFilepath , credentialFileContent ) ;
264297
265298 var credential = await credentialProvider . GetDefaultCredentialAsync ( ) ;
266299
267- Assert . IsType < UrlSourcedExternalAccountCredential > ( credential . UnderlyingCredential ) ;
300+ Assert . IsType ( expectedCredentialType , credential . UnderlyingCredential ) ;
268301 }
269302
270- [ Fact ]
271- public async Task GetDefaultCredential_UrlSourcedExternalAccountCredential_Impersonated ( )
303+ public static TheoryData < string , Type > ExternalImpersonatedAccountCredentialTestData => new TheoryData < string , Type >
304+ {
305+ { DummyUrlSourcedImpersonatedExternalAccountCredentialFileContents , typeof ( UrlSourcedExternalAccountCredential ) } ,
306+ { DummyFileSourcedImpersonatedExternalAccountCredentialFileContents , typeof ( FileSourcedExternalAccountCredential ) } ,
307+ } ;
308+
309+ [ Theory ]
310+ [ MemberData ( nameof ( ExternalImpersonatedAccountCredentialTestData ) ) ]
311+ public async Task GetDefaultCredential_UrlSourcedExternalAccountCredential_Impersonated ( string credentialFileContent , Type expectedCredentialType )
272312 {
273313 // Setup fake environment variables and credential file contents.
274314 var credentialFilepath = "TempFilePath.json" ;
275315 credentialProvider . SetEnvironmentVariable ( CredentialEnvironmentVariable , credentialFilepath ) ;
276- credentialProvider . SetFileContents ( credentialFilepath , DummyUrlSourcedImpersonatedExternalAccountCredentialFileContents ) ;
316+ credentialProvider . SetFileContents ( credentialFilepath , credentialFileContent ) ;
277317
278318 var credential = await credentialProvider . GetDefaultCredentialAsync ( ) ;
279319
280- var impersonatedExternalCredential = Assert . IsType < UrlSourcedExternalAccountCredential > ( credential . UnderlyingCredential ) ;
320+ Assert . IsType ( expectedCredentialType , credential . UnderlyingCredential ) ;
321+
322+ var impersonatedExternalCredential = ( ExternalAccountCredential ) credential . UnderlyingCredential ;
281323 Assert . IsType < ImpersonatedCredential > ( impersonatedExternalCredential . ImplicitlyImpersonated . Value ) ;
282324 }
283325
284- [ Fact ]
285- public async Task GetDefaultCredential_UrlSourcedExternalAccountCredential_WorkforceIdentity ( )
326+ public static TheoryData < string , Type > ExternalWorkforceAccountCredentialTestData => new TheoryData < string , Type >
327+ {
328+ { DummyUrlSourcedWorkforceExternalAccountCredentialFileContents , typeof ( UrlSourcedExternalAccountCredential ) } ,
329+ { DummyFileSourcedWorkforceExternalAccountCredentialFileContents , typeof ( FileSourcedExternalAccountCredential ) } ,
330+ } ;
331+
332+ [ Theory ]
333+ [ MemberData ( nameof ( ExternalWorkforceAccountCredentialTestData ) ) ]
334+ public async Task GetDefaultCredential_UrlSourcedExternalAccountCredential_WorkforceIdentity ( string credentialFileContent , Type expectedCredentialType )
286335 {
287336 // Setup fake environment variables and credential file contents.
288337 var credentialFilepath = "TempFilePath.json" ;
289338 credentialProvider . SetEnvironmentVariable ( CredentialEnvironmentVariable , credentialFilepath ) ;
290- credentialProvider . SetFileContents ( credentialFilepath , DummyUrlSourcedWorkforceExternalAccountCredentialFileContents ) ;
339+ credentialProvider . SetFileContents ( credentialFilepath , credentialFileContent ) ;
291340
292341 var credential = await credentialProvider . GetDefaultCredentialAsync ( ) ;
293342
294- var workforceCredntial = Assert . IsType < UrlSourcedExternalAccountCredential > ( credential . UnderlyingCredential ) ;
295- Assert . Equal ( "user_project" , workforceCredntial . WorkforcePoolUserProject ) ;
343+ Assert . IsType ( expectedCredentialType , credential . UnderlyingCredential ) ;
344+
345+ var workforceCredential = ( ExternalAccountCredential ) credential . UnderlyingCredential ;
346+ Assert . Equal ( "user_project" , workforceCredential . WorkforcePoolUserProject ) ;
296347 }
297348
298349 #endregion
0 commit comments