Skip to content

Commit bab6cfe

Browse files
committed
Verify existence of TLDs and registrars for tokens
Just in case someone makes a typo when running the commands
1 parent ee3866e commit bab6cfe

File tree

5 files changed

+80
-2
lines changed

5 files changed

+80
-2
lines changed

core/src/main/java/google/registry/tools/GenerateAllocationTokensCommand.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package google.registry.tools;
1616

1717
import static com.google.common.base.Preconditions.checkArgument;
18+
import static com.google.common.collect.ImmutableList.toImmutableList;
1819
import static com.google.common.collect.ImmutableSet.toImmutableSet;
1920
import static com.google.common.collect.Sets.difference;
2021
import static google.registry.model.billing.BillingBase.RenewalPriceBehavior.DEFAULT;
@@ -46,6 +47,9 @@
4647
import google.registry.model.domain.token.AllocationToken.RegistrationBehavior;
4748
import google.registry.model.domain.token.AllocationToken.TokenStatus;
4849
import google.registry.model.domain.token.AllocationToken.TokenType;
50+
import google.registry.model.registrar.Registrar;
51+
import google.registry.model.tld.Tld;
52+
import google.registry.model.tld.Tlds;
4953
import google.registry.persistence.VKey;
5054
import google.registry.tools.params.MoneyParameter;
5155
import google.registry.tools.params.TransitionListParameter.TokenStatusTransitions;
@@ -291,10 +295,12 @@ private void verifyInput() {
291295
!ImmutableList.of("").equals(allowedClientIds),
292296
"Either omit --allowed_client_ids if all registrars are allowed, or include a"
293297
+ " comma-separated list");
298+
verifyAllRegistrarIdsExist(allowedClientIds);
294299

295300
checkArgument(
296301
!ImmutableList.of("").equals(allowedTlds),
297302
"Either omit --allowed_tlds if all TLDs are allowed, or include a comma-separated list");
303+
verifyAllTldsExist(allowedTlds);
298304

299305
if (ImmutableList.of("").equals(allowedEppActions)) {
300306
allowedEppActions = ImmutableList.of();
@@ -326,6 +332,32 @@ private void verifyInput() {
326332
}
327333
}
328334

