Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,111 @@

package google.registry.persistence.transaction;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import static java.util.function.Predicate.not;

import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.auth.http.HttpCredentialsAdapter;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.sql.CredentialFactory;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.CharMatcher;
import java.io.IOException;
import java.util.Optional;
import javax.annotation.Nullable;

/**
* Supplier class to provide credentials for the Cloud SQL JDBC Socket Factory.
*
* <p>When {@code CLOUDSDK_CORE_REQUEST_REASON} is set in the environment, outgoing Cloud SQL Admin
* API requests also include it in the {@code X-Goog-Request-Reason} HTTP header.
*/
public final class CloudSqlCredentialSupplier implements CredentialFactory {

private static final String REQUEST_REASON_ENV_VAR = "CLOUDSDK_CORE_REQUEST_REASON";
private static final String REQUEST_REASON_HEADER = "X-Goog-Request-Reason";
private static final CharMatcher PRINTABLE_ASCII = CharMatcher.inRange(' ', '~');

/** Supplier class to provide Credential for Cloud SQL library. */
public class CloudSqlCredentialSupplier implements CredentialFactory {
private static GoogleCredentials credential;
@Nullable private static volatile SupplierConfig config;

/** Initialize the supplier with given credential json and scopes. */
/** Required public no-arg constructor for {@link CredentialFactory} reflective instantiation. */
public CloudSqlCredentialSupplier() {}

/** Initializes the supplier with the given credential and environment request reason, if any. */
public static void setupCredentialSupplier(GoogleCredentials credential) {
setupCredentialSupplier(credential, System.getenv(REQUEST_REASON_ENV_VAR));
}

@VisibleForTesting
static void setupCredentialSupplier(
GoogleCredentials credential, @Nullable String requestReason) {
GoogleCredentials validatedCredential = checkNotNull(credential, "credential");
Optional<String> normalizedReason = normalizeRequestReason(requestReason);
config = new SupplierConfig(validatedCredential, normalizedReason);
System.setProperty(
CredentialFactory.CREDENTIAL_FACTORY_PROPERTY, CloudSqlCredentialSupplier.class.getName());
CloudSqlCredentialSupplier.credential = credential;
}

@VisibleForTesting
static void resetForTesting() {
config = null;
}

@Override
public HttpRequestInitializer create() {
return new HttpCredentialsAdapter(credential);
SupplierConfig currentConfig = getConfig();
Optional<String> requestReason = currentConfig.requestReason();
if (requestReason.isEmpty()) {
return new HttpCredentialsAdapter(currentConfig.credential());
}
return new RequestReasonCredentialsAdapter(currentConfig.credential(), requestReason.get());
}

@Override
public GoogleCredentials getCredentials() {
return getConfig().credential();
}

private static SupplierConfig getConfig() {
SupplierConfig currentConfig = config;
checkState(currentConfig != null, "CloudSqlCredentialSupplier has not been initialized");
return currentConfig;
}

private static Optional<String> normalizeRequestReason(@Nullable String requestReason) {
// Use Guava's CharMatcher.whitespace() rather than String.strip() so Unicode non-breaking
// spaces (such as U+00A0) are also trimmed.
Optional<String> normalized =
Optional.ofNullable(requestReason)
.map(CharMatcher.whitespace()::trimFrom)
.filter(not(String::isEmpty));
normalized.ifPresent(
reason ->
checkArgument(
PRINTABLE_ASCII.matchesAllOf(reason),
"%s must contain only printable ASCII characters",
REQUEST_REASON_ENV_VAR));
return normalized;
}

private record SupplierConfig(GoogleCredentials credential, Optional<String> requestReason) {}

private static final class RequestReasonCredentialsAdapter extends HttpCredentialsAdapter {

private final String requestReason;

private RequestReasonCredentialsAdapter(GoogleCredentials credential, String requestReason) {
super(credential);
this.requestReason = requestReason;
}

@Override
public void initialize(HttpRequest request) throws IOException {
super.initialize(request);
request.getHeaders().set(REQUEST_REASON_HEADER, requestReason);
}
}
Comment on lines +80 to 123

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Must-Fix (Asymmetric uninitialized-state error handling between create() and getCredentials()):

Calling create() (lines 59-71) before setupCredentialSupplier(...) passes null into HttpCredentialsAdapter.<init>, which throws a message-less NullPointerException from inside google-auth-library, whereas getCredentials() throws NullPointerException("Credential has not been initialized").

Unify both methods through a getConfig() helper using Preconditions.checkState (IllegalStateException is more appropriate than NullPointerException for uninitialized state):

private static SupplierConfig getConfig() {
  SupplierConfig currentConfig = config;
  checkState(currentConfig != null, "CloudSqlCredentialSupplier has not been initialized");
  return currentConfig;
}

(Keeping the getCredentials() override itself is good — it avoids allocating a throwaway HttpCredentialsAdapter when callers only need GoogleCredentials.)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in f5a95ac: unified both create() and getCredentials() through getConfig(), throwing IllegalStateException via Preconditions.checkState when uninitialized.

}
Loading
Loading