Skip to content

Commit

Permalink
Use sql instance name in SecretManager (#2625)
Browse files Browse the repository at this point in the history
  • Loading branch information
weiminyu authored Dec 18, 2024
1 parent c9c61e4 commit d130e74
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 23 deletions.
21 changes: 0 additions & 21 deletions core/src/main/java/google/registry/config/RegistryConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -384,27 +384,6 @@ public static String provideCloudSqlJdbcUrl(RegistryConfigSettings config) {
return config.cloudSql.jdbcUrl;
}

@Provides
@Config("cloudSqlInstanceConnectionName")
public static String provideCloudSqlInstanceConnectionName(RegistryConfigSettings config) {
return config.cloudSql.instanceConnectionName;
}

@Provides
@Config("cloudSqlReplicaInstanceConnectionName")
public static Optional<String> provideCloudSqlReplicaInstanceConnectionName(
RegistryConfigSettings config) {
return Optional.ofNullable(config.cloudSql.replicaInstanceConnectionName);
}

@Provides
@Config("cloudSqlDbInstanceName")
public static String provideCloudSqlDbInstance(RegistryConfigSettings config) {
// Format of instanceConnectionName: project-id:region:instance-name
int lastColonIndex = config.cloudSql.instanceConnectionName.lastIndexOf(':');
return config.cloudSql.instanceConnectionName.substring(lastColonIndex + 1);
}

@Provides
@Config("cloudDnsRootUrl")
public static Optional<String> getCloudDnsRootUrl(RegistryConfigSettings config) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public static class Hibernate {
/** Configuration for Cloud SQL. */
public static class CloudSql {
public String jdbcUrl;
// TODO(05012021): remove username field after it is removed from all yaml files.
// TODO(05012021): remove 3 fields below after they are removed from all yaml files.
public String username;
public String instanceConnectionName;
public String replicaInstanceConnectionName;
Expand Down
22 changes: 22 additions & 0 deletions core/src/main/java/google/registry/keyring/KeyringModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import google.registry.config.RegistryConfig.Config;
import google.registry.keyring.api.Keyring;
import java.util.Map;
import java.util.Optional;
import javax.inject.Singleton;

/** Dagger module for {@link Keyring} */
Expand All @@ -38,4 +39,25 @@ public static Keyring provideKeyring(
keyrings.keySet());
return keyrings.get(activeKeyring);
}

@Provides
@Config("cloudSqlInstanceConnectionName")
public static String provideCloudSqlInstanceConnectionName(Keyring keyring) {
return keyring.getSqlPrimaryConnectionName();
}

@Provides
@Config("cloudSqlReplicaInstanceConnectionName")
public static Optional<String> provideCloudSqlReplicaInstanceConnectionName(Keyring keyring) {
return Optional.ofNullable(keyring.getSqlReplicaConnectionName());
}

@Provides
@Config("cloudSqlDbInstanceName")
public static String provideCloudSqlDbInstance(
@Config("cloudSqlInstanceConnectionName") String instanceConnectionName) {
// Format of instanceConnectionName: project-id:region:instance-name
int lastColonIndex = instanceConnectionName.lastIndexOf(':');
return instanceConnectionName.substring(lastColonIndex + 1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,13 @@
import dagger.Module;
import dagger.Provides;
import google.registry.config.RegistryConfig.Config;
import google.registry.keyring.KeyringModule;
import google.registry.keyring.api.DummyKeyringModule;
import google.registry.persistence.transaction.CloudSqlCredentialSupplier;
import google.registry.persistence.transaction.JpaTransactionManager;
import google.registry.persistence.transaction.JpaTransactionManagerImpl;
import google.registry.persistence.transaction.TransactionManager;
import google.registry.privileges.secretmanager.SecretManagerModule;
import google.registry.privileges.secretmanager.SqlCredential;
import google.registry.privileges.secretmanager.SqlCredentialStore;
import google.registry.privileges.secretmanager.SqlUser;
Expand All @@ -63,7 +66,7 @@
import org.hibernate.cfg.Environment;

/** Dagger module class for the persistence layer. */
@Module
@Module(includes = {KeyringModule.class, SecretManagerModule.class, DummyKeyringModule.class})
public abstract class PersistenceModule {

// This name must be the same as the one defined in persistence.xml.
Expand Down

0 comments on commit d130e74

Please sign in to comment.