335+
static void verifyAllRegistrarIdsExist(@Nullable List<String> allowedClientIds) {
336+
if (isNullOrEmpty(allowedClientIds)) {
337+
return;
338+
}
339+
ImmutableSet<String> allRegistrarIds =
340+
Registrar.loadAllKeysCached().stream()
341+
.map(VKey::getKey)
342+
.map(Object::toString)
343+
.collect(toImmutableSet());
344+
ImmutableList<String> badRegistrarIds =
345+
allowedClientIds.stream()
346+
.filter(id -> !allRegistrarIds.contains(id))
347+
.collect(toImmutableList());
348+
checkArgument(badRegistrarIds.isEmpty(), "Unknown registrar ID(s) %s", badRegistrarIds);
349+
}
350+
351+
static void verifyAllTldsExist(@Nullable List<String> allowedTlds) {
352+
if (isNullOrEmpty(allowedTlds)) {
353+
return;
354+
}
355+
ImmutableSet<String> allTlds = Tlds.getTldsOfType(Tld.TldType.REAL);
356+
ImmutableList<String> badTlds =
357+
allowedTlds.stream().filter(tld -> !allTlds.contains(tld)).collect(toImmutableList());
358+
checkArgument(badTlds.isEmpty(), "Unknown REAL TLD(s) %s", badTlds);
359+
}
360+
329361
private void verifyTokenStringsDoNotExist() {
330362
ImmutableSet<String> existingTokenStrings =
331363
getExistingTokenStrings(ImmutableSet.copyOf(tokenStrings));

core/src/main/java/google/registry/tools/UpdateAllocationTokensCommand.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,9 @@ public void init() {
157157
endToken = true;
158158
}
159159

160+
GenerateAllocationTokensCommand.verifyAllRegistrarIdsExist(allowedClientIds);
161+
GenerateAllocationTokensCommand.verifyAllTldsExist(allowedTlds);
162+
160163
tokensToSave =
161164
tm().transact(
162165
() ->

core/src/test/java/google/registry/tools/CommandTestCase.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import google.registry.persistence.transaction.JpaTestExtensions;
3333
import google.registry.persistence.transaction.JpaTestExtensions.JpaIntegrationTestExtension;
3434
import google.registry.testing.CertificateSamples;
35+
import google.registry.testing.DatabaseHelper;
3536
import google.registry.testing.FakeClock;
3637
import google.registry.testing.SystemPropertyExtension;
3738
import google.registry.tools.params.ParameterFactory;
@@ -76,6 +77,7 @@ public abstract class CommandTestCase<C extends Command> {
7677

7778
@BeforeEach
7879
public final void beforeEachCommandTestCase() throws Exception {
80+
DatabaseHelper.createTlds("tld", "example");
7981
// Ensure the UNITTEST environment has been set before constructing a new command instance.
8082
RegistryToolEnvironment.UNITTEST.setup(systemPropertyExtension);
8183
command = newCommandInstance();

core/src/test/java/google/registry/tools/GenerateAllocationTokensCommandTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,26 @@ void testFailure_defaultPromoMustHaveTransitions() {
531531
.isEqualTo("For DEFAULT_PROMO tokens, must specify --token_status_transitions");
532532
}
533533

534+
@Test
535+
void testFailure_badTld() {
536+
assertThat(
537+
assertThrows(
538+
IllegalArgumentException.class,
539+
() -> runCommand("--number", "10", "--allowed_tlds", "badtld")))
540+
.hasMessageThat()
541+
.isEqualTo("Unknown REAL TLD(s) [badtld]");
542+
}
543+
544+
@Test
545+
void testFailure_badRegistrar() {
546+
assertThat(
547+
assertThrows(
548+
IllegalArgumentException.class,
549+
() -> runCommand("--number", "10", "--allowed_client_ids", "badregistrar")))
550+
.hasMessageThat()
551+
.isEqualTo("Unknown registrar ID(s) [badregistrar]");
552+
}
553+
534554
private AllocationToken createToken(
535555
String token,
536556
@Nullable HistoryEntryId redemptionHistoryEntryId,

core/src/test/java/google/registry/tools/UpdateAllocationTokensCommandTest.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,24 @@ void testUpdateTlds_clearTlds() throws Exception {
6464
assertThat(reloadResource(token).getAllowedTlds()).isEmpty();
6565
}
6666

67+
@Test
68+
void testUpdateTlds_badTlds() {
69+
persistResource(builderWithPromo().build());
70+
assertThat(
71+
assertThrows(
72+
IllegalArgumentException.class, () -> runCommandForced("--allowed_tlds=badtld")))
73+
.hasMessageThat()
74+
.isEqualTo("Unknown REAL TLD(s) [badtld]");
75+
}
76+
6777
@Test
6878
void testUpdateClientIds_setClientIds() throws Exception {
6979
AllocationToken token =
7080
persistResource(
7181
builderWithPromo().setAllowedRegistrarIds(ImmutableSet.of("toRemove")).build());
72-
runCommandForced("--prefix", "token", "--allowed_client_ids", "clientone,clienttwo");
82+
runCommandForced("--prefix", "token", "--allowed_client_ids", "TheRegistrar,NewRegistrar");
7383
assertThat(reloadResource(token).getAllowedRegistrarIds())
74-
.containsExactly("clientone", "clienttwo");
84+
.containsExactly("TheRegistrar", "NewRegistrar");
7585
}
7686

7787
@Test
@@ -83,6 +93,17 @@ void testUpdateClientIds_clearClientIds() throws Exception {
8393
assertThat(reloadResource(token).getAllowedRegistrarIds()).isEmpty();
8494
}
8595

96+
@Test
97+
void testUpdateClientIds_badClientId() {
98+
persistResource(builderWithPromo().build());
99+
assertThat(
100+
assertThrows(
101+
IllegalArgumentException.class,
102+
() -> runCommandForced("--allowed_client_ids=badregistrar")))
103+
.hasMessageThat()
104+
.isEqualTo("Unknown registrar ID(s) [badregistrar]");
105+
}
106+
86107
@Test
87108
void testUpdateEppActions_setEppActions() throws Exception {
88109
AllocationToken token =

0 commit comments

Comments
 (0)