Java Code Examples for com.aliyuncs.sts.model.v20150401.AssumeRoleRequest#setProtocol()

The following examples show how to use com.aliyuncs.sts.model.v20150401.AssumeRoleRequest#setProtocol() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OSSUtil.java    From xnx3 with Apache License 2.0 6 votes vote down vote up
static AssumeRoleResponse assumeRole(String accessKeyId, String accessKeySecret,String roleArn, String roleSessionName, String policy,ProtocolType protocolType) throws ClientException {
	try {
		// 创建一个 Aliyun Acs Client, 用于发起 OpenAPI 请求
		IClientProfile profile = DefaultProfile.getProfile(region_cn_hangzhou, accessKeyId, accessKeySecret);
		DefaultAcsClient client = new DefaultAcsClient(profile);
		// 创建一个 AssumeRoleRequest 并设置请求参数
		final AssumeRoleRequest request = new AssumeRoleRequest();
		request.setVersion(sta_api_version);
		request.setMethod(MethodType.POST);
		request.setProtocol(protocolType);
		request.setRoleArn(roleArn);
		request.setRoleSessionName(roleSessionName);
		request.setPolicy(policy);
		// 发起请求,并得到response
		final AssumeRoleResponse response = client.getAcsResponse(request);
		return response;
	} catch (ClientException e) {
		throw e;
	}
}
 
Example 2
Source File: STSServiceImpl.java    From jframe with Apache License 2.0 6 votes vote down vote up
AssumeRoleResponse assumeRole(String id, String roleArn, String roleSessionName, String policy, ProtocolType protocolType)
        throws ServerException, com.aliyuncs.exceptions.ClientException {
    DefaultAcsClient client = clients.get(id);

    // 创建一个 AssumeRoleRequest 并设置请求参数
    final AssumeRoleRequest request = new AssumeRoleRequest();
    request.setVersion(_config.getConf(id, K_api_version));
    request.setMethod(MethodType.POST);
    request.setProtocol(protocolType);

    request.setRoleArn(roleArn);
    request.setRoleSessionName(roleSessionName);
    request.setPolicy(policy);

    request.setDurationSeconds(Long.parseLong(_config.getConf(id, K_durationSeconds, "3600"))); // 默认值为3600

    // 发起请求,并得到response
    final AssumeRoleResponse response = client.getAcsResponse(request);
    // client.shutdown();

    return response;
}
 
Example 3
Source File: StsServiceSample.java    From jframe with Apache License 2.0 6 votes vote down vote up
static AssumeRoleResponse assumeRole(String accessKeyId, String accessKeySecret, String roleArn, String roleSessionName, String policy,
                                     ProtocolType protocolType) throws ClientException {
    try {
        // 创建一个 Aliyun Acs Client, 用于发起 OpenAPI 请求
        IClientProfile profile = DefaultProfile.getProfile(REGION_CN_HANGZHOU, accessKeyId, accessKeySecret);
        DefaultAcsClient client = new DefaultAcsClient(profile);

        // 创建一个 AssumeRoleRequest 并设置请求参数
        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);

        // 发起请求,并得到response
        final AssumeRoleResponse response = client.getAcsResponse(request);

        return response;
    } catch (ClientException e) {
        throw e;
    }
}
 
Example 4
Source File: FunctionComputeClientTest.java    From fc-java-sdk with MIT License 5 votes vote down vote up
private Credentials getAssumeRoleCredentials(String policy)
    throws com.aliyuncs.exceptions.ClientException {
    IClientProfile profile = DefaultProfile
        .getProfile(REGION, ACCESS_KEY, SECRET_KEY);
    //DefaultProfile.addEndpoint("sts.us-west-1.aliyuncs.com", "us-west-1", "Sts", "sts.us-west-1.aliyuncs.com");
    DefaultAcsClient client = new DefaultAcsClient(profile);

    AssumeRoleRequest request = new AssumeRoleRequest();
    request.setVersion(STS_API_VERSION);
    request.setMethod(MethodType.POST);
    request.setProtocol(ProtocolType.HTTPS);
    request.setRoleArn(STS_ROLE);
    request.setRoleSessionName("test-session");
    if (policy != null) {
        request.setPolicy(policy);
    }

    AssumeRoleResponse stsResponse;
    try {
        stsResponse = client.getAcsResponse(request);
    } catch (com.aliyuncs.exceptions.ClientException e) {
        throw new RuntimeException(e);
    }

    String accessKey = stsResponse.getCredentials().getAccessKeyId();
    String secretKey = stsResponse.getCredentials().getAccessKeySecret();
    String stsToken = stsResponse.getCredentials().getSecurityToken();

    assertNotNull(accessKey);
    assertNotNull(secretKey);
    assertNotNull(stsToken);

    return stsResponse.getCredentials();
}
 
Example 5
Source File: StsServiceRequest.java    From alibaba-flink-connectors with Apache License 2.0 4 votes vote down vote up
public static AssumeRoleResponse assumeRoleWithServiceIdentity(
			final String streamAccessId, final String streamAccessKey,
			final String roleArn, final String roleSessionName,
			final String assumeRoleFor,
			Configuration properties) throws Exception {
		//decode
		String decodeKey = DecodeUtil.decrypt(streamAccessKey, StsConstants.STS_SECRET_KEY);

		String regionId = properties.getString(BlinkOptions.STS.STS_REGION_ID);

		// 创建一个 Aliyun Acs Client, 用于发起 OpenAPI 请求
		IClientProfile profile = DefaultProfile.getProfile(
				regionId, streamAccessId, decodeKey);
		DefaultAcsClient client = new DefaultAcsClient(profile);

		// endPoints format:   endPointName#regionId#product#domain,endPointName1#regionId1#product1#domain1
		if (properties.containsKey(INNER_STS_ENDPOINT) && properties.getString(INNER_STS_ENDPOINT, null) != null){
			String endPoints = properties.toMap().get(INNER_STS_ENDPOINT);
			if (!endPoints.isEmpty()) {
				String[] endPointItem = endPoints.split(",");
				for (String item : endPointItem) {
					String[] partItems = item.split("#");
					if (null != partItems && partItems.length == 4) {
						DefaultProfile.addEndpoint(partItems[0], partItems[1], partItems[2], partItems[3]);
					}
				}
			}
		}

		// 创建一个 AssumeRoleRequest 并设置请求参数
		final AssumeRoleRequest request = new AssumeRoleRequest();
		request.setMethod(MethodType.POST);

		request.setProtocol(PROTOCOL_TYPE);
		request.setDurationSeconds(DURATION);
		request.setRoleArn(roleArn);
		request.setRoleSessionName(roleSessionName);
//		request.setAssumeRoleFor(assumeRoleFor);
		X509TrustAll.ignoreSSLCertificate();
		// 发起请求,并得到response
		final AssumeRoleResponse response = client.getAcsResponse(request);

		return response;
	}