|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package org.apache.doris.datasource.property.common; |
| 19 | + |
| 20 | +import org.apache.doris.datasource.property.storage.S3Properties; |
| 21 | + |
| 22 | +import org.apache.commons.lang3.StringUtils; |
| 23 | +import org.apache.iceberg.aws.AwsClientProperties; |
| 24 | +import org.apache.iceberg.aws.AwsProperties; |
| 25 | +import org.apache.iceberg.aws.s3.S3FileIOProperties; |
| 26 | +import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider; |
| 27 | + |
| 28 | +import java.util.Map; |
| 29 | + |
| 30 | +public final class IcebergAwsClientCredentialsProperties { |
| 31 | + |
| 32 | + private IcebergAwsClientCredentialsProperties() {} |
| 33 | + |
| 34 | + public static void putCredentialProviderProperties(Map<String, String> target, S3Properties s3Properties) { |
| 35 | + switch (getCredentialType(s3Properties)) { |
| 36 | + case EXPLICIT: |
| 37 | + putExplicitRestCredentials(target, s3Properties.getAccessKey(), s3Properties.getSecretKey(), |
| 38 | + s3Properties.getSessionToken()); |
| 39 | + return; |
| 40 | + case ASSUME_ROLE: |
| 41 | + IcebergAwsAssumeRoleProperties.putAssumeRoleProperties(target, s3Properties); |
| 42 | + return; |
| 43 | + case PROVIDER_CHAIN: |
| 44 | + putCredentialsProvider(target, s3Properties.getAwsCredentialsProviderMode()); |
| 45 | + return; |
| 46 | + default: |
| 47 | + throw new IllegalStateException("Unsupported Iceberg AWS credential type"); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + public static void putCredentialProviderProperties(Map<String, String> target, |
| 52 | + String accessKey, String secretKey, String sessionToken, AwsCredentialsProviderMode providerMode) { |
| 53 | + if (StringUtils.isNotBlank(accessKey) && StringUtils.isNotBlank(secretKey)) { |
| 54 | + putExplicitRestCredentials(target, accessKey, secretKey, sessionToken); |
| 55 | + return; |
| 56 | + } |
| 57 | + putCredentialsProvider(target, providerMode); |
| 58 | + } |
| 59 | + |
| 60 | + public static void putS3FileIOCredentialProperties(Map<String, String> target, |
| 61 | + S3Properties s3Properties) { |
| 62 | + putS3FileIOProperties(target, s3Properties); |
| 63 | + switch (getCredentialType(s3Properties)) { |
| 64 | + case EXPLICIT: |
| 65 | + return; |
| 66 | + case ASSUME_ROLE: |
| 67 | + IcebergAwsAssumeRoleProperties.putAssumeRoleProperties(target, s3Properties); |
| 68 | + return; |
| 69 | + case PROVIDER_CHAIN: |
| 70 | + putCredentialsProvider(target, s3Properties.getAwsCredentialsProviderMode()); |
| 71 | + return; |
| 72 | + default: |
| 73 | + throw new IllegalStateException("Unsupported Iceberg AWS credential type"); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + public static AwsCredentialsProvider createAwsCredentialsProvider(S3Properties s3Properties, |
| 78 | + boolean includeAnonymousInDefault) { |
| 79 | + switch (getCredentialType(s3Properties)) { |
| 80 | + case EXPLICIT: |
| 81 | + case ASSUME_ROLE: |
| 82 | + return s3Properties.getAwsCredentialsProvider(); |
| 83 | + case PROVIDER_CHAIN: |
| 84 | + return AwsCredentialsProviderFactory.createV2( |
| 85 | + s3Properties.getAwsCredentialsProviderMode(), includeAnonymousInDefault); |
| 86 | + default: |
| 87 | + throw new IllegalStateException("Unsupported Iceberg AWS credential type"); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + private static CredentialType getCredentialType(S3Properties s3Properties) { |
| 92 | + if (StringUtils.isNotBlank(s3Properties.getAccessKey()) |
| 93 | + && StringUtils.isNotBlank(s3Properties.getSecretKey())) { |
| 94 | + return CredentialType.EXPLICIT; |
| 95 | + } |
| 96 | + if (StringUtils.isNotBlank(s3Properties.getS3IAMRole())) { |
| 97 | + return CredentialType.ASSUME_ROLE; |
| 98 | + } |
| 99 | + return CredentialType.PROVIDER_CHAIN; |
| 100 | + } |
| 101 | + |
| 102 | + private static void putExplicitRestCredentials(Map<String, String> target, |
| 103 | + String accessKey, String secretKey, String sessionToken) { |
| 104 | + target.put(AwsProperties.REST_ACCESS_KEY_ID, accessKey); |
| 105 | + target.put(AwsProperties.REST_SECRET_ACCESS_KEY, secretKey); |
| 106 | + if (StringUtils.isNotBlank(sessionToken)) { |
| 107 | + target.put(AwsProperties.REST_SESSION_TOKEN, sessionToken); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + private static void putS3FileIOProperties(Map<String, String> target, |
| 112 | + S3Properties s3Properties) { |
| 113 | + if (StringUtils.isNotBlank(s3Properties.getEndpoint())) { |
| 114 | + target.put(S3FileIOProperties.ENDPOINT, s3Properties.getEndpoint()); |
| 115 | + } |
| 116 | + if (StringUtils.isNotBlank(s3Properties.getUsePathStyle())) { |
| 117 | + target.put(S3FileIOProperties.PATH_STYLE_ACCESS, s3Properties.getUsePathStyle()); |
| 118 | + } |
| 119 | + if (StringUtils.isNotBlank(s3Properties.getAccessKey())) { |
| 120 | + target.put(S3FileIOProperties.ACCESS_KEY_ID, s3Properties.getAccessKey()); |
| 121 | + } |
| 122 | + if (StringUtils.isNotBlank(s3Properties.getSecretKey())) { |
| 123 | + target.put(S3FileIOProperties.SECRET_ACCESS_KEY, s3Properties.getSecretKey()); |
| 124 | + } |
| 125 | + if (StringUtils.isNotBlank(s3Properties.getSessionToken())) { |
| 126 | + target.put(S3FileIOProperties.SESSION_TOKEN, s3Properties.getSessionToken()); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + private static void putCredentialsProvider(Map<String, String> target, |
| 131 | + AwsCredentialsProviderMode providerMode) { |
| 132 | + if (providerMode == null || providerMode == AwsCredentialsProviderMode.DEFAULT) { |
| 133 | + return; |
| 134 | + } |
| 135 | + target.put(AwsClientProperties.CLIENT_CREDENTIALS_PROVIDER, |
| 136 | + AwsCredentialsProviderFactory.getV2ClassName(providerMode)); |
| 137 | + } |
| 138 | + |
| 139 | + private enum CredentialType { |
| 140 | + EXPLICIT, |
| 141 | + ASSUME_ROLE, |
| 142 | + PROVIDER_CHAIN |
| 143 | + } |
| 144 | +} |
0 commit comments