mirror of
https://github.com/google/nomulus.git
synced 2025-07-25 12:08:36 +02:00
Use the new IANA url for registrar RDAP base URLs (#1703)
Fortunately this no longer requires a log-in, we can just send a GET request and receive a CSV result in return. This also adds the apache-commons CSV parser to the dependencies See https://b.corp.google.com/issues/237784559 for more details
This commit is contained in:
parent
bda8961ea7
commit
2a502261bb
128 changed files with 9164 additions and 9264 deletions
|
@ -14,37 +14,26 @@
|
|||
|
||||
package google.registry.rdap;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkArgument;
|
||||
import static com.google.common.base.Preconditions.checkState;
|
||||
import static google.registry.persistence.transaction.TransactionManagerFactory.tm;
|
||||
import static java.nio.charset.StandardCharsets.UTF_8;
|
||||
|
||||
import com.google.api.client.http.GenericUrl;
|
||||
import com.google.api.client.http.HttpRequest;
|
||||
import com.google.api.client.http.HttpRequestFactory;
|
||||
import com.google.api.client.http.HttpResponse;
|
||||
import com.google.api.client.http.HttpTransport;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.common.collect.ImmutableSetMultimap;
|
||||
import com.google.common.flogger.FluentLogger;
|
||||
import com.google.common.io.ByteStreams;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import google.registry.keyring.api.KeyModule;
|
||||
import google.registry.model.registrar.Registrar;
|
||||
import google.registry.model.tld.Registries;
|
||||
import google.registry.model.tld.Registry.TldType;
|
||||
import google.registry.request.Action;
|
||||
import google.registry.request.auth.Auth;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.UncheckedIOException;
|
||||
import java.net.HttpCookie;
|
||||
import java.util.Optional;
|
||||
import java.io.StringReader;
|
||||
import javax.inject.Inject;
|
||||
import org.apache.commons.csv.CSVFormat;
|
||||
import org.apache.commons.csv.CSVParser;
|
||||
import org.apache.commons.csv.CSVRecord;
|
||||
|
||||
/**
|
||||
* Loads the current list of RDAP Base URLs from the ICANN servers.
|
||||
|
@ -52,24 +41,9 @@ import javax.inject.Inject;
|
|||
* <p>This will update ALL the REAL registrars. If a REAL registrar doesn't have an RDAP entry in
|
||||
* MoSAPI, we'll delete any BaseUrls it has.
|
||||
*
|
||||
* <p>The ICANN endpoint is described in the MoSAPI specifications, part 11:
|
||||
* https://www.icann.org/en/system/files/files/mosapi-specification-30may19-en.pdf
|
||||
*
|
||||
* <p>It is a "login/query/logout" system where you login using the ICANN Reporting credentials, get
|
||||
* a cookie you then send to get the list and finally logout.
|
||||
*
|
||||
* <p>For clarity, this is how one would contact this endpoint "manually", from an allow-listed IP
|
||||
* server:
|
||||
*
|
||||
* <p>$ curl [base]/login -I --user [tld]_ry:[password]
|
||||
*
|
||||
* <p>get the id=xxx value from the reply
|
||||
*
|
||||
* <p>$ curl [base]/registrarRdapBaseUrl/list -b 'id=xxx'
|
||||
*
|
||||
* <p>$ curl [base]/logout -b 'id=xxx'
|
||||
*
|
||||
* <p>where [base] is https://mosapi.icann.org/mosapi/v1/[tld]
|
||||
* <p>The ICANN base website that provides this information can be found at
|
||||
* https://www.iana.org/assignments/registrar-ids/registrar-ids.xhtml. The provided CSV endpoint
|
||||
* requires no authentication.
|
||||
*/
|
||||
@Action(
|
||||
service = Action.Service.BACKEND,
|
||||
|
@ -78,153 +52,72 @@ import javax.inject.Inject;
|
|||
auth = Auth.AUTH_INTERNAL_OR_ADMIN)
|
||||
public final class UpdateRegistrarRdapBaseUrlsAction implements Runnable {
|
||||
|
||||
private static final String MOSAPI_BASE_URL = "https://mosapi.icann.org/mosapi/v1/%s/";
|
||||
private static final String LOGIN_URL = MOSAPI_BASE_URL + "login";
|
||||
private static final String LIST_URL = MOSAPI_BASE_URL + "registrarRdapBaseUrl/list";
|
||||
private static final String LOGOUT_URL = MOSAPI_BASE_URL + "logout";
|
||||
private static final String COOKIE_ID = "id";
|
||||
private static final GenericUrl RDAP_IDS_URL =
|
||||
new GenericUrl("https://www.iana.org/assignments/registrar-ids/registrar-ids-1.csv");
|
||||
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
|
||||
|
||||
@Inject HttpTransport httpTransport;
|
||||
@Inject @KeyModule.Key("icannReportingPassword") String password;
|
||||
|
||||
@Inject
|
||||
UpdateRegistrarRdapBaseUrlsAction() {}
|
||||
|
||||
private String loginAndGetId(HttpRequestFactory requestFactory, String tld) throws IOException {
|
||||
logger.atInfo().log("Logging in to MoSAPI.");
|
||||
HttpRequest request =
|
||||
requestFactory.buildGetRequest(new GenericUrl(String.format(LOGIN_URL, tld)));
|
||||
request.getHeaders().setBasicAuthentication(String.format("%s_ry", tld), password);
|
||||
HttpResponse response = request.execute();
|
||||
|
||||
Optional<HttpCookie> idCookie =
|
||||
response.getHeaders().getHeaderStringValues("Set-Cookie").stream()
|
||||
.flatMap(value -> HttpCookie.parse(value).stream())
|
||||
.filter(cookie -> cookie.getName().equals(COOKIE_ID))
|
||||
.findAny();
|
||||
checkState(
|
||||
idCookie.isPresent(),
|
||||
"Didn't get the ID cookie from the login response. Code: %s, headers: %s",
|
||||
response.getStatusCode(),
|
||||
response.getHeaders());
|
||||
return idCookie.get().getValue();
|
||||
}
|
||||
|
||||
private void logout(HttpRequestFactory requestFactory, String id, String tld) {
|
||||
try {
|
||||
HttpRequest request =
|
||||
requestFactory.buildGetRequest(new GenericUrl(String.format(LOGOUT_URL, tld)));
|
||||
request.getHeaders().setCookie(String.format("%s=%s", COOKIE_ID, id));
|
||||
request.execute();
|
||||
} catch (IOException e) {
|
||||
logger.atWarning().withCause(e).log("Failed to log out of MoSAPI server. Continuing.");
|
||||
// No need for the whole Action to fail if only the logout failed. We can just continue with
|
||||
// the data we got.
|
||||
}
|
||||
}
|
||||
|
||||
private ImmutableSetMultimap<String, String> getRdapBaseUrlsPerIanaIdWithTld(
|
||||
String tld, String id, HttpRequestFactory requestFactory) {
|
||||
String content;
|
||||
try {
|
||||
HttpRequest request =
|
||||
requestFactory.buildGetRequest(new GenericUrl(String.format(LIST_URL, tld)));
|
||||
request.getHeaders().setAcceptEncoding("identity");
|
||||
request.getHeaders().setCookie(String.format("%s=%s", COOKIE_ID, id));
|
||||
HttpResponse response = request.execute();
|
||||
|
||||
try (InputStream input = response.getContent()) {
|
||||
content = new String(ByteStreams.toByteArray(input), UTF_8);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new UncheckedIOException(
|
||||
"Error reading RDAP list from MoSAPI server: " + e.getMessage(), e);
|
||||
} finally {
|
||||
logout(requestFactory, id, tld);
|
||||
}
|
||||
|
||||
logger.atInfo().log("list reply: '%s'", content);
|
||||
JsonObject listReply = new Gson().fromJson(content, JsonObject.class);
|
||||
JsonArray services = listReply.getAsJsonArray("services");
|
||||
// The format of the response "services" is an array of "ianaIDs to baseUrls", where "ianaIDs
|
||||
// to baseUrls" is an array of size 2 where the first item is all the "iana IDs" and the
|
||||
// second item all the "baseUrls".
|
||||
ImmutableSetMultimap.Builder<String, String> builder = new ImmutableSetMultimap.Builder<>();
|
||||
for (JsonElement service : services) {
|
||||
for (JsonElement ianaId : service.getAsJsonArray().get(0).getAsJsonArray()) {
|
||||
for (JsonElement baseUrl : service.getAsJsonArray().get(1).getAsJsonArray()) {
|
||||
builder.put(ianaId.getAsString(), baseUrl.getAsString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
private ImmutableSetMultimap<String, String> getRdapBaseUrlsPerIanaId() {
|
||||
// All TLDs have the same data, so just keep trying until one works
|
||||
// (the expectation is that all / any should work)
|
||||
ImmutableList<String> tlds = ImmutableList.sortedCopyOf(Registries.getTldsOfType(TldType.REAL));
|
||||
checkArgument(!tlds.isEmpty(), "There must exist at least one REAL TLD.");
|
||||
Throwable finalThrowable = null;
|
||||
for (String tld : tlds) {
|
||||
HttpRequestFactory requestFactory = httpTransport.createRequestFactory();
|
||||
String id;
|
||||
try {
|
||||
id = loginAndGetId(requestFactory, tld);
|
||||
} catch (Throwable e) {
|
||||
// Login failures are bad but not unexpected for certain TLDs. We shouldn't store those
|
||||
// but rather should only store useful Throwables.
|
||||
logger.atWarning().withCause(e).log("Error logging in to MoSAPI server.");
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
return getRdapBaseUrlsPerIanaIdWithTld(tld, id, requestFactory);
|
||||
} catch (Throwable throwable) {
|
||||
logger.atWarning().withCause(throwable).log(
|
||||
"Error retrieving RDAP URLs for TLD '%s'.", tld);
|
||||
finalThrowable = throwable;
|
||||
}
|
||||
}
|
||||
throw new RuntimeException(
|
||||
String.format("Error contacting MosAPI server. Tried TLDs %s", tlds), finalThrowable);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
ImmutableSetMultimap<String, String> ianaToBaseUrls = getRdapBaseUrlsPerIanaId();
|
||||
Registrar.loadAllKeysCached()
|
||||
.forEach(
|
||||
(key) ->
|
||||
tm().transact(
|
||||
() -> {
|
||||
Registrar registrar = tm().loadByKey(key);
|
||||
// Has the registrar been deleted since we loaded the key? (unlikely,
|
||||
// especially given we don't delete registrars...)
|
||||
if (registrar == null) {
|
||||
return;
|
||||
}
|
||||
// Only update REAL registrars
|
||||
if (registrar.getType() != Registrar.Type.REAL) {
|
||||
return;
|
||||
}
|
||||
String ianaId = String.valueOf(registrar.getIanaIdentifier());
|
||||
ImmutableSet<String> baseUrls = ianaToBaseUrls.get(ianaId);
|
||||
// If this registrar already has these values, skip it
|
||||
if (registrar.getRdapBaseUrls().equals(baseUrls)) {
|
||||
logger.atInfo().log(
|
||||
"No change in RdapBaseUrls for registrar %s (ianaId %s).",
|
||||
registrar.getRegistrarId(), ianaId);
|
||||
return;
|
||||
}
|
||||
logger.atInfo().log(
|
||||
"Updating RdapBaseUrls for registrar %s (ianaId %s) from %s to %s",
|
||||
registrar.getRegistrarId(),
|
||||
ianaId,
|
||||
registrar.getRdapBaseUrls(),
|
||||
baseUrls);
|
||||
tm().put(registrar.asBuilder().setRdapBaseUrls(baseUrls).build());
|
||||
}));
|
||||
ImmutableMap<String, String> ianaIdsToUrls = getIanaIdsToUrls();
|
||||
tm().transact(() -> processAllRegistrars(ianaIdsToUrls));
|
||||
}
|
||||
|
||||
private void processAllRegistrars(ImmutableMap<String, String> ianaIdsToUrls) {
|
||||
int nonUpdatedRegistrars = 0;
|
||||
for (Registrar registrar : Registrar.loadAll()) {
|
||||
// Only update REAL registrars
|
||||
if (registrar.getType() != Registrar.Type.REAL) {
|
||||
continue;
|
||||
}
|
||||
String ianaId = String.valueOf(registrar.getIanaIdentifier());
|
||||
String baseUrl = ianaIdsToUrls.get(ianaId);
|
||||
ImmutableSet<String> baseUrls =
|
||||
baseUrl == null ? ImmutableSet.of() : ImmutableSet.of(baseUrl);
|
||||
if (registrar.getRdapBaseUrls().equals(baseUrls)) {
|
||||
nonUpdatedRegistrars++;
|
||||
} else {
|
||||
if (baseUrls.isEmpty()) {
|
||||
logger.atInfo().log(
|
||||
"Removing RDAP base URLs for registrar %s", registrar.getRegistrarId());
|
||||
} else {
|
||||
logger.atInfo().log(
|
||||
"Updating RDAP base URLs for registrar %s from %s to %s",
|
||||
registrar.getRegistrarId(), registrar.getRdapBaseUrls(), baseUrls);
|
||||
}
|
||||
tm().put(registrar.asBuilder().setRdapBaseUrls(baseUrls).build());
|
||||
}
|
||||
}
|
||||
logger.atInfo().log("No change in RDAP base URLs for %d registrars", nonUpdatedRegistrars);
|
||||
}
|
||||
|
||||
private ImmutableMap<String, String> getIanaIdsToUrls() {
|
||||
CSVParser csv;
|
||||
try {
|
||||
HttpRequest request = httpTransport.createRequestFactory().buildGetRequest(RDAP_IDS_URL);
|
||||
HttpResponse response = request.execute();
|
||||
String csvString = new String(ByteStreams.toByteArray(response.getContent()), UTF_8);
|
||||
csv =
|
||||
CSVFormat.Builder.create(CSVFormat.DEFAULT)
|
||||
.setHeader()
|
||||
.setSkipHeaderRecord(true)
|
||||
.build()
|
||||
.parse(new StringReader(csvString));
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("Error when retrieving RDAP base URL CSV file", e);
|
||||
}
|
||||
ImmutableMap.Builder<String, String> result = new ImmutableMap.Builder<>();
|
||||
for (CSVRecord record : csv) {
|
||||
String ianaIdentifierString = record.get("ID");
|
||||
String rdapBaseUrl = record.get("RDAP Base URL");
|
||||
if (!rdapBaseUrl.isEmpty()) {
|
||||
result.put(ianaIdentifierString, rdapBaseUrl);
|
||||
}
|
||||
}
|
||||
return result.build();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,26 +17,21 @@ package google.registry.rdap;
|
|||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static com.google.common.truth.Truth8.assertThat;
|
||||
import static google.registry.testing.DatabaseHelper.createTld;
|
||||
import static google.registry.testing.DatabaseHelper.deleteTld;
|
||||
import static google.registry.testing.DatabaseHelper.loadRegistrar;
|
||||
import static google.registry.testing.DatabaseHelper.persistResource;
|
||||
import static google.registry.testing.DatabaseHelper.persistSimpleResource;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import com.google.api.client.http.HttpResponseException;
|
||||
import com.google.api.client.http.HttpStatusCodes;
|
||||
import com.google.api.client.http.LowLevelHttpRequest;
|
||||
import com.google.api.client.testing.http.MockHttpTransport;
|
||||
import com.google.api.client.testing.http.MockLowLevelHttpRequest;
|
||||
import com.google.api.client.testing.http.MockLowLevelHttpResponse;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.gson.JsonSyntaxException;
|
||||
import google.registry.model.registrar.Registrar;
|
||||
import google.registry.model.registrar.RegistrarAddress;
|
||||
import google.registry.model.tld.Registry;
|
||||
import google.registry.model.tld.Registry.TldType;
|
||||
import google.registry.testing.AppEngineExtension;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.RegisterExtension;
|
||||
|
@ -44,59 +39,46 @@ import org.junit.jupiter.api.extension.RegisterExtension;
|
|||
/** Unit tests for {@link UpdateRegistrarRdapBaseUrlsAction}. */
|
||||
public final class UpdateRegistrarRdapBaseUrlsActionTest {
|
||||
|
||||
/**
|
||||
* Example reply from the MoSAPI server.
|
||||
*
|
||||
* <p>This is the exact reply we got from the server when we tried to access it manually, with the
|
||||
* addition of the 4000 and 4001 ones to test "multiple iana/servers in an element"
|
||||
*
|
||||
* <p>NOTE that 4000 has the same URL twice to make sure it doesn't break
|
||||
* ImmutableSetMultimap.Builder
|
||||
*
|
||||
* <p>Also added value for IANA ID 9999, so we can check non-REAL registrars
|
||||
*/
|
||||
private static final String JSON_LIST_REPLY =
|
||||
"{\"publication\":\"2019-06-04T13:02:06Z\","
|
||||
+ "\"description\":\"ICANN-accredited Registrar's RDAP base URL list\","
|
||||
+ "\"services\":["
|
||||
+ "[[\"81\"],[\"https://rdap.gandi.net\"]],"
|
||||
+ "[[\"100\"],[\"https://yesnic.com/?_task=main&_action=whois_search\"]],"
|
||||
+ "[[\"134\"],[\"https://rdap.bb-online.com\"]],"
|
||||
+ "[[\"1316\"],[\"https://whois.35.com\"]],"
|
||||
+ "[[\"1448\"],[\"https://rdap.blacknight.com\"]],"
|
||||
+ "[[\"1463\"],[\"https://rdap.domaincostclub.com/\"]],"
|
||||
+ "[[\"99999\"],[\"https://rdaptest.com\"]],"
|
||||
+ "[[\"1556\"],[\"https://rdap.west.cn\"]],"
|
||||
+ "[[\"2288\"],[\"https://rdap.metaregistrar.com\"]],"
|
||||
+ "[[\"4000\",\"4001\"],[\"https://rdap.example.com\"]],"
|
||||
+ "[[\"4000\"],[\"https://rdap.example.net\",\"https://rdap.example.org\"]],"
|
||||
+ "[[\"4000\"],[\"https://rdap.example.net\"]],"
|
||||
+ "[[\"9999\"],[\"https://rdap.example.net\"]]"
|
||||
+ "],"
|
||||
+ "\"version\":\"1.0\"}";
|
||||
// This reply simulates part of the actual IANA CSV reply
|
||||
private static final String CSV_REPLY =
|
||||
"\"ID\",Registrar Name,Status,RDAP Base URL\n"
|
||||
+ "1,Reserved,Reserved,\n"
|
||||
+ "81,Gandi SAS,Accredited,https://rdap.gandi.net/\n"
|
||||
+ "100,Whois Corp.,Accredited,https://www.yesnic.com/rdap/\n"
|
||||
+ "134,BB-Online UK Limited,Accredited,https://rdap.bb-online.com/\n"
|
||||
+ "1316,\"Xiamen 35.Com Technology Co., Ltd.\",Accredited,https://rdap.35.com/rdap/\n"
|
||||
+ "1448,Blacknight Internet Solutions Ltd.,Accredited,https://rdap.blacknight.com/\n"
|
||||
+ "1463,\"Global Domains International, Inc. DBA"
|
||||
+ " DomainCostClub.com\",Accredited,https://rdap.domaincostclub.com/\n"
|
||||
+ "1556,\"Chengdu West Dimension Digital Technology Co.,"
|
||||
+ " Ltd.\",Accredited,https://rdap.west.cn/rdap/\n"
|
||||
+ "2288,Metaregistrar BV,Accredited,https://rdap.metaregistrar.com/\n"
|
||||
+ "4000,Gname 031 Inc,Accredited,\n"
|
||||
+ "9999,Reserved for non-billable transactions where Registry Operator acts as"
|
||||
+ " Registrar,Reserved,\n";
|
||||
|
||||
@RegisterExtension
|
||||
public AppEngineExtension appEngineExtension =
|
||||
new AppEngineExtension.Builder().withCloudSql().build();
|
||||
|
||||
private static class TestHttpTransport extends MockHttpTransport {
|
||||
private final ArrayList<MockLowLevelHttpRequest> requestsSent = new ArrayList<>();
|
||||
private final ArrayList<MockLowLevelHttpResponse> simulatedResponses = new ArrayList<>();
|
||||
private MockLowLevelHttpRequest requestSent;
|
||||
private MockLowLevelHttpResponse response;
|
||||
|
||||
void addNextResponse(MockLowLevelHttpResponse response) {
|
||||
simulatedResponses.add(response);
|
||||
void setResponse(MockLowLevelHttpResponse response) {
|
||||
this.response = response;
|
||||
}
|
||||
|
||||
List<MockLowLevelHttpRequest> getRequestsSent() {
|
||||
return requestsSent;
|
||||
MockLowLevelHttpRequest getRequestSent() {
|
||||
return requestSent;
|
||||
}
|
||||
|
||||
@Override
|
||||
public LowLevelHttpRequest buildRequest(String method, String url) {
|
||||
assertThat(method).isEqualTo("GET");
|
||||
MockLowLevelHttpRequest httpRequest = new MockLowLevelHttpRequest(url);
|
||||
httpRequest.setResponse(simulatedResponses.get(requestsSent.size()));
|
||||
requestsSent.add(httpRequest);
|
||||
httpRequest.setResponse(response);
|
||||
requestSent = httpRequest;
|
||||
return httpRequest;
|
||||
}
|
||||
}
|
||||
|
@ -106,40 +88,16 @@ public final class UpdateRegistrarRdapBaseUrlsActionTest {
|
|||
|
||||
@BeforeEach
|
||||
void beforeEach() {
|
||||
httpTransport = new TestHttpTransport();
|
||||
action = new UpdateRegistrarRdapBaseUrlsAction();
|
||||
|
||||
action.password = "myPassword";
|
||||
httpTransport = new TestHttpTransport();
|
||||
action.httpTransport = httpTransport;
|
||||
addValidResponses(httpTransport);
|
||||
|
||||
setValidResponse();
|
||||
createTld("tld");
|
||||
}
|
||||
|
||||
private void assertCorrectRequestsSent() {
|
||||
// Doing assertThat on the "getUrl()" of the elements to get better error message if we have the
|
||||
// wrong number of requests.
|
||||
// This way we'll see which URLs were hit on failure.
|
||||
assertThat(httpTransport.getRequestsSent().stream().map(request -> request.getUrl()))
|
||||
.hasSize(3);
|
||||
|
||||
MockLowLevelHttpRequest loginRequest = httpTransport.getRequestsSent().get(0);
|
||||
MockLowLevelHttpRequest listRequest = httpTransport.getRequestsSent().get(1);
|
||||
MockLowLevelHttpRequest logoutRequest = httpTransport.getRequestsSent().get(2);
|
||||
|
||||
assertThat(loginRequest.getUrl()).isEqualTo("https://mosapi.icann.org/mosapi/v1/tld/login");
|
||||
// base64.b64encode("tld_ry:myPassword") gives "dGxkX3J5Om15UGFzc3dvcmQ="
|
||||
assertThat(loginRequest.getHeaders())
|
||||
.containsEntry("authorization", ImmutableList.of("Basic dGxkX3J5Om15UGFzc3dvcmQ="));
|
||||
|
||||
assertThat(listRequest.getUrl())
|
||||
.isEqualTo("https://mosapi.icann.org/mosapi/v1/tld/registrarRdapBaseUrl/list");
|
||||
assertThat(listRequest.getHeaders())
|
||||
.containsEntry("cookie", ImmutableList.of("id=myAuthenticationId"));
|
||||
|
||||
assertThat(logoutRequest.getUrl()).isEqualTo("https://mosapi.icann.org/mosapi/v1/tld/logout");
|
||||
assertThat(logoutRequest.getHeaders())
|
||||
.containsEntry("cookie", ImmutableList.of("id=myAuthenticationId"));
|
||||
private void assertCorrectRequestSent() {
|
||||
assertThat(httpTransport.getRequestSent().getUrl())
|
||||
.isEqualTo("https://www.iana.org/assignments/registrar-ids/registrar-ids-1.csv");
|
||||
}
|
||||
|
||||
private static void persistRegistrar(
|
||||
|
@ -160,182 +118,79 @@ public final class UpdateRegistrarRdapBaseUrlsActionTest {
|
|||
.build());
|
||||
}
|
||||
|
||||
private void setValidResponse() {
|
||||
MockLowLevelHttpResponse csvResponse = new MockLowLevelHttpResponse();
|
||||
csvResponse.setContent(CSV_REPLY);
|
||||
httpTransport.setResponse(csvResponse);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testUnknownIana_cleared() {
|
||||
// The IANA ID isn't in the JSON_LIST_REPLY
|
||||
// The IANA ID isn't in the CSV reply
|
||||
persistRegistrar("someRegistrar", 4123L, Registrar.Type.REAL, "http://rdap.example/blah");
|
||||
|
||||
action.run();
|
||||
|
||||
assertCorrectRequestsSent();
|
||||
|
||||
assertCorrectRequestSent();
|
||||
assertThat(loadRegistrar("someRegistrar").getRdapBaseUrls()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testKnownIana_changed() {
|
||||
// The IANA ID is in the JSON_LIST_REPLY
|
||||
// The IANA ID is in the CSV reply
|
||||
persistRegistrar("someRegistrar", 1448L, Registrar.Type.REAL, "http://rdap.example/blah");
|
||||
|
||||
action.run();
|
||||
|
||||
assertCorrectRequestsSent();
|
||||
|
||||
assertCorrectRequestSent();
|
||||
assertThat(loadRegistrar("someRegistrar").getRdapBaseUrls())
|
||||
.containsExactly("https://rdap.blacknight.com");
|
||||
.containsExactly("https://rdap.blacknight.com/");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testKnownIana_notReal_noChange() {
|
||||
// The IANA ID is in the JSON_LIST_REPLY
|
||||
// The IANA ID is in the CSV reply
|
||||
persistRegistrar("someRegistrar", 9999L, Registrar.Type.INTERNAL, "http://rdap.example/blah");
|
||||
|
||||
// Real registrars should actually change
|
||||
persistRegistrar("otherRegistrar", 2288L, Registrar.Type.REAL, "http://old.example");
|
||||
action.run();
|
||||
|
||||
assertCorrectRequestsSent();
|
||||
|
||||
assertCorrectRequestSent();
|
||||
assertThat(loadRegistrar("someRegistrar").getRdapBaseUrls())
|
||||
.containsExactly("http://rdap.example/blah");
|
||||
assertThat(loadRegistrar("otherRegistrar").getRdapBaseUrls())
|
||||
.containsExactly("https://rdap.metaregistrar.com/");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testKnownIana_notReal_nullIANA_noChange() {
|
||||
persistRegistrar("someRegistrar", null, Registrar.Type.TEST, "http://rdap.example/blah");
|
||||
|
||||
action.run();
|
||||
|
||||
assertCorrectRequestsSent();
|
||||
|
||||
assertCorrectRequestSent();
|
||||
assertThat(loadRegistrar("someRegistrar").getRdapBaseUrls())
|
||||
.containsExactly("http://rdap.example/blah");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testKnownIana_multipleValues() {
|
||||
// The IANA ID is in the JSON_LIST_REPLY
|
||||
persistRegistrar("registrar4000", 4000L, Registrar.Type.REAL, "http://rdap.example/blah");
|
||||
persistRegistrar("registrar4001", 4001L, Registrar.Type.REAL, "http://rdap.example/blah");
|
||||
|
||||
action.run();
|
||||
|
||||
assertCorrectRequestsSent();
|
||||
|
||||
assertThat(loadRegistrar("registrar4000").getRdapBaseUrls())
|
||||
.containsExactly(
|
||||
"https://rdap.example.com", "https://rdap.example.net", "https://rdap.example.org");
|
||||
assertThat(loadRegistrar("registrar4001").getRdapBaseUrls())
|
||||
.containsExactly("https://rdap.example.com");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testNoTlds() {
|
||||
deleteTld("tld");
|
||||
IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, action::run);
|
||||
assertThat(thrown).hasMessageThat().isEqualTo("There must exist at least one REAL TLD.");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOnlyTestTlds() {
|
||||
persistResource(Registry.get("tld").asBuilder().setTldType(TldType.TEST).build());
|
||||
IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, action::run);
|
||||
assertThat(thrown).hasMessageThat().isEqualTo("There must exist at least one REAL TLD.");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSecondTldSucceeds() {
|
||||
createTld("secondtld");
|
||||
httpTransport = new TestHttpTransport();
|
||||
action.httpTransport = httpTransport;
|
||||
|
||||
// the first TLD request will return a bad cookie but the second will succeed
|
||||
MockLowLevelHttpResponse badLoginResponse = new MockLowLevelHttpResponse();
|
||||
badLoginResponse.addHeader(
|
||||
"Set-Cookie",
|
||||
"Expires=Thu, 01-Jan-1970 00:00:10 GMT; Path=/mosapi/v1/app; Secure; HttpOnly");
|
||||
|
||||
httpTransport.addNextResponse(badLoginResponse);
|
||||
addValidResponses(httpTransport);
|
||||
|
||||
action.run();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testBothFail() {
|
||||
createTld("secondtld");
|
||||
httpTransport = new TestHttpTransport();
|
||||
action.httpTransport = httpTransport;
|
||||
|
||||
MockLowLevelHttpResponse badLoginResponse = new MockLowLevelHttpResponse();
|
||||
badLoginResponse.addHeader(
|
||||
"Set-Cookie",
|
||||
"Expires=Thu, 01-Jan-1970 00:00:10 GMT; Path=/mosapi/v1/app; Secure; HttpOnly");
|
||||
|
||||
// it should fail for both TLDs
|
||||
httpTransport.addNextResponse(badLoginResponse);
|
||||
httpTransport.addNextResponse(badLoginResponse);
|
||||
void testFailure_serverErrorResponse() {
|
||||
MockLowLevelHttpResponse badResponse = new MockLowLevelHttpResponse();
|
||||
badResponse.setZeroContent();
|
||||
badResponse.setStatusCode(HttpStatusCodes.STATUS_CODE_SERVER_ERROR);
|
||||
httpTransport.setResponse(badResponse);
|
||||
|
||||
RuntimeException thrown = assertThrows(RuntimeException.class, action::run);
|
||||
assertThat(thrown).hasMessageThat().isEqualTo("Error when retrieving RDAP base URL CSV file");
|
||||
Throwable cause = thrown.getCause();
|
||||
assertThat(cause).isInstanceOf(HttpResponseException.class);
|
||||
assertThat(cause)
|
||||
.hasMessageThat()
|
||||
.isEqualTo("500\nGET https://www.iana.org/assignments/registrar-ids/registrar-ids-1.csv");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFailure_invalidCsv() {
|
||||
MockLowLevelHttpResponse csvResponse = new MockLowLevelHttpResponse();
|
||||
csvResponse.setContent("foo,bar\nbaz,foo");
|
||||
httpTransport.setResponse(csvResponse);
|
||||
|
||||
IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, action::run);
|
||||
assertThat(thrown)
|
||||
.hasMessageThat()
|
||||
.isEqualTo("Error contacting MosAPI server. Tried TLDs [secondtld, tld]");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFailureCause_ignoresLoginFailure() {
|
||||
// Login failures aren't particularly interesting so we should log them, but the final
|
||||
// throwable should be some other failure if one existed
|
||||
createTld("secondtld");
|
||||
httpTransport = new TestHttpTransport();
|
||||
action.httpTransport = httpTransport;
|
||||
|
||||
MockLowLevelHttpResponse loginResponse = new MockLowLevelHttpResponse();
|
||||
loginResponse.addHeader(
|
||||
"Set-Cookie",
|
||||
"id=myAuthenticationId; "
|
||||
+ "Expires=Tue, 11-Jun-2019 16:34:21 GMT; Path=/mosapi/v1/app; Secure; HttpOnly");
|
||||
|
||||
MockLowLevelHttpResponse badListResponse = new MockLowLevelHttpResponse();
|
||||
String badListReply = JSON_LIST_REPLY.substring(50);
|
||||
badListResponse.setContent(badListReply);
|
||||
|
||||
MockLowLevelHttpResponse logoutResponse = new MockLowLevelHttpResponse();
|
||||
logoutResponse.addHeader(
|
||||
"Set-Cookie",
|
||||
"id=id; Expires=Thu, 01-Jan-1970 00:00:10 GMT; Path=/mosapi/v1/app; Secure; HttpOnly");
|
||||
|
||||
MockLowLevelHttpResponse badLoginResponse = new MockLowLevelHttpResponse();
|
||||
badLoginResponse.addHeader(
|
||||
"Set-Cookie",
|
||||
"Expires=Thu, 01-Jan-1970 00:00:10 GMT; Path=/mosapi/v1/app; Secure; HttpOnly");
|
||||
|
||||
httpTransport.addNextResponse(loginResponse);
|
||||
httpTransport.addNextResponse(badListResponse);
|
||||
httpTransport.addNextResponse(logoutResponse);
|
||||
httpTransport.addNextResponse(badLoginResponse);
|
||||
|
||||
RuntimeException thrown = assertThrows(RuntimeException.class, action::run);
|
||||
assertThat(thrown).hasCauseThat().isInstanceOf(JsonSyntaxException.class);
|
||||
}
|
||||
|
||||
private static void addValidResponses(TestHttpTransport httpTransport) {
|
||||
MockLowLevelHttpResponse loginResponse = new MockLowLevelHttpResponse();
|
||||
loginResponse.addHeader(
|
||||
"Set-Cookie",
|
||||
"JSESSIONID=bogusid; " + "Expires=Tue, 11-Jun-2019 16:34:21 GMT; Path=/; Secure; HttpOnly");
|
||||
loginResponse.addHeader(
|
||||
"Set-Cookie",
|
||||
"id=myAuthenticationId; "
|
||||
+ "Expires=Tue, 11-Jun-2019 16:34:21 GMT; Path=/mosapi/v1/app; Secure; HttpOnly");
|
||||
|
||||
MockLowLevelHttpResponse listResponse = new MockLowLevelHttpResponse();
|
||||
listResponse.setContent(JSON_LIST_REPLY);
|
||||
|
||||
MockLowLevelHttpResponse logoutResponse = new MockLowLevelHttpResponse();
|
||||
logoutResponse.addHeader(
|
||||
"Set-Cookie",
|
||||
"id=id; Expires=Thu, 01-Jan-1970 00:00:10 GMT; Path=/mosapi/v1/app; Secure; HttpOnly");
|
||||
httpTransport.addNextResponse(loginResponse);
|
||||
httpTransport.addNextResponse(listResponse);
|
||||
httpTransport.addNextResponse(logoutResponse);
|
||||
.isEqualTo("Mapping for ID not found, expected one of [foo, bar]");
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue