From d2ee63cf69bf99cd5c2cb5e2b013a69ac33ff93d Mon Sep 17 00:00:00 2001 From: mcilwain Date: Thu, 17 Jan 2019 08:30:07 -0800 Subject: [PATCH] Consolidate Dagger modules for utils classes There was no reason to have several different modules all providing a single thing. This approach, which creates a single UtilsModule for everything in the util package, is cleaner. This also removes provisioning of Random and StringGenerator objects in RegistryConfig.ConfigModule, which don't belong there because they aren't configuration options. This also removes insecure random entirely; it was only used in a single place to generate 24 bytes a couple times per day. We can live with the lower speed if it means we don't have to worry about multiple types of Random, or possibly using an insecure random accidentally in a place that security actually does matter. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=229751915 --- .../registry/config/RegistryConfig.java | 57 +------------ .../module/backend/BackendComponent.java | 8 +- .../module/frontend/FrontendComponent.java | 8 +- .../module/pubapi/PubApiComponent.java | 8 +- .../registry/module/tools/ToolsComponent.java | 8 +- .../rde/imports/RdeDomainImportAction.java | 3 +- .../registry/tmch/NordnUploadAction.java | 3 +- .../tools/CreateAnchorTenantCommand.java | 4 +- .../registry/tools/CreateContactCommand.java | 4 +- .../registry/tools/CreateDomainCommand.java | 4 +- .../GenerateAllocationTokensCommand.java | 4 +- .../registry/tools/RegistryToolComponent.java | 8 +- .../registry/tools/SetupOteCommand.java | 4 +- .../registrar/ConsoleOteSetupAction.java | 3 +- .../ConsoleRegistrarCreatorAction.java | 5 +- .../util/AppEngineServiceUtilsImpl.java | 20 ----- java/google/registry/util/SystemClock.java | 17 +--- java/google/registry/util/SystemSleeper.java | 13 --- java/google/registry/util/UtilsModule.java | 82 +++++++++++++++++++ .../google/registry/dns/DnsTestComponent.java | 25 +++--- .../registry/tmch/NordnUploadActionTest.java | 4 +- .../registry/whois/WhoisTestComponent.java | 17 ++-- 22 files changed, 141 insertions(+), 168 deletions(-) create mode 100644 java/google/registry/util/UtilsModule.java diff --git a/java/google/registry/config/RegistryConfig.java b/java/google/registry/config/RegistryConfig.java index f3bc7bf1d..55ec838ec 100644 --- a/java/google/registry/config/RegistryConfig.java +++ b/java/google/registry/config/RegistryConfig.java @@ -29,19 +29,13 @@ import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import dagger.Module; import dagger.Provides; -import google.registry.util.RandomStringGenerator; -import google.registry.util.StringGenerator; import google.registry.util.TaskQueueUtils; import google.registry.util.YamlUtils; import java.lang.annotation.Documented; import java.lang.annotation.Retention; import java.net.URI; import java.net.URL; -import java.security.NoSuchAlgorithmException; -import java.security.ProviderException; -import java.security.SecureRandom; import java.util.Optional; -import java.util.Random; import java.util.stream.Collectors; import javax.annotation.Nullable; import javax.inject.Named; @@ -57,6 +51,9 @@ import org.joda.time.Duration; * meant for settings that need to be configured once. Settings which may be subject to * change in the future, should instead be retrieved from Datastore. The {@link * google.registry.model.registry.Registry Registry} class is one such example of this. + * + *

Note: Only settings that are actually configurable belong in this file. It's not a catch-all + * for anything widely used throughout the code base. */ public final class RegistryConfig { @@ -1356,54 +1353,6 @@ public final class RegistryConfig { .build()) .build(); } - - /** - * Returns a singleton insecure random number generator that is fast. - * - *

