google-nomulus/java/google/registry/tools/VerifyOteCommand.java
cgoldfeder c9d7e75946 Cache Registrars in memory
This replaces the memcache caching, which we think is overall a bad idea.
We load all registrars at once instead of caching each as needed, so that
the loadAllCached() methods can be cached as well, and therefore will
always produce results consistent with loadByClientIdCached()'s view of the
registrar's values. All of our prod registrars together total 300k of data
right now, so this is hardly worth optimizing further, and in any case this
will likely reduce latency even further since most requests will be
served out of memory.

While I was in the Registrar file I standardized the error messages for incorrect
password and clientId length to be the same format, and cleaned up a few
random things I noticed in the code.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=156151828
2017-05-17 12:26:11 -04:00

124 lines
5 KiB
Java

// 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.tools;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Predicates.notNull;
import static com.google.common.base.Verify.verifyNotNull;
import static google.registry.model.registrar.Registrar.loadByClientId;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import com.google.common.base.Function;
import com.google.common.base.Strings;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import google.registry.config.RegistryEnvironment;
import google.registry.model.registrar.Registrar;
import google.registry.tools.server.VerifyOteAction;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
/** Command to verify that a registrar has passed OT&E. */
@Parameters(
separators = " =",
commandDescription = "Verify passage of OT&E for specified (or all) registrars")
final class VerifyOteCommand implements ServerSideCommand {
@Parameter(
description = "List of registrar names to check; must be the same names as the ones used "
+ "when creating the OT&E accounts")
private List<String> mainParameters = new ArrayList<>();
@Parameter(
names = "--check-all",
description = "Check the OT&E pass status of all active registrars")
private boolean checkAll;
@Parameter(
names = "--summarize",
description = "Only show a summary of information")
private boolean summarize;
private Connection connection;
@Override
public void setConnection(Connection connection) {
this.connection = connection;
}
@Override
public void run() throws IOException {
if (RegistryEnvironment.get() != RegistryEnvironment.SANDBOX) {
System.err.printf(
"WARNING: Running against %s environment. Are "
+ "you sure you didn\'t mean to run this against sandbox (e.g. \"-e SANDBOX\")?%n",
RegistryEnvironment.get());
}
checkArgument(
mainParameters.isEmpty() == checkAll,
"Must provide at least one registrar name, or supply --check-all with no names.");
for (String clientId : mainParameters) {
// OT&E registrars are created with clientIDs of registrarName-[1-4], but this command is
// passed registrarName. So check the existence of the first persisted registrar to see if
// the input is valid.
verifyNotNull(loadByClientId(clientId + "-1"), "Registrar %s does not exist.", clientId);
}
Collection<String> registrars =
mainParameters.isEmpty() ? getAllRegistrarNames() : mainParameters;
Map<String, Object> response = connection.sendJson(
VerifyOteAction.PATH,
ImmutableMap.of(
"summarize", Boolean.toString(summarize),
"registrars", new ArrayList<>(registrars)));
System.out.println(Strings.repeat("-", 80));
for (Entry<String, Object> registrar : response.entrySet()) {
System.out.printf(
summarize ? "%-20s - %s\n" : "\n=========== %s OT&E status ============\n%s\n",
registrar.getKey(),
registrar.getValue());
}
System.out.println(Strings.repeat("-", 80));
}
/**
* Returns the names of all active registrars. Finds registrar accounts with clientIds matching
* the format used for OT&E accounts (regname-1, regname-2, etc.) and returns just the common
* prefixes of those accounts (in this case, regname).
*/
private ImmutableSet<String> getAllRegistrarNames() {
return FluentIterable.from(Registrar.loadAll())
.transform(new Function<Registrar, String>() {
@Override
public String apply(Registrar registrar) {
if (!registrar.isActive()) {
return null;
}
String name = registrar.getClientId();
// Look for names of the form "regname-1", "regname-2", etc. and strip the -# suffix.
String replacedName = name.replaceFirst("^(.*)-[1234]$", "$1");
// Check if any replacement happened, and thus whether the name matches the format.
// If it matches, provide the shortened name, and otherwise return null.
return name.equals(replacedName) ? null : replacedName;
}})
.filter(notNull())
.toSet();
}
}