Skip to content

Commit

Permalink
rename handler class, fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aeitzman committed Jul 7, 2023
1 parent b9bda94 commit ab12315
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ abstract static class CredentialSource implements java.io.Serializable {
private final CredentialSource credentialSource;
private final Collection<String> scopes;
private final ServiceAccountImpersonationOptions serviceAccountImpersonationOptions;
private ByoidMetricsHandler metricsHandler;
private ExternalAccountMetricsHandler metricsHandler;

@Nullable private final String tokenInfoUrl;
@Nullable private final String serviceAccountImpersonationUrl;
Expand Down Expand Up @@ -226,7 +226,7 @@ protected ExternalAccountCredentials(
validateServiceAccountImpersonationInfoUrl(serviceAccountImpersonationUrl);
}

this.metricsHandler = new ByoidMetricsHandler(this);
this.metricsHandler = new ExternalAccountMetricsHandler(this);

this.impersonatedCredentials = buildImpersonatedCredentials();
}
Expand Down Expand Up @@ -279,7 +279,9 @@ protected ExternalAccountCredentials(ExternalAccountCredentials.Builder builder)
}

this.metricsHandler =
builder.metricsHandler == null ? new ByoidMetricsHandler(this) : builder.metricsHandler;
builder.metricsHandler == null
? new ExternalAccountMetricsHandler(this)
: builder.metricsHandler;

this.impersonatedCredentials = buildImpersonatedCredentials();
}
Expand Down Expand Up @@ -515,7 +517,7 @@ protected AccessToken exchangeExternalCredentialForAccessToken(
// Set BYOID Metrics header.
HttpHeaders additionalHeaders = new HttpHeaders();
additionalHeaders.set(
MetricsUtils.API_CLIENT_HEADER, this.metricsHandler.getByoidMetricsHeader());
MetricsUtils.API_CLIENT_HEADER, this.metricsHandler.getExternalAccountMetricsHeader());
requestHandler.setHeaders(additionalHeaders);

if (stsTokenExchangeRequest.getInternalOptions() != null) {
Expand Down Expand Up @@ -734,7 +736,7 @@ public abstract static class Builder extends GoogleCredentials.Builder {
@Nullable protected String workforcePoolUserProject;
@Nullable protected ServiceAccountImpersonationOptions serviceAccountImpersonationOptions;
@Nullable protected String universeDomain;
@Nullable protected ByoidMetricsHandler metricsHandler;
@Nullable protected ExternalAccountMetricsHandler metricsHandler;

protected Builder() {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,38 +35,39 @@
* A handler for generating the x-goog-api-client header value for BYOID external account
* credentials.
*/
class ByoidMetricsHandler implements java.io.Serializable {
class ExternalAccountMetricsHandler implements java.io.Serializable {
private static final String SOURCE_KEY = "source";
private static final String IMPERSONATION_KEY = "sa-impersonation";
private static final String CONFIG_LIFETIME_KEY = "config-lifetime";
private static final String BYOID_METRICS_SECTION = "google-byoid-sdk";

private final boolean configLifetime;
private final boolean saImpersonation;
private String source;

/**
* Constructor for the BYOID Metrics Handler.
* Constructor for the external account metrics handler.
*
* @param creds the {@code ExternalAccountCredentials} object to set the BYOID metrics options
* from.
* @param creds the {@code ExternalAccountCredentials} object to set the external account metrics
* options from.
*/
ByoidMetricsHandler(ExternalAccountCredentials creds) {
ExternalAccountMetricsHandler(ExternalAccountCredentials creds) {
this.saImpersonation = creds.getServiceAccountImpersonationUrl() != null;
this.configLifetime =
creds.getServiceAccountImpersonationOptions().customTokenLifetimeRequested;
this.source = creds.getCredentialSourceType();
}

/**
* Gets the BYOID metrics header value for the x-goog-api-client header.
* Gets the external account metrics header value for the x-goog-api-client header.
*
* @return the header value.
*/
String getByoidMetricsHeader() {
String getExternalAccountMetricsHeader() {
return String.format(
"%s %s %s/%s %s/%s %s/%s",
MetricsUtils.API_CLIENT_HEADER,
MetricsUtils.getLanguageAndAuthLibraryVersions(),
BYOID_METRICS_SECTION,
SOURCE_KEY,
this.source,
IMPERSONATION_KEY,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1280,8 +1280,7 @@ static void validateMetricsHeader(
String actualMetricsValue = headers.get(MetricsUtils.API_CLIENT_HEADER).get(0);
String expectedMetricsValue =
String.format(
"%s %s source/%s sa-impersonation/%s config-lifetime/%s",
MetricsUtils.API_CLIENT_HEADER,
"%s google-byoid-sdk source/%s sa-impersonation/%s config-lifetime/%s",
MetricsUtils.getLanguageAndAuthLibraryVersions(),
source,
saImpersonationUsed,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
@RunWith(JUnit4.class)
public class MetricsUtilsTest {

public static void assertVersion(String version) {
public static void assertVersions(String version) {
assertNotNull("version constant should not be null", version);
Pattern semverPattern =
Pattern.compile("gl-java/[\\d\\._-]+ auth/\\d+\\.\\d+\\.\\d+(-sp\\.\\d+)?(-SNAPSHOT)?");
Expand All @@ -51,6 +51,6 @@ public static void assertVersion(String version) {
@Test
public void getVersionWorks() {
String version = MetricsUtils.getLanguageAndAuthLibraryVersions();
assertVersion(version);
assertVersions(version);
}
}

0 comments on commit ab12315

Please sign in to comment.