Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
# Conflicts:
#	package.json
  • Loading branch information
anycodes committed Jun 18, 2021
2 parents 4a024cf + dcfd753 commit e9c87ef
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 33 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ download
dist
lib
.vscode
.package-lock.json
.package-lock.json
.s.lock
43 changes: 24 additions & 19 deletions src/common/credential/getCredential/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import get from 'lodash.get';
import os from 'os';
import path from 'path';
import getYamlContent from '../../getYamlContent';
import { Logger } from '../../../logger';
const Crypto = require('crypto-js');
const logger = new Logger('S-CORE');

export function decryptCredential(info: { [key: string]: any }) {
const cloneInfo = Object.assign({}, info);
Expand All @@ -16,6 +18,22 @@ export function decryptCredential(info: { [key: string]: any }) {
return cloneInfo;
}

function formatValue(content: any, alias: string) {
const formatObj = decryptCredential(content[alias]);
if (Object.prototype.hasOwnProperty.call(formatObj, 'AccountID')) {
return {
Alias: alias,
...formatObj,
AccountID:
typeof formatObj.AccountID === 'string' ? formatObj.AccountID : String(formatObj.AccountID),
};
}
return {
Alias: alias,
...formatObj,
};
}

/**
* @param access 可选参数,密钥的别名
* @param args 可选参数,接收设置密钥的key,如果不传新建密钥的时候,方法内部提供了设置密钥的相关模版
Expand Down Expand Up @@ -48,21 +66,9 @@ async function getCredential(access?: string, ...args: any[]) {

// 找到已经创建过的密钥,直接返回密钥信息
if (accessKeys.length > 0) {
const formatObj = decryptCredential(accessContent[accessAlias]);
if (Object.prototype.hasOwnProperty.call(formatObj, 'AccountID')) {
return {
Alias: accessAlias,
...formatObj,
AccountID:
typeof formatObj.AccountID === 'string'
? formatObj.AccountID
: String(formatObj.AccountID),
};
}
return {
Alias: accessAlias,
...formatObj,
};
const result = formatValue(accessContent, accessAlias);
logger.debug(`密钥信息: ${JSON.stringify(result, null, 2)}`);
return result;
}
const userInfo = await getYamlContent(path.join(os.homedir(), '.s/access.yaml'));

Expand Down Expand Up @@ -94,10 +100,9 @@ async function getCredential(access?: string, ...args: any[]) {
if (selectAccess === 'create') {
return setCredential(...args);
}
return {
Alias: selectAccess,
...userInfo[selectAccess],
};
const result = formatValue(userInfo, selectAccess);
logger.debug(`密钥信息: ${JSON.stringify(result, null, 2)}`);
return result;
}

export default getCredential;
62 changes: 57 additions & 5 deletions src/logger/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,49 @@ export interface ILogger {
const args = minimist(process.argv.slice(2));
const enableDebug = args.debug || process.env?.temp_params?.includes('--debug');

function getSecretValue(val: string) {
const [key, value] = val.split(': ');
const valueLength = value.length;
if (valueLength < 6) return val;

let formatVal = value.slice(0, 4);
for (let i = 0; i < valueLength - 10; i++) {
formatVal += '*';
}
formatVal += value.slice(valueLength - 6, valueLength);
return `${key}: ${formatVal}`;
}

function secretCredentials(...data: any[]) {
const list = [];
for (const iterator of data) {
let str = iterator;
if (iterator.includes('AccountID')) {
const reg = /"AccountID(.*?)\n/g;
let arr = iterator.match(reg);
for (const item of arr) {
str = str.replace(item, getSecretValue(item));
}
}
if (iterator.includes('AccessKeyID')) {
const reg = /"AccessKeyID(.*?)\n/g;
let arr = iterator.match(reg);
for (const item of arr) {
str = str.replace(item, getSecretValue(item));
}
}
if (iterator.includes('AccessKeySecret')) {
const reg = /"AccessKeySecret(.*?)\n/g;
let arr = iterator.match(reg);
for (const item of arr) {
str = str.replace(item, getSecretValue(item));
}
}
list.push(str);
}
return list;
}

export const logger = (name: string): ILogger => {
const loggers = new MyLogger(name);
const stdLog = loggers.appenders.set('std-log', {
Expand All @@ -35,7 +78,7 @@ export const logger = (name: string): ILogger => {
levels: (enableDebug ? ['debug'] : []).concat(['info', 'warn', 'error', 'fatal']),
});

try{
try {
stdLog.set('app-file', {
type: 'file',
filename: `${S_ROOT_HOME}/logs/app.log`,
Expand All @@ -56,14 +99,22 @@ export const logger = (name: string): ILogger => {
separator: ',',
},
});
}catch (e){

}
} catch (e) {}

// @ts-ignore
loggers.log = (message: any, color?: LogColor) => {
return process.stdout.write(`${color ? chalk[color](message) : message}\n`);
};

// @ts-ignore
loggers.mydebug = loggers.debug;
loggers.debug = (...data: any[]): MyLogger => {
const list = secretCredentials(...data);
// @ts-ignore
loggers.mydebug(...list);
return loggers;
};

// @ts-ignore
return loggers;
};
Expand All @@ -82,7 +133,8 @@ export class Logger {
static debug(name: string, data) {
if (enableDebug) {
$log.name = name;
$log.debug(data);
const list = secretCredentials(data);
$log.debug(...list);
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/crendential.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getCredential } from '../../src/common';

async function get() {
const c = await getCredential('error1', 'AccountID', 'AccessKeyID', 'AccessKeySecret');
const c = await getCredential('err', 'AccountID', 'AccessKeyID', 'AccessKeySecret');
console.log(c);
}

Expand Down
36 changes: 29 additions & 7 deletions test/fixtures/logger.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,33 @@
import { HLogger } from '../../src';
import { HLogger, getCredential } from '../../src';
import { Logger } from '../../src/logger';

const input = {
Credentials: {
Alias: 'default',
AccountID: '1694024725952210',
AccessKeyID: 'LTAI4FztYLSCBjpgZMtMjtrY',
AccessKeySecret: 'OcoGWYshV2moQUUv6PPs9pHFbYevTL',
},
credentials: {
Alias: 'default',
AccountID: '1694024725952210',
AccessKeyID: 'LTAI4FztYLSCBjpgZMtMjtrY',
AccessKeySecret: 'OcoGWYshV2moQUUv6PPs9pHFbYevTL',
},
appName: 'appName',
Path: {
ConfigPath: '/Users/shihuali/workspace/jamstack-api/example/s.yaml',
},
};

class LoggerDemo {
@HLogger('S-CORE') logger;

getDefaultLog() {
this.logger.debug('abc');
async getDefaultLog() {
await getCredential('default');

this.logger.debug(`logger1密钥信息: ${JSON.stringify(input, null, 2)}`);
this.logger.debug(JSON.stringify(input, null, 2), 'xxx');
this.logger.info('abc');
this.logger.warn('abc');
this.logger.error('abc');
Expand Down Expand Up @@ -56,8 +78,8 @@ class LoggerDemo {
}

const demo = new LoggerDemo();
// demo.log();
demo.getDebugMsg();
demo.log();
// demo.getDefaultLog();
// demo.getInfoMsg();

// Logger.info('S-CORE', 'dankun');
Expand All @@ -84,7 +106,7 @@ demo.getDebugMsg();
//
// console.log(LogLevelEnum[2]);

Logger.debug('xx', 'abc');
Logger.debug('xx', `logger密钥信息: ${JSON.stringify(input, null, 2)}`);

const l = new Logger();
l.debug('abcxx');
l.debug(`logger密钥信息: ${JSON.stringify(input, null, 2)}`);

0 comments on commit e9c87ef

Please sign in to comment.