Skip to content

Commit

Permalink
handle encrypted config in presets
Browse files Browse the repository at this point in the history
  • Loading branch information
RahulGautamSingh committed Jan 6, 2025
1 parent bdd10d2 commit 673daeb
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
5 changes: 2 additions & 3 deletions lib/config/decrypt.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ describe('config/decrypt', () => {
expect(res).toMatchObject(config);
});

it('warns if no privateKey found', async () => {
it('warns if encryptedWarning is configured and encrypted object found', async () => {
config.encrypted = { a: '1' };
GlobalConfig.set({ encryptedWarning: 'text' });

process.env.RENOVATE_X_ENCRYPTED_STRICT = 'false';
const res = await decryptConfig(config, repository);

expect(logger.logger.once.warn).toHaveBeenCalledWith('text');
Expand All @@ -34,7 +34,6 @@ describe('config/decrypt', () => {

it('throws exception if encrypted found but no privateKey', async () => {
config.encrypted = { a: '1' };
process.env.RENOVATE_X_ENCRYPTED_STRICT = 'true';

await expect(decryptConfig(config, repository)).rejects.toThrow(
'config-validation',
Expand Down
6 changes: 3 additions & 3 deletions lib/config/decrypt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,14 @@ export async function decryptConfig(
}
}
} else {
if (process.env.RENOVATE_X_ENCRYPTED_STRICT === 'true') {
if (process.env.RENOVATE_X_ENCRYPTED_STRICT === 'false') {
logger.error('Found encrypted data but no privateKey');
} else {
const error = new Error(CONFIG_VALIDATION);
error.validationSource = 'config';
error.validationError = 'Encrypted config unsupported';
error.validationMessage = `This config contains an encrypted object at location \`$.${key}\` but no privateKey is configured. To support encrypted config, the Renovate administrator must configure a \`privateKey\` in Global Configuration.`;
throw error;
} else {
logger.error('Found encrypted data but no privateKey');
}
}
delete decryptedConfig.encrypted;
Expand Down
34 changes: 33 additions & 1 deletion lib/workers/global/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import type {
RenovateConfig,
RenovateRepository,
} from '../../config/types';
import { CONFIG_PRESETS_INVALID } from '../../constants/error-messages';
import {
CONFIG_PRESETS_INVALID,
CONFIG_VALIDATION,
} from '../../constants/error-messages';
import { pkg } from '../../expose.cjs';
import { instrument } from '../../instrumentation';
import { exportStats, finalizeReport } from '../../instrumentation/reporting';
Expand Down Expand Up @@ -145,6 +148,9 @@ export async function start(): Promise<number> {
);
}

// istanbul ignore next
checkEncryptedObject(config);

// Set allowedHeaders in case hostRules headers are configured in file config
GlobalConfig.set({
allowedHeaders: config.allowedHeaders,
Expand Down Expand Up @@ -248,3 +254,29 @@ export async function start(): Promise<number> {
}
return 0;
}

function checkEncryptedObject(config: AllConfig): void {
for (const [key, val] of Object.entries(config)) {
if (key === 'encrypted' && is.object(val)) {
if (!config.privateKey) {
if (process.env.RENOVATE_X_ENCRYPTED_STRICT === 'false') {
logger.error('Found encrypted data but no privateKey');

Check warning on line 263 in lib/workers/global/index.ts

View check run for this annotation

Codecov / codecov/patch

lib/workers/global/index.ts#L263

Added line #L263 was not covered by tests
} else {
const error = new Error(CONFIG_VALIDATION);
error.validationSource = 'config';
error.validationError = 'Encrypted config unsupported';
error.validationMessage = `This config contains an encrypted object at location \`$.${key}\` but no privateKey is configured. To support encrypted config, the Renovate administrator must configure a \`privateKey\` in Global Configuration.`;
throw error;

Check warning on line 269 in lib/workers/global/index.ts

View check run for this annotation

Codecov / codecov/patch

lib/workers/global/index.ts#L265-L269

Added lines #L265 - L269 were not covered by tests
}
}
} else if (is.array(val)) {
for (const item of val) {
if (is.object(item) && !is.array(item)) {
checkEncryptedObject(item as AllConfig);
}
}
} else if (is.object(val) && key !== 'content') {
checkEncryptedObject(val as AllConfig);

Check warning on line 279 in lib/workers/global/index.ts

View check run for this annotation

Codecov / codecov/patch

lib/workers/global/index.ts#L279

Added line #L279 was not covered by tests
}
}
}

0 comments on commit 673daeb

Please sign in to comment.