Add domain name support to AllocationToken entities

The design doc is at []
The next step will be to tie this into the domain create flow, and if the domain
name is on a reserved list, allow it to be created if the token is specified that
has the given domain name on it.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=207884521
This commit is contained in:
mcilwain 2018-08-08 07:59:50 -07:00 committed by jianglai
parent e3977024f3
commit d80f431e21
5 changed files with 117 additions and 24 deletions

View file

@ -21,12 +21,14 @@ import static google.registry.util.PreconditionsUtils.checkArgumentNotNull;
import com.googlecode.objectify.Key; import com.googlecode.objectify.Key;
import com.googlecode.objectify.annotation.Entity; import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Id; import com.googlecode.objectify.annotation.Id;
import com.googlecode.objectify.annotation.Index;
import google.registry.model.BackupGroupRoot; import google.registry.model.BackupGroupRoot;
import google.registry.model.Buildable; import google.registry.model.Buildable;
import google.registry.model.CreateAutoTimestamp; import google.registry.model.CreateAutoTimestamp;
import google.registry.model.annotations.ReportedOn; import google.registry.model.annotations.ReportedOn;
import google.registry.model.reporting.HistoryEntry; import google.registry.model.reporting.HistoryEntry;
import java.util.Optional; import java.util.Optional;
import javax.annotation.Nullable;
import org.joda.time.DateTime; import org.joda.time.DateTime;
/** An entity representing an allocation token. */ /** An entity representing an allocation token. */
@ -40,6 +42,9 @@ public class AllocationToken extends BackupGroupRoot implements Buildable {
/** The key of the history entry for which the token was used. Null if not yet used. */ /** The key of the history entry for which the token was used. Null if not yet used. */
Key<HistoryEntry> redemptionHistoryEntry; Key<HistoryEntry> redemptionHistoryEntry;
/** The fully-qualified domain name that this token is limited to, if any. */
@Nullable @Index String domainName;
/** When this token was created. */ /** When this token was created. */
CreateAutoTimestamp creationTime = CreateAutoTimestamp.create(null); CreateAutoTimestamp creationTime = CreateAutoTimestamp.create(null);
@ -55,6 +60,10 @@ public class AllocationToken extends BackupGroupRoot implements Buildable {
return redemptionHistoryEntry != null; return redemptionHistoryEntry != null;
} }
public Optional<String> getDomainName() {
return Optional.ofNullable(domainName);
}
public Optional<DateTime> getCreationTime() { public Optional<DateTime> getCreationTime() {
return Optional.ofNullable(creationTime.getTimestamp()); return Optional.ofNullable(creationTime.getTimestamp());
} }
@ -86,6 +95,11 @@ public class AllocationToken extends BackupGroupRoot implements Buildable {
return this; return this;
} }
public Builder setDomainName(@Nullable String domainName) {
getInstance().domainName = domainName;
return this;
}
public Builder setCreationTime(DateTime creationTime) { public Builder setCreationTime(DateTime creationTime) {
checkState( checkState(
getInstance().creationTime.getTimestamp() == null, "creationTime can only be set once"); getInstance().creationTime.getTimestamp() == null, "creationTime can only be set once");

View file

@ -14,22 +14,31 @@
package google.registry.tools; package google.registry.tools;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.collect.ImmutableSet.toImmutableSet; import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static com.google.common.collect.Queues.newArrayDeque;
import static com.google.common.collect.Sets.difference; import static com.google.common.collect.Sets.difference;
import static google.registry.model.ofy.ObjectifyService.ofy; import static google.registry.model.ofy.ObjectifyService.ofy;
import static java.nio.charset.StandardCharsets.UTF_8;
import com.beust.jcommander.Parameter; import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters; import com.beust.jcommander.Parameters;
import com.google.appengine.tools.remoteapi.RemoteApiException; import com.google.appengine.tools.remoteapi.RemoteApiException;
import com.google.common.annotations.VisibleForTesting; import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.common.io.Files;
import com.googlecode.objectify.Key; import com.googlecode.objectify.Key;
import google.registry.model.domain.token.AllocationToken; import google.registry.model.domain.token.AllocationToken;
import google.registry.tools.Command.RemoteApiCommand; import google.registry.tools.Command.RemoteApiCommand;
import google.registry.util.NonFinalForTesting; import google.registry.util.NonFinalForTesting;
import google.registry.util.Retrier; import google.registry.util.Retrier;
import google.registry.util.StringGenerator; import google.registry.util.StringGenerator;
import java.io.File;
import java.io.IOException;
import java.util.Collection; import java.util.Collection;
import java.util.Deque;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
@ -50,11 +59,16 @@ public class GenerateAllocationTokensCommand implements RemoteApiCommand {
@Parameter( @Parameter(
names = {"-n", "--number"}, names = {"-n", "--number"},
description = "The number of tokens to generate", description = "The number of tokens to generate"
required = true
) )
private long numTokens; private long numTokens;
@Parameter(
names = {"-d", "--domain_names_file"},
description = "A file with a list of newline-delimited domain names to create tokens for"
)
private String domainNamesFile;
@Parameter( @Parameter(
names = {"-l", "--length"}, names = {"-l", "--length"},
description = "The length of each token, exclusive of the prefix (if specified); defaults to 12" description = "The length of each token, exclusive of the prefix (if specified); defaults to 12"
@ -62,7 +76,7 @@ public class GenerateAllocationTokensCommand implements RemoteApiCommand {
private int tokenLength = 12; private int tokenLength = 12;
@Parameter( @Parameter(
names = {"-d", "--dry_run"}, names = {"--dry_run"},
description = "Do not actually persist the tokens; defaults to false") description = "Do not actually persist the tokens; defaults to false")
boolean dryRun; boolean dryRun;
@ -70,16 +84,41 @@ public class GenerateAllocationTokensCommand implements RemoteApiCommand {
@Inject Retrier retrier; @Inject Retrier retrier;
private static final int BATCH_SIZE = 20; private static final int BATCH_SIZE = 20;
private static final Joiner SKIP_NULLS = Joiner.on(", ").skipNulls();
@Override @Override
public void run() { public void run() throws IOException {
checkArgument(
(numTokens > 0) ^ (domainNamesFile != null),
"Must specify either --number or --domain_names_file, but not both");
Deque<String> domainNames;
if (domainNamesFile == null) {
domainNames = null;
} else {
domainNames =
newArrayDeque(
Splitter.on('\n')
.omitEmptyStrings()
.trimResults()
.split(Files.asCharSource(new File(domainNamesFile), UTF_8).read()));
numTokens = domainNames.size();
}
int tokensSaved = 0; int tokensSaved = 0;
do { do {
ImmutableSet<AllocationToken> tokens = ImmutableSet<AllocationToken> tokens =
generateTokens(BATCH_SIZE) generateTokens(BATCH_SIZE)
.stream() .stream()
.limit(numTokens - tokensSaved) .limit(numTokens - tokensSaved)
.map(t -> new AllocationToken.Builder().setToken(t).build()) .map(
t -> {
AllocationToken.Builder token = new AllocationToken.Builder().setToken(t);
if (domainNames != null) {
token.setDomainName(domainNames.removeFirst());
}
return token.build();
})
.collect(toImmutableSet()); .collect(toImmutableSet());
// Wrap in a retrier to deal with transient 404 errors (thrown as RemoteApiExceptions). // Wrap in a retrier to deal with transient 404 errors (thrown as RemoteApiExceptions).
tokensSaved += retrier.callWithRetry(() -> saveTokens(tokens), RemoteApiException.class); tokensSaved += retrier.callWithRetry(() -> saveTokens(tokens), RemoteApiException.class);
@ -90,7 +129,8 @@ public class GenerateAllocationTokensCommand implements RemoteApiCommand {
int saveTokens(final ImmutableSet<AllocationToken> tokens) { int saveTokens(final ImmutableSet<AllocationToken> tokens) {
Collection<AllocationToken> savedTokens = Collection<AllocationToken> savedTokens =
dryRun ? tokens : ofy().transact(() -> ofy().save().entities(tokens).now().values()); dryRun ? tokens : ofy().transact(() -> ofy().save().entities(tokens).now().values());
savedTokens.stream().map(AllocationToken::getToken).forEach(System.out::println); savedTokens.forEach(
t -> System.out.println(SKIP_NULLS.join(t.getDomainName().orElse(null), t.getToken())));
return savedTokens.size(); return savedTokens.size();
} }

View file

@ -36,6 +36,7 @@ public class AllocationTokenTest extends EntityTestCase {
new AllocationToken.Builder() new AllocationToken.Builder()
.setToken("abc123") .setToken("abc123")
.setRedemptionHistoryEntry(Key.create(HistoryEntry.class, 1L)) .setRedemptionHistoryEntry(Key.create(HistoryEntry.class, 1L))
.setDomainName("foo.example")
.setCreationTime(DateTime.parse("2010-11-12T05:00:00Z")) .setCreationTime(DateTime.parse("2010-11-12T05:00:00Z"))
.build()); .build());
assertThat(ofy().load().entity(token).now()).isEqualTo(token); assertThat(ofy().load().entity(token).now()).isEqualTo(token);
@ -44,11 +45,14 @@ public class AllocationTokenTest extends EntityTestCase {
@Test @Test
public void testIndexing() throws Exception { public void testIndexing() throws Exception {
verifyIndexing( verifyIndexing(
persistResource(
new AllocationToken.Builder() new AllocationToken.Builder()
.setToken("abc123") .setToken("abc123")
.setDomainName("blahdomain.fake")
.setCreationTime(DateTime.parse("2010-11-12T05:00:00Z")) .setCreationTime(DateTime.parse("2010-11-12T05:00:00Z"))
.build(), .build()),
"token"); "token",
"domainName");
} }
@Test @Test

View file

@ -307,6 +307,7 @@ class google.registry.model.domain.token.AllocationToken {
com.googlecode.objectify.Key<google.registry.model.reporting.HistoryEntry> redemptionHistoryEntry; com.googlecode.objectify.Key<google.registry.model.reporting.HistoryEntry> redemptionHistoryEntry;
google.registry.model.CreateAutoTimestamp creationTime; google.registry.model.CreateAutoTimestamp creationTime;
google.registry.model.UpdateAutoTimestamp updateTimestamp; google.registry.model.UpdateAutoTimestamp updateTimestamp;
java.lang.String domainName;
} }
class google.registry.model.eppcommon.AuthInfo$PasswordAuth { class google.registry.model.eppcommon.AuthInfo$PasswordAuth {
java.lang.String repoId; java.lang.String repoId;

View file

@ -18,12 +18,13 @@ import static com.google.common.truth.Truth.assertThat;
import static google.registry.model.ofy.ObjectifyService.ofy; import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.testing.DatastoreHelper.persistResource; import static google.registry.testing.DatastoreHelper.persistResource;
import static google.registry.testing.JUnitBackports.assertThrows; import static google.registry.testing.JUnitBackports.assertThrows;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.spy; import static org.mockito.Mockito.spy;
import com.beust.jcommander.ParameterException;
import com.google.appengine.tools.remoteapi.RemoteApiException; import com.google.appengine.tools.remoteapi.RemoteApiException;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.io.Files;
import com.googlecode.objectify.Key; import com.googlecode.objectify.Key;
import google.registry.model.domain.token.AllocationToken; import google.registry.model.domain.token.AllocationToken;
import google.registry.model.reporting.HistoryEntry; import google.registry.model.reporting.HistoryEntry;
@ -33,6 +34,7 @@ import google.registry.testing.FakeClock;
import google.registry.testing.FakeSleeper; import google.registry.testing.FakeSleeper;
import google.registry.util.Retrier; import google.registry.util.Retrier;
import google.registry.util.StringGenerator.Alphabets; import google.registry.util.StringGenerator.Alphabets;
import java.io.File;
import java.util.function.Function; import java.util.function.Function;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import org.joda.time.DateTime; import org.joda.time.DateTime;
@ -54,7 +56,7 @@ public class GenerateAllocationTokensCommandTest
@Test @Test
public void testSuccess_oneToken() throws Exception { public void testSuccess_oneToken() throws Exception {
runCommand("--prefix", "blah", "--number", "1", "--length", "9"); runCommand("--prefix", "blah", "--number", "1", "--length", "9");
assertAllocationTokens(createToken("blah123456789", null)); assertAllocationTokens(createToken("blah123456789", null, null));
assertInStdout("blah123456789"); assertInStdout("blah123456789");
} }
@ -62,16 +64,16 @@ public class GenerateAllocationTokensCommandTest
public void testSuccess_threeTokens() throws Exception { public void testSuccess_threeTokens() throws Exception {
runCommand("--prefix", "foo", "--number", "3", "--length", "10"); runCommand("--prefix", "foo", "--number", "3", "--length", "10");
assertAllocationTokens( assertAllocationTokens(
createToken("foo123456789A", null), createToken("foo123456789A", null, null),
createToken("fooBCDEFGHJKL", null), createToken("fooBCDEFGHJKL", null, null),
createToken("fooMNPQRSTUVW", null)); createToken("fooMNPQRSTUVW", null, null));
assertInStdout("foo123456789A\nfooBCDEFGHJKL\nfooMNPQRSTUVW"); assertInStdout("foo123456789A\nfooBCDEFGHJKL\nfooMNPQRSTUVW");
} }
@Test @Test
public void testSuccess_defaults() throws Exception { public void testSuccess_defaults() throws Exception {
runCommand("--number", "1"); runCommand("--number", "1");
assertAllocationTokens(createToken("123456789ABC", null)); assertAllocationTokens(createToken("123456789ABC", null, null));
assertInStdout("123456789ABC"); assertInStdout("123456789ABC");
} }
@ -85,7 +87,7 @@ public class GenerateAllocationTokensCommandTest
.when(spyCommand) .when(spyCommand)
.saveTokens(Mockito.any()); .saveTokens(Mockito.any());
runCommand("--number", "1"); runCommand("--number", "1");
assertAllocationTokens(createToken("123456789ABC", null)); assertAllocationTokens(createToken("123456789ABC", null, null));
assertInStdout("123456789ABC"); assertInStdout("123456789ABC");
} }
@ -94,7 +96,7 @@ public class GenerateAllocationTokensCommandTest
AllocationToken existingToken = AllocationToken existingToken =
persistResource(new AllocationToken.Builder().setToken("DEADBEEF123456789ABC").build()); persistResource(new AllocationToken.Builder().setToken("DEADBEEF123456789ABC").build());
runCommand("--number", "1", "--prefix", "DEADBEEF"); runCommand("--number", "1", "--prefix", "DEADBEEF");
assertAllocationTokens(existingToken, createToken("DEADBEEFDEFGHJKLMNPQ", null)); assertAllocationTokens(existingToken, createToken("DEADBEEFDEFGHJKLMNPQ", null, null));
assertInStdout("DEADBEEFDEFGHJKLMNPQ"); assertInStdout("DEADBEEFDEFGHJKLMNPQ");
} }
@ -116,10 +118,39 @@ public class GenerateAllocationTokensCommandTest
} }
@Test @Test
public void testFailure_mustSpecifyNumberOfTokens() { public void testSuccess_domainNames() throws Exception {
ParameterException thrown = File domainNamesFile = tmpDir.newFile("domain_names.txt");
assertThrows(ParameterException.class, () -> runCommand("--prefix", "FEET")); Files.asCharSink(domainNamesFile, UTF_8).write("foo1.tld\nboo2.tld\nbaz9.tld\n");
assertThat(thrown).hasMessageThat().contains("The following option is required: -n, --number"); runCommand("--domain_names_file", domainNamesFile.getPath());
assertAllocationTokens(
createToken("123456789ABC", null, "foo1.tld"),
createToken("DEFGHJKLMNPQ", null, "boo2.tld"),
createToken("RSTUVWXYZabc", null, "baz9.tld"));
assertInStdout("foo1.tld, 123456789ABC\nboo2.tld, DEFGHJKLMNPQ\nbaz9.tld, RSTUVWXYZabc");
}
@Test
public void testFailure_mustSpecifyNumberOfTokensOrDomainsFile() {
IllegalArgumentException thrown =
assertThrows(IllegalArgumentException.class, () -> runCommand("--prefix", "FEET"));
assertThat(thrown)
.hasMessageThat()
.isEqualTo("Must specify either --number or --domain_names_file, but not both");
}
@Test
public void testFailure_mustNotSpecifyBothNumberOfTokensAndDomainsFile() {
IllegalArgumentException thrown =
assertThrows(
IllegalArgumentException.class,
() ->
runCommand(
"--prefix", "FEET",
"--number", "999",
"--domain_names_file", "/path/to/blaaaaah"));
assertThat(thrown)
.hasMessageThat()
.isEqualTo("Must specify either --number or --domain_names_file, but not both");
} }
private void assertAllocationTokens(AllocationToken... expectedTokens) { private void assertAllocationTokens(AllocationToken... expectedTokens) {
@ -142,11 +173,14 @@ public class GenerateAllocationTokensCommandTest
} }
private AllocationToken createToken( private AllocationToken createToken(
String token, @Nullable Key<HistoryEntry> redemptionHistoryEntry) { String token,
@Nullable Key<HistoryEntry> redemptionHistoryEntry,
@Nullable String domainName) {
AllocationToken.Builder builder = new AllocationToken.Builder().setToken(token); AllocationToken.Builder builder = new AllocationToken.Builder().setToken(token);
if (redemptionHistoryEntry != null) { if (redemptionHistoryEntry != null) {
builder.setRedemptionHistoryEntry(redemptionHistoryEntry); builder.setRedemptionHistoryEntry(redemptionHistoryEntry);
} }
builder.setDomainName(domainName);
return builder.build(); return builder.build();
} }
} }