// 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.rdap;
import static com.google.common.base.Strings.nullToEmpty;
import static google.registry.model.EppResourceUtils.isLinked;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.util.CollectionUtils.union;
import static google.registry.util.DomainNameUtils.ACE_PREFIX;
import com.google.common.base.Function;
import com.google.common.base.Functions;
import com.google.common.base.Optional;
import com.google.common.base.Predicates;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;
import com.google.common.collect.Ordering;
import com.google.common.net.InetAddresses;
import com.googlecode.objectify.Key;
import google.registry.config.RdapNoticeDescriptor;
import google.registry.config.RegistryConfig.Config;
import google.registry.model.EppResource;
import google.registry.model.contact.ContactPhoneNumber;
import google.registry.model.contact.ContactResource;
import google.registry.model.contact.PostalInfo;
import google.registry.model.domain.DesignatedContact;
import google.registry.model.domain.DesignatedContact.Type;
import google.registry.model.domain.DomainResource;
import google.registry.model.eppcommon.Address;
import google.registry.model.eppcommon.StatusValue;
import google.registry.model.host.HostResource;
import google.registry.model.registrar.Registrar;
import google.registry.model.registrar.RegistrarAddress;
import google.registry.model.registrar.RegistrarContact;
import google.registry.model.reporting.HistoryEntry;
import google.registry.request.HttpException.InternalServerErrorException;
import google.registry.request.HttpException.NotFoundException;
import google.registry.util.FormattingLogger;
import google.registry.util.Idn;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.URI;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import javax.annotation.Nullable;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.joda.time.DateTime;
/**
* Helper class to create RDAP JSON objects for various registry entities and objects.
*
*
The JSON format specifies that entities should be supplied with links indicating how to fetch
* them via RDAP, which requires the URL to the RDAP server. The linkBase parameter, passed to many
* of the methods, is used as the first part of the link URL. For instance, if linkBase is
* "http://rdap.org/dir/", the link URLs will look like "http://rdap.org/dir/domain/XXXX", etc.
*
* @see
* RFC 7483: JSON Responses for the Registration Data Access Protocol (RDAP)
*/
@Singleton
public class RdapJsonFormatter {
@Inject @Config("rdapTosPath") String rdapTosPath;
@Inject @Config("rdapHelpMap") ImmutableMap rdapHelpMap;
@Inject RdapJsonFormatter() {}
private static final FormattingLogger logger = FormattingLogger.getLoggerForCallerClass();
/**
* What type of data to generate. Summary data includes only information about the object itself,
* while full data includes associated items (e.g. for domains, full data includes the hosts,
* contacts and history entries connected with the domain). Summary data is appropriate for search
* queries which return many results, to avoid load on the system. According to the ICANN
* operational profile, a remark must be attached to the returned object indicating that it
* includes only summary data.
*/
public enum OutputDataType {
FULL,
SUMMARY
}
/**
* Indication of what type of boilerplate notices are required for the RDAP JSON messages. The
* ICANN RDAP Profile specifies that, for instance, domain name responses should include a remark
* about domain status codes. So we need to know when to include such boilerplate. On the other
* hand, remarks are not allowed except in domain, nameserver and entity objects, so we need to
* suppress them for other types of responses (e.g. help).
*/
public enum BoilerplateType {
DOMAIN,
NAMESERVER,
ENTITY,
OTHER
}
private static final String RDAP_CONFORMANCE_LEVEL = "rdap_level_0";
private static final String VCARD_VERSION_NUMBER = "4.0";
static final String NOTICES = "notices";
private static final String REMARKS = "remarks";
private enum RdapStatus {
// Status values specified in RFC 7483 § 10.2.2.
VALIDATED("validated"),
RENEW_PROHIBITED("renew prohibited"),
UPDATE_PROHIBITED("update prohibited"),
TRANSFER_PROHIBITED("transfer prohibited"),
DELETE_PROHIBITED("delete prohibited"),
PROXY("proxy"),
PRIVATE("private"),
REMOVED("removed"),
OBSCURED("obscured"),
ASSOCIATED("associated"),
ACTIVE("active"),
INACTIVE("inactive"),
LOCKED("locked"),
PENDING_CREATE("pending create"),
PENDING_RENEW("pending renew"),
PENDING_TRANSFER("pending transfer"),
PENDING_UPDATE("pending update"),
PENDING_DELETE("pending delete"),
// Additional status values defined in
// https://tools.ietf.org/html/draft-ietf-regext-epp-rdap-status-mapping-01.
ADD_PERIOD("add period"),
AUTO_RENEW_PERIOD("auto renew period"),
CLIENT_DELETE_PROHIBITED("client delete prohibited"),
CLIENT_HOLD("client hold"),
CLIENT_RENEW_PROHIBITED("client renew prohibited"),
CLIENT_TRANSFER_PROHIBITED("client transfer prohibited"),
CLIENT_UPDATE_PROHIBITED("client update prohibited"),
PENDING_RESTORE("pending restore"),
REDEMPTION_PERIOD("redemption period"),
RENEW_PERIOD("renew period"),
SERVER_DELETE_PROHIBITED("server deleted prohibited"),
SERVER_RENEW_PROHIBITED("server renew prohibited"),
SERVER_TRANSFER_PROHIBITED("server transfer prohibited"),
SERVER_UPDATE_PROHIBITED("server update prohibited"),
SERVER_HOLD("server hold"),
TRANSFER_PERIOD("transfer period");
/** Value as it appears in RDAP messages. */
private final String rfc7483String;
private RdapStatus(String rfc7483String) {
this.rfc7483String = rfc7483String;
}
public String getDisplayName() {
return rfc7483String;
}
}
/** Map of EPP status values to the RDAP equivalents. */
private static final ImmutableMap statusToRdapStatusMap =
Maps.immutableEnumMap(
new ImmutableMap.Builder()
// RdapStatus.ADD_PERIOD not defined in our system
// RdapStatus.AUTO_RENEW_PERIOD not defined in our system
.put(StatusValue.CLIENT_DELETE_PROHIBITED, RdapStatus.CLIENT_DELETE_PROHIBITED)
.put(StatusValue.CLIENT_HOLD, RdapStatus.CLIENT_HOLD)
.put(StatusValue.CLIENT_RENEW_PROHIBITED, RdapStatus.CLIENT_RENEW_PROHIBITED)
.put(StatusValue.CLIENT_TRANSFER_PROHIBITED, RdapStatus.CLIENT_TRANSFER_PROHIBITED)
.put(StatusValue.CLIENT_UPDATE_PROHIBITED, RdapStatus.CLIENT_UPDATE_PROHIBITED)
.put(StatusValue.INACTIVE, RdapStatus.INACTIVE)
.put(StatusValue.LINKED, RdapStatus.ASSOCIATED)
.put(StatusValue.OK, RdapStatus.ACTIVE)
.put(StatusValue.PENDING_CREATE, RdapStatus.PENDING_CREATE)
.put(StatusValue.PENDING_DELETE, RdapStatus.PENDING_DELETE)
// RdapStatus.PENDING_RENEW not defined in our system
// RdapStatus.PENDING_RESTORE not defined in our system
.put(StatusValue.PENDING_TRANSFER, RdapStatus.PENDING_TRANSFER)
.put(StatusValue.PENDING_UPDATE, RdapStatus.PENDING_UPDATE)
// RdapStatus.REDEMPTION_PERIOD not defined in our system
// RdapStatus.RENEW_PERIOD not defined in our system
.put(StatusValue.SERVER_DELETE_PROHIBITED, RdapStatus.SERVER_DELETE_PROHIBITED)
.put(StatusValue.SERVER_HOLD, RdapStatus.SERVER_HOLD)
.put(StatusValue.SERVER_RENEW_PROHIBITED, RdapStatus.SERVER_RENEW_PROHIBITED)
.put(StatusValue.SERVER_TRANSFER_PROHIBITED, RdapStatus.SERVER_TRANSFER_PROHIBITED)
.put(StatusValue.SERVER_UPDATE_PROHIBITED, RdapStatus.SERVER_UPDATE_PROHIBITED)
// RdapStatus.TRANSFER_PERIOD not defined in our system
.build());
/** Role values specified in RFC 7483 § 10.2.4. */
private enum RdapEntityRole {
REGISTRANT("registrant"),
TECH("technical"),
ADMIN("administrative"),
ABUSE("abuse"),
BILLING("billing"),
REGISTRAR("registrar"),
RESELLER("reseller"),
SPONSOR("sponsor"),
PROXY("proxy"),
NOTIFICATIONS("notifications"),
NOC("noc");
/** Value as it appears in RDAP messages. */
final String rfc7483String;
private RdapEntityRole(String rfc7483String) {
this.rfc7483String = rfc7483String;
}
}
/** Status values specified in RFC 7483 § 10.2.2. */
private enum RdapEventAction {
REGISTRATION("registration"),
REREGISTRATION("reregistration"),
LAST_CHANGED("last changed"),
EXPIRATION("expiration"),
DELETION("deletion"),
REINSTANTIATION("reinstantiation"),
TRANSFER("transfer"),
LOCKED("locked"),
UNLOCKED("unlocked"),
LAST_UPDATE_OF_RDAP_DATABASE("last update of RDAP database");
/** Value as it appears in RDAP messages. */
private final String rfc7483String;
private RdapEventAction(String rfc7483String) {
this.rfc7483String = rfc7483String;
}
public String getDisplayName() {
return rfc7483String;
}
}
/** Map of EPP event values to the RDAP equivalents. */
private static final ImmutableMap
historyEntryTypeToRdapEventActionMap =
Maps.immutableEnumMap(
new ImmutableMap.Builder()
.put(HistoryEntry.Type.CONTACT_CREATE, RdapEventAction.REGISTRATION)
.put(HistoryEntry.Type.CONTACT_DELETE, RdapEventAction.DELETION)
.put(HistoryEntry.Type.CONTACT_TRANSFER_APPROVE, RdapEventAction.TRANSFER)
.put(HistoryEntry.Type.DOMAIN_APPLICATION_CREATE, RdapEventAction.REGISTRATION)
.put(HistoryEntry.Type.DOMAIN_APPLICATION_DELETE, RdapEventAction.DELETION)
.put(HistoryEntry.Type.DOMAIN_AUTORENEW, RdapEventAction.REREGISTRATION)
.put(HistoryEntry.Type.DOMAIN_CREATE, RdapEventAction.REGISTRATION)
.put(HistoryEntry.Type.DOMAIN_DELETE, RdapEventAction.DELETION)
.put(HistoryEntry.Type.DOMAIN_RENEW, RdapEventAction.REREGISTRATION)
.put(HistoryEntry.Type.DOMAIN_RESTORE, RdapEventAction.REINSTANTIATION)
.put(HistoryEntry.Type.DOMAIN_TRANSFER_APPROVE, RdapEventAction.TRANSFER)
.put(HistoryEntry.Type.HOST_CREATE, RdapEventAction.REGISTRATION)
.put(HistoryEntry.Type.HOST_DELETE, RdapEventAction.DELETION)
.build());
private static final ImmutableList CONFORMANCE_LIST =
ImmutableList.of(RDAP_CONFORMANCE_LEVEL);
private static final ImmutableList STATUS_LIST_ACTIVE =
ImmutableList.of(RdapStatus.ACTIVE.rfc7483String);
private static final ImmutableList STATUS_LIST_REMOVED =
ImmutableList.of(RdapStatus.REMOVED.rfc7483String);
private static final ImmutableMap> PHONE_TYPE_VOICE =
ImmutableMap.of("type", ImmutableList.of("voice"));
private static final ImmutableMap> PHONE_TYPE_FAX =
ImmutableMap.of("type", ImmutableList.of("fax"));
private static final ImmutableList> VCARD_ENTRY_VERSION =
ImmutableList.of("version", ImmutableMap.of(), "text", VCARD_VERSION_NUMBER);
/** Sets the ordering for hosts; just use the fully qualified host name. */
private static final Ordering HOST_RESOURCE_ORDERING =
Ordering.natural().onResultOf(new Function() {
@Override
public String apply(HostResource host) {
return host.getFullyQualifiedHostName();
}});
/** Sets the ordering for designated contacts; order them in a fixed order by contact type. */
private static final Ordering DESIGNATED_CONTACT_ORDERING =
Ordering.natural().onResultOf(new Function() {
@Override
public DesignatedContact.Type apply(DesignatedContact designatedContact) {
return designatedContact.getType();
}});
ImmutableMap getJsonTosNotice(String rdapLinkBase) {
return getJsonHelpNotice(rdapTosPath, rdapLinkBase);
}
ImmutableMap getJsonHelpNotice(
String pathSearchString, String rdapLinkBase) {
if (pathSearchString.isEmpty()) {
pathSearchString = "/";
}
if (!rdapHelpMap.containsKey(pathSearchString)) {
throw new NotFoundException("no help found for " + pathSearchString);
}
try {
return RdapJsonFormatter.makeRdapJsonNotice(rdapHelpMap.get(pathSearchString), rdapLinkBase);
} catch (Exception e) {
logger.warningfmt(e, "Error reading RDAP help file: %s", pathSearchString);
throw new InternalServerErrorException("unable to read help for " + pathSearchString);
}
}
/**
* Adds the required top-level boilerplate. RFC 7483 specifies that the top-level object should
* include an entry indicating the conformance level. The ICANN RDAP Profile document (dated 3
* December 2015) mandates several additional entries, in sections 1.4.4, 1.4.10, 1.5.18 and
* 1.5.20. Note that this method will only work if there are no object-specific remarks already in
* the JSON object being built. If there are, the boilerplate must be merged in.
*
* @param jsonBuilder a builder for a JSON map object
* @param boilerplateType type of boilerplate to be added; the ICANN RDAP Profile document
* mandates extra boilerplate for domain objects
* @param notices a list of notices to be inserted before the boilerplate notices. If the TOS
* notice is in this list, the method avoids adding a second copy.
* @param remarks a list of remarks to be inserted before the boilerplate notices.
* @param rdapLinkBase the base for link URLs
*/
void addTopLevelEntries(
ImmutableMap.Builder jsonBuilder,
BoilerplateType boilerplateType,
List> notices,
List> remarks,
String rdapLinkBase) {
jsonBuilder.put("rdapConformance", CONFORMANCE_LIST);
ImmutableList.Builder> noticesBuilder =
new ImmutableList.Builder<>();
ImmutableMap tosNotice = getJsonTosNotice(rdapLinkBase);
boolean tosNoticeFound = false;
if (!notices.isEmpty()) {
noticesBuilder.addAll(notices);
for (ImmutableMap notice : notices) {
if (notice.equals(tosNotice)) {
tosNoticeFound = true;
break;
}
}
}
if (!tosNoticeFound) {
noticesBuilder.add(tosNotice);
}
jsonBuilder.put(NOTICES, noticesBuilder.build());
ImmutableList.Builder> remarksBuilder =
new ImmutableList.Builder<>();
remarksBuilder.addAll(remarks);
switch (boilerplateType) {
case DOMAIN:
remarksBuilder.addAll(RdapIcannStandardInformation.domainBoilerplateRemarks);
break;
case NAMESERVER:
case ENTITY:
remarksBuilder.addAll(RdapIcannStandardInformation.nameserverAndEntityBoilerplateRemarks);
break;
default: // things other than domains, nameservers and entities cannot contain remarks
break;
}
ImmutableList> remarksToAdd = remarksBuilder.build();
if (!remarksToAdd.isEmpty()) {
jsonBuilder.put(REMARKS, remarksToAdd);
}
}
/**
* Creates a JSON object containing a notice or remark object, as defined by RFC 7483 § 4.3.
* The object should then be inserted into a notices or remarks array. The builder fields are:
*
*
title: the title of the notice; if null, the notice will have no title
*
*
description: objects which will be converted to strings to form the description of the
* notice (this is the only required field; all others are optional)
*
*
typeString: the notice or remark type as defined in § 10.2.1; if null, no type
*
*
linkValueSuffix: the path at the end of the URL used in the value field of the link,
* without any initial slash (e.g. a suffix of help/toc equates to a URL of
* http://example.net/help/toc); if null, no link is created; if it is not null, a single link is
* created; this method never creates more than one link)
*
*
htmlUrlString: the path, if any, to be used in the href value of the link; if the URL is
* absolute, it is used as is; if it is relative, starting with a slash, it is appended to the
* protocol and host of the link base; if it is relative, not starting with a slash, it is
* appended to the complete link base; if null, a self link is generated instead, using the link
* link value
*
*
linkBase: the base for the link value and href; if null, it is assumed to be the empty
* string
*
* @see
* RFC 7483: JSON Responses for the Registration Data Access Protocol (RDAP)
*/
static ImmutableMap makeRdapJsonNotice(
RdapNoticeDescriptor parameters, @Nullable String linkBase) {
ImmutableMap.Builder jsonBuilder = new ImmutableMap.Builder<>();
if (parameters.getTitle() != null) {
jsonBuilder.put("title", parameters.getTitle());
}
ImmutableList.Builder descriptionBuilder = new ImmutableList.Builder<>();
for (String line : parameters.getDescription()) {
descriptionBuilder.add(nullToEmpty(line));
}
jsonBuilder.put("description", descriptionBuilder.build());
if (parameters.getTypeString() != null) {
jsonBuilder.put("typeString", parameters.getTypeString());
}
String linkValueString =
nullToEmpty(linkBase) + nullToEmpty(parameters.getLinkValueSuffix());
if (parameters.getLinkHrefUrlString() == null) {
jsonBuilder.put("links", ImmutableList.of(ImmutableMap.of(
"value", linkValueString,
"rel", "self",
"href", linkValueString,
"type", "application/rdap+json")));
} else {
URI htmlBaseURI = URI.create(nullToEmpty(linkBase));
URI htmlUri = htmlBaseURI.resolve(parameters.getLinkHrefUrlString());
jsonBuilder.put("links", ImmutableList.of(ImmutableMap.of(
"value", linkValueString,
"rel", "alternate",
"href", htmlUri.toString(),
"type", "text/html")));
}
return jsonBuilder.build();
}
/**
* Creates a JSON object for a {@link DomainResource}.
*
* @param domainResource the domain resource object from which the JSON object should be created
* @param isTopLevel if true, the top-level boilerplate will be added
* @param linkBase the URL base to be used when creating links
* @param whoisServer the fully-qualified domain name of the WHOIS server to be listed in the
* port43 field; if null, port43 is not added to the object
* @param now the as-date
* @param outputDataType whether to generate full or summary data
* @param authorization the authorization level of the request; if not authorized for the
* registrar owning the domain, no contact information is included
*/
ImmutableMap makeRdapJsonForDomain(
DomainResource domainResource,
boolean isTopLevel,
@Nullable String linkBase,
@Nullable String whoisServer,
DateTime now,
OutputDataType outputDataType,
RdapAuthorization authorization) {
// Start with the domain-level information.
ImmutableMap.Builder jsonBuilder = new ImmutableMap.Builder<>();
jsonBuilder.put("objectClassName", "domain");
jsonBuilder.put("handle", domainResource.getRepoId());
jsonBuilder.put("ldhName", domainResource.getFullyQualifiedDomainName());
// Only include the unicodeName field if there are unicode characters.
if (hasUnicodeComponents(domainResource.getFullyQualifiedDomainName())) {
jsonBuilder.put("unicodeName", Idn.toUnicode(domainResource.getFullyQualifiedDomainName()));
}
jsonBuilder.put(
"status",
makeStatusValueList(
domainResource.getStatusValues(), domainResource.getDeletionTime().isBefore(now)));
jsonBuilder.put("links", ImmutableList.of(
makeLink("domain", domainResource.getFullyQualifiedDomainName(), linkBase)));
boolean displayContacts =
authorization.isAuthorizedForClientId(domainResource.getCurrentSponsorClientId());
// If we are outputting all data (not just summary data), also add information about hosts,
// contacts and events (history entries). If we are outputting summary data, instead add a
// remark indicating that fact.
List> remarks;
if (outputDataType == OutputDataType.SUMMARY) {
remarks = ImmutableList.of(RdapIcannStandardInformation.SUMMARY_DATA_REMARK);
} else {
remarks = displayContacts
? ImmutableList.>of()
: ImmutableList.of(RdapIcannStandardInformation.DOMAIN_CONTACTS_HIDDEN_DATA_REMARK);
ImmutableList