Skip to content
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 136 additions & 0 deletions core/src/main/java/google/registry/tools/RdapQueryCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
// Copyright 2025 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.tools;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;

import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import com.google.common.collect.ImmutableMap;
import com.google.common.flogger.FluentLogger;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import google.registry.config.RegistryConfig.Config;
import google.registry.request.Action.GkeService;
import jakarta.inject.Inject;
import java.io.IOException;

/** Command to manually perform an authenticated RDAP query. */
@Parameters(separators = " =", commandDescription = "Manually perform an authenticated RDAP query")
public final class RdapQueryCommand implements CommandWithConnection {

private static final FluentLogger logger = FluentLogger.forEnclosingClass();

/**
* A simple data class to hold the path and query parameters for an RDAP request.
*
* <p>This is used as a return type for the {@code RdapQueryType.getRequestData} method to
* bundle the two distinct return values into a single object.
*/
private static class RequestData {
final String path;
final ImmutableMap<String, String> queryParams;

private RequestData(String path, ImmutableMap<String, String> queryParams) {
this.path = path;
this.queryParams = queryParams;
}

static RequestData create(String path, ImmutableMap<String, String> queryParams) {
return new RequestData(path, queryParams);
}
}

/** Defines the RDAP query types, encapsulating their path logic and parameter requirements. */
enum RdapQueryType {
DOMAIN(true, "/rdap/domain/%s"),
DOMAIN_SEARCH(true, "/rdap/domains", "name"),
NAMESERVER(true, "/rdap/nameserver/%s"),
NAMESERVER_SEARCH(true, "/rdap/nameservers", "name"),
ENTITY(true, "/rdap/entity/%s"),
ENTITY_SEARCH(true, "/rdap/entities", "fn"),
HELP(false, "/rdap/help");

private final boolean requiresQueryTerm;
private final String pathFormat;
private final String searchParamKey;

RdapQueryType(boolean requiresQueryTerm, String pathFormat) {
this(requiresQueryTerm, pathFormat, null);
}

RdapQueryType(boolean requiresQueryTerm, String pathFormat, String searchParamKey) {
this.requiresQueryTerm = requiresQueryTerm;
this.pathFormat = pathFormat;
this.searchParamKey = searchParamKey;
}

/** Returns a RequestData object containing the path and query parameters for the request. */
public RequestData getRequestData(String queryTerm) {
if (requiresQueryTerm) {
checkArgument(queryTerm != null, "A query term is required for the %s query.", this);
} else {
checkArgument(queryTerm == null, "The %s query does not take a query term.", this);
}

if (searchParamKey != null) {
return RequestData.create(pathFormat, ImmutableMap.of(searchParamKey, queryTerm));
} else if (requiresQueryTerm) {
return RequestData.create(String.format(pathFormat, queryTerm), ImmutableMap.of());
} else {
return RequestData.create(pathFormat, ImmutableMap.of());
}
}
}

@Parameter(names = "--type", description = "The type of RDAP query to perform.", required = true)
private RdapQueryType type;

@Parameter(
description = "The main query term (e.g., a domain name or search pattern).",
required = false)
private String queryTerm;

private ServiceConnection defaultConnection;

@Inject
@Config("useCanary")
boolean useCanary;

@Override
public void setConnection(ServiceConnection connection) {
this.defaultConnection = connection;
}

@Override
public void run() throws IOException {
checkState(defaultConnection != null, "ServiceConnection was not set by RegistryCli.");

RequestData requestData = type.getRequestData(queryTerm);

ServiceConnection pubapiConnection =
defaultConnection.withService(GkeService.PUBAPI, useCanary);
String rdapResponse =
pubapiConnection.sendGetRequest(requestData.path, requestData.queryParams);

JsonElement rdapJson = JsonParser.parseString(rdapResponse);
Gson gson = new GsonBuilder().setPrettyPrinting().create();
logger.atInfo().log(gson.toJson(rdapJson));

}
}
1 change: 1 addition & 0 deletions core/src/main/java/google/registry/tools/RegistryTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ public final class RegistryTool {
.put("login", LoginCommand.class)
.put("logout", LogoutCommand.class)
.put("pending_escrow", PendingEscrowCommand.class)
.put("rdap_query", RdapQueryCommand.class)
.put("recreate_billing_recurrences", RecreateBillingRecurrencesCommand.class)
.put("registrar_poc", RegistrarPocCommand.class)
.put("renew_domain", RenewDomainCommand.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ interface RegistryToolComponent {

void inject(PendingEscrowCommand command);

void inject(RdapQueryCommand command);

void inject(RenewDomainCommand command);

void inject(SaveSqlCredentialCommand command);
Expand Down
109 changes: 109 additions & 0 deletions core/src/test/java/google/registry/tools/RdapQueryCommandTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// Copyright 2025 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.tools;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.beust.jcommander.ParameterException;
import com.google.common.collect.ImmutableMap;
import google.registry.request.Action.GkeService;
import java.io.IOException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;

/** Unit tests for {@link RdapQueryCommand}. */
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
class RdapQueryCommandTest extends CommandTestCase<RdapQueryCommand> {

@Mock private ServiceConnection mockDefaultConnection;
@Mock private ServiceConnection mockPubapiConnection;

@BeforeEach
void beforeEach() {
command.setConnection(mockDefaultConnection);
command.useCanary = false;

when(mockDefaultConnection.withService(eq(GkeService.PUBAPI), eq(false)))
.thenReturn(mockPubapiConnection);
}

private void mockGetResponse(
String path, ImmutableMap<String, ?> queryParams, String responseBody) throws IOException {
when(mockPubapiConnection.sendGetRequest(eq(path), eq(queryParams))).thenReturn(responseBody);
}

@Test
void testSuccess_domainLookup() throws Exception {
String path = "/rdap/domain/example.dev";
mockGetResponse(path, ImmutableMap.of(), "{}");
runCommand("--type=DOMAIN", "example.dev");
verify(mockPubapiConnection).sendGetRequest(eq(path), eq(ImmutableMap.of()));
}

@Test
void testSuccess_domainSearch() throws Exception {
String path = "/rdap/domains";
ImmutableMap<String, String> query = ImmutableMap.of("name", "exam*.dev");
mockGetResponse(path, query, "{}");
runCommand("--type=DOMAINSEARCH", "exam*.dev");
verify(mockPubapiConnection).sendGetRequest(eq(path), eq(query));
}

@Test
void testSuccess_entitySearch() throws Exception {
String path = "/rdap/entities";
ImmutableMap<String, String> query = ImmutableMap.of("fn", "John*");
mockGetResponse(path, query, "{}");
runCommand("--type=ENTITYSEARCH", "John*");
verify(mockPubapiConnection).sendGetRequest(eq(path), eq(query));
}

@Test
void testSuccess_help() throws Exception {
String path = "/rdap/help";
mockGetResponse(path, ImmutableMap.of(), "{}");
runCommand("--type=HELP");
verify(mockPubapiConnection).sendGetRequest(eq(path), eq(ImmutableMap.of()));
}

@Test
void testFailure_missingQueryTerm_forDomain() {
IllegalArgumentException thrown =
assertThrows(IllegalArgumentException.class, () -> runCommand("--type=DOMAIN"));
assertThat(thrown).hasMessageThat().contains("A query term is required for a DOMAIN lookup.");
}

@Test
void testFailure_hasQueryTerm_forHelp() {
IllegalArgumentException thrown =
assertThrows(IllegalArgumentException.class, () -> runCommand("--type=HELP", "foo"));
assertThat(thrown).hasMessageThat().contains("The HELP query does not take a query term.");
}

@Test
void testFailure_missingMainParameter() {
assertThrows(ParameterException.class, this::runCommand);
}
}
Loading