allow endpoint to be missing when secure endpoint is set

pull/61/merge
Ka-Hing Cheung 2015-05-11 18:28:49 -07:00 zatwierdzone przez Andrew Gaul
rodzic 6e4ebad1c8
commit f17d968a33
4 zmienionych plików z 59 dodań i 23 usunięć

Wyświetl plik

@ -57,18 +57,21 @@ public final class Main {
String endpoint = properties.getProperty(Constants.PROPERTY_ENDPOINT); String endpoint = properties.getProperty(Constants.PROPERTY_ENDPOINT);
String s3ProxyEndpointString = properties.getProperty( String s3ProxyEndpointString = properties.getProperty(
S3ProxyConstants.PROPERTY_ENDPOINT); S3ProxyConstants.PROPERTY_ENDPOINT);
String secureEndpoint = properties.getProperty(S3ProxyConstants.PROPERTY_SECURE_ENDPOINT); String secureEndpoint = properties.getProperty(
S3ProxyConstants.PROPERTY_SECURE_ENDPOINT);
String s3ProxyAuthorization = properties.getProperty( String s3ProxyAuthorization = properties.getProperty(
S3ProxyConstants.PROPERTY_AUTHORIZATION); S3ProxyConstants.PROPERTY_AUTHORIZATION);
if (provider == null || identity == null || credential == null || if (provider == null || identity == null || credential == null ||
s3ProxyEndpointString == null || (s3ProxyEndpointString == null && secureEndpoint == null) ||
s3ProxyAuthorization == null) { s3ProxyAuthorization == null) {
System.err.println("Properties file must contain:\n" + System.err.println("Properties file must contain:\n" +
Constants.PROPERTY_PROVIDER + "\n" + Constants.PROPERTY_PROVIDER + "\n" +
Constants.PROPERTY_IDENTITY + "\n" + Constants.PROPERTY_IDENTITY + "\n" +
Constants.PROPERTY_CREDENTIAL + "\n" + Constants.PROPERTY_CREDENTIAL + "\n" +
S3ProxyConstants.PROPERTY_AUTHORIZATION + "\n" +
"and one of\n" +
S3ProxyConstants.PROPERTY_ENDPOINT + "\n" + S3ProxyConstants.PROPERTY_ENDPOINT + "\n" +
S3ProxyConstants.PROPERTY_AUTHORIZATION); S3ProxyConstants.PROPERTY_SECURE_ENDPOINT);
System.exit(1); System.exit(1);
} }
@ -101,13 +104,14 @@ public final class Main {
builder = builder.endpoint(endpoint); builder = builder.endpoint(endpoint);
} }
BlobStoreContext context = builder.build(BlobStoreContext.class); BlobStoreContext context = builder.build(BlobStoreContext.class);
URI s3ProxyEndpoint = new URI(s3ProxyEndpointString);
S3Proxy s3Proxy; S3Proxy s3Proxy;
try { try {
S3Proxy.Builder s3ProxyBuilder = S3Proxy.builder() S3Proxy.Builder s3ProxyBuilder = S3Proxy.builder()
.blobStore(context.getBlobStore()) .blobStore(context.getBlobStore());
.endpoint(s3ProxyEndpoint); if (s3ProxyEndpointString != null) {
s3ProxyBuilder.endpoint(new URI(s3ProxyEndpointString));
}
if (secureEndpoint != null) { if (secureEndpoint != null) {
s3ProxyBuilder.secureEndpoint(new URI(secureEndpoint)); s3ProxyBuilder.secureEndpoint(new URI(secureEndpoint));
} }

Wyświetl plik

@ -40,6 +40,8 @@ import org.jclouds.blobstore.BlobStore;
public final class S3Proxy { public final class S3Proxy {
private final Server server; private final Server server;
private final S3ProxyHandler handler; private final S3ProxyHandler handler;
private final boolean listenHTTP;
private final boolean listenHTTPS;
static { static {
// Prevent Jetty from rewriting headers: // Prevent Jetty from rewriting headers:
@ -48,29 +50,41 @@ public final class S3Proxy {
} }
S3Proxy(Builder builder) { S3Proxy(Builder builder) {
requireNonNull(builder.endpoint); checkArgument(builder.endpoint != null ||
builder.secureEndpoint != null,
"Must provide endpoint or secure-endpoint");
// TODO: allow service paths? // TODO: allow service paths?
checkArgument(builder.endpoint.getPath().isEmpty(), if (builder.endpoint != null) {
"endpoint path must be empty, was: %s", checkArgument(builder.endpoint.getPath().isEmpty(),
builder.endpoint.getPath()); "endpoint path must be empty, was: %s",
checkArgument(Strings.isNullOrEmpty(builder.identity) ^ builder.endpoint.getPath());
!Strings.isNullOrEmpty(builder.credential), }
"Must provide both identity and credential"); if (builder.secureEndpoint != null) {
if (builder.endpoint.getScheme().equals("https:")) { checkArgument(builder.secureEndpoint.getPath().isEmpty(),
"secure-endpoint path must be empty, was: %s",
builder.secureEndpoint.getPath());
requireNonNull(builder.keyStorePath, requireNonNull(builder.keyStorePath,
"Must provide keyStorePath with HTTPS endpoint"); "Must provide keyStorePath with HTTPS endpoint");
requireNonNull(builder.keyStorePassword, requireNonNull(builder.keyStorePassword,
"Must provide keyStorePassword with HTTPS endpoint"); "Must provide keyStorePassword with HTTPS endpoint");
} }
checkArgument(Strings.isNullOrEmpty(builder.identity) ^
!Strings.isNullOrEmpty(builder.credential),
"Must provide both identity and credential");
server = new Server(); server = new Server();
HttpConnectionFactory httpConnectionFactory = HttpConnectionFactory httpConnectionFactory =
new HttpConnectionFactory(); new HttpConnectionFactory();
ServerConnector connector; ServerConnector connector;
connector = new ServerConnector(server, httpConnectionFactory); if (builder.endpoint != null) {
connector.setHost(builder.endpoint.getHost()); connector = new ServerConnector(server, httpConnectionFactory);
connector.setPort(builder.endpoint.getPort()); connector.setHost(builder.endpoint.getHost());
server.addConnector(connector); connector.setPort(builder.endpoint.getPort());
server.addConnector(connector);
listenHTTP = true;
} else {
listenHTTP = false;
}
if (builder.secureEndpoint != null) { if (builder.secureEndpoint != null) {
SslContextFactory sslContextFactory = new SslContextFactory(); SslContextFactory sslContextFactory = new SslContextFactory();
@ -81,6 +95,9 @@ public final class S3Proxy {
connector.setHost(builder.secureEndpoint.getHost()); connector.setHost(builder.secureEndpoint.getHost());
connector.setPort(builder.secureEndpoint.getPort()); connector.setPort(builder.secureEndpoint.getPort());
server.addConnector(connector); server.addConnector(connector);
listenHTTPS = true;
} else {
listenHTTPS = false;
} }
handler = new S3ProxyHandler(builder.blobStore, builder.identity, handler = new S3ProxyHandler(builder.blobStore, builder.identity,
builder.credential, Optional.fromNullable(builder.virtualHost)); builder.credential, Optional.fromNullable(builder.virtualHost));
@ -150,11 +167,25 @@ public final class S3Proxy {
} }
public int getPort() { public int getPort() {
return ((ServerConnector) server.getConnectors()[0]).getLocalPort(); if (listenHTTP) {
return ((ServerConnector) server.getConnectors()[0]).getLocalPort();
} else {
return -1;
}
} }
public int getSecurePort() { public int getSecurePort() {
return ((ServerConnector) server.getConnectors()[1]).getLocalPort(); if (listenHTTPS) {
ServerConnector connector;
if (listenHTTP) {
connector = (ServerConnector) server.getConnectors()[1];
} else {
connector = (ServerConnector) server.getConnectors()[0];
}
return connector.getLocalPort();
}
return -1;
} }
public String getState() { public String getState() {

Wyświetl plik

@ -153,8 +153,9 @@ public final class S3AwsSdkTest {
// reset endpoint to handle zero port // reset endpoint to handle zero port
s3Endpoint = new URI("https", s3Endpoint.getUserInfo(), s3Endpoint = new URI("https", s3Endpoint.getUserInfo(),
s3Endpoint.getHost(), s3Proxy.getSecurePort(), s3Endpoint.getPath(), s3Endpoint.getHost(), s3Proxy.getSecurePort(),
s3Endpoint.getQuery(), s3Endpoint.getFragment()); s3Endpoint.getPath(), s3Endpoint.getQuery(),
s3Endpoint.getFragment());
} }
@After @After

Wyświetl plik

@ -14,7 +14,7 @@ popd
# launch S3Proxy using HTTP and a fixed port # launch S3Proxy using HTTP and a fixed port
sed "s,^\(s3proxy.endpoint\)=.*,\1=http://127.0.0.1:${S3PROXY_PORT}," \ sed "s,^\(s3proxy.endpoint\)=.*,\1=http://127.0.0.1:${S3PROXY_PORT}," \
< src/test/resources/s3proxy.conf > target/s3proxy.conf < src/test/resources/s3proxy.conf | grep -v secure-endpoint > target/s3proxy.conf
$S3PROXY_BIN --properties target/s3proxy.conf & $S3PROXY_BIN --properties target/s3proxy.conf &
S3PROXY_PID=$! S3PROXY_PID=$!