|
| 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.common.maxcompute; |
| 19 | + |
| 20 | +import com.aliyun.auth.credentials.Credential; |
| 21 | +import com.aliyun.auth.credentials.provider.EcsRamRoleCredentialProvider; |
| 22 | +import com.aliyun.auth.credentials.provider.RamRoleArnCredentialProvider; |
| 23 | +import com.aliyun.odps.Odps; |
| 24 | +import com.aliyun.odps.account.Account; |
| 25 | +import com.aliyun.odps.account.AklessAccount; |
| 26 | +import com.aliyun.odps.account.AliyunAccount; |
| 27 | + |
| 28 | +import java.util.Map; |
| 29 | + |
| 30 | +public class MCUtils { |
| 31 | + public static void checkAuthProperties(Map<String, String> properties) { |
| 32 | + String authType = properties.getOrDefault(MCProperties.AUTH_TYPE, MCProperties.DEFAULT_AUTH_TYPE); |
| 33 | + if (authType.equalsIgnoreCase(MCProperties.AUTH_TYPE_AK_SK)) { |
| 34 | + if (!properties.containsKey(MCProperties.ACCESS_KEY) || !properties.containsKey(MCProperties.SECRET_KEY)) { |
| 35 | + throw new RuntimeException("Missing access key or secret key for AK/SK auth type"); |
| 36 | + } |
| 37 | + } else if (authType.equalsIgnoreCase(MCProperties.AUTH_TYPE_RAM_ROLE_ARN)) { |
| 38 | + if (!properties.containsKey(MCProperties.ACCESS_KEY) || !properties.containsKey(MCProperties.SECRET_KEY) |
| 39 | + || !properties.containsKey(MCProperties.RAM_ROLE_ARN)) { |
| 40 | + throw new RuntimeException("Missing access key, secret key or role arn for RAM Role ARN auth type"); |
| 41 | + } |
| 42 | + } else if (authType.equalsIgnoreCase(MCProperties.AUTH_TYPE_ECS_RAM_ROLE)) { |
| 43 | + if (!properties.containsKey(MCProperties.ECS_RAM_ROLE)) { |
| 44 | + throw new RuntimeException("Missing role name for ECS RAM Role auth type"); |
| 45 | + } |
| 46 | + } else { |
| 47 | + throw new RuntimeException("Unsupported auth type: " + authType); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + public static Odps createMcClient(Map<String, String> properties) { |
| 52 | + String authType = properties.getOrDefault(MCProperties.AUTH_TYPE, MCProperties.DEFAULT_AUTH_TYPE); |
| 53 | + if (authType.equalsIgnoreCase(MCProperties.AUTH_TYPE_AK_SK)) { |
| 54 | + String accessKey = properties.get(MCProperties.ACCESS_KEY); |
| 55 | + String secretKey = properties.get(MCProperties.SECRET_KEY); |
| 56 | + Account account = new AliyunAccount(accessKey, secretKey); |
| 57 | + return new Odps(account); |
| 58 | + } else if (authType.equalsIgnoreCase(MCProperties.AUTH_TYPE_RAM_ROLE_ARN)) { |
| 59 | + String accessKey = properties.get(MCProperties.ACCESS_KEY); |
| 60 | + String secretKey = properties.get(MCProperties.SECRET_KEY); |
| 61 | + String roleArn = properties.get(MCProperties.RAM_ROLE_ARN); |
| 62 | + RamRoleArnCredentialProvider ramRoleArnCredentialProvider = |
| 63 | + RamRoleArnCredentialProvider.builder().credential( |
| 64 | + Credential.builder().accessKeyId(accessKey) |
| 65 | + .accessKeySecret(secretKey).build()) |
| 66 | + .roleArn(roleArn).build(); |
| 67 | + AklessAccount aklessAccount = new AklessAccount(ramRoleArnCredentialProvider); |
| 68 | + return new Odps(aklessAccount); |
| 69 | + } else if (authType.equalsIgnoreCase(MCProperties.AUTH_TYPE_ECS_RAM_ROLE)) { |
| 70 | + String roleName = properties.get(MCProperties.ECS_RAM_ROLE); |
| 71 | + EcsRamRoleCredentialProvider credentialProvider = EcsRamRoleCredentialProvider.create(roleName); |
| 72 | + AklessAccount aklessAccount = new AklessAccount(credentialProvider); |
| 73 | + return new Odps(aklessAccount); |
| 74 | + } else { |
| 75 | + throw new RuntimeException("Unsupported auth type: " + authType); |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments