From 3f2707cff12ff91c8808167d7466749b39a71d72 Mon Sep 17 00:00:00 2001 From: Alexis Tyler Date: Thu, 22 Apr 2021 13:14:53 +0930 Subject: [PATCH] fix: ensure we return an empty string when key file is missing --- app/core/modules/info/get-license.ts | 8 +++----- app/core/utils/misc/get-key-file.ts | 15 +++++++++++++++ app/core/watchers/key-file.ts | 14 +++----------- app/graphql/resolvers/query/registration.ts | 10 ++-------- package-lock.json | 9 +++++++++ package.json | 3 ++- 6 files changed, 34 insertions(+), 25 deletions(-) create mode 100644 app/core/utils/misc/get-key-file.ts diff --git a/app/core/modules/info/get-license.ts b/app/core/modules/info/get-license.ts index 0181ca67e..9a74af2e1 100644 --- a/app/core/modules/info/get-license.ts +++ b/app/core/modules/info/get-license.ts @@ -3,11 +3,10 @@ * Written by: Alexis Tyler */ -import fs from 'fs'; -import btoa from 'btoa'; import { varState } from '../../states'; import { CoreContext, CoreResult } from '../../types'; import { ensurePermission } from '../../utils'; +import { getKeyFile } from '../../utils/misc/get-key-file'; /** * Get server's license info @@ -28,8 +27,7 @@ export const getLicense = async function (context: CoreContext): Promise { + return btoa(keyFile).trim().replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, ''); + }).catch(() => ''); +}; diff --git a/app/core/watchers/key-file.ts b/app/core/watchers/key-file.ts index 56e51f5ce..3d13a784c 100644 --- a/app/core/watchers/key-file.ts +++ b/app/core/watchers/key-file.ts @@ -3,13 +3,12 @@ * Written by: Alexis Tyler */ -import btoa from 'btoa'; -import { promises } from 'fs'; import { dirname } from 'path'; import chokidar from 'chokidar'; import { coreLogger } from '../log'; import { varState } from '../states'; import { pubsub } from '../pubsub'; +import { getKeyFile } from '../utils/misc/get-key-file'; export const keyFile = () => { const watchers: chokidar.FSWatcher[] = []; @@ -39,14 +38,7 @@ export const keyFile = () => { } // Get key file - const file = await promises.readFile(fullPath, 'binary').catch(() => ''); - // If the file throws an error then bail - if (!file) { - return; - } - - // Convert binary to base64 with no "+", "/" or "=" - const parsedFile = btoa(file).trim().replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, ''); + const keyFile = await getKeyFile(); // Publish event // This will end up going to the graphql endpoint @@ -57,7 +49,7 @@ export const keyFile = () => { state: varState.data.regState, keyFile: { location: fullPath, - contents: parsedFile + contents: keyFile } } }).catch(error => { diff --git a/app/graphql/resolvers/query/registration.ts b/app/graphql/resolvers/query/registration.ts index 30c9a05e3..c9500bd19 100644 --- a/app/graphql/resolvers/query/registration.ts +++ b/app/graphql/resolvers/query/registration.ts @@ -3,10 +3,8 @@ * Written by: Alexis Tyler */ -import btoa from 'btoa'; -import { promises } from 'fs'; import { varState } from '../../../core/states'; -import { ensurePermission } from '../../../core/utils'; +import { ensurePermission, getKeyFile } from '../../../core/utils'; import { Context } from '../../schema/utils'; export default async (_: unknown, __: unknown, context: Context) => { @@ -16,17 +14,13 @@ export default async (_: unknown, __: unknown, context: Context) => { possession: 'any' }); - // Get key file - const file = await promises.readFile(varState.data.regFile, 'binary'); - const parsedFile = btoa(file).trim().replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, ''); - return { guid: varState.data.regGuid, type: varState.data.regTy, state: varState.data.regState, keyFile: { location: varState.data.regFile, - contents: parsedFile + contents: await getKeyFile() } }; }; diff --git a/package-lock.json b/package-lock.json index b86434814..b3c3ed672 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1136,6 +1136,15 @@ "@types/node": "*" } }, + "@types/btoa": { + "version": "1.2.3", + "resolved": "https://registry.npmjs.org/@types/btoa/-/btoa-1.2.3.tgz", + "integrity": "sha512-ANNCZICS/ofxhzUl8V1DniBJs+sFQ+Yg5am1ZwVEf/sxoKY/J2+h5Fuw3xUErlZ7eJLdgzukBjZwnsV6+/2Rmg==", + "dev": true, + "requires": { + "@types/node": "*" + } + }, "@types/cli-table": { "version": "0.3.0", "resolved": "https://registry.npmjs.org/@types/cli-table/-/cli-table-0.3.0.tgz", diff --git a/package.json b/package.json index 2a9f44de8..39dd46a37 100644 --- a/package.json +++ b/package.json @@ -135,6 +135,7 @@ "devDependencies": { "@commitlint/cli": "^11.0.0", "@commitlint/config-conventional": "^11.0.0", + "@types/btoa": "^1.2.3", "@types/cli-table": "^0.3.0", "@types/dockerode": "^3.2.2", "@types/lodash.get": "^4.4.6", @@ -268,4 +269,4 @@ "uuid-apikey", "xhr2" ] -} \ No newline at end of file +}