-
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.
Implement support for request signing
- Use AWS SDK signing code - Added an initial test for a simple `aws s3 ls` based on this doc: https://min.io/docs/minio/linux/integrations/aws-cli-with-minio.html Closes #5
- Loading branch information
Showing
8 changed files
with
337 additions
and
10 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
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
34 changes: 34 additions & 0 deletions
34
trino-s3-proxy/src/main/java/io/trino/s3/proxy/server/credentials/Credentials.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,34 @@ | ||
/* | ||
* 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.credentials; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
public record Credentials(Credential emulated, Credential real) | ||
{ | ||
public Credentials | ||
{ | ||
requireNonNull(emulated, "emulated is null"); | ||
requireNonNull(real, "real is null"); | ||
} | ||
|
||
public record Credential(String accessKey, String secretKey) | ||
{ | ||
public Credential | ||
{ | ||
requireNonNull(accessKey, "accessKey is null"); | ||
requireNonNull(secretKey, "secretKey is null"); | ||
} | ||
} | ||
} |
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
91 changes: 91 additions & 0 deletions
91
trino-s3-proxy/src/main/java/io/trino/s3/proxy/server/credentials/Signer.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,91 @@ | ||
package io.trino.s3.proxy.server.credentials; | ||
|
||
import com.google.common.collect.ImmutableSet; | ||
import com.google.common.net.HostAndPort; | ||
import jakarta.ws.rs.core.MultivaluedHashMap; | ||
import jakarta.ws.rs.core.MultivaluedMap; | ||
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; | ||
import software.amazon.awssdk.auth.signer.Aws4Signer; | ||
import software.amazon.awssdk.auth.signer.params.Aws4SignerParams; | ||
import software.amazon.awssdk.http.SdkHttpFullRequest; | ||
import software.amazon.awssdk.http.SdkHttpMethod; | ||
import software.amazon.awssdk.regions.Region; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.net.URI; | ||
import java.time.Clock; | ||
import java.time.ZoneId; | ||
import java.time.ZonedDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
import java.util.Locale; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
|
||
final class Signer | ||
{ | ||
private static final DateTimeFormatter AMZ_DATE_FORMAT = DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmss'Z'", Locale.US).withZone(ZoneId.of("Z")); | ||
|
||
private static final Set<String> IGNORED_HEADERS = ImmutableSet.of( | ||
"accept-encoding", | ||
"authorization", | ||
"user-agent", | ||
"connection", | ||
"content-length"); | ||
|
||
private static final Aws4Signer aws4Signer = Aws4Signer.create(); | ||
|
||
private Signer() {} | ||
|
||
@SuppressWarnings("SameParameterValue") | ||
static String sign( | ||
String serviceName, | ||
URI requestURI, | ||
MultivaluedMap<String, String> requestHeaders, | ||
MultivaluedMap<String, String> queryParameters, | ||
String httpMethod, | ||
String encodedPath, | ||
String region, | ||
String accessKey, | ||
String secretKey, | ||
Optional<byte[]> entity) | ||
{ | ||
requestHeaders = lowercase(requestHeaders); | ||
queryParameters = lowercase(queryParameters); | ||
|
||
SdkHttpFullRequest.Builder requestBuilder = SdkHttpFullRequest.builder() | ||
.port(requestURI.getPort()) | ||
.protocol(requestURI.getScheme()) | ||
.host(HostAndPort.fromString(requestHeaders.getFirst("host")).getHost()) | ||
.method(SdkHttpMethod.fromValue(httpMethod)) | ||
.encodedPath(encodedPath); | ||
|
||
entity.ifPresent(entityBytes -> requestBuilder.contentStreamProvider(() -> new ByteArrayInputStream(entityBytes))); | ||
|
||
requestHeaders | ||
.entrySet() | ||
.stream() | ||
.filter(entry -> !IGNORED_HEADERS.contains(entry.getKey())) | ||
.forEach(entry -> entry.getValue().forEach(value -> requestBuilder.appendHeader(entry.getKey(), value))); | ||
|
||
queryParameters.forEach(requestBuilder::putRawQueryParameter); | ||
|
||
String xAmzDate = Optional.ofNullable(requestHeaders.getFirst("x-amz-date")).orElseThrow(); | ||
ZonedDateTime date = ZonedDateTime.parse(xAmzDate, AMZ_DATE_FORMAT); | ||
|
||
Aws4SignerParams signerParams = Aws4SignerParams.builder() | ||
.signingName(serviceName) | ||
.signingRegion(Region.of(region)) | ||
.awsCredentials(AwsBasicCredentials.create(accessKey, secretKey)) | ||
.signingClockOverride(Clock.fixed(date.toInstant(), date.getZone())) | ||
.build(); | ||
|
||
return aws4Signer.sign(requestBuilder.build(), signerParams).firstMatchingHeader("Authorization").orElseThrow(); | ||
} | ||
|
||
private static MultivaluedMap<String, String> lowercase(MultivaluedMap<String, String> map) | ||
{ | ||
MultivaluedMap<String, String> result = new MultivaluedHashMap<>(); | ||
map.forEach((name, values) -> result.put(name.toLowerCase(Locale.ROOT), values)); | ||
return result; | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
trino-s3-proxy/src/main/java/io/trino/s3/proxy/server/credentials/SigningController.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.credentials; | ||
|
||
import com.google.inject.Inject; | ||
import jakarta.ws.rs.core.MultivaluedMap; | ||
|
||
import java.net.URI; | ||
import java.util.Optional; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
public class SigningController | ||
{ | ||
private final CredentialsController credentialsController; | ||
|
||
@Inject | ||
public SigningController(CredentialsController credentialsController) | ||
{ | ||
this.credentialsController = requireNonNull(credentialsController, "credentialsController is null"); | ||
} | ||
|
||
public String signRequest( | ||
URI requestURI, | ||
MultivaluedMap<String, String> requestHeaders, | ||
MultivaluedMap<String, String> queryParameters, | ||
String httpMethod, | ||
String encodedPath, | ||
String region, | ||
String accessKey) | ||
{ | ||
// TODO | ||
Credentials credentials = credentialsController.credentials(accessKey).orElseThrow(); | ||
|
||
return Signer.sign( | ||
"s3", | ||
requestURI, | ||
requestHeaders, | ||
queryParameters, | ||
httpMethod, | ||
encodedPath, | ||
region, | ||
accessKey, | ||
credentials.emulated().secretKey(), | ||
Optional.empty()); | ||
} | ||
} |
Oops, something went wrong.