|
| 1 | +package com.aliyuncs.auth; |
| 2 | + |
| 3 | +import com.aliyuncs.exceptions.ClientException; |
| 4 | +import com.aliyuncs.utils.AuthUtils; |
| 5 | +import com.aliyuncs.utils.StringUtils; |
| 6 | +import org.ini4j.Profile; |
| 7 | +import org.ini4j.Wini; |
| 8 | + |
| 9 | +import java.io.File; |
| 10 | +import java.io.IOException; |
| 11 | +import java.util.HashMap; |
| 12 | +import java.util.Map; |
| 13 | + |
| 14 | +public class ProfileCredentialsProvider implements AlibabaCloudCredentialsProvider { |
| 15 | + private static volatile Wini ini; |
| 16 | + |
| 17 | + private static Wini getIni(String filePath) throws IOException { |
| 18 | + if (null == ini) { |
| 19 | + synchronized (ProfileCredentialsProvider.class) { |
| 20 | + if (null == ini) { |
| 21 | + ini = new Wini(new File(filePath)); |
| 22 | + } |
| 23 | + } |
| 24 | + } |
| 25 | + return ini; |
| 26 | + } |
| 27 | + |
| 28 | + @Override |
| 29 | + public AlibabaCloudCredentials getCredentials() throws ClientException { |
| 30 | + String filePath = AuthUtils.getEnvironmentCredentialsFile(); |
| 31 | + if (filePath == null) { |
| 32 | + filePath = AuthConstant.DEFAULT_CREDENTIALS_FILE_PATH; |
| 33 | + } |
| 34 | + if (filePath.length() == 0) { |
| 35 | + throw new ClientException("The specified credentials file is empty"); |
| 36 | + } |
| 37 | + Wini ini; |
| 38 | + try { |
| 39 | + ini = getIni(filePath); |
| 40 | + } catch (IOException e) { |
| 41 | + return null; |
| 42 | + } |
| 43 | + Map<String, Map<String, String>> client = loadIni(ini); |
| 44 | + Map<String, String> clientConfig = client.get(AuthUtils.getClientType()); |
| 45 | + if (clientConfig == null) { |
| 46 | + throw new ClientException("Client is not open in the specified credentials file"); |
| 47 | + } |
| 48 | + CredentialsProviderFactory credentialsProviderFactory = new CredentialsProviderFactory(); |
| 49 | + return createCredential(clientConfig, credentialsProviderFactory); |
| 50 | + } |
| 51 | + |
| 52 | + private Map<String, Map<String, String>> loadIni(Wini ini) { |
| 53 | + Map<String, Map<String, String>> client = new HashMap<String, Map<String, String>>(); |
| 54 | + boolean enable; |
| 55 | + for (Map.Entry<String, Profile.Section> clientType : ini.entrySet()) { |
| 56 | + enable = clientType.getValue().get(AuthConstant.INI_ENABLE, boolean.class); |
| 57 | + if (enable) { |
| 58 | + Map<String, String> clientConfig = new HashMap<String, String>(); |
| 59 | + for (Map.Entry<String, String> enabledClient : clientType.getValue().entrySet()) { |
| 60 | + clientConfig.put(enabledClient.getKey(), enabledClient.getValue()); |
| 61 | + } |
| 62 | + client.put(clientType.getKey(), clientConfig); |
| 63 | + } |
| 64 | + } |
| 65 | + return client; |
| 66 | + } |
| 67 | + |
| 68 | + private AlibabaCloudCredentials createCredential(Map<String, String> clientConfig, |
| 69 | + CredentialsProviderFactory factory) throws ClientException { |
| 70 | + String configType = clientConfig.get(AuthConstant.INI_TYPE); |
| 71 | + if (StringUtils.isEmpty(configType)) { |
| 72 | + throw new ClientException("The configured client type is empty"); |
| 73 | + } |
| 74 | + if (AuthConstant.INI_TYPE_ARN.equals(configType)) { |
| 75 | + return getSTSAssumeRoleSessionCredentials(clientConfig, factory); |
| 76 | + } |
| 77 | + if (AuthConstant.INI_TYPE_KEY_PAIR.equals(configType)) { |
| 78 | + return getSTSGetSessionAccessKeyCredentials(clientConfig, factory); |
| 79 | + } |
| 80 | + if (AuthConstant.INI_TYPE_RAM.equals(configType)) { |
| 81 | + return getInstanceProfileCredentials(clientConfig, factory); |
| 82 | + } |
| 83 | + String accessKeyId = clientConfig.get(AuthConstant.INI_ACCESS_KEY_ID); |
| 84 | + String accessKeySecret = clientConfig.get(AuthConstant.INI_ACCESS_KEY_IDSECRET); |
| 85 | + if (StringUtils.isEmpty(accessKeyId) || StringUtils.isEmpty(accessKeySecret)) { |
| 86 | + return null; |
| 87 | + } |
| 88 | + return new BasicCredentials(accessKeyId, accessKeySecret); |
| 89 | + } |
| 90 | + |
| 91 | + private AlibabaCloudCredentials getSTSAssumeRoleSessionCredentials(Map<String, String> clientConfig, |
| 92 | + CredentialsProviderFactory factory) |
| 93 | + throws ClientException { |
| 94 | + String accessKeyId = clientConfig.get(AuthConstant.INI_ACCESS_KEY_ID); |
| 95 | + String accessKeySecret = clientConfig.get(AuthConstant.INI_ACCESS_KEY_IDSECRET); |
| 96 | + String roleSessionName = clientConfig.get(AuthConstant.INI_ROLE_SESSION_NAME); |
| 97 | + String roleArn = clientConfig.get(AuthConstant.INI_ROLE_ARN); |
| 98 | + String regionId = clientConfig.get(AuthConstant.DEFAULT_REGION); |
| 99 | + if (StringUtils.isEmpty(accessKeyId) || StringUtils.isEmpty(accessKeySecret)) { |
| 100 | + throw new ClientException("The configured access_key_id or access_key_secret is empty"); |
| 101 | + } |
| 102 | + if (StringUtils.isEmpty(roleSessionName) || StringUtils.isEmpty(roleArn)) { |
| 103 | + throw new ClientException("The configured role_session_name or role_arn is empty"); |
| 104 | + } |
| 105 | + STSAssumeRoleSessionCredentialsProvider provider = |
| 106 | + factory.createCredentialsProvider(new STSAssumeRoleSessionCredentialsProvider(accessKeyId, |
| 107 | + accessKeySecret, roleSessionName, roleArn, regionId)); |
| 108 | + return provider.getCredentials(); |
| 109 | + } |
| 110 | + |
| 111 | + private AlibabaCloudCredentials getSTSGetSessionAccessKeyCredentials(Map<String, String> clientConfig, |
| 112 | + CredentialsProviderFactory factory) |
| 113 | + throws ClientException { |
| 114 | + String publicKeyId = clientConfig.get(AuthConstant.INI_PUBLIC_KEY_ID); |
| 115 | + String privateKeyFile = clientConfig.get(AuthConstant.INI_PRIVATE_KEY_FILE); |
| 116 | + if (StringUtils.isEmpty(privateKeyFile)) { |
| 117 | + throw new ClientException("The configured private_key_file is empty"); |
| 118 | + } |
| 119 | + String privateKey = AuthUtils.getPrivateKey(privateKeyFile); |
| 120 | + if (StringUtils.isEmpty(publicKeyId) || StringUtils.isEmpty(privateKey)) { |
| 121 | + throw new ClientException("The configured public_key_id or private_key_file content is empty"); |
| 122 | + } |
| 123 | + STSGetSessionAccessKeyCredentialsProvider provider = |
| 124 | + factory.createCredentialsProvider(new STSGetSessionAccessKeyCredentialsProvider(publicKeyId, privateKey)); |
| 125 | + return provider.getCredentials(); |
| 126 | + } |
| 127 | + |
| 128 | + private AlibabaCloudCredentials getInstanceProfileCredentials(Map<String, String> clientConfig, |
| 129 | + CredentialsProviderFactory factory) |
| 130 | + throws ClientException { |
| 131 | + String roleName = clientConfig.get(AuthConstant.INI_ROLE_NAME); |
| 132 | + if (StringUtils.isEmpty(roleName)) { |
| 133 | + throw new ClientException("The configured role_name is empty"); |
| 134 | + } |
| 135 | + InstanceProfileCredentialsProvider provider = |
| 136 | + factory.createCredentialsProvider(new InstanceProfileCredentialsProvider(roleName)); |
| 137 | + return provider.getCredentials(); |
| 138 | + } |
| 139 | + |
| 140 | + |
| 141 | +} |
0 commit comments