This binding is intentionally qualified so that any requester must explicitly acknowledge - * that using an insecure random number generator is fine for its use case. - */ - @Singleton - @Provides - @Config("insecureRandom") - public static Random provideInsecureRandom() { - return new Random(); - } - - /** Returns a singleton secure random number generator this is slow. */ - @Singleton - @Provides - public static SecureRandom provideSecureRandom() { - try { - return SecureRandom.getInstance("NativePRNG"); - } catch (NoSuchAlgorithmException e) { - throw new ProviderException(e); - } - } - - /** Returns a singleton random string generator that uses digits only. */ - @Singleton - @Provides - @Config("digitOnlyStringGenerator") - public static StringGenerator provideDigitsOnlyStringGenerator(SecureRandom secureRandom) { - return new RandomStringGenerator(StringGenerator.Alphabets.DIGITS_ONLY, secureRandom); - } - - /** Returns a singleton random string generator using Base58 encoding. */ - @Singleton - @Provides - @Config("base58StringGenerator") - public static StringGenerator provideBase58StringGenerator(SecureRandom secureRandom) { - return new RandomStringGenerator(StringGenerator.Alphabets.BASE_58, secureRandom); - } - - /** Returns a singleton random string generator using Base58 encoding. */ - @Singleton - @Provides - @Config("base64StringGenerator") - public static StringGenerator provideBase64StringGenerator(SecureRandom secureRandom) { - return new RandomStringGenerator(StringGenerator.Alphabets.BASE_64, secureRandom); - } } /** diff --git a/java/google/registry/module/backend/BackendComponent.java b/java/google/registry/module/backend/BackendComponent.java index 2f792cd9c..82cee8857 100644 --- a/java/google/registry/module/backend/BackendComponent.java +++ b/java/google/registry/module/backend/BackendComponent.java @@ -42,16 +42,13 @@ import google.registry.request.Modules.URLFetchServiceModule; import google.registry.request.Modules.UrlFetchTransportModule; import google.registry.request.Modules.UserServiceModule; import google.registry.request.auth.AuthModule; -import google.registry.util.AppEngineServiceUtilsImpl.AppEngineServiceUtilsModule; -import google.registry.util.SystemClock.SystemClockModule; -import google.registry.util.SystemSleeper.SystemSleeperModule; +import google.registry.util.UtilsModule; import javax.inject.Singleton; /** Dagger component with instance lifetime for "backend" App Engine module. */ @Singleton @Component( modules = { - AppEngineServiceUtilsModule.class, AuthModule.class, BackendRequestComponentModule.class, BigqueryModule.class, @@ -73,12 +70,11 @@ import javax.inject.Singleton; NetHttpTransportModule.class, SheetsServiceModule.class, StackdriverModule.class, - SystemClockModule.class, - SystemSleeperModule.class, URLFetchServiceModule.class, UrlFetchTransportModule.class, UserServiceModule.class, VoidDnsWriterModule.class, + UtilsModule.class }) interface BackendComponent { BackendRequestHandler requestHandler(); diff --git a/java/google/registry/module/frontend/FrontendComponent.java b/java/google/registry/module/frontend/FrontendComponent.java index 04314dbe2..56e867886 100644 --- a/java/google/registry/module/frontend/FrontendComponent.java +++ b/java/google/registry/module/frontend/FrontendComponent.java @@ -36,16 +36,13 @@ import google.registry.request.Modules.UrlFetchTransportModule; import google.registry.request.Modules.UserServiceModule; import google.registry.request.auth.AuthModule; import google.registry.ui.ConsoleDebug.ConsoleConfigModule; -import google.registry.util.AppEngineServiceUtilsImpl.AppEngineServiceUtilsModule; -import google.registry.util.SystemClock.SystemClockModule; -import google.registry.util.SystemSleeper.SystemSleeperModule; +import google.registry.util.UtilsModule; import javax.inject.Singleton; /** Dagger component with instance lifetime for "default" App Engine module. */ @Singleton @Component( modules = { - AppEngineServiceUtilsModule.class, AuthModule.class, ConfigModule.class, ConsoleConfigModule.class, @@ -63,10 +60,9 @@ import javax.inject.Singleton; NetHttpTransportModule.class, ServerTridProviderModule.class, StackdriverModule.class, - SystemClockModule.class, - SystemSleeperModule.class, UrlFetchTransportModule.class, UserServiceModule.class, + UtilsModule.class }) interface FrontendComponent { FrontendRequestHandler requestHandler(); diff --git a/java/google/registry/module/pubapi/PubApiComponent.java b/java/google/registry/module/pubapi/PubApiComponent.java index 1b65390a0..93e2b0c1a 100644 --- a/java/google/registry/module/pubapi/PubApiComponent.java +++ b/java/google/registry/module/pubapi/PubApiComponent.java @@ -35,16 +35,13 @@ import google.registry.request.Modules.NetHttpTransportModule; import google.registry.request.Modules.UrlFetchTransportModule; import google.registry.request.Modules.UserServiceModule; import google.registry.request.auth.AuthModule; -import google.registry.util.AppEngineServiceUtilsImpl.AppEngineServiceUtilsModule; -import google.registry.util.SystemClock.SystemClockModule; -import google.registry.util.SystemSleeper.SystemSleeperModule; +import google.registry.util.UtilsModule; import javax.inject.Singleton; /** Dagger component with instance lifetime for "pubapi" App Engine module. */ @Singleton @Component( modules = { - AppEngineServiceUtilsModule.class, AuthModule.class, ConfigModule.class, CredentialModule.class, @@ -61,10 +58,9 @@ import javax.inject.Singleton; PubApiRequestComponentModule.class, ServerTridProviderModule.class, StackdriverModule.class, - SystemClockModule.class, - SystemSleeperModule.class, UrlFetchTransportModule.class, UserServiceModule.class, + UtilsModule.class }) interface PubApiComponent { PubApiRequestHandler requestHandler(); diff --git a/java/google/registry/module/tools/ToolsComponent.java b/java/google/registry/module/tools/ToolsComponent.java index b7dc1747a..83eb02163 100644 --- a/java/google/registry/module/tools/ToolsComponent.java +++ b/java/google/registry/module/tools/ToolsComponent.java @@ -38,16 +38,13 @@ import google.registry.request.Modules.NetHttpTransportModule; import google.registry.request.Modules.UrlFetchTransportModule; import google.registry.request.Modules.UserServiceModule; import google.registry.request.auth.AuthModule; -import google.registry.util.AppEngineServiceUtilsImpl.AppEngineServiceUtilsModule; -import google.registry.util.SystemClock.SystemClockModule; -import google.registry.util.SystemSleeper.SystemSleeperModule; +import google.registry.util.UtilsModule; import javax.inject.Singleton; /** Dagger component with instance lifetime for "tools" App Engine module. */ @Singleton @Component( modules = { - AppEngineServiceUtilsModule.class, AuthModule.class, ConfigModule.class, CredentialModule.class, @@ -66,11 +63,10 @@ import javax.inject.Singleton; NetHttpTransportModule.class, ServerTridProviderModule.class, StackdriverModule.class, - SystemClockModule.class, - SystemSleeperModule.class, ToolsRequestComponentModule.class, UrlFetchTransportModule.class, UserServiceModule.class, + UtilsModule.class }) interface ToolsComponent { ToolsRequestHandler requestHandler(); diff --git a/java/google/registry/rde/imports/RdeDomainImportAction.java b/java/google/registry/rde/imports/RdeDomainImportAction.java index cc558a0c6..ddca063ff 100644 --- a/java/google/registry/rde/imports/RdeDomainImportAction.java +++ b/java/google/registry/rde/imports/RdeDomainImportAction.java @@ -60,6 +60,7 @@ import google.registry.xjc.rdedomain.XjcRdeDomain; import google.registry.xjc.rdedomain.XjcRdeDomainElement; import java.util.Optional; import javax.inject.Inject; +import javax.inject.Named; import org.joda.money.Money; import org.joda.time.DateTime; @@ -92,7 +93,7 @@ public class RdeDomainImportAction implements Runnable { @Config("rdeImportBucket") String importBucketName, @Parameter(PATH) String importFileName, @Parameter(PARAM_MAP_SHARDS) Optional mapShards, - @Config("base64StringGenerator") StringGenerator stringGenerator) { + @Named("base64StringGenerator") StringGenerator stringGenerator) { this.mrRunner = mrRunner; this.response = response; this.importBucketName = importBucketName; diff --git a/java/google/registry/tmch/NordnUploadAction.java b/java/google/registry/tmch/NordnUploadAction.java index 638d2204c..9033ccf19 100644 --- a/java/google/registry/tmch/NordnUploadAction.java +++ b/java/google/registry/tmch/NordnUploadAction.java @@ -52,6 +52,7 @@ import google.registry.util.TaskQueueUtils; import google.registry.util.UrlFetchException; import java.io.IOException; import java.net.URL; +import java.security.SecureRandom; import java.util.List; import java.util.Optional; import java.util.Random; @@ -90,7 +91,7 @@ public final class NordnUploadAction implements Runnable { @Inject Clock clock; @Inject Retrier retrier; - @Inject @Config("insecureRandom") Random random; + @Inject SecureRandom random; @Inject LordnRequestInitializer lordnRequestInitializer; @Inject URLFetchService fetchService; @Inject @Config("tmchMarksdbUrl") String tmchMarksdbUrl; diff --git a/java/google/registry/tools/CreateAnchorTenantCommand.java b/java/google/registry/tools/CreateAnchorTenantCommand.java index d7ceea2a6..d5606d2d9 100644 --- a/java/google/registry/tools/CreateAnchorTenantCommand.java +++ b/java/google/registry/tools/CreateAnchorTenantCommand.java @@ -25,10 +25,10 @@ import com.beust.jcommander.Parameter; import com.beust.jcommander.Parameters; import com.google.common.net.InternetDomainName; import com.google.template.soy.data.SoyMapData; -import google.registry.config.RegistryConfig.Config; import google.registry.tools.soy.CreateAnchorTenantSoyInfo; import google.registry.util.StringGenerator; import javax.inject.Inject; +import javax.inject.Named; import org.joda.money.Money; import org.joda.time.DateTime; @@ -73,7 +73,7 @@ final class CreateAnchorTenantCommand extends MutatingEppToolCommand { private boolean fee; @Inject - @Config("base64StringGenerator") + @Named("base64StringGenerator") StringGenerator passwordGenerator; @Override diff --git a/java/google/registry/tools/CreateContactCommand.java b/java/google/registry/tools/CreateContactCommand.java index fbd0f597d..347ed6185 100644 --- a/java/google/registry/tools/CreateContactCommand.java +++ b/java/google/registry/tools/CreateContactCommand.java @@ -20,12 +20,12 @@ import static com.google.common.base.Strings.isNullOrEmpty; import com.beust.jcommander.Parameter; import com.beust.jcommander.Parameters; import com.google.template.soy.data.SoyMapData; -import google.registry.config.RegistryConfig.Config; import google.registry.tools.params.PhoneNumberParameter; import google.registry.tools.soy.ContactCreateSoyInfo; import google.registry.util.StringGenerator; import java.util.List; import javax.inject.Inject; +import javax.inject.Named; /** A command to create a new contact via EPP. */ @Parameters(separators = " =", commandDescription = "Create a new contact via EPP.") @@ -104,7 +104,7 @@ final class CreateContactCommand extends MutatingEppToolCommand { private String password; @Inject - @Config("base64StringGenerator") + @Named("base64StringGenerator") StringGenerator passwordGenerator; private static final int PASSWORD_LENGTH = 16; diff --git a/java/google/registry/tools/CreateDomainCommand.java b/java/google/registry/tools/CreateDomainCommand.java index a41e81b4d..680286e62 100644 --- a/java/google/registry/tools/CreateDomainCommand.java +++ b/java/google/registry/tools/CreateDomainCommand.java @@ -23,11 +23,11 @@ import static org.joda.time.DateTimeZone.UTC; import com.beust.jcommander.Parameter; import com.beust.jcommander.Parameters; import com.google.template.soy.data.SoyMapData; -import google.registry.config.RegistryConfig.Config; import google.registry.model.pricing.PremiumPricingEngine.DomainPrices; import google.registry.tools.soy.DomainCreateSoyInfo; import google.registry.util.StringGenerator; import javax.inject.Inject; +import javax.inject.Named; import org.joda.money.Money; import org.joda.time.DateTime; @@ -47,7 +47,7 @@ final class CreateDomainCommand extends CreateOrUpdateDomainCommand private boolean forcePremiums; @Inject - @Config("base64StringGenerator") + @Named("base64StringGenerator") StringGenerator passwordGenerator; private static final int PASSWORD_LENGTH = 16; diff --git a/java/google/registry/tools/GenerateAllocationTokensCommand.java b/java/google/registry/tools/GenerateAllocationTokensCommand.java index 7274f57fa..f0c84fbbf 100644 --- a/java/google/registry/tools/GenerateAllocationTokensCommand.java +++ b/java/google/registry/tools/GenerateAllocationTokensCommand.java @@ -31,7 +31,6 @@ import com.google.common.base.Splitter; import com.google.common.collect.ImmutableSet; import com.google.common.io.Files; import com.googlecode.objectify.Key; -import google.registry.config.RegistryConfig.Config; import google.registry.model.domain.token.AllocationToken; import google.registry.util.NonFinalForTesting; import google.registry.util.Retrier; @@ -41,6 +40,7 @@ import java.io.IOException; import java.util.Collection; import java.util.Deque; import javax.inject.Inject; +import javax.inject.Named; /** Command to generate and persist {@link AllocationToken}s. */ @Parameters( @@ -81,7 +81,7 @@ class GenerateAllocationTokensCommand implements CommandWithRemoteApi { boolean dryRun; @Inject - @Config("base58StringGenerator") + @Named("base58StringGenerator") StringGenerator stringGenerator; @Inject Retrier retrier; diff --git a/java/google/registry/tools/RegistryToolComponent.java b/java/google/registry/tools/RegistryToolComponent.java index a5ef69cfb..08f0acad1 100644 --- a/java/google/registry/tools/RegistryToolComponent.java +++ b/java/google/registry/tools/RegistryToolComponent.java @@ -34,9 +34,7 @@ import google.registry.request.Modules.URLFetchServiceModule; import google.registry.request.Modules.UrlFetchTransportModule; import google.registry.request.Modules.UserServiceModule; import google.registry.tools.AuthModule.LocalCredentialModule; -import google.registry.util.AppEngineServiceUtilsImpl.AppEngineServiceUtilsModule; -import google.registry.util.SystemClock.SystemClockModule; -import google.registry.util.SystemSleeper.SystemSleeperModule; +import google.registry.util.UtilsModule; import google.registry.whois.WhoisModule; import javax.annotation.Nullable; import javax.inject.Named; @@ -52,7 +50,6 @@ import javax.inject.Singleton; @Component( modules = { AppEngineAdminApiModule.class, - AppEngineServiceUtilsModule.class, AuthModule.class, BigqueryModule.class, ConfigModule.class, @@ -68,11 +65,10 @@ import javax.inject.Singleton; LocalCredentialModule.class, RdeModule.class, RequestFactoryModule.class, - SystemClockModule.class, - SystemSleeperModule.class, URLFetchServiceModule.class, UrlFetchTransportModule.class, UserServiceModule.class, + UtilsModule.class, VoidDnsWriterModule.class, WhoisModule.class, }) diff --git a/java/google/registry/tools/SetupOteCommand.java b/java/google/registry/tools/SetupOteCommand.java index 31197cd4f..935bf81fd 100644 --- a/java/google/registry/tools/SetupOteCommand.java +++ b/java/google/registry/tools/SetupOteCommand.java @@ -22,7 +22,6 @@ import com.beust.jcommander.Parameter; import com.beust.jcommander.Parameters; import com.google.common.collect.ImmutableMap; import com.google.common.io.MoreFiles; -import google.registry.config.RegistryConfig.Config; import google.registry.config.RegistryEnvironment; import google.registry.model.OteAccountBuilder; import google.registry.tools.params.PathParameter; @@ -32,6 +31,7 @@ import java.nio.file.Path; import java.util.ArrayList; import java.util.List; import javax.inject.Inject; +import javax.inject.Named; /** Composite command to set up OT&E TLDs and accounts. */ @Parameters(separators = " =", commandDescription = "Set up OT&E TLDs and registrars") @@ -78,7 +78,7 @@ final class SetupOteCommand extends ConfirmingCommand implements CommandWithRemo private boolean overwrite = false; @Inject - @Config("base64StringGenerator") + @Named("base64StringGenerator") StringGenerator passwordGenerator; @Inject Clock clock; diff --git a/java/google/registry/ui/server/registrar/ConsoleOteSetupAction.java b/java/google/registry/ui/server/registrar/ConsoleOteSetupAction.java index 96ad6b292..ac3f1b3c6 100644 --- a/java/google/registry/ui/server/registrar/ConsoleOteSetupAction.java +++ b/java/google/registry/ui/server/registrar/ConsoleOteSetupAction.java @@ -50,6 +50,7 @@ import google.registry.util.StringGenerator; import java.util.HashMap; import java.util.Optional; import javax.inject.Inject; +import javax.inject.Named; import javax.servlet.http.HttpServletRequest; /** @@ -97,7 +98,7 @@ public final class ConsoleOteSetupAction implements Runnable { @Inject SendEmailUtils sendEmailUtils; @Inject @Config("logoFilename") String logoFilename; @Inject @Config("productName") String productName; - @Inject @Config("base58StringGenerator") StringGenerator passwordGenerator; + @Inject @Named("base58StringGenerator") StringGenerator passwordGenerator; @Inject @Parameter("clientId") Optional clientId; @Inject @Parameter("email") Optional email; @Inject @Parameter("password") Optional optionalPassword; diff --git a/java/google/registry/ui/server/registrar/ConsoleRegistrarCreatorAction.java b/java/google/registry/ui/server/registrar/ConsoleRegistrarCreatorAction.java index de79bc103..9d48c100c 100644 --- a/java/google/registry/ui/server/registrar/ConsoleRegistrarCreatorAction.java +++ b/java/google/registry/ui/server/registrar/ConsoleRegistrarCreatorAction.java @@ -59,6 +59,7 @@ import java.util.HashMap; import java.util.Optional; import java.util.stream.Stream; import javax.inject.Inject; +import javax.inject.Named; import javax.servlet.http.HttpServletRequest; import org.joda.money.CurrencyUnit; @@ -108,8 +109,8 @@ public final class ConsoleRegistrarCreatorAction implements Runnable { @Inject SendEmailUtils sendEmailUtils; @Inject @Config("logoFilename") String logoFilename; @Inject @Config("productName") String productName; - @Inject @Config("base58StringGenerator") StringGenerator passwordGenerator; - @Inject @Config("digitOnlyStringGenerator") StringGenerator passcodeGenerator; + @Inject @Named("base58StringGenerator") StringGenerator passwordGenerator; + @Inject @Named("digitOnlyStringGenerator") StringGenerator passcodeGenerator; @Inject @Parameter("clientId") Optional clientId; @Inject @Parameter("name") Optional name; @Inject @Parameter("billingAccount") Optional billingAccount; diff --git a/java/google/registry/util/AppEngineServiceUtilsImpl.java b/java/google/registry/util/AppEngineServiceUtilsImpl.java index 71b700468..1d9c7eb4e 100644 --- a/java/google/registry/util/AppEngineServiceUtilsImpl.java +++ b/java/google/registry/util/AppEngineServiceUtilsImpl.java @@ -18,10 +18,6 @@ import static com.google.common.base.Preconditions.checkArgument; import static google.registry.util.PreconditionsUtils.checkArgumentNotNull; import com.google.appengine.api.modules.ModulesService; -import com.google.appengine.api.modules.ModulesServiceFactory; -import dagger.Binds; -import dagger.Module; -import dagger.Provides; import javax.inject.Inject; /** A wrapper for {@link ModulesService} that provides a saner API. */ @@ -60,20 +56,4 @@ public class AppEngineServiceUtilsImpl implements AppEngineServiceUtils { checkArgument(numInstances > 0, "Number of instances must be greater than 0"); modulesService.setNumInstances(service, version, numInstances); } - - /** Dagger module for AppEngineServiceUtils. */ - @Module - public abstract static class AppEngineServiceUtilsModule { - - private static final ModulesService modulesService = ModulesServiceFactory.getModulesService(); - - @Provides - static ModulesService provideModulesService() { - return modulesService; - } - - @Binds - abstract AppEngineServiceUtils provideAppEngineServiceUtils( - AppEngineServiceUtilsImpl appEngineServiceUtilsImpl); - } } diff --git a/java/google/registry/util/SystemClock.java b/java/google/registry/util/SystemClock.java index ec6693025..7ae85df27 100644 --- a/java/google/registry/util/SystemClock.java +++ b/java/google/registry/util/SystemClock.java @@ -16,9 +16,8 @@ package google.registry.util; import static org.joda.time.DateTimeZone.UTC; -import dagger.Module; -import dagger.Provides; import javax.annotation.concurrent.ThreadSafe; +import javax.inject.Inject; import org.joda.time.DateTime; /** Clock implementation that proxies to the real system clock. */ @@ -27,20 +26,12 @@ public class SystemClock implements Clock { private static final long serialVersionUID = 5165372013848947515L; + @Inject + public SystemClock() {} + /** Returns the current time. */ @Override public DateTime nowUtc() { return DateTime.now(UTC); } - - /** Dagger module for {@link SystemClock}. */ - @Module - public static final class SystemClockModule { - private static final Clock clock = new SystemClock(); - - @Provides - static Clock provideClock() { - return clock; - } - } } diff --git a/java/google/registry/util/SystemSleeper.java b/java/google/registry/util/SystemSleeper.java index e660cf315..a913aa14a 100644 --- a/java/google/registry/util/SystemSleeper.java +++ b/java/google/registry/util/SystemSleeper.java @@ -17,8 +17,6 @@ package google.registry.util; import static com.google.common.base.Preconditions.checkArgument; import com.google.common.util.concurrent.Uninterruptibles; -import dagger.Module; -import dagger.Provides; import java.io.Serializable; import java.util.concurrent.TimeUnit; import javax.annotation.concurrent.ThreadSafe; @@ -45,15 +43,4 @@ public final class SystemSleeper implements Sleeper, Serializable { checkArgument(duration.getMillis() >= 0); Uninterruptibles.sleepUninterruptibly(duration.getMillis(), TimeUnit.MILLISECONDS); } - - /** Dagger module for {@link SystemSleeper}. */ - @Module - public static final class SystemSleeperModule { - private static final Sleeper sleeper = new SystemSleeper(); - - @Provides - static Sleeper provideSleeper() { - return sleeper; - } - } } diff --git a/java/google/registry/util/UtilsModule.java b/java/google/registry/util/UtilsModule.java new file mode 100644 index 000000000..61bb14128 --- /dev/null +++ b/java/google/registry/util/UtilsModule.java @@ -0,0 +1,82 @@ +// Copyright 2019 The Nomulus Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package google.registry.util; + +import com.google.appengine.api.modules.ModulesService; +import com.google.appengine.api.modules.ModulesServiceFactory; +import dagger.Binds; +import dagger.Module; +import dagger.Provides; +import java.security.NoSuchAlgorithmException; +import java.security.ProviderException; +import java.security.SecureRandom; +import javax.inject.Named; +import javax.inject.Singleton; + +/** Dagger module to provide instances of various utils classes. */ +@Module +public abstract class UtilsModule { + + @Binds + @Singleton + abstract Sleeper provideSleeper(SystemSleeper sleeper); + + @Binds + @Singleton + abstract Clock provideClock(SystemClock clock); + + @Provides + @Singleton + static ModulesService provideModulesService() { + return ModulesServiceFactory.getModulesService(); + } + + @Binds + @Singleton + abstract AppEngineServiceUtils provideAppEngineServiceUtils( + AppEngineServiceUtilsImpl appEngineServiceUtilsImpl); + + + @Singleton + @Provides + public static SecureRandom provideSecureRandom() { + try { + return SecureRandom.getInstance("NativePRNG"); + } catch (NoSuchAlgorithmException e) { + throw new ProviderException(e); + } + } + + @Singleton + @Provides + @Named("base58StringGenerator") + public static StringGenerator provideBase58StringGenerator(SecureRandom secureRandom) { + return new RandomStringGenerator(StringGenerator.Alphabets.BASE_58, secureRandom); + } + + @Singleton + @Provides + @Named("base64StringGenerator") + public static StringGenerator provideBase64StringGenerator(SecureRandom secureRandom) { + return new RandomStringGenerator(StringGenerator.Alphabets.BASE_64, secureRandom); + } + + @Singleton + @Provides + @Named("digitOnlyStringGenerator") + public static StringGenerator provideDigitsOnlyStringGenerator(SecureRandom secureRandom) { + return new RandomStringGenerator(StringGenerator.Alphabets.DIGITS_ONLY, secureRandom); + } +} diff --git a/javatests/google/registry/dns/DnsTestComponent.java b/javatests/google/registry/dns/DnsTestComponent.java index c422f01d5..e3cbae14b 100644 --- a/javatests/google/registry/dns/DnsTestComponent.java +++ b/javatests/google/registry/dns/DnsTestComponent.java @@ -20,19 +20,20 @@ import google.registry.cron.CronModule; import google.registry.dns.writer.VoidDnsWriterModule; import google.registry.module.backend.BackendModule; import google.registry.request.RequestModule; -import google.registry.util.SystemClock.SystemClockModule; -import google.registry.util.SystemSleeper.SystemSleeperModule; +import google.registry.util.UtilsModule; +import javax.inject.Singleton; -@Component(modules = { - SystemClockModule.class, - ConfigModule.class, - BackendModule.class, - DnsModule.class, - RequestModule.class, - VoidDnsWriterModule.class, - SystemSleeperModule.class, - CronModule.class, -}) +@Singleton +@Component( + modules = { + BackendModule.class, + ConfigModule.class, + CronModule.class, + DnsModule.class, + RequestModule.class, + UtilsModule.class, + VoidDnsWriterModule.class, + }) interface DnsTestComponent { DnsQueue dnsQueue(); RefreshDnsAction refreshDns(); diff --git a/javatests/google/registry/tmch/NordnUploadActionTest.java b/javatests/google/registry/tmch/NordnUploadActionTest.java index e17e602e1..529ed2d33 100644 --- a/javatests/google/registry/tmch/NordnUploadActionTest.java +++ b/javatests/google/registry/tmch/NordnUploadActionTest.java @@ -63,9 +63,9 @@ import google.registry.util.Retrier; import google.registry.util.TaskQueueUtils; import google.registry.util.UrlFetchException; import java.net.URL; +import java.security.SecureRandom; import java.util.List; import java.util.Optional; -import java.util.Random; import org.joda.time.DateTime; import org.junit.Before; import org.junit.Rule; @@ -125,7 +125,7 @@ public class NordnUploadActionTest { action.taskQueueUtils = new TaskQueueUtils(new Retrier(new FakeSleeper(clock), 3)); action.tld = "tld"; action.tmchMarksdbUrl = "http://127.0.0.1"; - action.random = new Random(); + action.random = new SecureRandom(); action.retrier = new Retrier(new FakeSleeper(clock), 3); } diff --git a/javatests/google/registry/whois/WhoisTestComponent.java b/javatests/google/registry/whois/WhoisTestComponent.java index 7053a9fb1..1665aa2f7 100644 --- a/javatests/google/registry/whois/WhoisTestComponent.java +++ b/javatests/google/registry/whois/WhoisTestComponent.java @@ -17,18 +17,17 @@ package google.registry.whois; import dagger.Component; import google.registry.config.RegistryConfig.ConfigModule; import google.registry.request.RequestModule; -import google.registry.util.SystemClock.SystemClockModule; -import google.registry.util.SystemSleeper.SystemSleeperModule; +import google.registry.util.UtilsModule; import javax.inject.Singleton; @Singleton -@Component(modules = { - ConfigModule.class, - RequestModule.class, - SystemClockModule.class, - SystemSleeperModule.class, - WhoisModule.class, -}) +@Component( + modules = { + ConfigModule.class, + RequestModule.class, + UtilsModule.class, + WhoisModule.class, + }) interface WhoisTestComponent { WhoisHttpAction whoisHttpAction();