Convert AppEngineConnection to use HttpTransport

Make AppEngineConnection use HttpTransport through HttpRequestFactory and
create factory factories for localhost and HTTPOverRPC.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=143680257
This commit is contained in:
mmuller 2017-01-05 10:21:24 -08:00 committed by Ben McIlwain
parent 0cf62be403
commit 270734fd9b
8 changed files with 255 additions and 67 deletions

View file

@ -15,51 +15,46 @@
package google.registry.tools; package google.registry.tools;
import static com.google.common.base.Suppliers.memoize; import static com.google.common.base.Suppliers.memoize;
import static com.google.common.net.HttpHeaders.CONTENT_TYPE;
import static com.google.common.net.HttpHeaders.X_REQUESTED_WITH; import static com.google.common.net.HttpHeaders.X_REQUESTED_WITH;
import static com.google.common.net.MediaType.JSON_UTF_8; import static com.google.common.net.MediaType.JSON_UTF_8;
import static google.registry.security.JsonHttp.JSON_SAFETY_PREFIX; import static google.registry.security.JsonHttp.JSON_SAFETY_PREFIX;
import static google.registry.security.XsrfTokenManager.X_CSRF_TOKEN; import static google.registry.security.XsrfTokenManager.X_CSRF_TOKEN;
import static java.nio.charset.StandardCharsets.UTF_8; import static java.nio.charset.StandardCharsets.UTF_8;
import com.beust.jcommander.Parameter; import com.google.api.client.http.ByteArrayContent;
import com.beust.jcommander.Parameters; import com.google.api.client.http.GenericUrl;
import com.google.common.base.Function; import com.google.api.client.http.HttpHeaders;
import com.google.common.base.Joiner; 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.HttpUnsuccessfulResponseHandler;
import com.google.common.base.Supplier; import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.google.common.io.CharStreams; import com.google.common.io.CharStreams;
import com.google.common.net.HostAndPort; import com.google.common.net.HostAndPort;
import com.google.common.net.MediaType; import com.google.common.net.MediaType;
import com.google.re2j.Matcher; import com.google.re2j.Matcher;
import com.google.re2j.Pattern; import com.google.re2j.Pattern;
import google.registry.config.RegistryEnvironment;
import google.registry.security.XsrfTokenManager; import google.registry.security.XsrfTokenManager;
import google.registry.tools.ServerSideCommand.Connection; import google.registry.tools.ServerSideCommand.Connection;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import javax.inject.Inject;
import org.json.simple.JSONValue; import org.json.simple.JSONValue;
/** An http connection to the appengine server. */ /** An http connection to the appengine server. */
@Parameters(separators = " =")
class AppEngineConnection implements Connection { class AppEngineConnection implements Connection {
/** Pattern to heuristically extract title tag contents in HTML responses. */ /** Pattern to heuristically extract title tag contents in HTML responses. */
private static final Pattern HTML_TITLE_TAG_PATTERN = Pattern.compile("<title>(.*?)</title>"); private static final Pattern HTML_TITLE_TAG_PATTERN = Pattern.compile("<title>(.*?)</title>");
@Inject HttpRequestFactory requestFactory;
@Inject HostAndPort server;
@Parameter( @Inject
names = "--server", AppEngineConnection() {}
description = "HOST[:PORT] to which remote commands are sent.")
private HostAndPort server = RegistryEnvironment.get().config().getServer();
/** /**
* Memoized XSRF security token. * Memoized XSRF security token.
@ -86,55 +81,50 @@ class AppEngineConnection implements Connection {
} }
/** Returns the HTML from the connection error stream, if any, otherwise the empty string. */ /** Returns the HTML from the connection error stream, if any, otherwise the empty string. */
private static String getErrorHtmlAsString(HttpURLConnection connection) throws IOException { private static String getErrorHtmlAsString(HttpResponse response) throws IOException {
return connection.getErrorStream() != null return CharStreams.toString(new InputStreamReader(response.getContent(), UTF_8));
? CharStreams.toString(new InputStreamReader(connection.getErrorStream(), UTF_8))
: "";
} }
@Override @Override
public String send( public String send(
String endpoint, Map<String, ?> params, MediaType contentType, byte[] payload) String endpoint, Map<String, ?> params, MediaType contentType, byte[] payload)
throws IOException { throws IOException {
HttpURLConnection connection = getHttpURLConnection( GenericUrl url = new GenericUrl(String.format("%s%s", getServerUrl(), endpoint));
new URL(String.format("%s%s?%s", getServerUrl(), endpoint, encodeParams(params)))); url.putAll(params);
connection.setRequestMethod("POST"); HttpRequest request =
// Disable following redirects, which we shouldn't normally encounter. requestFactory.buildPostRequest(url, new ByteArrayContent(contentType.toString(), payload));
connection.setInstanceFollowRedirects(false); HttpHeaders headers = request.getHeaders();
connection.setUseCaches(false); headers.setCacheControl("no-cache");
connection.setRequestProperty(CONTENT_TYPE, contentType.toString()); headers.put(X_CSRF_TOKEN, ImmutableList.of(xsrfToken.get()));
connection.setRequestProperty(X_CSRF_TOKEN, xsrfToken.get()); headers.put(X_REQUESTED_WITH, ImmutableList.of("RegistryTool"));
connection.setRequestProperty(X_REQUESTED_WITH, "RegistryTool"); request.setHeaders(headers);
connection.setDoOutput(true); request.setFollowRedirects(false);
connection.connect(); request.setThrowExceptionOnExecuteError(false);
try (OutputStream output = connection.getOutputStream()) { request.setUnsuccessfulResponseHandler(
output.write(payload); new HttpUnsuccessfulResponseHandler() {
} @Override
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) { public boolean handleResponse(
String errorTitle = extractHtmlTitle(getErrorHtmlAsString(connection)); HttpRequest request, HttpResponse response, boolean supportsRetry)
throw new IOException(String.format( throws IOException {
String errorTitle = extractHtmlTitle(getErrorHtmlAsString(response));
throw new IOException(
String.format(
"Error from %s: %d %s%s", "Error from %s: %d %s%s",
connection.getURL(), request.getUrl().toString(),
connection.getResponseCode(), response.getStatusCode(),
connection.getResponseMessage(), response.getStatusMessage(),
(errorTitle == null ? "" : ": " + errorTitle))); (errorTitle == null ? "" : ": " + errorTitle)));
} }
return CharStreams.toString(new InputStreamReader(connection.getInputStream(), UTF_8)); });
} HttpResponse response = null;
private String encodeParams(Map<String, ?> params) {
return Joiner.on('&').join(Iterables.transform(
params.entrySet(),
new Function<Entry<String, ?>, String>() {
@Override
public String apply(Entry<String, ?> entry) {
try { try {
return entry.getKey() response = request.execute();
+ "=" + URLEncoder.encode(entry.getValue().toString(), UTF_8.name()); return CharStreams.toString(new InputStreamReader(response.getContent(), UTF_8));
} catch (Exception e) { // UnsupportedEncodingException } finally {
throw new RuntimeException(e); if (response != null) {
response.disconnect();
}
} }
}}));
} }
@Override @Override
@ -148,11 +138,6 @@ class AppEngineConnection implements Connection {
return (Map<String, Object>) JSONValue.parse(response.substring(JSON_SAFETY_PREFIX.length())); return (Map<String, Object>) JSONValue.parse(response.substring(JSON_SAFETY_PREFIX.length()));
} }
private HttpURLConnection getHttpURLConnection(URL remoteUrl) throws IOException {
// TODO(b/28219927): Figure out authentication.
return (HttpURLConnection) remoteUrl.openConnection();
}
@Override @Override
public String getServerUrl() { public String getServerUrl() {
return (isLocalhost() ? "http://" : "https://") + getServer().toString(); return (isLocalhost() ? "http://" : "https://") + getServer().toString();

View file

@ -0,0 +1,38 @@
// Copyright 2016 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 com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import com.google.common.net.HostAndPort;
import google.registry.config.RegistryEnvironment;
/**
* Class to contain the configuration flags for AppEngineConnection.
*
* <p>This is broken out into its own class to make it cleaner to extract these from the dagger
* module, where these values are injected.
*/
@Parameters(separators = " =")
class AppEngineConnectionFlags {
@Parameter(names = "--server", description = "HOST[:PORT] to which remote commands are sent.")
private static HostAndPort server = RegistryEnvironment.get().config().getServer();
HostAndPort getServer() {
return server;
}
}

View file

@ -0,0 +1,36 @@
// Copyright 2016 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 com.google.common.net.HostAndPort;
import dagger.Module;
import dagger.Provides;
/**
* Dagger module to provide communication flags for talking to App Engine.
*/
@Module
public final class AppEngineConnectionFlagsModule {
private final AppEngineConnectionFlags flags;
AppEngineConnectionFlagsModule(AppEngineConnectionFlags flags) {
this.flags = flags;
}
@Provides
HostAndPort provideServer() {
return flags.getServer();
}
}

View file

@ -0,0 +1,29 @@
// Copyright 2016 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 com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
/** Creates a request factory for dealing with normal HTTP requests. */
class BasicHttpRequestFactoryComponent implements HttpRequestFactoryComponent {
private final HttpTransport transport = new NetHttpTransport();
@Override
public HttpRequestFactory httpRequestFactory() {
return transport.createRequestFactory();
}
}

View file

@ -0,0 +1,28 @@
// Copyright 2016 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 com.google.api.client.http.HttpRequestFactory;
/**
* This is a Dagger component interface for providing request factories.
*
* <p>It is not annotated as a component because it's just an interface used as a dependency in
* other components. We provide our own concrete implementations of this for creating specific
* connection types.
*/
interface HttpRequestFactoryComponent {
public HttpRequestFactory httpRequestFactory();
}

View file

@ -0,0 +1,46 @@
// Copyright 2016 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 com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.javanet.NetHttpTransport;
/**
* Request factory for dealing with a "localhost" connection.
*
* <p>Localhost connections go to the App Engine dev server. The dev server differs from most HTTP
* connections in that they don't require OAuth2 credentials, but instead require a special cookie.
*
* <p>This is an immplementation of HttpRequestFactoryComponent which can be a component dependency
* in a Dagger graph used for providing a request factory.
*/
class LocalhostRequestFactoryComponent implements HttpRequestFactoryComponent {
@Override
public HttpRequestFactory httpRequestFactory() {
return new NetHttpTransport()
.createRequestFactory(
new HttpRequestInitializer() {
@Override
public void initialize(HttpRequest request) {
request
.getHeaders()
.setCookie("dev_appserver_login=test@example.com:true:1858047912411");
}
});
}
}

View file

@ -49,7 +49,9 @@ final class RegistryCli {
// Do not make this final - compile-time constant inlining may interfere with JCommander. // Do not make this final - compile-time constant inlining may interfere with JCommander.
@ParametersDelegate @ParametersDelegate
private AppEngineConnection connection = new AppEngineConnection(); private AppEngineConnectionFlags appEngineConnectionFlags =
new AppEngineConnectionFlags();
// Do not make this final - compile-time constant inlining may interfere with JCommander. // Do not make this final - compile-time constant inlining may interfere with JCommander.
@ParametersDelegate @ParametersDelegate
@ -115,13 +117,29 @@ final class RegistryCli {
return; return;
} }
loggingParams.configureLogging(); // Must be called after parameters are parsed. loggingParams.configureLogging(); // Must be called after parameters are parsed.
injectReflectively(RegistryToolComponent.class, DaggerRegistryToolComponent.create(), command);
// Decide which HTTP connection to use for App Engine
HttpRequestFactoryComponent requestFactoryComponent;
if (appEngineConnectionFlags.getServer().getHostText().equals("localhost")) {
requestFactoryComponent = new LocalhostRequestFactoryComponent();
} else {
requestFactoryComponent = new BasicHttpRequestFactoryComponent();
}
// Create the main component and use it to inject the command class.
RegistryToolComponent component = DaggerRegistryToolComponent.builder()
.httpRequestFactoryComponent(requestFactoryComponent)
.appEngineConnectionFlagsModule(
new AppEngineConnectionFlagsModule(appEngineConnectionFlags))
.build();
injectReflectively(RegistryToolComponent.class, component, command);
if (!(command instanceof RemoteApiCommand)) { if (!(command instanceof RemoteApiCommand)) {
command.run(); command.run();
return; return;
} }
AppEngineConnection connection = component.appEngineConnection();
if (command instanceof ServerSideCommand) { if (command instanceof ServerSideCommand) {
((ServerSideCommand) command).setConnection(connection); ((ServerSideCommand) command).setConnection(connection);
} }

View file

@ -26,6 +26,7 @@ import google.registry.request.Modules.Jackson2Module;
import google.registry.request.Modules.URLFetchServiceModule; import google.registry.request.Modules.URLFetchServiceModule;
import google.registry.util.SystemClock.SystemClockModule; import google.registry.util.SystemClock.SystemClockModule;
import google.registry.util.SystemSleeper.SystemSleeperModule; import google.registry.util.SystemSleeper.SystemSleeperModule;
import javax.inject.Singleton;
/** /**
* Dagger component for Registry Tool. * Dagger component for Registry Tool.
@ -33,8 +34,10 @@ import google.registry.util.SystemSleeper.SystemSleeperModule;
* <p>Any command class with {@code @Inject} fields <i>must</i> be listed as a method here. * <p>Any command class with {@code @Inject} fields <i>must</i> be listed as a method here.
* Otherwise {@link RegistryCli} will not be able to populate those fields after its instantiation. * Otherwise {@link RegistryCli} will not be able to populate those fields after its instantiation.
*/ */
@Singleton
@Component( @Component(
modules = { modules = {
AppEngineConnectionFlagsModule.class,
ConfigModule.class, ConfigModule.class,
DatastoreServiceModule.class, DatastoreServiceModule.class,
CloudDnsWriterModule.class, CloudDnsWriterModule.class,
@ -47,6 +50,9 @@ import google.registry.util.SystemSleeper.SystemSleeperModule;
SystemSleeperModule.class, SystemSleeperModule.class,
URLFetchServiceModule.class, URLFetchServiceModule.class,
VoidDnsWriterModule.class, VoidDnsWriterModule.class,
},
dependencies = {
HttpRequestFactoryComponent.class,
} }
) )
interface RegistryToolComponent { interface RegistryToolComponent {
@ -68,4 +74,6 @@ interface RegistryToolComponent {
void inject(UpdateTldCommand command); void inject(UpdateTldCommand command);
void inject(ValidateEscrowDepositCommand command); void inject(ValidateEscrowDepositCommand command);
void inject(WhoisQueryCommand command); void inject(WhoisQueryCommand command);
AppEngineConnection appEngineConnection();
} }