|
| 1 | +package io.debezium.server.iceberg; |
| 2 | + |
| 3 | +import java.net.URI; |
| 4 | +import java.util.HashMap; |
| 5 | +import java.util.Map; |
| 6 | + |
| 7 | +import org.apache.iceberg.aws.AwsClientFactories; |
| 8 | +import org.apache.iceberg.aws.AwsClientFactory; |
| 9 | + |
| 10 | +import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; |
| 11 | +import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; |
| 12 | +import software.amazon.awssdk.regions.Region; |
| 13 | +import software.amazon.awssdk.services.dynamodb.DynamoDbClient; |
| 14 | +import software.amazon.awssdk.services.glue.GlueClient; |
| 15 | +import software.amazon.awssdk.services.glue.GlueClientBuilder; |
| 16 | +import software.amazon.awssdk.services.kms.KmsClient; |
| 17 | +import software.amazon.awssdk.services.s3.S3Client; |
| 18 | + |
| 19 | +// for custom glue endpoint credentials |
| 20 | +public class OlakeAwsClientFactory implements AwsClientFactory { |
| 21 | + |
| 22 | + private transient AwsClientFactory delegate; |
| 23 | + private transient Map<String, String> props; |
| 24 | + |
| 25 | + @Override |
| 26 | + public void initialize(Map<String, String> properties) { |
| 27 | + Map<String, String> p = new HashMap<>(); |
| 28 | + if (properties != null) { |
| 29 | + for (Map.Entry<String, String> e : properties.entrySet()) { |
| 30 | + if (e.getKey() != null && e.getValue() != null) { |
| 31 | + p.put(e.getKey(), e.getValue()); |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + this.props = p; |
| 37 | + this.delegate = AwsClientFactories.defaultFactory(); |
| 38 | + this.delegate.initialize(this.props); |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public S3Client s3() { |
| 43 | + return delegate.s3(); |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public GlueClient glue() { |
| 48 | + String glueAccessKey = props.get("glue.access-key-id"); |
| 49 | + String glueSecretKey = props.get("glue.secret-access-key"); |
| 50 | + |
| 51 | + GlueClientBuilder builder = GlueClient.builder(); |
| 52 | + if (!isBlank(glueAccessKey) && !isBlank(glueSecretKey)) { |
| 53 | + builder.credentialsProvider( |
| 54 | + StaticCredentialsProvider.create( |
| 55 | + AwsBasicCredentials.create(glueAccessKey, glueSecretKey) |
| 56 | + ) |
| 57 | + ); |
| 58 | + } |
| 59 | + |
| 60 | + // prefer glue.region if set, otherwise fall back to s3.region |
| 61 | + String region = props.get("glue.region"); |
| 62 | + if (isBlank(region)) { |
| 63 | + region = props.get("s3.region"); |
| 64 | + } |
| 65 | + if (!isBlank(region)) { |
| 66 | + builder.region(Region.of(region)); |
| 67 | + } |
| 68 | + |
| 69 | + String endpoint = props.get("glue.endpoint"); |
| 70 | + if (!isBlank(endpoint)) { |
| 71 | + builder.endpointOverride(URI.create(endpoint)); |
| 72 | + } |
| 73 | + |
| 74 | + return builder.build(); |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public KmsClient kms() { |
| 79 | + return delegate.kms(); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public DynamoDbClient dynamo() { |
| 84 | + return delegate.dynamo(); |
| 85 | + } |
| 86 | + |
| 87 | + private static boolean isBlank(String s) { |
| 88 | + return s == null || s.trim().isEmpty(); |
| 89 | + } |
| 90 | +} |
0 commit comments