$ npm install @serverless-devs/credential --save
import Credential from '@serverless-devs/credential';
const credential = new Credential(logger); // logger 选填,用于日志输出
import Credential from '@serverless-devs/credential';
const credential = new Credential();
const result = await credential.set(options);
参数 | 说明 | 类型 | 必填 | 默认值 |
---|---|---|---|---|
access | 新增密钥别名 | string | 否 | - |
force | 如果配置密钥别名已经存在,是否强制覆盖 | boolean | 否 | - |
其他参数 | 如果希望通过参数指定直接新增密钥(注意 |
- | 否 | - |
参数缺少出现交互,CiCd 环境下则报错
厂商密钥 Key:
Alibaba Cloud (alibaba): AccessKeyID、AccessKeySecret // AccountID 和 SecurityToken 选填
AWS (aws): AccessKeyID、SecretAccessKey
Azure (azure): KeyVaultName、TenantID、ClientID、ClientSecret
Baidu Cloud (baidu): AccessKeyID、SecretAccessKey
Google Cloud (google): PrivateKeyData
Huawei Cloud (huawei): AccessKeyID、SecretAccessKey
Tencent Cloud (tencent): AccountID、SecretID、SecretKey
可能返回 undefined 或者下面的数据结构
参数 | 说明 | 类型 |
---|---|---|
access | 密钥别名 | string |
credential | 创建的密钥对 | Record<string, string> |
const result = await credential.set()
const result = await credential.set({
access: 'alibaba',
AccessKeyID: 'LTAI5*****THK',
AccessKeySecret: 'RgeGJ*****FyMW',
AccountID: 'xxxxxx',
});
/**
result = {
access: 'alibaba',
credential: {
AccessKeyID: 'LTA*****THK',
AccessKeySecret: 'Rge*****yMW',
AccountID: '189****629',
}
*/
const result = await credential.set({
access: 'alias',
force: true,
AccessKeyID: 'AccessKeyID',
SecretAccessKey: 'SecretAccessKey',
})
// result = { access: 'alias', credential: { AccessKeyID: 'AccessKeyID', SecretAccessKey: 'SecretAccessKey' } }
const result = await credential.set({
access: 'azure',
force: true,
KeyVaultName: 'KeyVaultName',
TenantID: 'TenantID',
ClientID: 'ClientID',
ClientSecret: 'ClientSecret',
})
/**
result = {
access: 'azure',
credential: {
KeyVaultName: 'KeyVaultName',
TenantID: 'TenantID',
ClientID: 'ClientID',
ClientSecret: 'ClientSecret',
}
*/
const result = await credential.set({
access: 'google',
force: true,
PrivateKeyData: 'PrivateKeyData',
})
/**
result = {
access: 'google',
credential: {
PrivateKeyData: 'PrivateKeyData',
},
*/
const result = await credential.set({
access: 'tencent',
force: true,
AccountID: 'AccountID',
SecretID: 'SecretID',
SecretKey: 'SecretKey',
})
/**
result = {
access: 'tencent',
credential: {
AccountID: 'AccountID',
SecretID: 'SecretID',
SecretKey: 'SecretKey',
},
*/
const result = await credential.set({
access: 'custom',
force: true,
keyList: 'SecretID,SecretKey,Test',
infoList: 'abc,yyy,123',
})
/**
result = {
access: 'custom',
credential: {
Test: '123',
SecretID: 'abc',
SecretKey: 'yyy',
},
*/
import Credential from '@serverless-devs/credential';
const credential = new Credential();
const result = await credential.get('access');
import Credential from '@serverless-devs/credential';
const credential = new Credential();
await credential.getAll();
import Credential from '@serverless-devs/credential';
const credential = new Credential();
await credential.remove(access); // 如果不传入参数则出现交互,CiCd 环境下则报错
import Credential from '@serverless-devs/credential';
const credential = new Credential();
await credential.rename({ source, target }); // 如果不传入或者缺少参数则出现交互,CiCd 环境下则报错
import Credential from '@serverless-devs/credential';
const credential = new Credential();
await credential.default(access); // 如果不传入则出现交互,CiCd 环境下则报错
import Credential from '@serverless-devs/credential';
const credential = new Credential();
// 入参是通过 set 方法加密后的密钥键值对
const result = await credential.decrypt({
AccessKeyID: 'U2FsdGVkX1/vUriF3c62TFb2R8qgDP669jsyr6ilyD4=',
SecretAccessKey: 'U2FsdGVkX1/HygYh+uGL+wxglAQuxDzj/UUARUMW9NE=',
});
// result = {AccessKeyID: 'AccessKeyID', SecretAccessKey: 'SecretAccessKey' }