Search in sources :

Example 1 with Credentials

use of com.aliyuncs.sts.model.v20150401.AssumeRoleResponse.Credentials in project fc-java-sdk by aliyun.

the class FunctionComputeClientTest method overrideFCClient.

public FunctionComputeClient overrideFCClient(boolean useSts, boolean useHttps) throws com.aliyuncs.exceptions.ClientException {
    if (useSts) {
        Credentials creds = getAssumeRoleCredentials(null);
        FunctionComputeClient fcClient = new FunctionComputeClient(new Config(REGION, ACCOUNT_ID, creds.getAccessKeyId(), creds.getAccessKeySecret(), creds.getSecurityToken(), useHttps));
        if (!Strings.isNullOrEmpty(ENDPOINT)) {
            fcClient.setEndpoint(ENDPOINT);
        }
        return fcClient;
    }
    return new FunctionComputeClient(new Config(REGION, ACCOUNT_ID, ACCESS_KEY, SECRET_KEY, null, useHttps));
}
Also used : Config(com.aliyuncs.fc.config.Config) SignURLConfig(com.aliyuncs.fc.auth.SignURLConfig) NasMountConfig(com.aliyuncs.fc.model.NasConfig.NasMountConfig) FunctionComputeClient(com.aliyuncs.fc.client.FunctionComputeClient) Credentials(com.aliyuncs.sts.model.v20150401.AssumeRoleResponse.Credentials) BasicSessionCredentials(com.aliyuncs.auth.BasicSessionCredentials)

Example 2 with Credentials

use of com.aliyuncs.sts.model.v20150401.AssumeRoleResponse.Credentials in project fc-java-sdk by aliyun.

the class FunctionComputeClientTest method getAssumeRoleCredentials.

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();
}
Also used : AssumeRoleRequest(com.aliyuncs.sts.model.v20150401.AssumeRoleRequest) DefaultAcsClient(com.aliyuncs.DefaultAcsClient) AssumeRoleResponse(com.aliyuncs.sts.model.v20150401.AssumeRoleResponse) IClientProfile(com.aliyuncs.profile.IClientProfile)

Example 3 with Credentials

use of com.aliyuncs.sts.model.v20150401.AssumeRoleResponse.Credentials in project fc-java-sdk by aliyun.

the class FunctionComputeClientTest method testCreateServiceStsTokenNoPassRole.

@Test
public void testCreateServiceStsTokenNoPassRole() throws com.aliyuncs.exceptions.ClientException {
    // Use a policy that does not have ram:PassRole, this policy will intersect with the role policy
    // Access denied is expected if using STS without PassRole allowed
    // Policy intersection doc: https://help.aliyun.com/document_detail/31935.html
    String policy = "{\"Version\": \"1\",\"Statement\": [{\"Effect\": \"Allow\",\"Action\": [\"fc:*\"],\"Resource\": [\"*\"]}]}";
    Credentials creds = getAssumeRoleCredentials(policy);
    client = new FunctionComputeClient(new Config(REGION, ACCOUNT_ID, creds.getAccessKeyId(), creds.getAccessKeySecret(), creds.getSecurityToken(), false));
    try {
        createService(SERVICE_NAME);
        fail("ClientException is expected");
    } catch (ClientException e) {
        assertTrue(e.getErrorMessage(), e.getErrorMessage().contains("the caller is not authorized to perform 'ram:PassRole'"));
    }
}
Also used : Config(com.aliyuncs.fc.config.Config) SignURLConfig(com.aliyuncs.fc.auth.SignURLConfig) NasMountConfig(com.aliyuncs.fc.model.NasConfig.NasMountConfig) ClientException(com.aliyuncs.fc.exceptions.ClientException) FunctionComputeClient(com.aliyuncs.fc.client.FunctionComputeClient) Credentials(com.aliyuncs.sts.model.v20150401.AssumeRoleResponse.Credentials) BasicSessionCredentials(com.aliyuncs.auth.BasicSessionCredentials)

Aggregations

BasicSessionCredentials (com.aliyuncs.auth.BasicSessionCredentials)2 SignURLConfig (com.aliyuncs.fc.auth.SignURLConfig)2 FunctionComputeClient (com.aliyuncs.fc.client.FunctionComputeClient)2 Config (com.aliyuncs.fc.config.Config)2 NasMountConfig (com.aliyuncs.fc.model.NasConfig.NasMountConfig)2 Credentials (com.aliyuncs.sts.model.v20150401.AssumeRoleResponse.Credentials)2 DefaultAcsClient (com.aliyuncs.DefaultAcsClient)1 ClientException (com.aliyuncs.fc.exceptions.ClientException)1 IClientProfile (com.aliyuncs.profile.IClientProfile)1 AssumeRoleRequest (com.aliyuncs.sts.model.v20150401.AssumeRoleRequest)1 AssumeRoleResponse (com.aliyuncs.sts.model.v20150401.AssumeRoleResponse)1