From 183bd5da60ced86b05f1fd7fea86d6322dd86495 Mon Sep 17 00:00:00 2001 From: potados99 Date: Sat, 2 Oct 2021 22:25:30 +0900 Subject: [PATCH] =?UTF-8?q?chore:=20secret=EC=9D=80=20getSecret=EC=9C=BC?= =?UTF-8?q?=EB=A1=9C=20=EA=B0=80=EC=A0=B8=EC=98=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 3 ++ config.ts | 12 ++--- lib/common/utils/env.ts | 5 -- lib/common/utils/logSetupHelper.ts | 85 ------------------------------ lib/common/utils/logger.ts | 61 --------------------- lib/common/utils/nodeEnv.ts | 2 +- package-lock.json | 12 ++--- package.json | 2 +- 8 files changed, 17 insertions(+), 165 deletions(-) delete mode 100644 lib/common/utils/env.ts delete mode 100644 lib/common/utils/logSetupHelper.ts delete mode 100644 lib/common/utils/logger.ts diff --git a/README.md b/README.md index bd8b2d0..be556c0 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,9 @@ API 서버와 같은 DB를 공유하며, 웹 콘솔의 요청에 따라 운영 ## 업데이트 로그 +### 2021.10.2 v1.7.8 +- Secret 가져오는 방법 변경. + ### 2021.10.2 v1.7.7 - Docker 배포 설정. diff --git a/config.ts b/config.ts index 460428e..bdcf889 100644 --- a/config.ts +++ b/config.ts @@ -1,5 +1,5 @@ -import getEnv from './lib/common/utils/env'; import {isProduction} from './lib/common/utils/nodeEnv'; +import {getEnv, getSecret} from '@inu-cafeteria/backend-core'; export default { server: { @@ -24,15 +24,15 @@ export default { }, }, region: 'ap-northeast-2', - accessKeyId: getEnv('AWS_ACCESS_KEY_ID') || 'an_aws_id', - secretAccessKey: getEnv('AWS_SECRET_ACCESS_KEY') || 'blahblah', + accessKeyId: getSecret('AWS_ACCESS_KEY_ID') || 'an_aws_id', + secretAccessKey: getSecret('AWS_SECRET_ACCESS_KEY') || 'blahblah', }, auth: { - key: getEnv('JWT_SECRET_KEY') || 'whatever', + key: getSecret('JWT_SECRET_KEY') || 'whatever', expiresIn: '24h', - adminUsername: getEnv('ADMIN_ID') || 'potados', - adminPassword: getEnv('ADMIN_PW') || '1234', + adminUsername: getSecret('ADMIN_ID') || 'potados', + adminPassword: getSecret('ADMIN_PW') || '1234', }, }; diff --git a/lib/common/utils/env.ts b/lib/common/utils/env.ts deleted file mode 100644 index b63665b..0000000 --- a/lib/common/utils/env.ts +++ /dev/null @@ -1,5 +0,0 @@ -export default function getEnv(key: string, fallback: string | null = null): string | null { - const allEnvArgs = process.env; - - return allEnvArgs[key] || fallback; -} diff --git a/lib/common/utils/logSetupHelper.ts b/lib/common/utils/logSetupHelper.ts deleted file mode 100644 index ebb1f3c..0000000 --- a/lib/common/utils/logSetupHelper.ts +++ /dev/null @@ -1,85 +0,0 @@ -import config from '../../../config'; - -import WinstonCloudwatch from 'winston-cloudwatch'; -import stackTrace from 'stack-trace'; -import {setupAWS} from '../../external/cloud/aws'; -import winston from 'winston'; -import path from 'path'; - -const format = winston.format; - -function getConsoleFormat() { - return format.combine(format.colorize(), getFileFormat()); -} - -function getFileFormat() { - return format.printf((info) => `${info.timestamp} ${info.level}: ${info.message.trim()}`); -} - -function getConsoleTransport() { - return new winston.transports.Console({ - format: getConsoleFormat(), - }); -} - -function getCloudwatchTransport(prefix: string) { - setupAWS(); - - return new WinstonCloudwatch({ - logGroupName: config.aws.cloudwatch.logGroupName, - logStreamName: prefix, - }); -} - -function createLogger(transports: winston.transport[]) { - return winston.createLogger({ - level: 'verbose', - format: format.combine( - format.timestamp({ - format: 'YYYY-MM-DD HH:mm:ss', - }), - format.json() - ), - transports, - }); -} - -function stringify(object: any) { - if (object.stack) { - // For error objects. - return object.stack; - } else if (object.toString) { - // For those who can be string. - return object.toString(); - } else if (object) { - // For an object. - return JSON.stringify(object); - } else { - // Invalid. - return typeof object; - } -} - -function formatLog(message: string, showCaller = true) { - const caller = stackTrace.get()[2]; /* to get a real caller */ - - if (showCaller) { - return `${path.basename( - caller.getFileName() - )}:${caller.getFunctionName()}:${caller.getLineNumber()}:${caller.getColumnNumber()}: ${stringify( - message - )}`; - } else { - return `${stringify(message)}`; - } -} - -export default { - getConsoleTransport, - getCloudwatchTransport, - - createLogger, - - stringify, - formatLog, -}; diff --git a/lib/common/utils/logger.ts b/lib/common/utils/logger.ts deleted file mode 100644 index 314f886..0000000 --- a/lib/common/utils/logger.ts +++ /dev/null @@ -1,61 +0,0 @@ -import logSetupHelper from './logSetupHelper'; -import getEnv from './env'; -import winston from 'winston'; -import {isProduction} from './nodeEnv'; - -const consoleTransport = logSetupHelper.getConsoleTransport(); -const cloudwatchTransport = (prefix: string) => logSetupHelper.getCloudwatchTransport(prefix); -const combinedCloudwatchTransport = logSetupHelper.getCloudwatchTransport('combined'); - -function getLogger(prefix: string) { - const transports: winston.transport[] = [consoleTransport]; - - const productionTransports: winston.transport[] = [ - cloudwatchTransport(prefix), - combinedCloudwatchTransport, - ]; - - if (isProduction()) { - productionTransports.forEach((tp) => { - transports.push(tp); - }); - } - - const logger = logSetupHelper.createLogger(transports); - - logger.transports.forEach((transport) => { - transport.silent = getEnv('NODE_ENV') === 'test'; - }); - - return logger; -} - -const loggers = { - event: getLogger('event'), - verbose: getLogger('verbose'), - info: getLogger('info'), - warn: getLogger('warn'), - error: getLogger('error'), -}; - -export default { - event(message: any) { - loggers.event.info(logSetupHelper.formatLog(message, false)); - }, - - verbose(message: any) { - loggers.verbose.verbose(logSetupHelper.formatLog(message)); - }, - - info(message: any) { - loggers.info.info(logSetupHelper.formatLog(message)); - }, - - warn(message: any) { - loggers.warn.warn(logSetupHelper.formatLog(message)); - }, - - error(message: any) { - loggers.error.error(logSetupHelper.formatLog(message)); - }, -}; diff --git a/lib/common/utils/nodeEnv.ts b/lib/common/utils/nodeEnv.ts index 9ce4c82..7b0690d 100644 --- a/lib/common/utils/nodeEnv.ts +++ b/lib/common/utils/nodeEnv.ts @@ -1,4 +1,4 @@ -import getEnv from './env'; +import {getEnv} from '@inu-cafeteria/backend-core'; const envName = 'NODE_ENV'; diff --git a/package-lock.json b/package-lock.json index 3866d0b..bbf7989 100644 --- a/package-lock.json +++ b/package-lock.json @@ -512,9 +512,9 @@ } }, "@inu-cafeteria/backend-core": { - "version": "0.6.21", - "resolved": "https://registry.npmjs.org/@inu-cafeteria/backend-core/-/backend-core-0.6.21.tgz", - "integrity": "sha512-34WUBsJ1jPkKG6iKTieswSvUTBgRhOTDRXMPQrYvsWrqLzaOCvkDpLhgcNB68FaGA/8YZputPmZeF60Z52GH9g==", + "version": "0.7.1", + "resolved": "https://registry.npmjs.org/@inu-cafeteria/backend-core/-/backend-core-0.7.1.tgz", + "integrity": "sha512-AVx4buxfEZzi5bkdxFT7wczUbRuRk98y/Iu3cuVxhVw4onl3TZUeGdJZkn8wlO2YPHanOy+uEp44YEOPW8lhZA==", "requires": { "aws-sdk": "^2.983.0", "date-fns": "^2.23.0", @@ -533,9 +533,9 @@ }, "dependencies": { "aws-sdk": { - "version": "2.995.0", - "resolved": "https://registry.npmjs.org/aws-sdk/-/aws-sdk-2.995.0.tgz", - "integrity": "sha512-2tZv0GLI7SAhV6hGIhZArCvF2OcQNPrGNmtPtOhBWmovYJ5qIaibtFFtSee3O8jWX87KGDkBA5fgBMXrcG7i/w==", + "version": "2.999.0", + "resolved": "https://registry.npmjs.org/aws-sdk/-/aws-sdk-2.999.0.tgz", + "integrity": "sha512-OcnD7m+HCZv2qDzmS7TgABGf26mVPfIyah0Dgz7hHAxBtx78qFWi/s9U6BDxVBKWLg7OKWVHf0opiMG4ujteqg==", "requires": { "buffer": "4.9.2", "events": "1.1.1", diff --git a/package.json b/package.json index a60cad3..aa8859f 100644 --- a/package.json +++ b/package.json @@ -43,7 +43,7 @@ "typescript": "^4.3.5" }, "dependencies": { - "@inu-cafeteria/backend-core": "^0.6.21", + "@inu-cafeteria/backend-core": "^0.7.1", "aws-sdk": "^2.973.0", "cookie-parser": "^1.4.5", "cors": "^2.8.5",