Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finalize new HTTP framework changes #2118

Merged
merged 2 commits into from
Nov 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
* This failover behavior is similar to how the Docker client works:
* https://docs.docker.com/registry/insecure/#deploy-a-plain-http-registry
*/
public class Connection { // TODO: rename to TlsFailoverHttpClient
public class FailoverHttpClient {

private static boolean isHttpsProtocol(URL url) {
return "https".equals(url.getProtocol());
Expand Down Expand Up @@ -141,20 +141,20 @@ private static void addProxyCredentials(ApacheHttpTransport transport, String pr
private final Supplier<HttpTransport> secureHttpTransportFactory;
private final Supplier<HttpTransport> insecureHttpTransportFactory;

public Connection(
public FailoverHttpClient(
boolean enableHttpAndInsecureFailover,
boolean sendAuthorizationOverHttp,
Consumer<LogEvent> logger) {
this(
enableHttpAndInsecureFailover,
sendAuthorizationOverHttp,
logger,
Connection::getSecureHttpTransport,
Connection::getInsecureHttpTransport);
FailoverHttpClient::getSecureHttpTransport,
FailoverHttpClient::getInsecureHttpTransport);
}

@VisibleForTesting
Connection(
FailoverHttpClient(
boolean enableHttpAndInsecureFailover,
boolean sendAuthorizationOverHttp,
Consumer<LogEvent> logger,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.google.api.client.http.HttpStatusCodes;
import com.google.cloud.tools.jib.api.RegistryAuthenticationFailedException;
import com.google.cloud.tools.jib.http.BlobHttpContent;
import com.google.cloud.tools.jib.http.Connection;
import com.google.cloud.tools.jib.http.FailoverHttpClient;
import com.google.cloud.tools.jib.http.Response;
import com.google.cloud.tools.jib.http.ResponseException;
import java.net.MalformedURLException;
Expand All @@ -36,12 +36,12 @@ class AuthenticationMethodRetriever

private final RegistryEndpointRequestProperties registryEndpointRequestProperties;
private final String userAgent;
private final Connection httpClient;
private final FailoverHttpClient httpClient;

AuthenticationMethodRetriever(
RegistryEndpointRequestProperties registryEndpointRequestProperties,
String userAgent,
Connection httpClient) {
FailoverHttpClient httpClient) {
this.registryEndpointRequestProperties = registryEndpointRequestProperties;
this.userAgent = userAgent;
this.httpClient = httpClient;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.google.cloud.tools.jib.http.BlobHttpContent;
import com.google.cloud.tools.jib.http.NotifyingOutputStream;
import com.google.cloud.tools.jib.http.Response;
import com.google.cloud.tools.jib.http.ResponseException;
import java.io.IOException;
import java.io.OutputStream;
import java.net.MalformedURLException;
Expand Down Expand Up @@ -114,4 +115,10 @@ public String getActionDescription() {
+ " with digest "
+ blobDigest;
}

@Override
public Void handleHttpResponseException(ResponseException responseException)
throws ResponseException, RegistryErrorException {
throw responseException;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.google.cloud.tools.jib.blob.Blob;
import com.google.cloud.tools.jib.http.BlobHttpContent;
import com.google.cloud.tools.jib.http.Response;
import com.google.cloud.tools.jib.http.ResponseException;
import com.google.common.net.MediaType;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
Expand Down Expand Up @@ -107,6 +108,12 @@ public String getHttpMethod() {
public String getActionDescription() {
return BlobPusher.this.getActionDescription();
}

@Override
public Optional<URL> handleHttpResponseException(ResponseException responseException)
throws ResponseException, RegistryErrorException {
throw responseException;
}
}

/** Writes the BLOB content to the upload location. */
Expand Down Expand Up @@ -152,6 +159,12 @@ private Writer(URL location, Consumer<Long> writtenByteCountListener) {
this.location = location;
this.writtenByteCountListener = writtenByteCountListener;
}

@Override
public URL handleHttpResponseException(ResponseException responseException)
throws ResponseException, RegistryErrorException {
throw responseException;
}
}

/** Commits the written BLOB. */
Expand Down Expand Up @@ -194,6 +207,12 @@ public String getActionDescription() {
private Committer(URL location) {
this.location = location;
}

@Override
public Void handleHttpResponseException(ResponseException responseException)
throws ResponseException, RegistryErrorException {
throw responseException;
}
}

BlobPusher(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.google.cloud.tools.jib.hash.Digests;
import com.google.cloud.tools.jib.http.BlobHttpContent;
import com.google.cloud.tools.jib.http.Response;
import com.google.cloud.tools.jib.http.ResponseException;
import com.google.cloud.tools.jib.image.json.ManifestTemplate;
import com.google.cloud.tools.jib.image.json.OCIManifestTemplate;
import com.google.cloud.tools.jib.image.json.UnknownManifestFormatException;
Expand Down Expand Up @@ -160,4 +161,10 @@ private T getManifestTemplateFromJson(String jsonString)
throw new UnknownManifestFormatException(
"Unknown schemaVersion: " + schemaVersion + " - only 1 and 2 are supported");
}

@Override
public ManifestAndDigest<T> handleHttpResponseException(ResponseException responseException)
throws ResponseException, RegistryErrorException {
throw responseException;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import com.google.cloud.tools.jib.global.JibSystemProperties;
import com.google.cloud.tools.jib.http.Authorization;
import com.google.cloud.tools.jib.http.BlobHttpContent;
import com.google.cloud.tools.jib.http.Connection;
import com.google.cloud.tools.jib.http.FailoverHttpClient;
import com.google.cloud.tools.jib.http.Request;
import com.google.cloud.tools.jib.http.Response;
import com.google.cloud.tools.jib.http.ResponseException;
Expand Down Expand Up @@ -70,7 +70,7 @@ static Optional<RegistryAuthenticator> fromAuthenticationMethod(
String authenticationMethod,
RegistryEndpointRequestProperties registryEndpointRequestProperties,
String userAgent,
Connection httpClient)
FailoverHttpClient httpClient)
throws RegistryAuthenticationFailedException {
// If the authentication method starts with 'basic ' (case insensitive), no registry
// authentication is needed.
Expand Down Expand Up @@ -143,14 +143,14 @@ private String getToken() {
private final String realm;
private final String service;
private final String userAgent;
private final Connection httpClient;
private final FailoverHttpClient httpClient;

private RegistryAuthenticator(
String realm,
String service,
RegistryEndpointRequestProperties registryEndpointRequestProperties,
String userAgent,
Connection httpClient) {
FailoverHttpClient httpClient) {
this.realm = realm;
this.service = service;
this.registryEndpointRequestProperties = registryEndpointRequestProperties;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import com.google.cloud.tools.jib.event.EventHandlers;
import com.google.cloud.tools.jib.global.JibSystemProperties;
import com.google.cloud.tools.jib.http.Authorization;
import com.google.cloud.tools.jib.http.Connection;
import com.google.cloud.tools.jib.http.FailoverHttpClient;
import com.google.cloud.tools.jib.http.Response;
import com.google.cloud.tools.jib.image.json.BuildableManifestTemplate;
import com.google.cloud.tools.jib.image.json.ManifestTemplate;
Expand Down Expand Up @@ -244,7 +244,7 @@ static Multimap<String, String> decodeTokenRepositoryGrants(String token) {
@Nullable private final Authorization authorization;
private final RegistryEndpointRequestProperties registryEndpointRequestProperties;
private final String userAgent;
private final Connection httpClient;
private final FailoverHttpClient httpClient;

/**
* Instantiate with {@link #factory}.
Expand All @@ -266,7 +266,7 @@ private RegistryClient(
this.registryEndpointRequestProperties = registryEndpointRequestProperties;
this.userAgent = userAgent;
this.httpClient =
new Connection(
new FailoverHttpClient(
allowInsecureRegistries,
JibSystemProperties.sendCredentialsOverHttp(),
eventHandlers::dispatch);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import com.google.cloud.tools.jib.event.EventHandlers;
import com.google.cloud.tools.jib.global.JibSystemProperties;
import com.google.cloud.tools.jib.http.Authorization;
import com.google.cloud.tools.jib.http.Connection;
import com.google.cloud.tools.jib.http.FailoverHttpClient;
import com.google.cloud.tools.jib.http.Request;
import com.google.cloud.tools.jib.http.Response;
import com.google.cloud.tools.jib.http.ResponseException;
Expand Down Expand Up @@ -74,7 +74,7 @@ static boolean isBrokenPipe(IOException original) {
private final RegistryEndpointProvider<T> registryEndpointProvider;
@Nullable private final Authorization authorization;
private final RegistryEndpointRequestProperties registryEndpointRequestProperties;
private final Connection httpClient;
private final FailoverHttpClient httpClient;

/**
* Constructs with parameters for making the request.
Expand All @@ -93,7 +93,7 @@ static boolean isBrokenPipe(IOException original) {
RegistryEndpointProvider<T> registryEndpointProvider,
@Nullable Authorization authorization,
RegistryEndpointRequestProperties registryEndpointRequestProperties,
Connection httpClient) {
FailoverHttpClient httpClient) {
this.eventHandlers = eventHandlers;
this.userAgent = userAgent;
this.registryEndpointProvider = registryEndpointProvider;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,16 @@ interface RegistryEndpointProvider<T> {
T handleResponse(Response response) throws IOException, RegistryException;

/**
* Handles an {@link ResponseException} that occurs.
* Handles an {@link ResponseException} that occurs. Implementation must re-throw the given
* exception if it did not conclusively handled the response exception.
*
* @param responseException the {@link ResponseException} to handle
* @throws HttpResponseException {@code responseException} if {@code responseException} could not
* be handled
* @throws RegistryErrorException if there is an error with a remote registry
*/
default T handleHttpResponseException(ResponseException responseException)
throws ResponseException, RegistryErrorException {
throw responseException;
}
T handleHttpResponseException(ResponseException responseException)
throws ResponseException, RegistryErrorException;

/**
* @return a description of the registry action performed, used in error messages to describe the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@
import org.junit.Test;
import org.junit.contrib.java.lang.system.RestoreSystemProperties;

/** Tests for {@link Connection} with setting proxy credentials. */
// TODO: rename to TlsFailoverHttpClientProxyCredentialsTest
public class ConnectionWithProxyCredentialsTest {
/** Tests for {@link FailoverHttpClient} with setting proxy credentials. */
public class FailoverHttpClientProxyCredentialsTest {

private static final ImmutableList<String> proxyProperties =
ImmutableList.of(
Expand All @@ -53,7 +52,7 @@ public void setUp() {

@Test
public void testAddProxyCredentials_undefined() {
Connection.addProxyCredentials(transport);
FailoverHttpClient.addProxyCredentials(transport);
DefaultHttpClient httpClient = (DefaultHttpClient) transport.getHttpClient();
Credentials credentials = httpClient.getCredentialsProvider().getCredentials(AuthScope.ANY);
Assert.assertNull(credentials);
Expand All @@ -71,7 +70,7 @@ public void testAddProxyCredentials() {
System.setProperty("https.proxyUser", "s-user");
System.setProperty("https.proxyPassword", "s-pass");

Connection.addProxyCredentials(transport);
FailoverHttpClient.addProxyCredentials(transport);
DefaultHttpClient httpClient = (DefaultHttpClient) transport.getHttpClient();
Credentials httpCredentials =
httpClient.getCredentialsProvider().getCredentials(new AuthScope("http://localhost", 1080));
Expand All @@ -94,7 +93,7 @@ public void testAddProxyCredentials_defaultPorts() {
System.setProperty("https.proxyUser", "s-user");
System.setProperty("https.proxyPassword", "s-pass");

Connection.addProxyCredentials(transport);
FailoverHttpClient.addProxyCredentials(transport);
DefaultHttpClient httpClient = (DefaultHttpClient) transport.getHttpClient();
Credentials httpCredentials =
httpClient.getCredentialsProvider().getCredentials(new AuthScope("http://localhost", 80));
Expand All @@ -115,7 +114,7 @@ public void testAddProxyCredentials_hostUndefined() {
System.setProperty("https.proxyUser", "s-user");
System.setProperty("https.proxyPassword", "s-pass");

Connection.addProxyCredentials(transport);
FailoverHttpClient.addProxyCredentials(transport);
DefaultHttpClient httpClient = (DefaultHttpClient) transport.getHttpClient();
Credentials credentials = httpClient.getCredentialsProvider().getCredentials(AuthScope.ANY);
Assert.assertNull(credentials);
Expand All @@ -129,7 +128,7 @@ public void testAddProxyCredentials_userUndefined() {
System.setProperty("https.proxyHost", "https://host.com");
System.setProperty("https.proxyPassword", "s-pass");

Connection.addProxyCredentials(transport);
FailoverHttpClient.addProxyCredentials(transport);
DefaultHttpClient httpClient = (DefaultHttpClient) transport.getHttpClient();
Credentials credentials = httpClient.getCredentialsProvider().getCredentials(AuthScope.ANY);
Assert.assertNull(credentials);
Expand All @@ -143,7 +142,7 @@ public void testAddProxyCredentials_passwordUndefined() {
System.setProperty("https.proxyHost", "https://host.com");
System.setProperty("https.proxyUser", "s-user");

Connection.addProxyCredentials(transport);
FailoverHttpClient.addProxyCredentials(transport);
DefaultHttpClient httpClient = (DefaultHttpClient) transport.getHttpClient();
Credentials credentials = httpClient.getCredentialsProvider().getCredentials(AuthScope.ANY);
Assert.assertNull(credentials);
Expand Down
Loading