Enforce canonicalization of premium/reserved list labels

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=193401336
This commit is contained in:
mcilwain 2018-04-18 12:48:05 -07:00 committed by jianglai
parent c6a4264606
commit 2c0fb6d5a6
7 changed files with 108 additions and 17 deletions

View file

@ -14,7 +14,9 @@
package google.registry.model.registry.label;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Strings.emptyToNull;
import static google.registry.util.DomainNameUtils.canonicalizeDomainName;
import static google.registry.util.PreconditionsUtils.checkArgumentNotNull;
import com.googlecode.objectify.annotation.Id;
@ -76,6 +78,10 @@ public abstract class DomainLabelEntry<T extends Comparable<?>, D extends Domain
@Override
public T build() {
checkArgumentNotNull(emptyToNull(getInstance().label), "Label must be specified");
checkArgument(
getInstance().label.equals(canonicalizeDomainName(getInstance().label)),
"Label '%s' must be in puny-coded, lower-case form",
getInstance().label);
checkArgumentNotNull(getInstance().getValue(), "Value must be specified");
return super.build();
}

View file

@ -248,6 +248,7 @@ public final class PremiumList extends BaseDomainLabelList<Money, PremiumList.Pr
/** A builder for constructing {@link PremiumListEntry} objects, since they are immutable. */
public static class Builder extends DomainLabelEntry.Builder<PremiumListEntry, Builder> {
public Builder() {}
private Builder(PremiumListEntry instance) {

View file

@ -44,6 +44,7 @@ import com.googlecode.objectify.annotation.Embed;
import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Mapify;
import com.googlecode.objectify.mapper.Mapper;
import google.registry.model.Buildable;
import google.registry.model.registry.Registry;
import google.registry.model.registry.label.DomainLabelMetrics.MetricsReservedListMatch;
import java.util.List;
@ -72,7 +73,7 @@ public final class ReservedList
*/
@Embed
public static class ReservedListEntry
extends DomainLabelEntry<ReservationType, ReservedListEntry> {
extends DomainLabelEntry<ReservationType, ReservedListEntry> implements Buildable {
ReservationType reservationType;
@ -114,8 +115,12 @@ public final class ReservedList
String label,
ReservationType reservationType,
@Nullable String restrictions,
String comment) {
ReservedListEntry entry = new ReservedListEntry();
@Nullable String comment) {
ReservedListEntry.Builder builder =
new ReservedListEntry.Builder()
.setLabel(label)
.setComment(comment)
.setReservationType(reservationType);
if (restrictions != null) {
checkArgument(
reservationType == RESERVED_FOR_ANCHOR_TENANT
@ -123,12 +128,10 @@ public final class ReservedList
"Only anchor tenant and nameserver restricted reservations "
+ "should have restrictions imposed");
if (reservationType == RESERVED_FOR_ANCHOR_TENANT) {
entry.authCode = restrictions;
builder.setAuthCode(restrictions);
} else if (reservationType == NAMESERVER_RESTRICTED) {
Set<String> allowedNameservers =
ImmutableSet.copyOf(Splitter.on(':').trimResults().split(restrictions));
checkNameserversAreValid(allowedNameservers);
entry.allowedNameservers = Joiner.on(',').join(allowedNameservers);
builder.setAllowedNameservers(
ImmutableSet.copyOf(Splitter.on(':').trimResults().split(restrictions)));
}
} else {
checkArgument(
@ -138,10 +141,7 @@ public final class ReservedList
reservationType != NAMESERVER_RESTRICTED,
"Nameserver restricted reservations must have at least one nameserver configured");
}
entry.label = label;
entry.comment = comment;
entry.reservationType = reservationType;
return entry;
return builder.build();
}
private static void checkNameserversAreValid(Set<String> nameservers) {
@ -166,6 +166,38 @@ public final class ReservedList
public ImmutableSet<String> getAllowedNameservers() {
return ImmutableSet.copyOf(Splitter.on(',').splitToList(allowedNameservers));
}
@Override
public ReservedListEntry.Builder asBuilder() {
return new ReservedListEntry.Builder(clone(this));
}
/** A builder for constructing {@link ReservedListEntry} objects, since they are immutable. */
private static class Builder
extends DomainLabelEntry.Builder<ReservedListEntry, ReservedListEntry.Builder> {
public Builder() {}
private Builder(ReservedListEntry instance) {
super(instance);
}
ReservedListEntry.Builder setAllowedNameservers(Set<String> allowedNameservers) {
checkNameserversAreValid(allowedNameservers);
getInstance().allowedNameservers = Joiner.on(',').join(allowedNameservers);
return this;
}
ReservedListEntry.Builder setAuthCode(String authCode) {
getInstance().authCode = authCode;
return this;
}
ReservedListEntry.Builder setReservationType(ReservationType reservationType) {
getInstance().reservationType = reservationType;
return this;
}
}
}
@Override