Skip to content
This repository was archived by the owner on May 12, 2026. It is now read-only.

Commit 37f2713

Browse files
committed
chore: Address PR comments
1 parent bcb4c1d commit 37f2713

3 files changed

Lines changed: 48 additions & 18 deletions

File tree

oauth2_http/java/com/google/auth/oauth2/DefaultCredentialsProvider.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,11 +218,13 @@ private final GoogleCredentials getDefaultCredentialsUnsynchronized(
218218
LOGGER.log(Level.FINE, "Attempting to load credentials from GCE");
219219
credentials = tryGetComputeCredentials(transportFactory);
220220
// tryGetComputeCredentials can return a null value. This check won't set the source
221-
// if the ComputeEngineCredentials fails (prevents a NPE)
221+
// if the ComputeEngineCredentials is unable to be created
222222
if (credentials != null) {
223223
credentials =
224224
credentials.withSource(
225-
String.format("Metadata Server URL set to %s", getEnv(GCE_METADATA_HOST_ENV_VAR)));
225+
String.format(
226+
"Metadata Server URL set to %s",
227+
ComputeEngineCredentials.getMetadataServerUrl(this)));
226228
}
227229
}
228230

oauth2_http/java/com/google/auth/oauth2/GoogleCredentials.java

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -88,18 +88,19 @@ String getCredentialName() {
8888
return credentialName;
8989
}
9090

91+
@Nullable
9192
String getFileType() {
9293
return fileType;
9394
}
9495
}
9596

96-
/* The following package-private fields to provide additional info for errors message */
97+
// The following package-private fields to provide additional info for errors message
9798
// Source of the credential (e.g. env var value or well know file location)
9899
String source;
99100
// User-friendly name of the Credential class
100101
String name;
101102
// Identity of the credential
102-
// Note: This field is marked transient to prevent it from being serialized
103+
// Note: This field may contain data such as serviceAccountEmail which should not be serialized
103104
transient String principal;
104105

105106
private final String universeDomain;
@@ -245,24 +246,23 @@ public static GoogleCredentials fromStream(
245246
throw new IOException("Error reading credentials from stream, 'type' field not specified.");
246247
}
247248

