English | 简体中文
Alibaba Cloud Credentials for Python
Installation
Python SDK uses a common package management tool named pip
. If pip is not installed, see the pip user guide to install pip.
pip install alibabacloud_credentials
Usage
Before you begin, you need to sign up for an Alibaba Cloud account and retrieve your Credentials.
Credential Type
Access Key
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.
from alibabacloud_credentials.client import Client
from alibabacloud_credentials.models import Config
config = Config(
type='access_key',
access_key_id='accessKeyId',
access_key_secret='accessKeySecret',
)
cred = Client(config)
access_key_id = cred.get_access_key_id()
access_key_secret = cred.get_access_key_secret()
cred_type = cred.get_type()
STS
Create a temporary security credential by applying Temporary Security Credentials (TSC) through the Security Token Service (STS).
from alibabacloud_credentials.client import Client
from alibabacloud_credentials.models import Config
config = Config(
type='sts',
access_key_id='accessKeyId',
access_key_secret='accessKeySecret',
security_token='securityToken'
)
cred = Client(config)
access_key_id = cred.get_access_key_id()
access_key_secret = cred.get_access_key_secret()
security_token = cred.get_security_token()
cred_type = cred.get_type()
RAM Role ARN
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
.
from alibabacloud_credentials.client import Client
from alibabacloud_credentials.models import Config
config = Config(
type='ram_role_arn',
access_key_id='accessKeyId',
access_key_secret='accessKeySecret',
security_token='securityToken',
role_arn='roleArn',
role_session_name='roleSessionName',
policy='policy',
role_session_expiration=3600
)
cred = Client(config)
access_key_id = cred.get_access_key_id()
access_key_secret = cred.get_access_key_secret()
security_token = cred.get_security_token()
cred_type = cred.get_type()
OIDC Role ARN
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
.
from alibabacloud_credentials.client import Client
from alibabacloud_credentials.models import Config
config = Config(
type='oidc_role_arn',
access_key_id='accessKeyId',
access_key_secret='accessKeySecret',
security_token='securityToken',
role_arn='roleArn',
oidc_provider_arn='oidcProviderArn',
oidc_token_file_path='/Users/xxx/xxx',
role_session_name='roleSessionName',
policy='policy',
role_session_expiration=3600
)
cred = Client(config)
access_key_id = cred.get_access_key_id()
access_key_secret = cred.get_access_key_secret()
security_token = cred.get_security_token()
cred_type = cred.get_type()
ECS RAM Role
Both ECS and ECI instances support binding instance RAM roles. When the Credentials tool is used in an instance, the RAM role bound to the instance will be automatically obtained, and the STS Token of the RAM role will be obtained by accessing the metadata service to complete the initialization of the credential client.
The instance metadata server supports two access modes: hardened mode and normal mode. The Credentials tool uses hardened mode (IMDSv2) by default to obtain access credentials. If an exception occurs when using hardened mode, you can set disable_imds_v1 to perform different exception handling logic:
-
When the value is false (default value), the normal mode will continue to be used to obtain access credentials.
-
When the value is true, it means that only hardened mode can be used to obtain access credentials, and an exception will be thrown.
Whether the server supports IMDSv2 depends on your configuration on the server.
from alibabacloud_credentials.client import Client
from alibabacloud_credentials.models import Config
config = Config(
type='ecs_ram_role',
role_name='roleName',
disable_imds_v1=True
)
cred = Client(config)
access_key_id = cred.get_access_key_id()
access_key_secret = cred.get_access_key_secret()
security_token = cred.get_security_token()
cred_type = cred.get_type()
Credentials URI
By specifying a credentials uri, get credential from the local or remote uri, the credential will be able to automatically request maintenance to keep it update.
from alibabacloud_credentials.client import Client
from alibabacloud_credentials.models import Config
config = Config(
type='credentials_uri',
credentials_uri='http://local_or_remote_uri/',
)
cred = Client(config)
access_key_id = cred.get_access_key_id()
access_key_secret = cred.get_access_key_secret()
security_token = cred.get_security_token()
cred_type = cred.get_type()
Bearer
If credential is required by the Cloud Call Centre (CCC), please apply for Bearer Token maintenance by yourself.
from alibabacloud_credentials.client import Client
from alibabacloud_credentials.models import Config
config = Config(
type='bearer',
bearer_token='bearerToken',
)
cred = Client(config)
access_key_id = cred.get_access_key_id()
access_key_secret = cred.get_access_key_secret()
security_token = cred.get_security_token()
cred_type = cred.get_type()
Use the default credential provider chain
from alibabacloud_credentials.client import Client as CredClient
from alibabacloud_ocr20191230.client import Client as OcrClient
from alibabacloud_ocr20191230.models import GetAsyncJobResultRequest
from alibabacloud_tea_rpc.models import Config
from alibabacloud_tea_util.models import RuntimeOptions
cred = CredClient()
config = Config(credential=cred)
client = OcrClient(config)
request = GetAsyncJobResultRequest(
job_id='<job_id>'
)
runtime_options = RuntimeOptions()
response = client.get_async_job_result(request, runtime_options)
The default credential provider chain looks for available credentials, with following order:
-
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.
-
Credentials File
If there is ~/.alibabacloud/credentials.ini default file (Windows shows C:\Users\USER_NAME\.alibabacloud\credentials.ini)
, 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.
[default]
enable = true
type = access_key
access_key_id = foo
access_key_secret = bar
[client1]
type = ecs_ram_role
role_name = EcsRamRoleTest
[client2]
enable = false
type = ram_role_arn
region_id = cn-test
policy = test
access_key_id = foo
access_key_secret = bar
role_arn = role_arn
role_session_name = session_name
[client3]
enable = false
type = oidc_role_arn
region_id = cn-test
policy = test
access_key_id = foo
access_key_secret = bar
role_arn = role_arn
oidc_provider_arn = oidc_provider_arn
oidc_token_file_path = /xxx/xxx
role_session_name = session_name
-
Instance RAM Role
If there is no credential information with a higher priority, the Credentials tool will obtain the value of ALIBABA_CLOUD_ECS_METADATA (ECS instance RAM role name) through the environment variable. If the value of this variable exists, the program will use the hardened mode (IMDSv2) to access the metadata service (Meta Data Server) of ECS to obtain the STS Token of the ECS instance RAM role as the default credential information. If an exception occurs when using the hardened mode, the normal mode will be used as a fallback to obtain access credentials. You can also set the environment variable ALIBABA_CLOUD_IMDSV1_DISABLED to perform different exception handling logic:
-
When the value is false, the normal mode will continue to obtain access credentials.
-
When the value is true, it means that only the hardened mode can be used to obtain access credentials, and an exception will be thrown.
Whether the server supports IMDSv2 depends on your configuration on the server.
-
Credentials URI
If the environment variable ALIBABA_CLOUD_CREDENTIALS_URI
is defined and not empty, the program will take the value of the environment variable as credentials uri to get the temporary Security credentials.
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.
References
License
Apache-2.0
Copyright (c) 2009-present, Alibaba Cloud All rights reserved.