Refactor out creation of server TRIDs so they can be tested

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=152837185
This commit is contained in:
mcilwain 2017-04-11 11:28:11 -07:00 committed by Ben McIlwain
parent af942774f4
commit 8653d2f204
29 changed files with 174 additions and 62 deletions

View file

@ -50,6 +50,7 @@ public final class EppController {
@Inject EppMetric.Builder eppMetricBuilder;
@Inject EppMetrics eppMetrics;
@Inject BigQueryMetricsEnqueuer bigQueryMetricsEnqueuer;
@Inject ServerTridProvider serverTridProvider;
@Inject EppController() {}
/** Reads EPP XML, executes the matching flow, and returns an {@link EppOutput}. */
@ -83,7 +84,8 @@ public final class EppController {
Strings.repeat("=", 40));
// Return early by sending an error message, with no clTRID since we couldn't unmarshal it.
eppMetricBuilder.setStatus(e.getResult().getCode());
return getErrorResponse(e.getResult(), Trid.create(null));
return getErrorResponse(
e.getResult(), Trid.create(null, serverTridProvider.createServerTrid()));
}
if (!eppInput.getTargetIds().isEmpty()) {
eppMetricBuilder.setEppTarget(Joiner.on(',').join(eppInput.getTargetIds()));

View file

@ -152,6 +152,13 @@ public class FlowModule {
return credentials;
}
@Provides
@FlowScope
Trid provideTrid(EppInput eppInput, ServerTridProvider serverTridProvider) {
return Trid.create(
eppInput.getCommandWrapper().getClTrid(), serverTridProvider.createServerTrid());
}
@Provides
@FlowScope
@ClientId
@ -161,12 +168,6 @@ public class FlowModule {
return Strings.nullToEmpty(sessionMetadata.getClientId());
}
@Provides
@FlowScope
static Trid provideTrid(EppInput eppInput) {
return Trid.create(eppInput.getCommandWrapper().getClTrid());
}
@Provides
@FlowScope
static Class<? extends Flow> provideFlowClass(EppInput eppInput) {

View file

@ -0,0 +1,28 @@
// Copyright 2017 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.flows;
import google.registry.model.eppcommon.Trid;
/**
* An interface that provides server Trids.
*
* @see Trid
*/
public interface ServerTridProvider {
/** Creates a new server Trid. */
public String createServerTrid();
}

View file

@ -0,0 +1,50 @@
// Copyright 2017 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.flows;
import static com.google.common.primitives.Longs.BYTES;
import com.google.common.io.BaseEncoding;
import java.nio.ByteBuffer;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicLong;
import javax.inject.Inject;
/** A server Trid provider that generates globally incrementing UUIDs. */
public class ServerTridProviderImpl implements ServerTridProvider {
private static final String SERVER_ID = getServerId();
private static final AtomicLong idCounter = new AtomicLong();
@Inject public ServerTridProviderImpl() {}
/** Creates a unique id for this server instance, as a base64 encoded UUID. */
private static String getServerId() {
UUID uuid = UUID.randomUUID();
ByteBuffer buffer =
ByteBuffer.allocate(BYTES * 2)
.putLong(uuid.getMostSignificantBits())
.putLong(uuid.getLeastSignificantBits());
return BaseEncoding.base64().encode(buffer.array());
}
@Override
public String createServerTrid() {
// The server id can be at most 64 characters. The SERVER_ID is at most 22 characters (128
// bits in base64), plus the dash. That leaves 41 characters, so we just append the counter in
// hex.
return String.format("%s-%x", SERVER_ID, idCounter.incrementAndGet());
}
}

View file

@ -0,0 +1,28 @@
// Copyright 2017 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.flows;
import dagger.Module;
import dagger.Provides;
/** Dagger module for the server Trid provider. */
@Module
public class ServerTridProviderModule {
@Provides
static ServerTridProvider provideServerTridProvider(ServerTridProviderImpl defaultProvider) {
return defaultProvider;
}
}

View file

@ -14,13 +14,12 @@
package google.registry.model.eppcommon;
import static google.registry.util.PreconditionsUtils.checkArgumentNotNull;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.io.BaseEncoding;
import com.googlecode.objectify.annotation.Embed;
import google.registry.model.ImmutableObject;
import java.nio.ByteBuffer;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicLong;
import javax.annotation.Nullable;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
@ -34,19 +33,6 @@ import javax.xml.bind.annotation.XmlType;
@XmlType(propOrder = {"clientTransactionId", "serverTransactionId"})
public class Trid extends ImmutableObject {
private static final String SERVER_ID = getServerId();
private static final AtomicLong COUNTER = new AtomicLong();
/** Creates a unique id for this server instance, as a base64 encoded UUID. */
private static String getServerId() {
UUID uuid = UUID.randomUUID();
ByteBuffer buffer = ByteBuffer.allocate(16);
buffer.asLongBuffer()
.put(0, uuid.getMostSignificantBits())
.put(1, uuid.getLeastSignificantBits());
return BaseEncoding.base64().encode(buffer.array());
}
/** The server transaction id. */
@XmlElement(name = "svTRID", namespace = "urn:ietf:params:xml:ns:epp-1.0")
String serverTransactionId;
@ -63,17 +49,9 @@ public class Trid extends ImmutableObject {
return clientTransactionId;
}
public static Trid create(String clientTransactionId) {
Trid instance = new Trid();
instance.clientTransactionId = clientTransactionId;
// The server id can be at most 64 characters. The SERVER_ID is at most 22 characters (128 bits
// in base64), plus the dash. That leaves 41 characters, so we just append the counter in hex.
instance.serverTransactionId = String.format("%s-%x", SERVER_ID, COUNTER.incrementAndGet());
return instance;
}
@VisibleForTesting
public static Trid create(String clientTransactionId, String serverTransactionId) {
public static Trid create(@Nullable String clientTransactionId, String serverTransactionId) {
checkArgumentNotNull(serverTransactionId, "serverTransactionId cannot be null");
Trid instance = new Trid();
instance.clientTransactionId = clientTransactionId;
instance.serverTransactionId = serverTransactionId;

View file

@ -17,6 +17,7 @@ package google.registry.module.frontend;
import dagger.Component;
import google.registry.braintree.BraintreeModule;
import google.registry.config.RegistryConfig.ConfigModule;
import google.registry.flows.ServerTridProviderModule;
import google.registry.flows.custom.CustomLogicFactoryModule;
import google.registry.keyring.api.DummyKeyringModule;
import google.registry.keyring.api.KeyModule;
@ -51,6 +52,7 @@ import javax.inject.Singleton;
Jackson2Module.class,
KeyModule.class,
ModulesServiceModule.class,
ServerTridProviderModule.class,
StackdriverModule.class,
SystemClockModule.class,
SystemSleeperModule.class,

View file

@ -17,6 +17,7 @@ package google.registry.module.tools;
import dagger.Component;
import google.registry.config.RegistryConfig.ConfigModule;
import google.registry.export.DriveModule;
import google.registry.flows.ServerTridProviderModule;
import google.registry.flows.custom.CustomLogicFactoryModule;
import google.registry.gcs.GcsServiceModule;
import google.registry.groups.DirectoryModule;
@ -57,12 +58,13 @@ import javax.inject.Singleton;
Jackson2Module.class,
KeyModule.class,
ModulesServiceModule.class,
ServerTridProviderModule.class,
SystemClockModule.class,
SystemSleeperModule.class,
ToolsRequestComponentModule.class,
UrlFetchTransportModule.class,
UseAppIdentityCredentialForGoogleApisModule.class,
UserServiceModule.class,
SystemClockModule.class,
SystemSleeperModule.class,
})
interface ToolsComponent {
ToolsRequestHandler requestHandler();

View file

@ -18,6 +18,7 @@ import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
import static google.registry.util.DateTimeUtils.END_OF_TIME;
import static google.registry.util.DateTimeUtils.START_OF_TIME;
import static java.nio.charset.StandardCharsets.US_ASCII;
import static java.nio.charset.StandardCharsets.UTF_8;
import com.google.appengine.tools.cloudstorage.GcsFilename;
@ -25,6 +26,8 @@ import com.google.common.collect.ImmutableSet;
import com.google.common.io.BaseEncoding;
import com.googlecode.objectify.Key;
import google.registry.config.RegistryConfig.Config;
import google.registry.flows.ServerTridProvider;
import google.registry.flows.ServerTridProviderImpl;
import google.registry.gcs.GcsUtils;
import google.registry.model.EppResource;
import google.registry.model.EppResource.ForeignKeyedEppResource;
@ -70,6 +73,8 @@ public class RdeImportUtils {
private final String escrowBucketName;
private final GcsUtils gcsUtils;
private static final ServerTridProvider serverTridProvider = new ServerTridProviderImpl();
@Inject
public RdeImportUtils(
Ofy ofy, Clock clock, @Config("rdeImportBucket") String escrowBucketName, GcsUtils gcsUtils) {
@ -175,7 +180,8 @@ public class RdeImportUtils {
// Client trids must be a token between 3 and 64 characters long
// Base64 encoded UUID string meets this requirement
return Trid.create(
"Import_" + BaseEncoding.base64().encode(UUID.randomUUID().toString().getBytes()));
"Import_" + BaseEncoding.base64().encode(UUID.randomUUID().toString().getBytes(US_ASCII)),
serverTridProvider.createServerTrid());
}
public static BillingEvent.Recurring createAutoRenewBillingEventForDomainImport(