项目作者: aliyun

项目描述 :
Alibaba Cloud Credentials for Java
高级语言: Java
项目地址: git://github.com/aliyun/credentials-java.git
创建时间: 2019-09-03T08:00:58Z
项目社区:https://github.com/aliyun/credentials-java

开源协议:Apache License 2.0

下载


English | 简体中文

Alibaba Cloud Logo

Alibaba Cloud Credentials for Java

Java CI
codecov
Latest Stable Version

Alibaba Cloud Credentials for Java is a tool for Java developers to manage credentials.

This document introduces how to obtain and use Credentials for Java.

Requirements

  • The Alibaba Cloud Credentials for Java requires JDK 1.8 or later.

Installation

  1. <dependency>
  2. <groupId>com.aliyun</groupId>
  3. <artifactId>credentials-java</artifactId>
  4. <version>Use the version shown in the maven badge</version>
  5. </dependency>

Quick Examples

Before you begin, you need to sign up for an Alibaba Cloud account and retrieve your Credentials.

Credential Type

AccessKey

Setup access_key credential through User Information Management, it have full authority over the account, please keep it safe. Sometimes for security reasons, you cannot hand over a primary account AccessKey with full access to the developer of a project. You may create a sub-account RAM Sub-account , grant its authorization,and use the AccessKey of RAM Sub-account.

  1. import com.aliyun.credentials.Client;
  2. import com.aliyun.credentials.models.Config;
  3. public class DemoTest {
  4. public static void main(String[] args) throws Exception{
  5. Config config = new Config();
  6. // Which type of credential you want
  7. config.setType("access_key");
  8. // AccessKeyId of your account
  9. config.setAccessKeyId("AccessKeyId");
  10. // AccessKeySecret of your account
  11. config.setAccessKeySecret("AccessKeySecret");
  12. Client client = new Client(config);
  13. }
  14. }

STS

Create a temporary security credential by applying Temporary Security Credentials (TSC) through the Security Token Service (STS).

  1. import com.aliyun.credentials.Client;
  2. import com.aliyun.credentials.models.Config;
  3. public class DemoTest {
  4. public static void main(String[] args) throws Exception{
  5. Config config = new Config();
  6. // Which type of credential you want
  7. config.setType("sts");
  8. // AccessKeyId of your account
  9. config.setAccessKeyId("AccessKeyId");
  10. // AccessKeySecret of your account
  11. config.setAccessKeySecret("AccessKeySecret");
  12. // Temporary Security Token
  13. config.setSecurityToken("SecurityToken");
  14. Client client = new Client(config);
  15. }
  16. }

RamRoleArn

By specifying RAM Role, the credential will be able to automatically request maintenance of STS Token. If you want to limit the permissions(How to make a policy) of STS Token, you can assign value for Policy.

  1. import com.aliyun.credentials.Client;
  2. import com.aliyun.credentials.models.Config;
  3. public class DemoTest {
  4. public static void main(String[] args) throws Exception{
  5. Config config = new Config();
  6. // Which type of credential you want
  7. config.setType("ram_role_arn");
  8. // AccessKeyId of your account
  9. config.setAccessKeyId("AccessKeyId");
  10. // AccessKeySecret of your account
  11. config.setAccessKeySecret("AccessKeySecret");
  12. // Format: acs:ram::USER_Id:role/ROLE_NAME
  13. // roleArn can be replaced by setting environment variable: ALIBABA_CLOUD_ROLE_ARN
  14. config.setRoleArn("RoleArn");
  15. // Role Session Name
  16. config.setRoleSessionName("RoleSessionName");
  17. // Not required, limit the permissions of STS Token
  18. config.setPolicy("policy");
  19. // Not required, the external ID of the RAM role
  20. // This parameter is provided by an external party and is used to prevent the confused deputy problem.
  21. config.setExternalId("externalId");
  22. // Not required, limit the Valid time of STS Token
  23. config.setRoleSessionExpiration(3600);
  24. Client client = new Client(config);
  25. }
  26. }

OIDCRoleArn

By specifying OIDC Role, the credential will be able to automatically request maintenance of STS Token. If you want to limit the permissions(How to make a policy) of STS Token, you can assign value for Policy.

  1. import com.aliyun.credentials.Client;
  2. import com.aliyun.credentials.models.Config;
  3. public class DemoTest {
  4. public static void main(String[] args) throws Exception{
  5. Config config = new Config();
  6. // Which type of credential you want
  7. config.setType("oidc_role_arn");
  8. // Format: acs:ram::USER_Id:role/ROLE_NAME
  9. // roleArn can be replaced by setting environment variable: ALIBABA_CLOUD_ROLE_ARN
  10. config.setRoleArn("RoleArn");
  11. // Format: acs:ram::USER_Id:oidc-provider/OIDC Providers
  12. // oidcProviderArn can be replaced by setting environment variable: ALIBABA_CLOUD_OIDC_PROVIDER_ARN
  13. config.setOidcProviderArn("OIDCProviderArn");
  14. // Format: path
  15. // OIDCTokenFilePath can be replaced by setting environment variable: ALIBABA_CLOUD_OIDC_TOKEN_FILE
  16. config.setOidcTokenFilePath("/Users/xxx/xxx");
  17. // Role Session Name
  18. config.setRoleSessionName("RoleSessionName");
  19. // Not required, limit the permissions of STS Token
  20. config.setPolicy("policy");
  21. // Not required, limit the Valid time of STS Token
  22. config.setRoleSessionExpiration(3600);
  23. Client client = new Client(config);
  24. }
  25. }

