|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.iceberg.aws.s3; |
| 20 | + |
| 21 | +import java.time.Instant; |
| 22 | +import java.time.temporal.ChronoUnit; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.stream.Collectors; |
| 26 | +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; |
| 27 | +import org.apache.iceberg.rest.ErrorHandlers; |
| 28 | +import org.apache.iceberg.rest.HTTPClient; |
| 29 | +import org.apache.iceberg.rest.RESTClient; |
| 30 | +import org.apache.iceberg.rest.auth.OAuth2Properties; |
| 31 | +import org.apache.iceberg.rest.auth.OAuth2Util; |
| 32 | +import org.apache.iceberg.rest.credentials.Credential; |
| 33 | +import org.apache.iceberg.rest.responses.LoadCredentialsResponse; |
| 34 | +import software.amazon.awssdk.auth.credentials.AwsCredentials; |
| 35 | +import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider; |
| 36 | +import software.amazon.awssdk.auth.credentials.AwsSessionCredentials; |
| 37 | +import software.amazon.awssdk.utils.IoUtils; |
| 38 | +import software.amazon.awssdk.utils.SdkAutoCloseable; |
| 39 | +import software.amazon.awssdk.utils.cache.CachedSupplier; |
| 40 | +import software.amazon.awssdk.utils.cache.RefreshResult; |
| 41 | + |
| 42 | +public class VendedCredentialsProvider implements AwsCredentialsProvider, SdkAutoCloseable { |
| 43 | + public static final String URI = "credentials.uri"; |
| 44 | + private volatile HTTPClient client; |
| 45 | + private final Map<String, String> properties; |
| 46 | + private final CachedSupplier<AwsCredentials> credentialCache; |
| 47 | + |
| 48 | + private VendedCredentialsProvider(Map<String, String> properties) { |
| 49 | + Preconditions.checkArgument(null != properties, "Invalid properties: null"); |
| 50 | + Preconditions.checkArgument(null != properties.get(URI), "Invalid URI: null"); |
| 51 | + this.properties = properties; |
| 52 | + this.credentialCache = |
| 53 | + CachedSupplier.builder(this::refreshCredential) |
| 54 | + .cachedValueName(VendedCredentialsProvider.class.getName()) |
| 55 | + .build(); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public AwsCredentials resolveCredentials() { |
| 60 | + return credentialCache.get(); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public void close() { |
| 65 | + IoUtils.closeQuietly(client, null); |
| 66 | + credentialCache.close(); |
| 67 | + } |
| 68 | + |
| 69 | + public static VendedCredentialsProvider create(Map<String, String> properties) { |
| 70 | + return new VendedCredentialsProvider(properties); |
| 71 | + } |
| 72 | + |
| 73 | + private RESTClient httpClient() { |
| 74 | + if (null == client) { |
| 75 | + synchronized (this) { |
| 76 | + if (null == client) { |
| 77 | + client = HTTPClient.builder(properties).uri(properties.get(URI)).build(); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + return client; |
| 83 | + } |
| 84 | + |
| 85 | + private LoadCredentialsResponse fetchCredentials() { |
| 86 | + return httpClient() |
| 87 | + .get( |
| 88 | + properties.get(URI), |
| 89 | + null, |
| 90 | + LoadCredentialsResponse.class, |
| 91 | + OAuth2Util.authHeaders(properties.get(OAuth2Properties.TOKEN)), |
| 92 | + ErrorHandlers.defaultErrorHandler()); |
| 93 | + } |
| 94 | + |
| 95 | + private RefreshResult<AwsCredentials> refreshCredential() { |
| 96 | + LoadCredentialsResponse response = fetchCredentials(); |
| 97 | + |
| 98 | + List<Credential> s3Credentials = |
| 99 | + response.credentials().stream() |
| 100 | + .filter(c -> c.prefix().startsWith("s3")) |
| 101 | + .collect(Collectors.toList()); |
| 102 | + |
| 103 | + Preconditions.checkState(!s3Credentials.isEmpty(), "Invalid S3 Credentials: empty"); |
| 104 | + Preconditions.checkState( |
| 105 | + s3Credentials.size() == 1, "Invalid S3 Credentials: only one S3 credential should exist"); |
| 106 | + |
| 107 | + Credential s3Credential = s3Credentials.get(0); |
| 108 | + checkCredential(s3Credential, S3FileIOProperties.ACCESS_KEY_ID); |
| 109 | + checkCredential(s3Credential, S3FileIOProperties.SECRET_ACCESS_KEY); |
| 110 | + checkCredential(s3Credential, S3FileIOProperties.SESSION_TOKEN); |
| 111 | + checkCredential(s3Credential, S3FileIOProperties.SESSION_TOKEN_EXPIRES_AT_MS); |
| 112 | + |
| 113 | + String accessKeyId = s3Credential.config().get(S3FileIOProperties.ACCESS_KEY_ID); |
| 114 | + String secretAccessKey = s3Credential.config().get(S3FileIOProperties.SECRET_ACCESS_KEY); |
| 115 | + String sessionToken = s3Credential.config().get(S3FileIOProperties.SESSION_TOKEN); |
| 116 | + String tokenExpiresAtMillis = |
| 117 | + s3Credential.config().get(S3FileIOProperties.SESSION_TOKEN_EXPIRES_AT_MS); |
| 118 | + Instant expiresAt = Instant.ofEpochMilli(Long.parseLong(tokenExpiresAtMillis)); |
| 119 | + Instant prefetchAt = expiresAt.minus(5, ChronoUnit.MINUTES); |
| 120 | + |
| 121 | + return RefreshResult.builder( |
| 122 | + (AwsCredentials) |
| 123 | + AwsSessionCredentials.builder() |
| 124 | + .accessKeyId(accessKeyId) |
| 125 | + .secretAccessKey(secretAccessKey) |
| 126 | + .sessionToken(sessionToken) |
| 127 | + .expirationTime(expiresAt) |
| 128 | + .build()) |
| 129 | + .staleTime(expiresAt) |
| 130 | + .prefetchTime(prefetchAt) |
| 131 | + .build(); |
| 132 | + } |
| 133 | + |
| 134 | + private void checkCredential(Credential credential, String property) { |
| 135 | + Preconditions.checkState( |
| 136 | + credential.config().containsKey(property), "Invalid S3 Credentials: %s not set", property); |
| 137 | + } |
| 138 | +} |
0 commit comments