Add escrow file import validation logic

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=129116064
This commit is contained in:
Wolfgang Meyers 2016-08-02 10:56:27 -07:00 committed by Justine Tunney
parent f9636b6cea
commit d3d33ccfdc
11 changed files with 956 additions and 33 deletions

View file

@ -314,6 +314,15 @@ public final class ConfigModule {
return projectId + "-rde";
}
/**
* Returns the Google Cloud Storage bucket for importing escrow files.
*/
@Provides
@Config("rdeImportBucket")
public String provideRdeImportBucket(@Config("projectId") String projectId) {
return projectId + "-rde-import";
}
/**
* Size of Ghostryde buffer in bytes for each layer in the pipeline.
*

View file

@ -98,11 +98,6 @@ public final class ProductionRegistryConfigExample implements RegistryConfig {
return getProjectId() + "-zonefiles";
}
@Override
public String getEscrowFileImportBucket() {
return getProjectId() + "-escrow-import";
}
@Override
public boolean getTmchCaTestingMode() {
switch (environment) {

View file

@ -89,11 +89,6 @@ public interface RegistryConfig {
*/
public String getZoneFilesBucket();
/**
* Returns the Google Cloud Storage bucket for importing escrow files.
*/
public String getEscrowFileImportBucket();
/**
* Returns {@code true} if TMCH certificate authority should be in testing mode.
*

View file

@ -65,11 +65,6 @@ public class TestRegistryConfig implements RegistryConfig {
return getProjectId() + "-zonefiles";
}
@Override
public String getEscrowFileImportBucket() {
return getProjectId() + "-escrow-import";
}
@Override
public boolean getTmchCaTestingMode() {
return true;

View file

@ -35,7 +35,7 @@ import javax.annotation.CheckReturnValue;
import javax.inject.Inject;
/** Utilities for working with Google Cloud Storage. */
public final class GcsUtils {
public class GcsUtils {
private static final FormattingLogger logger = FormattingLogger.getLoggerForCallerClass();

View file

@ -14,17 +14,30 @@
package google.registry.rde;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
import com.google.appengine.tools.cloudstorage.GcsFilename;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.Work;
import google.registry.config.ConfigModule.Config;
import google.registry.gcs.GcsUtils;
import google.registry.model.contact.ContactResource;
import google.registry.model.index.EppResourceIndex;
import google.registry.model.index.ForeignKeyIndex;
import google.registry.model.ofy.Ofy;
import google.registry.model.registrar.Registrar;
import google.registry.model.registry.Registry;
import google.registry.model.registry.Registry.RegistryNotFoundException;
import google.registry.model.registry.Registry.TldState;
import google.registry.util.Clock;
import google.registry.util.FormattingLogger;
import google.registry.xjc.rderegistrar.XjcRdeRegistrar;
import java.io.IOException;
import java.io.InputStream;
import javax.inject.Inject;
import javax.xml.stream.XMLStreamException;
import org.joda.time.DateTime;
/** Utility functions for escrow file import. */
public final class RdeImportUtils {
@ -33,11 +46,16 @@ public final class RdeImportUtils {
private final Ofy ofy;
private final Clock clock;
private final String escrowBucketName;
private final GcsUtils gcsUtils;
@Inject
public RdeImportUtils(Ofy ofy, Clock clock) {
public RdeImportUtils(
Ofy ofy, Clock clock, @Config("rdeImportBucket") String escrowBucketName, GcsUtils gcsUtils) {
this.ofy = ofy;
this.clock = clock;
this.gcsUtils = gcsUtils;
this.escrowBucketName = escrowBucketName;
}
/**
@ -84,4 +102,55 @@ public final class RdeImportUtils {
}
});
}
/**
* Validates an escrow file for import.
*
* <p>Before an escrow file is imported into the registry, the following conditions must be met:
*
* <ul>
* <li>The TLD must already exist in the registry
* <li>The TLD must be in the PREDELEGATION state
* <li>Each registrar must already exist in the registry
* <li>Each IDN table referenced must already exist in the registry
* </ul>
*
* <p>If any of the above conditions is not true, an {@link IllegalStateException} will be thrown.
*
* @param escrowFilePath Path to the escrow file to validate
* @throws IOException If the escrow file cannot be read
* @throws IllegalArgumentException if the escrow file cannot be imported
*/
public void validateEscrowFileForImport(String escrowFilePath) throws IOException {
// TODO (wolfgang): Add validation method for IDN tables
try (InputStream input =
gcsUtils.openInputStream(new GcsFilename(escrowBucketName, escrowFilePath))) {
try {
RdeParser parser = new RdeParser(input);
// validate that tld exists and is in PREDELEGATION state
String tld = parser.getHeader().getTld();
try {
Registry registry = Registry.get(tld);
TldState currentState = registry.getTldState(DateTime.now());
checkArgument(
currentState == TldState.PREDELEGATION,
String.format("Tld '%s' is in state %s and cannot be imported", tld, currentState));
} catch (RegistryNotFoundException e) {
throw new IllegalArgumentException(
String.format("Tld '%s' not found in the registry", tld));
}
// validate that all registrars exist
while (parser.nextRegistrar()) {
XjcRdeRegistrar registrar = parser.getRegistrar();
if (Registrar.loadByClientId(registrar.getId()) == null) {
throw new IllegalArgumentException(
String.format("Registrar '%s' not found in the registry", registrar.getId()));
}
}
} catch (XMLStreamException e) {
throw new IllegalArgumentException(
String.format("Invalid XML file: '%s'", escrowFilePath), e);
}
}
}
}