From 7447f0bc5e1adca191978f2c813860e03f130a26 Mon Sep 17 00:00:00 2001 From: Riya Mehta Date: Fri, 25 Oct 2024 09:20:14 -0700 Subject: [PATCH] run linter. --- .../java/com/google/auth/oauth2/S2A.java | 64 ++++++++++--------- .../com/google/auth/oauth2/S2AConfig.java | 8 +-- .../oauth2/MockMetadataServerTransport.java | 4 +- .../com/google/auth/oauth2/S2ATest.java | 1 - 4 files changed, 40 insertions(+), 37 deletions(-) diff --git a/oauth2_http/java/com/google/auth/oauth2/S2A.java b/oauth2_http/java/com/google/auth/oauth2/S2A.java index eead5b4e7..067eb4647 100644 --- a/oauth2_http/java/com/google/auth/oauth2/S2A.java +++ b/oauth2_http/java/com/google/auth/oauth2/S2A.java @@ -45,7 +45,6 @@ import java.io.InputStream; import java.util.Arrays; import java.util.HashSet; -import java.util.Optional; import java.util.ServiceLoader; import java.util.Set; import javax.annotation.concurrent.ThreadSafe; @@ -64,9 +63,11 @@ public final class S2A { static final String METADATA_FLAVOR = "Metadata-Flavor"; static final String GOOGLE = "Google"; - private static final Set RETRYABLE_STATUS_CODES = new HashSet<>(Arrays.asList(500, 502, 503)); + private static final Set RETRYABLE_STATUS_CODES = + new HashSet<>(Arrays.asList(500, 502, 503)); private static final String PARSE_ERROR_S2A = "Error parsing S2A Config from MDS JSON response."; - private static final String MDS_MTLS_ENDPOINT = ComputeEngineCredentials.getMetadataServerUrl() + S2A_CONFIG_ENDPOINT_POSTFIX; + private static final String MDS_MTLS_ENDPOINT = + ComputeEngineCredentials.getMetadataServerUrl() + S2A_CONFIG_ENDPOINT_POSTFIX; private S2AConfig config; @@ -114,22 +115,22 @@ public S2A build() { /** * Queries the MDS mTLS Autoconfiguration endpoint and returns the {@link S2AConfig}. * - *

Returns {@link S2AConfig}. If S2A is not running, or if any error occurs when - * making the request to MDS / processing the response, {@link S2AConfig} will be - * populated with empty addresses. - * - * Users are expected to try to fetch the mTLS-S2A address first (via - * {@link getMtlsS2AAddress}). If it is empty or they have some problem loading the - * mTLS-MDS credentials, they should then fallback to fetching the plaintext-S2A address - * (via {@link getPlaintextS2AAddress}). If the plaintext-S2A address is empty it means - * that an error occurred when talking to the MDS / processing the response or that S2A - * is not running in the environment; in either case this indicates S2A shouldn't be used. + *

Returns {@link S2AConfig}. If S2A is not running, or if any error occurs when making the + * request to MDS / processing the response, {@link S2AConfig} will be populated with empty + * addresses. + * + *

Users are expected to try to fetch the mTLS-S2A address first (via {@link + * getMtlsS2AAddress}). If it is empty or they have some problem loading the mTLS-MDS credentials, + * they should then fallback to fetching the plaintext-S2A address (via {@link + * getPlaintextS2AAddress}). If the plaintext-S2A address is empty it means that an error occurred + * when talking to the MDS / processing the response or that S2A is not running in the + * environment; in either case this indicates S2A shouldn't be used. * * @return the {@link S2AConfig}. */ private S2AConfig getS2AConfigFromMDS() { GenericUrl genericUrl = new GenericUrl(MDS_MTLS_ENDPOINT); - JsonObjectParser parser = new JsonObjectParser(OAuth2Utils.JSON_FACTORY); + JsonObjectParser parser = new JsonObjectParser(OAuth2Utils.JSON_FACTORY); if (transportFactory == null) { transportFactory = Iterables.getFirst( @@ -138,26 +139,24 @@ private S2AConfig getS2AConfigFromMDS() { HttpRequest request; try { - request = - transportFactory.create().createRequestFactory().buildGetRequest(genericUrl); + request = transportFactory.create().createRequestFactory().buildGetRequest(genericUrl); request.setParser(parser); request.getHeaders().set(METADATA_FLAVOR, GOOGLE); request.setThrowExceptionOnExecuteError(false); request.setNumberOfRetries(OAuth2Utils.DEFAULT_NUMBER_OF_RETRIES); ExponentialBackOff backoff = - new ExponentialBackOff.Builder() - .setInitialIntervalMillis(OAuth2Utils.INITIAL_RETRY_INTERVAL_MILLIS) - .setRandomizationFactor(OAuth2Utils.RETRY_RANDOMIZATION_FACTOR) - .setMultiplier(OAuth2Utils.RETRY_MULTIPLIER) - .build(); + new ExponentialBackOff.Builder() + .setInitialIntervalMillis(OAuth2Utils.INITIAL_RETRY_INTERVAL_MILLIS) + .setRandomizationFactor(OAuth2Utils.RETRY_RANDOMIZATION_FACTOR) + .setMultiplier(OAuth2Utils.RETRY_MULTIPLIER) + .build(); // Retry on 5xx status codes. request.setUnsuccessfulResponseHandler( - new HttpBackOffUnsuccessfulResponseHandler(backoff) - .setBackOffRequired( - response -> - RETRYABLE_STATUS_CODES.contains(response.getStatusCode()))); + new HttpBackOffUnsuccessfulResponseHandler(backoff) + .setBackOffRequired( + response -> RETRYABLE_STATUS_CODES.contains(response.getStatusCode()))); request.setIOExceptionHandler(new HttpBackOffIOExceptionHandler(backoff)); } catch (IOException e) { return S2AConfig.createBuilder().build(); @@ -174,11 +173,15 @@ private S2AConfig getS2AConfigFromMDS() { GenericData responseData = response.parseAs(GenericData.class); try { plaintextS2AAddress = - OAuth2Utils.validateString(responseData, S2A_PLAINTEXT_ADDRESS_JSON_KEY, PARSE_ERROR_S2A); - } catch (IOException ignore) {} - try { - mtlsS2AAddress = OAuth2Utils.validateString(responseData, S2A_MTLS_ADDRESS_JSON_KEY, PARSE_ERROR_S2A); - } catch (IOException ignore) {} + OAuth2Utils.validateString( + responseData, S2A_PLAINTEXT_ADDRESS_JSON_KEY, PARSE_ERROR_S2A); + } catch (IOException ignore) { + } + try { + mtlsS2AAddress = + OAuth2Utils.validateString(responseData, S2A_MTLS_ADDRESS_JSON_KEY, PARSE_ERROR_S2A); + } catch (IOException ignore) { + } } catch (IOException ignore) { /* * Return empty addresses in {@link S2AConfig} once all retries have been exhausted. @@ -189,6 +192,5 @@ private S2AConfig getS2AConfigFromMDS() { .setPlaintextAddress(plaintextS2AAddress) .setMtlsAddress(mtlsS2AAddress) .build(); - } } diff --git a/oauth2_http/java/com/google/auth/oauth2/S2AConfig.java b/oauth2_http/java/com/google/auth/oauth2/S2AConfig.java index 97e8c883e..6b9adb543 100644 --- a/oauth2_http/java/com/google/auth/oauth2/S2AConfig.java +++ b/oauth2_http/java/com/google/auth/oauth2/S2AConfig.java @@ -66,20 +66,20 @@ public static final class Builder { @CanIgnoreReturnValue public Builder setPlaintextAddress(String plaintextAddress) { - /* + /* * No validation / format check is necessary here. It is up to the client which consumes this address * to return error if there is a problem connecting to S2A at that address. - */ + */ this.plaintextAddress = plaintextAddress; return this; } @CanIgnoreReturnValue public Builder setMtlsAddress(String mtlsAddress) { - /* + /* * No validation / format check is necessary here. It is up to the client which consumes this address * to return error if there is a problem connecting to S2A at that address. - */ + */ this.mtlsAddress = mtlsAddress; return this; } diff --git a/oauth2_http/javatests/com/google/auth/oauth2/MockMetadataServerTransport.java b/oauth2_http/javatests/com/google/auth/oauth2/MockMetadataServerTransport.java index c810bee68..f8eaf8b84 100644 --- a/oauth2_http/javatests/com/google/auth/oauth2/MockMetadataServerTransport.java +++ b/oauth2_http/javatests/com/google/auth/oauth2/MockMetadataServerTransport.java @@ -355,6 +355,8 @@ protected boolean isMtlsConfigRequestUrl(String url) { && plaintextS2AAddress != null && mtlsS2AAddress != null && mtlsS2AAddressJsonKey != null - && url.equals(String.format(ComputeEngineCredentials.getMetadataServerUrl() + S2A.S2A_CONFIG_ENDPOINT_POSTFIX)); + && url.equals( + String.format( + ComputeEngineCredentials.getMetadataServerUrl() + S2A.S2A_CONFIG_ENDPOINT_POSTFIX)); } } diff --git a/oauth2_http/javatests/com/google/auth/oauth2/S2ATest.java b/oauth2_http/javatests/com/google/auth/oauth2/S2ATest.java index 5ef3af3fa..b8850ebe7 100644 --- a/oauth2_http/javatests/com/google/auth/oauth2/S2ATest.java +++ b/oauth2_http/javatests/com/google/auth/oauth2/S2ATest.java @@ -35,7 +35,6 @@ import com.google.api.client.http.HttpStatusCodes; import com.google.auth.oauth2.ComputeEngineCredentialsTest.MockMetadataServerTransportFactory; -import java.util.Optional; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4;