package com.aliyun.openservices.log.sample;
import java.util.Date;
import java.util.Vector;
import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.LogItem;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.request.PutLogsRequest;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.http.ProtocolType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.aliyuncs.sts.model.v20150401.AssumeRoleRequest;
import com.aliyuncs.sts.model.v20150401.AssumeRoleResponse;
public class StsSample {
// now sts only support "cn-hangzhou"
public static final String REGION_CN_HANGZHOU = "cn-hangzhou";
// current sts api version
public static final String STS_API_VERSION = "2015-04-01";
static AssumeRoleResponse assumeRole(String accessKeyId, String accessKeySecret,
String roleArn, String roleSessionName, String policy,
ProtocolType protocolType) throws ClientException {
try {
// construct Aliyun Acs Client to ivoke OpenAPI
IClientProfile profile = DefaultProfile.getProfile(REGION_CN_HANGZHOU, accessKeyId, accessKeySecret);
DefaultAcsClient client = new DefaultAcsClient(profile);
// create AssumeRoleRequest object
final AssumeRoleRequest request = new AssumeRoleRequest();
request.setVersion(STS_API_VERSION);
request.setMethod(MethodType.POST);
request.setProtocol(protocolType);
request.setRoleArn(roleArn);
request.setRoleSessionName(roleSessionName);
request.setPolicy(policy);
// send request
final AssumeRoleResponse response = client.getAcsResponse(request);
return response;
} catch (ClientException e) {
throw e;
}
}
public static void main(String[] args) {
// only RAM user(sub account)can invoke AssumeRole interface
// Aliyun root account's AccessKeys can't invoke AssumeRole
// please create sub account in RAM web console(https://ram.console.aliyun.com), and create AK for this sub account
String accessKeyId = "<subaccountaccesskey>";
String accessKeySecret = "<subaccountaccesssecret>";
// AssumeRole API parameter: RoleArn, RoleSessionName, Policy, and DurationSeconds
// RoleArn can retrieve in RAM web console
// https://ram.console.aliyun.com/#/role/detail/< specifid rolename>/info
String roleArn = "<rolearn found in web console>";
// RoleSessionName is temporary Token(mainly used for audit)
String roleSessionName = "bluemix-001";
String policy = "{\n" +
" \"Version\": \"1\", \n" +
" \"Statement\": [\n" +
" {\n" +
" \"Action\": \"log:PostLogStoreLogs\",\n" +
" \"Resource\": \"*\",\n" +
" \"Effect\": \"Allow\"\n" +
" }\n" +
" ]\n" +
"}";
System.out.println(policy);
// only support HTTPS here
ProtocolType protocolType = ProtocolType.HTTPS;
AssumeRoleResponse response = new AssumeRoleResponse();
try {
response = assumeRole(accessKeyId, accessKeySecret,
roleArn, roleSessionName, policy, protocolType);
System.out.println("Expiration: " + response.getCredentials().getExpiration());
System.out.println("Access Key Id: " + response.getCredentials().getAccessKeyId());
System.out.println("Access Key Secret: " + response.getCredentials().getAccessKeySecret());
System.out.println("Security Token: " + response.getCredentials().getSecurityToken());
} catch (ClientException e) {
System.out.println("Failed to get a token.");
System.out.println("Error code: " + e.getErrCode());
System.out.println("Error message: " + e.getErrMsg());
}
// log service parameter
// log service endpoint doc: https://help.aliyun.com/zh/sls/developer-reference/api-sls-2020-12-30-endpoint
String logServiceEndpoint = "cn-hangzhou.log.aliyuncs.com";
// means project region must be cn-hangzhou
String project = "<log service project name>";
String logstore = "<log service logstore name>";
// construct log service client object
Client client = new Client(logServiceEndpoint,
response.getCredentials().getAccessKeyId(),
response.getCredentials().getAccessKeySecret());
// notice: the AK & Security Token will be expire in 1hour
// so you must invoke asumeRole interface when expired
client.SetSecurityToken(response.getCredentials().getSecurityToken());
Vector<LogItem> logGroup = new Vector<LogItem>();
LogItem logItem = new LogItem((int) (new Date().getTime() / 1000));
logItem.PushBack("StsSample", "Send Data");
logGroup.add(logItem);
PutLogsRequest req2 = new PutLogsRequest(project, logstore, "", "", logGroup);
try {
client.PutLogs(req2);
} catch (LogException e) {
System.out.println("Failed to send data.");
System.out.println("Error code: " + e.GetErrorCode());
System.out.println("Error message: " + e.GetErrorMessage());
}
}
}