Skip to content

Commit

Permalink
feat: 🎸 #336 密钥获取逻辑升级
Browse files Browse the repository at this point in the history
  • Loading branch information
xsahxl committed Dec 1, 2021
1 parent d0387bc commit d5f32f3
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 31 deletions.
21 changes: 5 additions & 16 deletions src/common/credential/getCredential/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import getYamlContent from '../../getYamlContent';
import { logger } from '../../../libs/utils';
import { getRootHome } from '../../../libs/common';
import chalk from 'chalk';
import { transformInputs, trim, getServerlessDevsAccessFromEnv } from './utils'

const Crypto = require('crypto-js');

Expand Down Expand Up @@ -42,15 +43,6 @@ async function getCredential(...args: any[]) {
return result;
}

function transformInputs(inputs, result) {
if (!inputs || !result) return;
const { Alias } = result;
inputs.project = { ...inputs.project, access: Alias };
inputs.Project = { ...inputs.Project, accessAlias: Alias, AccessAlias: Alias };
inputs.credentials = result;
inputs.Credentials = result;
}

function formatValue(content: any, alias: string) {
const formatObj = decryptCredential(content[alias]);
if (Object.prototype.hasOwnProperty.call(formatObj, 'AccountID')) {
Expand All @@ -67,13 +59,6 @@ function formatValue(content: any, alias: string) {
};
}

function trim(obj) {
for (const key in obj) {
obj[key] = obj[key] && obj[key].replace(/(^\s*)|(\s*$)/g, '');
}
return obj;
}

/**
* @param access 可选参数,密钥的别名
* @param args 可选参数,接收设置密钥的key,如果不传新建密钥的时候,方法内部提供了设置密钥的相关模版
Expand All @@ -93,6 +78,10 @@ async function getCredentialWithAccess(access?: string, ...args: any[]) {
SecurityToken: get(process, 'env.SecurityToken'),
});
}
const serverlessDevsAccessFromEnv = getServerlessDevsAccessFromEnv()
if (serverlessDevsAccessFromEnv) {
return trim(serverlessDevsAccessFromEnv)
}

const accessContent = await getAccess(accessAlias);

Expand Down
25 changes: 25 additions & 0 deletions src/common/credential/getCredential/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { jsonparse } from '../../../libs/utils';

export function transformInputs(inputs, result) {
if (!inputs || !result) return;
const { Alias } = result;
inputs.project = { ...inputs.project, access: Alias };
inputs.Project = { ...inputs.Project, accessAlias: Alias, AccessAlias: Alias };
inputs.credentials = result;
inputs.Credentials = result;
}

export function trim(obj) {
for (const key in obj) {
if (typeof obj[key] === 'string') {
obj[key] = obj[key].replace(/(^\s*)|(\s*$)/g, '');
}
}
return obj;
}

export function getServerlessDevsAccessFromEnv() {
for (const key in process.env) {
if (key.endsWith('serverless_devs_access')) return jsonparse(process.env[key]);
}
}
30 changes: 15 additions & 15 deletions test/fixtures/crendential.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// import { getCredential } from '../../src/common';
import { getCredential } from '../../src/common';

// async function get() {
// const c = await getCredential();
// console.log(c);
// }
async function get() {
const c = await getCredential();
console.log(c);
}

// get();
get();

// async function set() {
// const c = await setCredential('AccountID', 'SecretID', 'SecretKey');
Expand All @@ -15,14 +15,14 @@
// set();


import {setKnownCredential} from '../../src/common';
// import {setKnownCredential} from '../../src/common';

const info = {
AccountID: 'U2FsdGVkX1+jAj7Kxp3X1lHwFSUtBoSqkpFXp/dYEB0=',
SecretID: 'U2FsdGVkX1/NKNJ6MDERFRhQ6GIukaUogeKcFJrhMRU=',
SecretKey: 'U2FsdGVkX1/OSprVoM65l3trkwg4CgAjtQZzt/wN798=',
};
// const info = {
// AccountID: 'U2FsdGVkX1+jAj7Kxp3X1lHwFSUtBoSqkpFXp/dYEB0=',
// SecretID: 'U2FsdGVkX1/NKNJ6MDERFRhQ6GIukaUogeKcFJrhMRU=',
// SecretKey: 'U2FsdGVkX1/OSprVoM65l3trkwg4CgAjtQZzt/wN798=',
// };

(async()=>{
await setKnownCredential(info, 'xxx')
})()
// (async()=>{
// await setKnownCredential(info, 'xxx')
// })()

0 comments on commit d5f32f3

Please sign in to comment.