renovate/lib/util/host-rules.ts

153 lines
4.1 KiB
TypeScript
Raw Normal View History

import merge from 'deepmerge';
import { logger } from '../logger';
import { HostRule } from '../types';
import { clone } from './clone';
2020-05-01 16:03:48 +00:00
import * as sanitize from './sanitize';
import { parseUrl, validateUrl } from './url';
let hostRules: HostRule[] = [];
const legacyHostFields = ['hostName', 'domainName', 'baseUrl'];
2021-05-01 21:18:14 +00:00
export function add(params: HostRule): void {
const rule = clone(params);
const matchedFields = legacyHostFields.filter((field) => rule[field]);
if (matchedFields.length) {
logger.warn(
`Legacy hostRules fields ${matchedFields.join(
'+'
)} should be migrated to "matchHost"`
2021-05-01 21:18:14 +00:00
);
if (rule.matchHost || matchedFields.length > 1) {
matchedFields.push('matchHost');
throw new Error(
`hostRules cannot contain more than one host-matching field - use "matchHost" only. Found: [${matchedFields.join(
', '
)}]`
);
}
rule.matchHost = rule[matchedFields[0]];
delete rule[matchedFields[0]];
2018-07-06 05:26:36 +00:00
}
const confidentialFields = ['password', 'token'];
if (rule.matchHost) {
const parsedUrl = parseUrl(rule.matchHost);
rule.resolvedHost = parsedUrl?.hostname || rule.matchHost;
2021-04-20 08:52:38 +00:00
confidentialFields.forEach((field) => {
if (rule[field]) {
2021-04-20 08:52:38 +00:00
logger.debug(
`Adding ${field} authentication for ${rule.matchHost} to hostRules`
2021-04-20 08:52:38 +00:00
);
}
});
}
confidentialFields.forEach((field) => {
const secret = rule[field];
if (secret && secret.length > 3) {
sanitize.add(secret);
}
});
if (rule.username && rule.password) {
const secret = Buffer.from(`${rule.username}:${rule.password}`).toString(
'base64'
);
2019-09-12 10:48:31 +00:00
sanitize.add(secret);
}
hostRules.push(rule);
}
export interface HostRuleSearch {
hostType?: string;
2019-07-17 08:14:56 +00:00
url?: string;
}
function isEmptyRule(rule: HostRule): boolean {
return !rule.hostType && !rule.resolvedHost;
}
function isHostTypeRule(rule: HostRule): boolean {
return rule.hostType && !rule.resolvedHost;
}
function isHostOnlyRule(rule: HostRule): boolean {
return !rule.hostType && !!rule.matchHost;
}
function isMultiRule(rule: HostRule): boolean {
return rule.hostType && !!rule.resolvedHost;
}
function matchesHostType(rule: HostRule, search: HostRuleSearch): boolean {
return rule.hostType === search.hostType;
}
function matchesHost(rule: HostRule, search: HostRuleSearch): boolean {
if (validateUrl(rule.matchHost)) {
return search.url.startsWith(rule.matchHost);
}
const parsedUrl = parseUrl(search.url);
if (!parsedUrl?.hostname) {
return false;
}
const { hostname } = parsedUrl;
return hostname === rule.matchHost || hostname.endsWith(`.${rule.matchHost}`);
}
export function find(search: HostRuleSearch): HostRule {
if (!(search.hostType || search.url)) {
logger.warn({ search }, 'Invalid hostRules search');
return {};
2018-09-13 04:48:08 +00:00
}
let res = ({} as any) as HostRule;
// First, apply empty rule matches
hostRules
.filter((rule) => isEmptyRule(rule))
.forEach((rule) => {
res = merge(res, rule);
});
// Next, find hostType-only matches
hostRules
.filter((rule) => isHostTypeRule(rule) && matchesHostType(rule, search))
.forEach((rule) => {
res = merge(res, rule);
});
hostRules
.filter((rule) => isHostOnlyRule(rule) && matchesHost(rule, search))
.forEach((rule) => {
res = merge(res, rule);
});
// Finally, find combination matches
hostRules
.filter(
(rule) =>
isMultiRule(rule) &&
matchesHostType(rule, search) &&
matchesHost(rule, search)
)
.forEach((rule) => {
res = merge(res, rule);
});
delete res.hostType;
delete res.resolvedHost;
delete res.matchHost;
return res;
2018-07-06 05:26:36 +00:00
}
export function hosts({ hostType }: { hostType: string }): string[] {
return hostRules
.filter((rule) => rule.hostType === hostType)
.map((rule) => rule.resolvedHost)
.filter(Boolean);
2018-07-06 05:26:36 +00:00
}
2020-02-11 13:37:35 +00:00
export function findAll({ hostType }: { hostType: string }): HostRule[] {
return hostRules.filter((rule) => rule.hostType === hostType);
2020-02-11 13:37:35 +00:00
}
export function clear(): void {
2021-04-20 08:52:38 +00:00
logger.debug('Clearing hostRules');
hostRules = [];
2019-09-12 10:48:31 +00:00
sanitize.clear();
2018-07-06 05:26:36 +00:00
}