Skip to content

Commit d0547e3

Browse files
committed
chore: address review comments for PR #1896
1 parent 6980520 commit d0547e3

4 files changed

Lines changed: 21 additions & 28 deletions

File tree

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

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,16 @@ public class GdchCredentials extends GoogleCredentials {
7575
private static final LoggerProvider LOGGER_PROVIDER =
7676
LoggerProvider.forClazz(GdchCredentials.class);
7777
private static final String PARSE_ERROR_PREFIX = "Error parsing token refresh response. ";
78-
@VisibleForTesting static final String SUPPORTED_FORMAT_VERSION = "1";
7978

79+
/**
80+
* The expected format version for GDCH credential profiles.
81+
* Version "1" indicates the initial and currently supported JSON format for these credentials.
82+
* See go/gdch-python-auth-lib for more info.
83+
*/
84+
@VisibleForTesting static final String SUPPORTED_JSON_FORMAT_VERSION = "1";
85+
86+
// Custom URN used by GDCH to identify service account tokens in token exchange requests.
87+
// See go/gdch-python-auth-lib for more information.
8088
private static final String SERVICE_ACCOUNT_TOKEN_TYPE =
8189
"urn:k8s:params:oauth:token-type:serviceaccount";
8290

@@ -198,9 +206,9 @@ static GdchCredentials fromJson(Map<String, Object> json, HttpTransportFactory t
198206
validateField((String) json.get("token_uri"), "token_uri");
199207
String caCertPath = (String) json.get("ca_cert_path");
200208

201-
if (!SUPPORTED_FORMAT_VERSION.equals(formatVersion)) {
209+
if (!SUPPORTED_JSON_FORMAT_VERSION.equals(formatVersion)) {
202210
throw new IOException(
203-
String.format("Only format version %s is supported.", SUPPORTED_FORMAT_VERSION));
211+
String.format("Only format version %s is supported.", SUPPORTED_JSON_FORMAT_VERSION));
204212
}
205213

206214
URI tokenServerUriFromCreds = null;
@@ -247,10 +255,8 @@ static GdchCredentials fromPkcs8(String privateKeyPkcs8, GdchCredentials.Builder
247255
*/
248256
@ObsoleteApi("Use createWithGdchAudience(String) instead.")
249257
public GdchCredentials createWithGdchAudience(URI apiAudience) {
250-
if (apiAudience == null) {
251-
throw new IllegalArgumentException(
252-
"Audience cannot be null or empty for GDCH service account credentials.");
253-
}
258+
Preconditions.checkNotNull(
259+
apiAudience, "Audience are not configured for GDCH service account credentials.");
254260
return this.toBuilder().setGdchAudience(apiAudience.toString()).build();
255261
}
256262

@@ -372,6 +378,9 @@ static String getIssuerSubjectValue(String projectId, String serviceIdentityName
372378
return String.format("system:serviceaccount:%s:%s", projectId, serviceIdentityName);
373379
}
374380

381+
/**
382+
* @return the projectId set in the GDCH SA Key file or the user set projectId
383+
*/
375384
@Override
376385
public final String getProjectId() {
377386
return projectId;
@@ -571,16 +580,7 @@ public Builder setGdchAudience(String apiAudience) {
571580
return this;
572581
}
573582

574-
@CanIgnoreReturnValue
575-
@ObsoleteApi("Use setGdchAudience(String) instead")
576-
public Builder setGdchAudience(URI apiAudience) {
577-
if (apiAudience == null) {
578-
throw new IllegalArgumentException(
579-
"Audience cannot be null for GDCH service account credentials.");
580-
}
581-
this.apiAudience = apiAudience.toString();
582-
return this;
583-
}
583+
584584

585585
public String getProjectId() {
586586
return projectId;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class DefaultCredentialsProviderTest {
8383
private static final String SA_PRIVATE_KEY_ID = "d84a4fefcf50791d4a90f2d7af17469d6282df9d";
8484
private static final String SA_PRIVATE_KEY_PKCS8 =
8585
ServiceAccountCredentialsTest.PRIVATE_KEY_PKCS8;
86-
private static final String GDCH_SA_FORMAT_VERSION = GdchCredentials.SUPPORTED_FORMAT_VERSION;
86+
private static final String GDCH_SA_FORMAT_VERSION = GdchCredentials.SUPPORTED_JSON_FORMAT_VERSION;
8787
private static final String GDCH_SA_PROJECT_ID = "gdch-service-account-project-id";
8888
private static final String GDCH_SA_PRIVATE_KEY_ID = "d84a4fefcf50791d4a90f2d7af17469d6282df9d";
8989
private static final String GDCH_SA_PRIVATE_KEY_PKC8 = GdchCredentialsTest.PRIVATE_KEY_PKCS8;

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

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767

6868
/** Test case for {@link GdchCredentials}. */
6969
class GdchCredentialsTest extends BaseSerializationTest {
70-
private static final String FORMAT_VERSION = GdchCredentials.SUPPORTED_FORMAT_VERSION;
70+
private static final String FORMAT_VERSION = GdchCredentials.SUPPORTED_JSON_FORMAT_VERSION;
7171
private static final String PRIVATE_KEY_ID = "d84a4fefcf50791d4a90f2d7af17469d6282df9d";
7272
static final String PRIVATE_KEY_PKCS8 =
7373
"-----BEGIN PRIVATE KEY-----\n"
@@ -1201,14 +1201,7 @@ void builder_setGdchAudience_nullString() {
12011201
.contains("Audience cannot be null or empty for GDCH service account credentials."));
12021202
}
12031203

1204-
@Test
1205-
void builder_setGdchAudience_nullUri() {
1206-
GdchCredentials.Builder builder = GdchCredentials.newBuilder();
1207-
IllegalArgumentException ex =
1208-
assertThrows(IllegalArgumentException.class, () -> builder.setGdchAudience((URI) null));
1209-
assertTrue(
1210-
ex.getMessage().contains("Audience cannot be null for GDCH service account credentials."));
1211-
}
1204+
12121205

12131206
static GenericJson writeGdchServiceAccountJson(
12141207
String formatVersion,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class GoogleCredentialsTest extends BaseSerializationTest {
7171
private static final String SA_PRIVATE_KEY_ID = "d84a4fefcf50791d4a90f2d7af17469d6282df9d";
7272
private static final String SA_PRIVATE_KEY_PKCS8 =
7373
ServiceAccountCredentialsTest.PRIVATE_KEY_PKCS8;
74-
private static final String GDCH_SA_FORMAT_VERSION = GdchCredentials.SUPPORTED_FORMAT_VERSION;
74+
private static final String GDCH_SA_FORMAT_VERSION = GdchCredentials.SUPPORTED_JSON_FORMAT_VERSION;
7575
private static final String GDCH_SA_PROJECT_ID = "gdch-service-account-project-id";
7676
private static final String GDCH_SA_PRIVATE_KEY_ID = "d84a4fefcf50791d4a90f2d7af17469d6282df9d";
7777
private static final String GDCH_SA_PRIVATE_KEY_PKC8 = GdchCredentialsTest.PRIVATE_KEY_PKCS8;

0 commit comments

Comments
 (0)