|
| 1 | +/* |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | +package io.trino.aws.proxy.server.remote.provider.file; |
| 15 | + |
| 16 | +import com.google.common.io.Files; |
| 17 | +import com.google.inject.Inject; |
| 18 | +import io.airlift.json.JsonCodec; |
| 19 | +import io.trino.aws.proxy.spi.credentials.Identity; |
| 20 | +import io.trino.aws.proxy.spi.remote.RemoteS3Connection; |
| 21 | +import io.trino.aws.proxy.spi.remote.RemoteS3ConnectionProvider; |
| 22 | +import io.trino.aws.proxy.spi.rest.ParsedS3Request; |
| 23 | +import io.trino.aws.proxy.spi.signing.SigningMetadata; |
| 24 | + |
| 25 | +import java.util.Map; |
| 26 | +import java.util.Optional; |
| 27 | + |
| 28 | +/** |
| 29 | + * <p>File-based RemoteS3ConnectionProvider that reads a JSON file containing a mapping from emulated access key to |
| 30 | + * RemoteS3Connection.</p> |
| 31 | + * <pre>{@code |
| 32 | + * { |
| 33 | + * "emulated-access-key-1": { |
| 34 | + * "remoteCredential": { |
| 35 | + * "accessKey": "remote-access-key", |
| 36 | + * "secretKey": "remote-secret-key" |
| 37 | + * }, |
| 38 | + * "remoteSessionRole": { |
| 39 | + * "region": "us-east-1", |
| 40 | + * "roleArn": "arn:aws:iam::123456789012:role/role-name", |
| 41 | + * "externalId": "external-id", |
| 42 | + * "stsEndpoint": "https://sts.us-east-1.amazonaws.com" |
| 43 | + * }, |
| 44 | + * "remoteS3FacadeConfiguration": { |
| 45 | + * "remoteS3.https": true, |
| 46 | + * "remoteS3.domain": "s3.amazonaws.com", |
| 47 | + * "remoteS3.port": 443, |
| 48 | + * "remoteS3.virtual-host-style": false, |
| 49 | + * "remoteS3.hostname.template": "${domain}" |
| 50 | + * } |
| 51 | + * } |
| 52 | + * } |
| 53 | + * }</pre> |
| 54 | + */ |
| 55 | +public class FileBasedRemoteS3ConnectionProvider |
| 56 | + implements RemoteS3ConnectionProvider |
| 57 | +{ |
| 58 | + private final Map<String, RemoteS3Connection> remoteS3Connections; |
| 59 | + |
| 60 | + @Inject |
| 61 | + public FileBasedRemoteS3ConnectionProvider(FileBasedRemoteS3ConnectionProviderConfig config, JsonCodec<Map<String, RemoteS3Connection>> jsonCodec) |
| 62 | + { |
| 63 | + try { |
| 64 | + this.remoteS3Connections = jsonCodec.fromJson(Files.toByteArray(config.getConnectionsFile())); |
| 65 | + } |
| 66 | + catch (Exception e) { |
| 67 | + throw new RuntimeException("Failed to read remote S3 connections file", e); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public Optional<RemoteS3Connection> remoteConnection(SigningMetadata signingMetadata, Optional<Identity> identity, ParsedS3Request request) |
| 73 | + { |
| 74 | + return Optional.ofNullable(remoteS3Connections.get(signingMetadata.credential().accessKey())); |
| 75 | + } |
| 76 | +} |
0 commit comments