Search in sources :

Example 1 with BasicSessionCredentials

use of com.aliyuncs.auth.BasicSessionCredentials in project fc-java-sdk by aliyun.

the class Config method refreshCredentials.

/**
 * refresh credentials if CredentialProvider set
 */
public void refreshCredentials() {
    if (this.credsProvider == null)
        return;
    try {
        AlibabaCloudCredentials creds = this.credsProvider.getCredentials();
        this.accessKeyID = creds.getAccessKeyId();
        this.accessKeySecret = creds.getAccessKeySecret();
        if (creds instanceof BasicSessionCredentials) {
            this.securityToken = ((BasicSessionCredentials) creds).getSessionToken();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : BasicSessionCredentials(com.aliyuncs.auth.BasicSessionCredentials) AlibabaCloudCredentials(com.aliyuncs.auth.AlibabaCloudCredentials) IOException(java.io.IOException)

Example 2 with BasicSessionCredentials

use of com.aliyuncs.auth.BasicSessionCredentials in project fc-java-sdk by aliyun.

the class FunctionComputeClientTest method testClientCredentialProviderMock.

@Test
public void testClientCredentialProviderMock() {
    // init CredentialProvider
    String ak = "ak";
    String sk = "sk";
    String stsToken = "sts_token";
    BasicSessionCredentials creds = new BasicSessionCredentials(ak, sk, stsToken);
    // mock
    InstanceProfileCredentialsProvider credsProvider = mock(InstanceProfileCredentialsProvider.class);
    try {
        when(credsProvider.getCredentials()).thenReturn(creds);
    } catch (com.aliyuncs.exceptions.ClientException e) {
        e.printStackTrace();
    }
    // init fc client
    Config config = new Config(REGION, ACCOUNT_ID, credsProvider, false);
    FunctionComputeClient fcClient = new FunctionComputeClient(config);
    client = fcClient;
    // Create a service
    try {
        createService(SERVICE_NAME);
    } catch (Exception e) {
    }
    assertEquals(creds.getAccessKeyId(), config.getAccessKeyID());
    assertEquals(creds.getAccessKeySecret(), config.getAccessKeySecret());
    assertEquals(creds.getSessionToken(), config.getSecurityToken());
}
Also used : InstanceProfileCredentialsProvider(com.aliyuncs.auth.InstanceProfileCredentialsProvider) BasicSessionCredentials(com.aliyuncs.auth.BasicSessionCredentials) 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) JSONException(org.json.JSONException) ParseException(java.text.ParseException) ClientException(com.aliyuncs.fc.exceptions.ClientException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException)

Aggregations

BasicSessionCredentials (com.aliyuncs.auth.BasicSessionCredentials)2 IOException (java.io.IOException)2 AlibabaCloudCredentials (com.aliyuncs.auth.AlibabaCloudCredentials)1 InstanceProfileCredentialsProvider (com.aliyuncs.auth.InstanceProfileCredentialsProvider)1 SignURLConfig (com.aliyuncs.fc.auth.SignURLConfig)1 FunctionComputeClient (com.aliyuncs.fc.client.FunctionComputeClient)1 Config (com.aliyuncs.fc.config.Config)1 ClientException (com.aliyuncs.fc.exceptions.ClientException)1 NasMountConfig (com.aliyuncs.fc.model.NasConfig.NasMountConfig)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 ParseException (java.text.ParseException)1 JSONException (org.json.JSONException)1