From 6db6e0dea5010b00c575c46347b46fe8c63cc1b1 Mon Sep 17 00:00:00 2001 From: Nils Hirsekorn Date: Thu, 11 Jan 2024 14:13:10 +0100 Subject: [PATCH] provide functionality to dump certificates from app server (#53) * provide functionality to dump certificates from app server * fix tests * respect app instance & applied feedback * default appInstance in parameter --------- Co-authored-by: d060338 --- docs/index.md | 1 + src/cli.js | 1 + src/cliOptions.js | 7 +++++++ src/submodules/serverDiagnostic.js | 11 +++++++++++ 4 files changed, 20 insertions(+) diff --git a/docs/index.md b/docs/index.md index 739aa93..f1cbfd5 100644 --- a/docs/index.md +++ b/docs/index.md @@ -103,6 +103,7 @@ commands: ~ srv --server-info call server /info ~ srvd --server-debug [APP_NAME] [APP_INSTANCE] open ssh tunnel to port /info {debugPort} ~ srvenv --server-env [APP_NAME] dump system environment +~ srvcrt --server-certificates [APP_NAME] [APP_INSTANCE] dump instance certificates * --server-start-debugger [APP_NAME] [APP_INSTANCE] start debugger on server node process ... [APP_NAME] run server commands for a specific app ... [APP_INSTANCE] tunnel to specific app instance, fallback to 0 diff --git a/src/cli.js b/src/cli.js index 0b821df..290c1cd 100644 --- a/src/cli.js +++ b/src/cli.js @@ -78,6 +78,7 @@ commands: ~ srv --server-info call server /info ~ srvd --server-debug [APP_NAME] [APP_INSTANCE] open ssh tunnel to port /info {debugPort} ~ srvenv --server-env [APP_NAME] dump system environment +~ srvcrt --server-certificates [APP_NAME] [APP_INSTANCE] dump instance certificates * --server-start-debugger [APP_NAME] [APP_INSTANCE] start debugger on server node process ... [APP_NAME] run server commands for a specific app ... [APP_INSTANCE] tunnel to specific app instance, fallback to 0 diff --git a/src/cliOptions.js b/src/cliOptions.js index 4f85297..2623325 100644 --- a/src/cliOptions.js +++ b/src/cliOptions.js @@ -249,6 +249,13 @@ module.exports = { useCache: false, readonly: true, }, + SRV_CERTIFICATES: { + commandVariants: ["srvcrt", "--server-certificates"], + optionalPassArgs: [PASS_ARG.APP_NAME, PASS_ARG.APP_INSTANCE], + callback: srv.serverCertificates, + useCache: false, + readonly: true, + }, SRV_START_DEBUGGER: { commandVariants: ["--server-start-debugger"], optionalPassArgs: [PASS_ARG.APP_NAME, PASS_ARG.APP_INSTANCE], diff --git a/src/submodules/serverDiagnostic.js b/src/submodules/serverDiagnostic.js index 235e82c..c2f9284 100644 --- a/src/submodules/serverDiagnostic.js +++ b/src/submodules/serverDiagnostic.js @@ -82,6 +82,16 @@ const serverEnvironment = async (context, [appName]) => { ); console.log(`saved system environment to ${DEFAULT_ENV_FILENAME}`); }; +const serverCertificates = async (context, [appName, appInstance = 0]) => { + const { cfSsh, cfAppName } = appName ? await context.getAppNameInfoCached(appName) : await context.getSrvInfo(); + const dumpFile = async (cfFilename, localFilename) => { + const [file] = await cfSsh({ command: `cat ${cfFilename}`, appInstance }); + writeFileSync(localFilename, file); + }; + await dumpFile("$CF_INSTANCE_CERT", `certificate-${cfAppName}-${appInstance}.crt`); + await dumpFile("$CF_INSTANCE_KEY", `certificate-${cfAppName}-${appInstance}.key`); + console.log("saved instance certificates"); +}; const serverStartDebugger = async (context, [appName, appInstance]) => { const { cfSsh } = appName ? await context.getAppNameInfoCached(appName) : await context.getSrvInfo(); @@ -92,5 +102,6 @@ module.exports = { serverInfo, serverDebug, serverEnvironment, + serverCertificates, serverStartDebugger, };