in src/main/java/com/aliyun/credentials/provider/RamRoleArnCredentialProvider.java [173:235]
public RefreshResult<CredentialModel> getNewSessionCredentials(CompatibleUrlConnClient client) {
ParameterHelper parameterHelper = new ParameterHelper();
HttpRequest httpRequest = new HttpRequest();
httpRequest.setUrlParameter("Action", "AssumeRole");
httpRequest.setUrlParameter("Format", "JSON");
httpRequest.setUrlParameter("Version", "2015-04-01");
httpRequest.setUrlParameter("DurationSeconds", String.valueOf(durationSeconds));
httpRequest.setUrlParameter("RoleArn", this.roleArn);
httpRequest.setUrlParameter("RoleSessionName", this.roleSessionName);
if (policy != null) {
httpRequest.setUrlParameter("Policy", this.policy);
}
if (externalId != null) {
httpRequest.setUrlParameter("ExternalId", this.externalId);
}
httpRequest.setSysMethod(MethodType.GET);
httpRequest.setSysConnectTimeout(this.connectTimeout);
httpRequest.setSysReadTimeout(this.readTimeout);
CredentialModel credentials = this.credentialsProvider.getCredentials();
Validate.notNull(credentials, "Unable to load original credentials from the providers in RAM role arn.");
httpRequest.setUrlParameter("AccessKeyId", credentials.getAccessKeyId());
if (!StringUtils.isEmpty(credentials.getSecurityToken())) {
httpRequest.setUrlParameter("SecurityToken", credentials.getSecurityToken());
}
String strToSign = parameterHelper.composeStringToSign(MethodType.GET, httpRequest.getUrlParameters());
String signature = parameterHelper.signString(strToSign, credentials.getAccessKeySecret() + "&");
httpRequest.setUrlParameter("Signature", signature);
httpRequest.setSysUrl(parameterHelper.composeUrl(this.STSEndpoint, httpRequest.getUrlParameters(),
"https"));
HttpResponse httpResponse;
try {
httpResponse = client.syncInvoke(httpRequest);
} catch (Exception e) {
throw new CredentialException("Failed to connect RamRoleArn Service: " + e);
}
if (httpResponse.getResponseCode() != 200) {
throw new CredentialException(String.format("Error refreshing credentials from RamRoleArn, HttpCode: %s, result: %s.", httpResponse.getResponseCode(), httpResponse.getHttpContentString()));
}
Gson gson = new Gson();
Map<String, Object> map = gson.fromJson(httpResponse.getHttpContentString(), Map.class);
if (null == map || !map.containsKey("Credentials")) {
throw new CredentialException(String.format("Error retrieving credentials from RamRoleArn result: %s.", httpResponse.getHttpContentString()));
}
Map<String, String> result = (Map<String, String>) map.get("Credentials");
if (!result.containsKey("AccessKeyId") || !result.containsKey("AccessKeySecret") || !result.containsKey("SecurityToken")) {
throw new CredentialException(String.format("Error retrieving credentials from RamRoleArn result: %s.", httpResponse.getHttpContentString()));
}
long expiration = ParameterHelper.getUTCDate(result.get("Expiration")).getTime();
CredentialModel credential = CredentialModel.builder()
.accessKeyId(result.get("AccessKeyId"))
.accessKeySecret(result.get("AccessKeySecret"))
.securityToken(result.get("SecurityToken"))
.type(AuthConstant.RAM_ROLE_ARN)
.providerName(String.format("%s/%s", this.getProviderName(), credentials.getProviderName()))
.expiration(expiration)
.build();
return RefreshResult.builder(credential)
.staleTime(getStaleTime(expiration))
.build();
}