@@ -123,11 +123,10 @@ public class ImpersonatedCredentialsTest extends BaseSerializationTest {
123123 static final List <String > IMMUTABLE_SCOPES_LIST = ImmutableList .of ("scope1" , "scope2" );
124124 static final int VALID_LIFETIME = 300 ;
125125 private static final int INVALID_LIFETIME = 43210 ;
126- private static JsonFactory JSON_FACTORY = GsonFactory .getDefaultInstance ();
126+ private static final JsonFactory JSON_FACTORY = GsonFactory .getDefaultInstance ();
127127
128128 private static final String RFC3339 = "yyyy-MM-dd'T'HH:mm:ssX" ;
129129
130- private static final String DEFAULT_UNIVERSE_DOMAIN = "googleapis.com" ;
131130 private static final String TEST_UNIVERSE_DOMAIN = "test.xyz" ;
132131 private static final String OLD_IMPERSONATION_URL =
133132 "https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/"
@@ -136,7 +135,7 @@ public class ImpersonatedCredentialsTest extends BaseSerializationTest {
136135 public static final String DEFAULT_IMPERSONATION_URL =
137136 String .format (
138137 IamUtils .IAM_ACCESS_TOKEN_ENDPOINT_FORMAT ,
139- DEFAULT_UNIVERSE_DOMAIN ,
138+ GoogleCredentials . GOOGLE_DEFAULT_UNIVERSE ,
140139 IMPERSONATED_CLIENT_EMAIL );
141140 private static final String NONGDU_IMPERSONATION_URL =
142141 String .format (
@@ -190,14 +189,15 @@ public void fromJson_userAsSource_WithQuotaProjectId() throws IOException {
190189 QUOTA_PROJECT_ID ,
191190 USER_ACCOUNT_CLIENT_ID ,
192191 USER_ACCOUNT_CLIENT_SECRET ,
193- REFRESH_TOKEN );
192+ REFRESH_TOKEN ,
193+ IMMUTABLE_SCOPES_LIST );
194194 ImpersonatedCredentials credentials =
195195 ImpersonatedCredentials .fromJson (json , mockTransportFactory );
196196 assertEquals (IMPERSONATED_CLIENT_EMAIL , credentials .getAccount ());
197197 assertEquals (IMPERSONATION_OVERRIDE_URL , credentials .getIamEndpointOverride ());
198198 assertEquals (QUOTA_PROJECT_ID , credentials .getQuotaProjectId ());
199199 assertEquals (DELEGATES , credentials .getDelegates ());
200- assertEquals (new ArrayList < String >() , credentials .getScopes ());
200+ assertEquals (IMMUTABLE_SCOPES_LIST , credentials .getScopes ());
201201 assertEquals (3600 , credentials .getLifetime ());
202202 GoogleCredentials sourceCredentials = credentials .getSourceCredentials ();
203203 assertTrue (sourceCredentials instanceof UserCredentials );
@@ -212,14 +212,15 @@ public void fromJson_userAsSource_WithoutQuotaProjectId() throws IOException {
212212 null ,
213213 USER_ACCOUNT_CLIENT_ID ,
214214 USER_ACCOUNT_CLIENT_SECRET ,
215- REFRESH_TOKEN );
215+ REFRESH_TOKEN ,
216+ IMMUTABLE_SCOPES_LIST );
216217 ImpersonatedCredentials credentials =
217218 ImpersonatedCredentials .fromJson (json , mockTransportFactory );
218219 assertEquals (IMPERSONATED_CLIENT_EMAIL , credentials .getAccount ());
219220 assertEquals (IMPERSONATION_OVERRIDE_URL , credentials .getIamEndpointOverride ());
220221 assertNull (credentials .getQuotaProjectId ());
221222 assertEquals (DELEGATES , credentials .getDelegates ());
222- assertEquals (new ArrayList < String >() , credentials .getScopes ());
223+ assertEquals (IMMUTABLE_SCOPES_LIST , credentials .getScopes ());
223224 assertEquals (3600 , credentials .getLifetime ());
224225 GoogleCredentials sourceCredentials = credentials .getSourceCredentials ();
225226 assertTrue (sourceCredentials instanceof UserCredentials );
@@ -234,15 +235,16 @@ public void fromJson_userAsSource_MissingDelegatesField() throws IOException {
234235 null ,
235236 USER_ACCOUNT_CLIENT_ID ,
236237 USER_ACCOUNT_CLIENT_SECRET ,
237- REFRESH_TOKEN );
238+ REFRESH_TOKEN ,
239+ IMMUTABLE_SCOPES_LIST );
238240 json .remove ("delegates" );
239241 ImpersonatedCredentials credentials =
240242 ImpersonatedCredentials .fromJson (json , mockTransportFactory );
241243 assertEquals (IMPERSONATED_CLIENT_EMAIL , credentials .getAccount ());
242244 assertEquals (IMPERSONATION_OVERRIDE_URL , credentials .getIamEndpointOverride ());
243245 assertNull (credentials .getQuotaProjectId ());
244246 assertEquals (new ArrayList <String >(), credentials .getDelegates ());
245- assertEquals (new ArrayList < String >() , credentials .getScopes ());
247+ assertEquals (IMMUTABLE_SCOPES_LIST , credentials .getScopes ());
246248 assertEquals (3600 , credentials .getLifetime ());
247249 GoogleCredentials sourceCredentials = credentials .getSourceCredentials ();
248250 assertTrue (sourceCredentials instanceof UserCredentials );
@@ -251,14 +253,15 @@ public void fromJson_userAsSource_MissingDelegatesField() throws IOException {
251253 @ Test ()
252254 public void fromJson_ServiceAccountAsSource () throws IOException {
253255 GenericJson json =
254- buildImpersonationCredentialsJson (IMPERSONATION_OVERRIDE_URL , DELEGATES , QUOTA_PROJECT_ID );
256+ buildImpersonationCredentialsJson (
257+ IMPERSONATION_OVERRIDE_URL , DELEGATES , QUOTA_PROJECT_ID , IMMUTABLE_SCOPES_LIST );
255258 ImpersonatedCredentials credentials =
256259 ImpersonatedCredentials .fromJson (json , mockTransportFactory );
257260 assertEquals (IMPERSONATED_CLIENT_EMAIL , credentials .getAccount ());
258261 assertEquals (IMPERSONATION_OVERRIDE_URL , credentials .getIamEndpointOverride ());
259262 assertEquals (QUOTA_PROJECT_ID , credentials .getQuotaProjectId ());
260263 assertEquals (DELEGATES , credentials .getDelegates ());
261- assertEquals (new ArrayList < String >() , credentials .getScopes ());
264+ assertEquals (IMMUTABLE_SCOPES_LIST , credentials .getScopes ());
262265 assertEquals (3600 , credentials .getLifetime ());
263266 GoogleCredentials sourceCredentials = credentials .getSourceCredentials ();
264267 assertTrue (sourceCredentials instanceof ServiceAccountCredentials );
@@ -481,18 +484,11 @@ public void credential_with_invalid_lifetime() throws IOException, IllegalStateE
481484
482485 @ Test ()
483486 public void credential_with_invalid_scope () throws IOException , IllegalStateException {
484-
485- try {
486- ImpersonatedCredentials targetCredentials =
487- ImpersonatedCredentials .create (
488- sourceCredentials , IMPERSONATED_CLIENT_EMAIL , null , null , VALID_LIFETIME );
489- targetCredentials .refreshAccessToken ().getTokenValue ();
490- fail (
491- String .format (
492- "Should throw exception with message containing '%s'" , "Scopes cannot be null" ));
493- } catch (IllegalStateException expected ) {
494- assertTrue (expected .getMessage ().contains ("Scopes cannot be null" ));
495- }
487+ assertThrows (
488+ NullPointerException .class ,
489+ () ->
490+ ImpersonatedCredentials .create (
491+ sourceCredentials , IMPERSONATED_CLIENT_EMAIL , null , null , VALID_LIFETIME ));
496492 }
497493
498494 @ Test ()
@@ -1221,6 +1217,42 @@ public void universeDomain_whenExplicit_AllowedIfMatchesSourceUD() throws IOExce
12211217 assertTrue (impersonatedCredentials .isExplicitUniverseDomain ());
12221218 }
12231219
1220+ @ Test
1221+ public void scopes_userConfigured () {
1222+ ImpersonatedCredentials impersonatedCredentials =
1223+ ImpersonatedCredentials .newBuilder ().setScopes (IMMUTABLE_SCOPES_LIST ).build ();
1224+ assertArrayEquals (
1225+ IMMUTABLE_SCOPES_LIST .toArray (), impersonatedCredentials .getScopes ().toArray ());
1226+ }
1227+
1228+ @ Test
1229+ public void scopes_fromJson () throws IOException {
1230+ ImpersonatedCredentials impersonatedCredentials =
1231+ ImpersonatedCredentials .fromJson (
1232+ buildImpersonationCredentialsJson (
1233+ DEFAULT_IMPERSONATION_URL , DELEGATES , null , IMMUTABLE_SCOPES_LIST ),
1234+ mockTransportFactory );
1235+ assertArrayEquals (
1236+ IMMUTABLE_SCOPES_LIST .toArray (), impersonatedCredentials .getScopes ().toArray ());
1237+ }
1238+
1239+ // Tests that user configured scopes has precedence over the one in the json.
1240+ // From the ADC flow, the json is parsed and the credential is returned back
1241+ // to the user
1242+ @ Test
1243+ public void scopes_userConfiguredAndFromJson () throws IOException {
1244+ List <String > userConfiguredScopes = ImmutableList .of ("nonsense-scopes" );
1245+ ImpersonatedCredentials impersonatedCredentials =
1246+ ImpersonatedCredentials .fromJson (
1247+ buildImpersonationCredentialsJson (
1248+ DEFAULT_IMPERSONATION_URL , DELEGATES , null , IMMUTABLE_SCOPES_LIST ),
1249+ mockTransportFactory );
1250+ ImpersonatedCredentials newImpersonatedCredentials =
1251+ impersonatedCredentials .toBuilder ().setScopes (userConfiguredScopes ).build ();
1252+ assertArrayEquals (
1253+ userConfiguredScopes .toArray (), newImpersonatedCredentials .getScopes ().toArray ());
1254+ }
1255+
12241256 @ Test
12251257 public void hashCode_equals () throws IOException {
12261258 mockTransportFactory .getTransport ().setTargetPrincipal (IMPERSONATED_CLIENT_EMAIL );
@@ -1333,7 +1365,8 @@ static GenericJson buildImpersonationCredentialsJson(
13331365 String quotaProjectId ,
13341366 String sourceClientId ,
13351367 String sourceClientSecret ,
1336- String sourceRefreshToken ) {
1368+ String sourceRefreshToken ,
1369+ List <String > scopes ) {
13371370 GenericJson sourceJson = new GenericJson ();
13381371
13391372 sourceJson .put ("client_id" , sourceClientId );
@@ -1348,12 +1381,13 @@ static GenericJson buildImpersonationCredentialsJson(
13481381 json .put ("quota_project_id" , quotaProjectId );
13491382 }
13501383 json .put ("source_credentials" , sourceJson );
1384+ json .put ("scopes" , scopes );
13511385 json .put ("type" , "impersonated_service_account" );
13521386 return json ;
13531387 }
13541388
13551389 static GenericJson buildImpersonationCredentialsJson (
1356- String impersonationUrl , List <String > delegates , String quotaProjectId ) {
1390+ String impersonationUrl , List <String > delegates , String quotaProjectId , List < String > scopes ) {
13571391 GenericJson sourceJson = new GenericJson ();
13581392 sourceJson .put ("type" , "service_account" );
13591393 sourceJson .put ("project_id" , PROJECT_ID );
@@ -1375,6 +1409,7 @@ static GenericJson buildImpersonationCredentialsJson(
13751409 if (quotaProjectId != null ) {
13761410 json .put ("quota_project_id" , quotaProjectId );
13771411 }
1412+ json .put ("scopes" , scopes );
13781413 json .put ("type" , "impersonated_service_account" );
13791414 return json ;
13801415 }
@@ -1386,9 +1421,10 @@ static GenericJson buildInvalidCredentialsJson() {
13861421 }
13871422
13881423 static InputStream writeImpersonationCredentialsStream (
1389- String impersonationUrl , List <String > delegates , String quotaProjectId ) throws IOException {
1424+ String impersonationUrl , List <String > delegates , String quotaProjectId , List <String > scopes )
1425+ throws IOException {
13901426 GenericJson json =
1391- buildImpersonationCredentialsJson (impersonationUrl , delegates , quotaProjectId );
1427+ buildImpersonationCredentialsJson (impersonationUrl , delegates , quotaProjectId , scopes );
13921428 return TestUtils .jsonToInputStream (json );
13931429 }
13941430}
0 commit comments