Skip to content

Commit 41c1ef5

Browse files
committed
fixup! feat(ncu-config): add support for partially encrypted config files
1 parent 8afd57b commit 41c1ef5

File tree

3 files changed

+22
-10
lines changed

3 files changed

+22
-10
lines changed

bin/ncu-config.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import yargs from 'yargs';
77
import { hideBin } from 'yargs/helpers';
88

99
import {
10-
getConfig, updateConfig, GLOBAL_CONFIG, PROJECT_CONFIG, LOCAL_CONFIG
10+
getConfig, updateConfig, GLOBAL_CONFIG, PROJECT_CONFIG, LOCAL_CONFIG,
11+
encryptValue
1112
} from '../lib/config.js';
1213
import { setVerbosityFromEnv } from '../lib/verbosity.js';
13-
import { runSync } from '../lib/run.js';
1414

1515
setVerbosityFromEnv();
1616

@@ -91,9 +91,7 @@ async function setHandler(argv) {
9191
console.warn('Passing sensitive config value via the shell is discouraged');
9292
}
9393
if (argv.encrypt) {
94-
argv.value = runSync('gpg', ['--default-recipient-self', '--encrypt', '--armor'], {
95-
input: argv.value
96-
});
94+
argv.value = await encryptValue(argv.value);
9795
}
9896
console.log(
9997
`Updating ${configName} configuration ` +

lib/auth.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { ClientRequest } from 'node:http';
33

44
import ghauth from 'ghauth';
55

6-
import { clearCachedConfig, getMergedConfig, getNcurcPath } from './config.js';
6+
import { clearCachedConfig, encryptValue, getMergedConfig, getNcurcPath } from './config.js';
77

88
export default lazy(auth);
99

@@ -83,7 +83,12 @@ async function auth(
8383
'see https://github.com/nodejs/node-core-utils/blob/main/README.md.\n');
8484
const credentials = await tryCreateGitHubToken(githubAuth);
8585
username = credentials.user;
86-
token = credentials.token;
86+
try {
87+
token = await encryptValue(credentials.token);
88+
} catch (err) {
89+
console.warn('Failed encrypt token, storing unencrypted instead');
90+
token = credentials.token;
91+
}
8792
const json = JSON.stringify({ username, token }, null, 2);
8893
fs.writeFileSync(getNcurcPath(), json, {
8994
mode: 0o600 /* owner read/write */

lib/config.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import os from 'node:os';
44
import { readJson, writeJson } from './file.js';
55
import { existsSync, mkdtempSync, rmSync } from 'node:fs';
66
import { spawnSync } from 'node:child_process';
7-
import { runSync } from './run.js';
7+
import { forceRunAsync, runSync } from './run.js';
88

99
export const GLOBAL_CONFIG = Symbol('globalConfig');
1010
export const PROJECT_CONFIG = Symbol('projectConfig');
@@ -33,6 +33,15 @@ export function clearCachedConfig() {
3333
mergedConfig = null;
3434
}
3535

36+
export async function encryptValue(input) {
37+
console.warn('Spawning gpg to encrypt the config value');
38+
return forceRunAsync(process.env.GPG_BIN || 'gpg', ['--default-recipient-self', '--encrypt', '--armor'], {
39+
captureStdout: true,
40+
ignoreFailure: false,
41+
spawnArgs: { input }
42+
});
43+
}
44+
3645
function setOwnProperty(target, key, value) {
3746
return Object.defineProperty(target, key, {
3847
__proto__: null,
@@ -42,13 +51,13 @@ function setOwnProperty(target, key, value) {
4251
});
4352
}
4453
function addEncryptedPropertyGetter(target, key, input) {
45-
if (input.startsWith('-----BEGIN PGP MESSAGE-----\n')) {
54+
if (input.startsWith?.('-----BEGIN PGP MESSAGE-----\n')) {
4655
return Object.defineProperty(target, key, {
4756
__proto__: null,
4857
configurable: true,
4958
get() {
5059
console.warn(`The config value for ${key} is encrypted, spawning gpg to decrypt it...`);
51-
const value = runSync('gpg', ['--decrypt'], { input });
60+
const value = runSync(process.env.GPG_BIN || 'gpg', ['--decrypt'], { input });
5261
setOwnProperty(target, key, value);
5362
return value;
5463
},

0 commit comments

Comments
 (0)