Files
renovate/lib/workers/global/initialize.spec.ts
Jamie Tanna 09d2da2f1b feat(host-rules): add allowInternal
As part of follow-up changes, we're looking to tighten the way that
Renovate handles outgoing HTTP traffic.

Before we do this, we can introduce a new config option,
`hostRules[].allowInternal`, that allows a self-hosted administrator to
mark an internal host as "trusted" for access.

This wires in the new global-only config option, `allowInternal`, which
implies a new internal field `internalHostGrant`.

We'll add full documentation in a future commit, when the full
enforcement is in place, as there are some wider changes to enforce it
fully.

Co-authored-by: Claude Fable 5 <jamie.tanna+claude-code@mend.io>
Co-authored-by: Claude Opus 5 <jamie.tanna+claude-code@mend.io>
2026-09-10 14:52:01 +01:00

133 lines
4.7 KiB
TypeScript

import { git, logger } from '~test/util.ts';
import { GlobalConfig } from '../../config/global.ts';
import type { AllConfig, RenovateConfig } from '../../config/types.ts';
import { initPlatform as _initPlatform } from '../../modules/platform/index.ts';
import * as hostRules from '../../util/host-rules.ts';
import { globalInitialize } from './initialize.ts';
const initPlatform = vi.mocked(_initPlatform);
describe('workers/global/initialize', () => {
beforeEach(() => {
initPlatform.mockImplementationOnce((r) => Promise.resolve(r));
});
describe('checkVersions()', () => {
it('throws if invalid version', async () => {
const config: RenovateConfig = {};
git.validateGitVersion.mockResolvedValueOnce(false);
await expect(globalInitialize(config)).rejects.toThrow(
'Init: git version needs upgrading',
);
});
it('returns if valid git version', async () => {
const config: RenovateConfig = { prCommitsPerRunLimit: 2 };
git.validateGitVersion.mockResolvedValueOnce(true);
await expect(globalInitialize(config)).toResolve();
});
it('supports containerbase', async () => {
const config: AllConfig = { binarySource: 'docker' };
git.validateGitVersion.mockResolvedValueOnce(true);
await expect(globalInitialize(config)).toResolve();
});
it('supports containerbase cache dir', async () => {
const config: AllConfig = {
binarySource: 'docker',
containerbaseDir: '/tmp/containerbase',
};
git.validateGitVersion.mockResolvedValueOnce(true);
await expect(globalInitialize(config)).toResolve();
});
});
describe('setGlobalHostRules', () => {
beforeEach(() => {
hostRules.clear();
});
it('should have run before initPlatform', async () => {
const hostRule = {
hostType: 'github',
matchHost: 'https://some.github-enterprise.host',
httpsPrivateKey: 'private-key',
httpsCertificate: 'certificate',
httpsCertificateAuthority: 'certificate-authority',
};
initPlatform.mockReset();
initPlatform.mockImplementationOnce((r) => {
const foundRule = hostRules.find({
hostType: hostRule.hostType,
url: hostRule.matchHost,
});
expect(foundRule.httpsPrivateKey).toEqual(hostRule.httpsPrivateKey);
expect(foundRule.httpsCertificateAuthority).toEqual(
hostRule.httpsCertificateAuthority,
);
expect(foundRule.httpsCertificate).toEqual(hostRule.httpsCertificate);
return Promise.resolve(r);
});
const config: RenovateConfig = {
hostRules: [hostRule],
};
git.validateGitVersion.mockResolvedValueOnce(true);
await expect(globalInitialize(config)).toResolve();
});
it('filters headers against allowedHeaders', async () => {
// the self-hosted admin gets no exemption here either, as `applyHostRule` filters by header name whoever set it
GlobalConfig.set({ allowedHeaders: ['X-*'] });
const config: RenovateConfig = {
hostRules: [
{
matchHost: 'registry.example.com',
headers: { 'X-Allowed': 'yes', Authorization: 'from-admin' },
},
],
};
git.validateGitVersion.mockResolvedValueOnce(true);
await expect(globalInitialize(config)).toResolve();
expect(hostRules.find({ url: 'https://registry.example.com' })).toEqual({
headers: { 'X-Allowed': 'yes' },
internalHostGrant: { implicit: true },
});
expect(logger.logger.warn).toHaveBeenCalledWith(
{ denied: ['Authorization'] },
"Ignoring hostRules headers not permitted by this Renovate instance's `allowedHeaders`",
);
});
});
describe('configureThirdPartyLibraries()', () => {
beforeEach(() => {
vi.stubEnv('AWS_EC2_METADATA_DISABLED', undefined);
vi.stubEnv('METADATA_SERVER_DETECTION', undefined);
});
it('sets env vars when cloud metadata services disabled', async () => {
const config: RenovateConfig = { useCloudMetadataServices: false };
git.validateGitVersion.mockResolvedValueOnce(true);
await expect(globalInitialize(config)).toResolve();
expect(process.env.AWS_EC2_METADATA_DISABLED).toBe('true');
expect(process.env.METADATA_SERVER_DETECTION).toBe('none');
});
it('does not set env vars when cloud metadata services enabled', async () => {
const config: RenovateConfig = { useCloudMetadataServices: true };
git.validateGitVersion.mockResolvedValueOnce(true);
await expect(globalInitialize(config)).toResolve();
expect(process.env.AWS_EC2_METADATA_DISABLED).toBeUndefined();
expect(process.env.METADATA_SERVER_DETECTION).toBeUndefined();
});
});
});