EcsRamRole

By specifying the role name, the credential will be able to automatically request maintenance of STS Token.

  1. import com.aliyun.credentials.Client;
  2. import com.aliyun.credentials.models.Config;
  3. public class DemoTest {
  4. public static void main(String[] args) throws Exception {
  5. Config config = new Config();
  6. // Which type of credential you want
  7. config.setType("ecs_ram_role");
  8. // `roleName` is optional. It will be retrieved automatically if not set. It is highly recommended to set it up to reduce requests
  9. config.setRoleName("RoleName");
  10. // `disableIMDSv1` is optional and is recommended to be turned on. It can be replaced by setting environment variable: ALIBABA_CLOUD_IMDSV1_DISABLED
  11. config.setDisableIMDSv1(true);
  12. Client client = new Client(config);
  13. }
  14. }

URLCredential

By specifying the url, the credential will be able to automatically request maintenance of STS Token.

  1. import com.aliyun.credentials.Client;
  2. import com.aliyun.credentials.models.Config;
  3. public class DemoTest {
  4. public static void main(String[] args) throws Exception {
  5. Config config = new Config();
  6. // Which type of credential you want
  7. config.setType("credentials_uri");
  8. // Format: http url. `credentialsURI` can be replaced by setting environment variable: ALIBABA_CLOUD_CREDENTIALS_URI
  9. config.setCredentialsUri("http://xxx");
  10. Client client = new Client(config);
  11. }
  12. }

Bearer Token

If credential is required by the Cloud Call Centre (CCC), please apply for Bearer Token maintenance by yourself.

  1. import com.aliyun.credentials.Client;
  2. import com.aliyun.credentials.models.Config;
  3. public class DemoTest {
  4. public static void main(String[] args) throws Exception {
  5. Config config = new Config();
  6. // Which type of credential you want
  7. config.setType("bearer");
  8. // BearerToken of your account
  9. config.setBearerToken("BearerToken");
  10. Client client = new Client(config);
  11. }
  12. }

Use the default credential provider chain

If you call Client client = new Client(), it will use provider chain to get credential for you.

The default credential provider chain looks for available credentials, with following order:

  1. System Properties

    Look for environment credentials in system properties. If the alibabacloud.accessKeyId and alibabacloud.accessKeySecret system properties are defined and not empty, the program will use them to create default credentials. If the alibabacloud.accessKeyId, alibabacloud.accessKeySecret and alibabacloud.sessionToken environment variables are defined and are not empty, the program will use them to create temporary security credentials(STS). Note: This token has an expiration time, it is recommended to use it in a temporary environment.

  2. Environment Credentials

    Look for environment credentials in environment variable. If the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables are defined and are not empty, the program will use them to create default credentials. If the ALIBABA_CLOUD_ACCESS_KEY_ID, ALIBABA_CLOUD_ACCESS_KEY_SECRET and ALIBABA_CLOUD_SECURITY_TOKEN environment variables are defined and are not empty, the program will use them to create temporary security credentials(STS). Note: This token has an expiration time, it is recommended to use it in a temporary environment.

  3. Credentials File

    If there is ~/.alibabacloud/credentials default file (Windows shows C:\Users\USER_NAME\.alibabacloud\credentials), the program automatically creates credentials with the specified type and name. The default file is not necessarily exist, but a parse error will throw an exception. The name of configuration item is lowercase.This configuration file can be shared between different projects and between different tools. Because it is outside of the project and will not be accidentally committed to the version control. The path to the default file can be modified by defining the ALIBABA_CLOUD_CREDENTIALS_FILE environment variable. If not configured, use the default configuration default. You can also set the environment variables ALIBABA_CLOUD_PROFILE to use the configuration.

    1. [default] # default setting
    2. enable = true # Enable,Enabled by default if this option is not present
    3. type = access_key # Certification type: access_key
    4. access_key_id = foo # Key
    5. access_key_secret = bar # Secret
    6. [client1] # configuration that is named as `client1`
    7. type = ecs_ram_role # Certification type: ecs_ram_role
    8. role_name = EcsRamRoleTest # Role Name
    9. [client2] # configuration that is named as `client2`
    10. enable = false # Disable
    11. type = ram_role_arn # Certification type: ram_role_arn
    12. region_id = cn-test
    13. policy = test # optional Specify permissions
    14. access_key_id = foo
    15. access_key_secret = bar
    16. role_arn = role_arn # can be replaced by setting environment variable: ALIBABA_CLOUD_ROLE_ARN
    17. role_session_name = session_name # optional
    18. [client3] # configuration that is named as `client3`
    19. enable = false # Disable
    20. type = oidc_role_arn # Certification type: oidc_role_arn
    21. region_id = cn-test
    22. policy = test # optional Specify permissions
    23. role_arn = role_arn # can be replaced by setting environment variable: ALIBABA_CLOUD_ROLE_ARN
    24. oidc_provider_arn = oidc_provider_arn # can be replaced by setting environment variable: ALIBABA_CLOUD_OIDC_PROVIDER_ARN
    25. oidc_token_file_path = /xxx/xxx # can be replaced by setting environment variable: ALIBABA_CLOUD_OIDC_TOKEN_FILE
    26. role_session_name = session_name # optional

Issues

Opening an Issue, Issues not conforming to the guidelines may be closed immediately.

Changelog

Detailed changes for each release are documented in the release notes.

Contribution

Please make sure to read the Contributing Guide before making a pull request.

References

License

Apache-2.0

Copyright 2009-present Alibaba Cloud All rights reserved.