-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #10
- Loading branch information
Showing
11 changed files
with
278 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
trino-s3-proxy/src/main/java/io/trino/s3/proxy/server/BytesResponseHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* 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 io.trino.s3.proxy.server; | ||
|
||
import io.airlift.http.client.HttpStatus; | ||
import io.airlift.http.client.Request; | ||
import io.airlift.http.client.Response; | ||
import io.airlift.http.client.ResponseHandler; | ||
import jakarta.ws.rs.WebApplicationException; | ||
|
||
import static io.airlift.http.client.ResponseHandlerUtils.propagate; | ||
import static io.airlift.http.client.ResponseHandlerUtils.readResponseBytes; | ||
import static java.util.Objects.requireNonNull; | ||
|
||
public class BytesResponseHandler | ||
implements ResponseHandler<BytesResponseHandler.BytesResponse, RuntimeException> | ||
{ | ||
@Override | ||
public BytesResponse handleException(Request request, Exception exception) | ||
throws RuntimeException | ||
{ | ||
throw propagate(request, exception); | ||
} | ||
|
||
public record BytesResponse(Response response, byte[] entity) | ||
{ | ||
public BytesResponse | ||
{ | ||
requireNonNull(response, "response is null"); | ||
requireNonNull(entity, "entity is null"); | ||
} | ||
} | ||
|
||
@Override | ||
public BytesResponse handle(Request request, Response response) | ||
throws RuntimeException | ||
{ | ||
if (HttpStatus.familyForStatusCode(response.getStatusCode()) != HttpStatus.Family.SUCCESSFUL) { | ||
// TODO | ||
throw new WebApplicationException(response.getStatusCode()); | ||
} | ||
|
||
byte[] bytes = readResponseBytes(request, response); | ||
|
||
return new BytesResponse(response, bytes); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
trino-s3-proxy/src/main/java/io/trino/s3/proxy/server/ProxyClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/* | ||
* 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 io.trino.s3.proxy.server; | ||
|
||
import com.google.inject.BindingAnnotation; | ||
import com.google.inject.Inject; | ||
import io.airlift.http.client.HeaderName; | ||
import io.airlift.http.client.HttpClient; | ||
import io.airlift.http.client.Request; | ||
import io.trino.s3.proxy.server.BytesResponseHandler.BytesResponse; | ||
import io.trino.s3.proxy.server.Credentials.CredentialsEntry; | ||
import io.trino.s3.proxy.server.SigningController.Scope; | ||
import io.trino.s3.proxy.server.rest.TrinoS3ProxyRestConstants; | ||
import jakarta.ws.rs.WebApplicationException; | ||
import jakarta.ws.rs.core.MultivaluedHashMap; | ||
import jakarta.ws.rs.core.MultivaluedMap; | ||
import jakarta.ws.rs.core.Response; | ||
import org.glassfish.jersey.server.ContainerRequest; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
import java.net.URI; | ||
import java.util.Map; | ||
|
||
import static java.lang.annotation.ElementType.FIELD; | ||
import static java.lang.annotation.ElementType.METHOD; | ||
import static java.lang.annotation.ElementType.PARAMETER; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
import static java.util.Objects.requireNonNull; | ||
|
||
public class ProxyClient | ||
{ | ||
private final HttpClient httpClient; | ||
private final SigningController signingController; | ||
private final CredentialsController credentialsController; | ||
|
||
@Retention(RUNTIME) | ||
@Target({FIELD, PARAMETER, METHOD}) | ||
@BindingAnnotation | ||
public @interface ForProxyClient {} | ||
|
||
@Inject | ||
public ProxyClient(@ForProxyClient HttpClient httpClient, SigningController signingController, CredentialsController credentialsController) | ||
{ | ||
this.httpClient = requireNonNull(httpClient, "httpClient is null"); | ||
this.signingController = requireNonNull(signingController, "signingController is null"); | ||
this.credentialsController = requireNonNull(credentialsController, "credentialsController is null"); | ||
} | ||
|
||
public Response proxyRequest(ContainerRequest request, String bucket) | ||
{ | ||
String fullPath = "/" + request.getPath(false); | ||
if (!fullPath.startsWith(TrinoS3ProxyRestConstants.S3_PATH)) { | ||
throw new WebApplicationException(Response.Status.BAD_REQUEST); | ||
} | ||
fullPath = fullPath.substring(TrinoS3ProxyRestConstants.S3_PATH.length()); | ||
if (fullPath.startsWith("/" + bucket)) { | ||
fullPath = fullPath.substring(("/" + bucket).length()); | ||
} | ||
|
||
Scope scope = Scope.fromHeaders(request.getHeaders()).orElseThrow(() -> new WebApplicationException(Response.Status.BAD_REQUEST)); | ||
|
||
String host = buildHost(bucket, scope.region()); | ||
URI uri = request.getUriInfo().getRequestUriBuilder() | ||
.host(host) | ||
.port(-1) | ||
.scheme("https") | ||
.replacePath(fullPath) | ||
.build(); | ||
|
||
Request.Builder requestBuilder = new Request.Builder() | ||
.setMethod(request.getMethod()) | ||
.setUri(uri) | ||
.setFollowRedirects(true); | ||
|
||
// TODO | ||
CredentialsEntry credentials = credentialsController.credentials(scope.accessKey()).orElseThrow().real(); | ||
|
||
MultivaluedMap<String, String> requestHeaders = new MultivaluedHashMap<>(request.getRequestHeaders()); | ||
requestHeaders.remove("X-Amz-Security-Token"); | ||
requestHeaders.putSingle("Host", host); | ||
|
||
Map<String, String> signedRequestHeaders = signingController.signedRequestHeaders(scope, credentials, request.getMethod(), requestHeaders, uri.getRawPath(), uri.getRawQuery()); | ||
signedRequestHeaders.forEach(requestBuilder::addHeader); | ||
|
||
requestHeaders.forEach((name, values) -> { | ||
if (!signedRequestHeaders.containsKey(name)) { | ||
values.forEach(value -> requestBuilder.addHeader(name, value)); | ||
} | ||
}); | ||
|
||
BytesResponse bytesResponse = httpClient.execute(requestBuilder.build(), new BytesResponseHandler()); | ||
Response.ResponseBuilder responseBuilder = Response.status(bytesResponse.response().getStatusCode()).entity(bytesResponse.entity()); | ||
bytesResponse.response().getHeaders() | ||
.keySet() | ||
.stream() | ||
.map(HeaderName::toString) | ||
.forEach(name -> bytesResponse.response().getHeaders(name).forEach(value -> responseBuilder.header(name, value))); | ||
return responseBuilder.build(); | ||
} | ||
|
||
private String buildHost(String bucket, String region) | ||
{ | ||
return "%s.s3.%s.amazonaws.com".formatted(bucket, region); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.