248-
if (GoogleCredentialsInfo.USER_CREDENTIALS.getFileType().equals(fileType)) {
249+
if (fileType.equals(GoogleCredentialsInfo.USER_CREDENTIALS.getFileType())) {
249250
return UserCredentials.fromJson(fileContents, transportFactory);
250251
}
251-
if (GoogleCredentialsInfo.SERVICE_ACCOUNT_CREDENTIALS.getFileType().equals(fileType)) {
252+
if (fileType.equals(GoogleCredentialsInfo.SERVICE_ACCOUNT_CREDENTIALS.getFileType())) {
252253
return ServiceAccountCredentials.fromJson(fileContents, transportFactory);
253254
}
254-
if (GoogleCredentialsInfo.GDCH_CREDENTIALS.getFileType().equals(fileType)) {
255+
if (fileType.equals(GoogleCredentialsInfo.GDCH_CREDENTIALS.getFileType())) {
255256
return GdchCredentials.fromJson(fileContents);
256257
}
257-
if (GoogleCredentialsInfo.EXTERNAL_ACCOUNT_CREDENTIALS.getFileType().equals(fileType)) {
258+
if (fileType.equals(GoogleCredentialsInfo.EXTERNAL_ACCOUNT_CREDENTIALS.getFileType())) {
258259
return ExternalAccountCredentials.fromJson(fileContents, transportFactory);
259260
}
260-
if (GoogleCredentialsInfo.EXTERNAL_ACCOUNT_AUTHORIZED_USER_CREDENTIALS
261-
.getFileType()
262-
.equals(fileType)) {
261+
if (fileType.equals(
262+
GoogleCredentialsInfo.EXTERNAL_ACCOUNT_AUTHORIZED_USER_CREDENTIALS.getFileType())) {
263263
return ExternalAccountAuthorizedUserCredentials.fromJson(fileContents, transportFactory);
264264
}
265-
if (GoogleCredentialsInfo.IMPERSONATED_CREDENTIALS.getFileType().equals(fileType)) {
265+
if (fileType.equals(GoogleCredentialsInfo.IMPERSONATED_CREDENTIALS.getFileType())) {
266266
return ImpersonatedCredentials.fromJson(fileContents, transportFactory);
267267
}
268268
throw new IOException(
@@ -556,9 +556,14 @@ GoogleCredentials withSource(String source) {
556556
* <li>principal - Identity used for the credential
557557
* </ul>
558558
*
559-
* Unknown field values are not included in the mapping (e.g. ComputeCredentials may not know the
560-
* principal value until after a call to MDS is made and will be excluded if `getCredentialInfo`
561-
* is called prior to retrieving that value)
559+
* Unknown field values (i.e. null) are not included in the mapping (e.g. ComputeCredentials may
560+
* not know the principal value until after a call to MDS is made and the field will be excluded
561+
* if `getCredentialInfo` is called prior to retrieving that value). A new map of the fields is
562+
* created on every time this method is called as fields may be updated throughout the Credential
563+
* lifecycle. This mapping is intended to provide information about the Credential that is created
564+
* via ADC. Some fields may not be known if a Credential is directly created (e.g.
565+
* `ServiceAccountCredential.fromStream(InputStream)` may not know the source of the file stream).
566+
* These fields are populated on a best effort basis.
562567
*
563568
* @return ImmutableMap of information regarding how the Credential was initialized
564569
*/

oauth2_http/javatests/com/google/auth/oauth2/DefaultCredentialsProviderTest.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,7 @@ public void getDefaultCredentials_envVarSet_userCredential_correctCredentialInfo
792792
"Env Var %s set to %s", DefaultCredentialsProvider.CREDENTIAL_ENV_VAR, userPath),
793793
credentialInfo.get("Credential Source"));
794794
assertEquals("User Credentials", credentialInfo.get("Credential Name"));
795+
// No `account` field
795796
assertNull(credentialInfo.get("Principal"));
796797
}
797798

@@ -818,15 +819,36 @@ public void getDefaultCredentials_wellKnownFile_userCredential_correctCredential
818819
String.format("Well Known File at %s", wellKnownFile.getCanonicalPath()),
819820
credentialInfo.get("Credential Source"));
820821
assertEquals("User Credentials", credentialInfo.get("Credential Name"));
822+
// No `account` field
821823
assertNull(credentialInfo.get("Principal"));
822824
}
823825

824826
@Test
825-
public void getDefaultCredentials_computeEngineCredentials_correctCredentialInfo()
827+
public void getDefaultCredentials_computeEngineCredentials_defaultMDSUrl_correctCredentialInfo()
826828
throws IOException {
827829
MockMetadataServerTransportFactory transportFactory = new MockMetadataServerTransportFactory();
828830
TestDefaultCredentialsProvider testProvider = new TestDefaultCredentialsProvider();
829-
String gceMetadataHost = "192.0.2.0";
831+
832+
GoogleCredentials credentials = testProvider.getDefaultCredentials(transportFactory);
833+
834+
assertNotNull(credentials);
835+
assertTrue(credentials instanceof ComputeEngineCredentials);
836+
Map<String, String> credentialInfo = credentials.getCredentialInfo();
837+
assertEquals(
838+
String.format(
839+
"Metadata Server URL set to %s", ComputeEngineCredentials.DEFAULT_METADATA_SERVER_URL),
840+
credentials.getCredentialInfo().get("Credential Source"));
841+
assertEquals("Compute Engine Credentials", credentialInfo.get("Credential Name"));
842+
// Principal is null until the first MDS call
843+
assertNull(credentialInfo.get("Principal"));
844+
}
845+
846+
@Test
847+
public void getDefaultCredentials_computeEngineCredentials_customMDSUrl_correctCredentialInfo()
848+
throws IOException {
849+
MockMetadataServerTransportFactory transportFactory = new MockMetadataServerTransportFactory();
850+
TestDefaultCredentialsProvider testProvider = new TestDefaultCredentialsProvider();
851+
String gceMetadataHost = "8.8.8.8";
830852
testProvider.setEnv(DefaultCredentialsProvider.GCE_METADATA_HOST_ENV_VAR, gceMetadataHost);
831853

832854
GoogleCredentials credentials = testProvider.getDefaultCredentials(transportFactory);
@@ -835,9 +857,10 @@ public void getDefaultCredentials_computeEngineCredentials_correctCredentialInfo
835857
assertTrue(credentials instanceof ComputeEngineCredentials);
836858
Map<String, String> credentialInfo = credentials.getCredentialInfo();
837859
assertEquals(
838-
String.format("Metadata Server URL set to %s", gceMetadataHost),
860+
String.format("Metadata Server URL set to http://%s", gceMetadataHost),
839861
credentials.getCredentialInfo().get("Credential Source"));
840862
assertEquals("Compute Engine Credentials", credentialInfo.get("Credential Name"));
863+
// Principal is null until the first MDS call
841864
assertNull(credentialInfo.get("Principal"));
842865
}
843866

0 commit comments

Comments
 (0)