English | 简体中文
Alibaba Cloud Credentials for TypeScript/Node.js 是帮助 Node.js 开发者管理凭据的工具。
本文将介绍如何获取和使用 Alibaba Cloud Credentials for TypeScript/Node.js。
- 请确保你的系统安装了不低于 12 版本的 Node.js 环境。
使用 npm
下载安装
npm install @alicloud/credentials
在您开始之前,您需要注册阿里云帐户并获取您的凭证。
通过用户信息管理设置 access_key,它们具有该账户完全的权限,请妥善保管。有时出于安全考虑,您不能把具有完全访问权限的主账户 AccessKey 交于一个项目的开发者使用,您可以创建RAM子账户并为子账户授权,使用RAM子用户的 AccessKey 来进行API调用。
import Credential, { Config } from '@alicloud/credentials';
const config: Config = {
type: 'access_key', // 凭证类型
accessKeyId: 'accessKeyId', // AccessKeyId
accessKeySecret: 'accessKeySecret', // AccessKeySecret
}
const cred = new Credential(config);
const {
accessKeyId,
accessKeySecret,
type
} = await cred.getCredential();
通过安全令牌服务(Security Token Service,简称 STS),申请临时安全凭证(Temporary Security Credentials,简称 TSC),创建临时安全凭证。
import Credential, { Config } from '@alicloud/credentials';
const config: Config = {
type: 'sts', // 凭证类型
accessKeyId: 'accessKeyId', // AccessKeyId
accessKeySecret: 'accessKeySecret', // AccessKeySecret
securityToken: 'securityToken', // STS Token
}
const cred = new Credential(config);
const {
accessKeyId,
accessKeySecret,
securityToken,
type
} = await cred.getCredential();
通过指定RAM角色,让凭证自动申请维护 STS Token。你可以通过为 Policy
赋值来限制获取到的 STS Token 的权限(如何创建策略)。
import Credential, { Config } from '@alicloud/credentials';
const config: Config = {
type: 'ram_role_arn', // 凭证类型
accessKeyId: 'accessKeyId', // AccessKeyId
accessKeySecret: 'accessKeySecret', // AccessKeySecret
roleArn: 'roleArn', // 格式: acs:ram::用户ID:role/角色名
roleSessionName: 'roleSessionName', // 角色会话名称
policy: 'policy', // 可选, 限制 STS Token 的权限
roleSessionExpiration: 3600, // 可选, 限制 STS Token 的有效时间
}
const cred = new Credential(config);
const {
accessKeyId,
accessKeySecret,
securityToken,
type
} = await cred.getCredential();
通过指定OIDC 角色,让凭证自动申请维护 STS Token。你可以通过为 Policy
赋值来限制获取到的 STS Token 的权限。
import Credential, { Config } from '@alicloud/credentials';
const config: Config = {
type: 'oidc_role_arn', // 凭证类型
roleArn: 'roleArn', // 格式: acs:ram::用户ID:role/角色名 roleArn 可不设,但需要通过设置 ALIBABA_CLOUD_ROLE_ARN 来代替
oidcProviderArn: 'oidcProviderArn', // 格式: acs:ram::用户Id:oidc-provider/角色名 oidcProviderArn 可不设,但需要通过设置 ALIBABA_CLOUD_OIDC_PROVIDER_ARN 来代替
oidcTokenFilePath: '/Users/xxx/xxx', // 格式: path OIDCTokenFilePath 可不设,但需要通过设置 ALIBABA_CLOUD_OIDC_TOKEN_FILE 来代替
roleSessionName: 'roleSessionName', // 角色会话名称
policy: 'policy', // 可选, 限制 STS Token 的权限
roleSessionExpiration: 3600, // 可选, 限制 STS Token 的有效时间
}
const cred = new Credential(config);
const {
accessKeyId,
accessKeySecret,
securityToken,
type
} = await cred.getCredential();
通过指定角色名称,让凭证自动申请维护 STS Token
import Credential, { Config } from '@alicloud/credentials';
const config: Config = {
type: 'ecs_ram_role', // 凭证类型
roleName: 'roleName', // 账户RoleName,非必填,不填则自动获取,建议设置,可以减少请求
disableIMDSv1: true, // 禁用 V1 兜底,获取安全令牌失败则报错,可以设置环境变量来开启:ALIBABA_CLOUD_IMDSV1_DISABLED=true
}
const cred = new Credential(config);
const {
accessKeyId,
accessKeySecret,
securityToken,
type
} = await cred.getCredential();
通过指定公钥ID和私钥文件,让凭证自动申请维护 AccessKey。仅支持日本站。 By specifying the public key ID and the private key file, the credential will be able to automatically request maintenance of the AccessKey before sending the request. Only Japan station is supported.
import Credential, { Config } from '@alicloud/credentials';
const config: Config = {
type: 'rsa_key_pair', // 凭证类型
privateKeyFile: 'privateKeyFile', // PrivateKey文件路径
publicKeyId: 'publicKeyId', // 账户PublicKeyId
}
const cred = new Credential(config);
const {
accessKeyId,
accessKeySecret,
securityToken,
type
} = await cred.getCredential();
通过本地或者远程的 URI,来获取凭证,并支持自动刷新。
import Credential, { Config } from '@alicloud/credentials';
const config: Config = {
type: 'credentials_uri',
credentialsURI: 'http://a_local_or_remote_address/' // credentialsURI 可不设,但需要通过设置 ALIBABA_CLOUD_CREDENTIALS_URI 来代替
};
const cred = new Credential(config);
const {
accessKeyId,
accessKeySecret,
securityToken,
type
} = await cred.getCredential();
该地址必须满足如下条件:
- 响应 200 状态码
- 响应体为如下的结构:
{
"Code": "Success",
"AccessKeySecret": "AccessKeySecret",
"AccessKeyId": "AccessKeyId",
"Expiration": "2021-09-26T03:46:38Z",
"SecurityToken": "SecurityToken"
}
如呼叫中心(CCC)需用此凭证,请自行申请维护 Bearer Token。
import Credential, { Config } from '@alicloud/credentials';
const config: Config = {
type: 'bearer', // 凭证类型
bearerToken: 'bearerToken', // BearerToken
}
const cred = new Credential(config);
let bearerToken: string = cred.getBearerToken();
let type: string = cred.getType();
如果你调用 new Credential()
时传入空, 将通过凭证提供链来为你获取凭证。
程序首先会在环境变量里寻找环境凭证,如果定义了 ALIBABA_CLOUD_ACCESS_KEY_ID
和 ALIBABA_CLOUD_ACCESS_KEY_SECRET
环境变量且不为空,程序将使用他们创建凭证。如否则,程序会在配置文件中加载和寻找凭证。
如果用户主目录存在默认文件 ~/.alibabacloud/credentials
(Windows 为 C:\Users\USER_NAME\.alibabacloud\credentials
),程序会自动创建指定类型和名称的凭证。默认文件可以不存在,但解析错误会抛出异常。不同的项目、工具之间可以共用这个配置文件,因为超出项目之外,也不会被意外提交到版本控制。Windows 上可以使用环境变量引用到主目录 %UserProfile%。类 Unix 的系统可以使用环境变量 $HOME 或 ~ (tilde)。 可以通过定义 ALIBABA_CLOUD_CREDENTIALS_FILE
环境变量修改默认文件的路径。
[default] # 默认凭证
type = access_key # 认证方式为 access_key
access_key_id = foo # access key id
access_key_secret = bar # access key secret
如果定义了环境变量 ALIBABA_CLOUD_ECS_METADATA
且不为空,程序会将该环境变量的值作为角色名称,请求 http://100.100.100.200/latest/meta-data/ram/security-credentials/
获取临时安全凭证。
如果定义了环境变量 ALIBABA_CLOUD_CREDENTIALS_URI
且不为空,程序会将该环境变量的值作为 credentials_uri 模式的地址,在调用时获取临时安全凭证。
Copyright (c) 2009-present, Alibaba Cloud All rights reserved.