Skip to content

Commit

Permalink
run linter.
Browse files Browse the repository at this point in the history
  • Loading branch information
rmehta19 committed Oct 25, 2024
1 parent 4d05638 commit 7447f0b
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 37 deletions.
64 changes: 33 additions & 31 deletions oauth2_http/java/com/google/auth/oauth2/S2A.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -64,9 +63,11 @@ public final class S2A {

static final String METADATA_FLAVOR = "Metadata-Flavor";
static final String GOOGLE = "Google";
private static final Set<Integer> RETRYABLE_STATUS_CODES = new HashSet<>(Arrays.asList(500, 502, 503));
private static final Set<Integer> 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;

Expand Down Expand Up @@ -114,22 +115,22 @@ public S2A build() {
/**
* Queries the MDS mTLS Autoconfiguration endpoint and returns the {@link S2AConfig}.
*
* <p>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.
* <p>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.
*
* <p>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(
Expand All @@ -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();
Expand All @@ -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.
Expand All @@ -189,6 +192,5 @@ private S2AConfig getS2AConfigFromMDS() {
.setPlaintextAddress(plaintextS2AAddress)
.setMtlsAddress(mtlsS2AAddress)
.build();

}
}
8 changes: 4 additions & 4 deletions oauth2_http/java/com/google/auth/oauth2/S2AConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
1 change: 0 additions & 1 deletion oauth2_http/javatests/com/google/auth/oauth2/S2ATest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 7447f0b

Please sign in to comment.