Fix nomulus tool when the environment is localhost (#2365)

Also only caches/resets the original TM when in unit tests (TBT I'm not so sure
that even this is necessary as we don't seem to call the tool from tests
that often. There is only ShellCommandTest that calls the run() function
in RegistryCli and we could just put these tests in fragileTest and make
them run sequentially and fork every time to get around issue with
inference).

The issue with caching is that it tries to first create the to-be-cached
TM, and when the environment given is prod/sandbox/... It will try to
retrieve SQL credentials from prod/sandbox/... secret manager. This
works fine locally as we all have access to prod/sandbox/..., but fails
in Cloud Build jobs such as sync-db-objects where it provides it own
credential that has direct SQL access, but not access to
prod/sandbox/... secret manager.

TESTED=ran `./gradlew devTool --args="-e localhost generate_sql_er_diagram -o ../db/src/main/resources/sql/er_diagram"`
This commit is contained in:
Lai Jiang 2024-03-13 00:49:07 -04:00 committed by GitHub
parent d0b036227a
commit bdc9a1fd1d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 16 additions and 4 deletions

View file

@ -47,7 +47,7 @@ class GradleFlag:
PROPERTIES_HEADER = """\
# This file defines properties used by the gradle build. It must be kept in
# This file defines properties used by the gradle build. It must be kept in
# sync with config/nom_build.py.
#
# To regenerate, run ./nom_build --generate-gradle-properties

View file

@ -18,6 +18,7 @@ import static com.google.common.base.Preconditions.checkState;
import static google.registry.util.PreconditionsUtils.checkArgumentNotNull;
import com.google.common.base.Suppliers;
import com.google.common.collect.ImmutableSet;
import google.registry.persistence.DaggerPersistenceComponent;
import google.registry.tools.RegistryToolEnvironment;
import google.registry.util.NonFinalForTesting;
@ -27,6 +28,9 @@ import java.util.function.Supplier;
/** Factory class to create {@link TransactionManager} instance. */
public final class TransactionManagerFactory {
private static final ImmutableSet<RegistryEnvironment> NON_SERVING_ENVS =
ImmutableSet.of(RegistryEnvironment.UNITTEST, RegistryEnvironment.LOCAL);
/** Supplier for jpaTm so that it is initialized only once, upon first usage. */
@NonFinalForTesting
private static Supplier<JpaTransactionManager> jpaTm =
@ -41,7 +45,7 @@ public final class TransactionManagerFactory {
private static JpaTransactionManager createJpaTransactionManager() {
// If we are running a nomulus command, jpaTm will be injected in RegistryCli.java
// by calling setJpaTm().
if (RegistryEnvironment.get() != RegistryEnvironment.UNITTEST) {
if (!NON_SERVING_ENVS.contains(RegistryEnvironment.get())) {
return DaggerPersistenceComponent.create().jpaTransactionManager();
} else {
return DummyJpaTransactionManager.create();

View file

@ -30,6 +30,7 @@ import google.registry.persistence.transaction.JpaTransactionManager;
import google.registry.persistence.transaction.TransactionManagerFactory;
import google.registry.tools.AuthModule.LoginRequiredException;
import google.registry.tools.params.ParameterFactory;
import google.registry.util.RegistryEnvironment;
import java.security.Security;
import java.util.Map;
import java.util.Optional;
@ -218,12 +219,19 @@ final class RegistryCli implements CommandRunner {
// Reset the JPA transaction manager after every command to avoid a situation where a test can
// interfere with other tests
JpaTransactionManager cachedJpaTm = tm();
final JpaTransactionManager cachedJpaTm;
if (RegistryEnvironment.get() == RegistryEnvironment.UNITTEST) {
cachedJpaTm = tm();
} else {
cachedJpaTm = null;
}
TransactionManagerFactory.setJpaTm(() -> component.nomulusToolJpaTransactionManager().get());
TransactionManagerFactory.setReplicaJpaTm(
() -> component.nomulusToolReplicaJpaTransactionManager().get());
command.run();
TransactionManagerFactory.setJpaTm(() -> cachedJpaTm);
if (RegistryEnvironment.get() == RegistryEnvironment.UNITTEST) {
TransactionManagerFactory.setJpaTm(() -> cachedJpaTm);
}
}
void setEnvironment(RegistryToolEnvironment environment) {