mirror of
https://github.com/tale/headplane.git
synced 2026-08-19 01:16:18 +00:00
Implement path loading
Got the build working Maybe actually fix builds Copy drizzle as well
This commit is contained in:
committed by
Aarnav Tale
parent
7691f74d43
commit
5cfd9e411b
+90
-20
@@ -1,5 +1,5 @@
|
||||
import { constants, access, readFile } from 'node:fs/promises';
|
||||
import { env, exit } from 'node:process';
|
||||
import { env } from 'node:process';
|
||||
import { type } from 'arktype';
|
||||
import { configDotenv } from 'dotenv';
|
||||
import { parseDocument } from 'yaml';
|
||||
@@ -11,6 +11,28 @@ import {
|
||||
partialHeadplaneConfig,
|
||||
} from './schema';
|
||||
|
||||
// Custom error for config issues
|
||||
export class ConfigError extends Error {
|
||||
constructor(message: string) {
|
||||
super(message);
|
||||
this.name = 'ConfigError';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Interpolate environment variables in a string
|
||||
* Replaces ${VAR_NAME} patterns with the actual environment variable values
|
||||
*/
|
||||
function interpolateEnvVars(str: string): string {
|
||||
return str.replace(/\$\{([^}]+)\}/g, (match, varName) => {
|
||||
const value = env[varName];
|
||||
if (value === undefined) {
|
||||
throw new ConfigError(`Environment variable "${varName}" not found`);
|
||||
}
|
||||
return value;
|
||||
});
|
||||
}
|
||||
|
||||
// loadConfig is a has a lifetime of the entire application and is
|
||||
// used to load the configuration for Headplane. It is called once.
|
||||
//
|
||||
@@ -18,37 +40,83 @@ import {
|
||||
// But this may not be necessary as a use-case anyways
|
||||
export async function loadConfig({ loadEnv, path }: EnvOverrides) {
|
||||
log.debug('config', 'Loading configuration file: %s', path);
|
||||
const valid = await validateConfigPath(path);
|
||||
if (!valid) {
|
||||
exit(1);
|
||||
}
|
||||
await validateConfigPath(path);
|
||||
|
||||
const data = await loadConfigFile(path);
|
||||
if (!data) {
|
||||
exit(1);
|
||||
throw new ConfigError('Failed to load configuration file');
|
||||
}
|
||||
|
||||
let config = validateConfig({ ...data, debug: log.debugEnabled });
|
||||
if (!config) {
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (!loadEnv) {
|
||||
log.debug('config', 'Environment variable overrides are disabled');
|
||||
log.debug('config', 'This also disables the loading of a .env file');
|
||||
config = await loadSecretsFromFiles(config);
|
||||
log.debug('config', 'Loaded file-based secrets');
|
||||
return config;
|
||||
}
|
||||
|
||||
log.info('config', 'Loading a .env file (if available)');
|
||||
configDotenv({ override: true });
|
||||
config = coalesceEnv(config);
|
||||
if (!config) {
|
||||
exit(1);
|
||||
const merged = coalesceEnv(config);
|
||||
if (merged) config = merged;
|
||||
if (config.headscale && typeof config.headscale.config_path === 'string') {
|
||||
config.headscale.config_path = interpolateEnvVars(
|
||||
config.headscale.config_path,
|
||||
);
|
||||
}
|
||||
|
||||
config = await loadSecretsFromFiles(config);
|
||||
log.debug('config', 'Loaded file-based secrets');
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Recursively walks the config object; for any key in the whitelist of secret path keys,
|
||||
* reads that file and assigns its contents to the corresponding key
|
||||
* without the suffix, then removes the "_path" property.
|
||||
*/
|
||||
const SECRET_PATH_KEYS = new Set([
|
||||
'pre_authkey_path',
|
||||
'client_secret_path',
|
||||
'headscale_api_key_path',
|
||||
'cookie_secret_path',
|
||||
]);
|
||||
async function loadSecretsFromFiles<T extends object>(obj: T): Promise<T> {
|
||||
// Work with a Record so we can mutate/delete properties
|
||||
const record = obj as Record<string, unknown>;
|
||||
|
||||
for (const key of Object.keys(record)) {
|
||||
const val = record[key];
|
||||
|
||||
if (val && typeof val === 'object') {
|
||||
// recurse into nested objects
|
||||
record[key] = await loadSecretsFromFiles(val);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (SECRET_PATH_KEYS.has(key) && typeof val === 'string') {
|
||||
try {
|
||||
const path = interpolateEnvVars(val);
|
||||
const content = await readFile(path, 'utf8');
|
||||
const secretKey = key.slice(0, -5); // drop '_path'
|
||||
record[secretKey] = content.trim();
|
||||
delete record[key];
|
||||
log.debug('config', 'Loaded secret from %s → %s', val, secretKey);
|
||||
} catch (err) {
|
||||
if (err instanceof ConfigError) throw err;
|
||||
log.error('config', 'Failed to read secret file %s: %s', val, err);
|
||||
throw new ConfigError(`Failed to read secret file ${val}: ${err}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Cast back to the original T so callers keep their precise type
|
||||
return record as T;
|
||||
}
|
||||
|
||||
export async function hp_loadConfig() {
|
||||
// // OIDC Related Checks
|
||||
// if (config.oidc) {
|
||||
@@ -76,7 +144,9 @@ async function validateConfigPath(path: string) {
|
||||
} catch (error) {
|
||||
log.error('config', 'Unable to read a configuration file at %s', path);
|
||||
log.error('config', '%s', error);
|
||||
return false;
|
||||
throw new ConfigError(
|
||||
`Unable to read configuration file at ${path}: ${error}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -91,7 +161,7 @@ async function loadConfigFile(path: string): Promise<unknown> {
|
||||
log.error('config', ` - ${error.toString()}`);
|
||||
}
|
||||
|
||||
return false;
|
||||
throw new ConfigError(`Cannot parse configuration file at ${path}`);
|
||||
}
|
||||
|
||||
if (configYaml.warnings.length > 0) {
|
||||
@@ -109,7 +179,7 @@ async function loadConfigFile(path: string): Promise<unknown> {
|
||||
} catch (e) {
|
||||
log.error('config', 'Error reading configuration file at %s', path);
|
||||
log.error('config', '%s', e);
|
||||
return false;
|
||||
throw new ConfigError(`Error reading configuration file at ${path}: ${e}`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -117,14 +187,14 @@ export function validateConfig(config: unknown) {
|
||||
log.debug('config', 'Validating Headplane configuration');
|
||||
const result = headplaneConfig(config);
|
||||
if (result instanceof type.errors) {
|
||||
log.error('config', 'Error validating Headplane configuration:');
|
||||
const errorMessages = [];
|
||||
for (const [number, error] of result.entries()) {
|
||||
log.error('config', ` - (${number}): ${error.toString()}`);
|
||||
const errorMsg = error.toString();
|
||||
log.error('config', ` - (${number}): ${errorMsg}`);
|
||||
errorMessages.push(errorMsg);
|
||||
}
|
||||
|
||||
return;
|
||||
throw new ConfigError(errorMessages.join('\n'));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
+103
-17
@@ -20,8 +20,35 @@ const serverConfig = type({
|
||||
host: 'string.ip',
|
||||
port: type('string | number.integer').pipe((v) => Number(v)),
|
||||
data_path: 'string = "/var/lib/headplane/"',
|
||||
cookie_secret: '32 <= string <= 32',
|
||||
cookie_secret: '(32 <= string <= 32)?',
|
||||
cookie_secret_path: 'string?',
|
||||
cookie_secure: stringToBool,
|
||||
})
|
||||
.narrow((obj: Record<string, unknown>, ctx: any) => {
|
||||
const hasVal = obj.cookie_secret != null && `${obj.cookie_secret}` !== '';
|
||||
const hasPath =
|
||||
obj.cookie_secret_path != null && obj.cookie_secret_path !== '';
|
||||
if (hasVal && hasPath)
|
||||
return ctx.reject(
|
||||
`Only one of "cookie_secret" or "cookie_secret_path" may be set.`,
|
||||
);
|
||||
if (!hasVal && !hasPath)
|
||||
return ctx.reject(
|
||||
`Either "cookie_secret" or "cookie_secret_path" must be provided for cookie_secret.`,
|
||||
);
|
||||
return true;
|
||||
})
|
||||
.onDeepUndeclaredKey('reject');
|
||||
|
||||
const partialServerConfig = type({
|
||||
host: 'string.ip?',
|
||||
port: type('string | number.integer')
|
||||
.pipe((v) => Number(v))
|
||||
.optional(),
|
||||
data_path: 'string = "/var/lib/headplane/"',
|
||||
cookie_secret: '32 <= string <= 32?',
|
||||
cookie_secret_path: 'string?',
|
||||
cookie_secure: stringToBool.optional(),
|
||||
});
|
||||
|
||||
const oidcConfig = type({
|
||||
@@ -34,9 +61,41 @@ const oidcConfig = type({
|
||||
redirect_uri: 'string.url?',
|
||||
user_storage_file: 'string = "/var/lib/headplane/users.json"',
|
||||
disable_api_key_login: stringToBool,
|
||||
headscale_api_key: 'string',
|
||||
headscale_api_key: 'string?',
|
||||
headscale_api_key_path: 'string?',
|
||||
strict_validation: stringToBool.default(true),
|
||||
}).onDeepUndeclaredKey('reject');
|
||||
})
|
||||
.narrow((obj: Record<string, unknown>, ctx: any) => {
|
||||
const hasVal =
|
||||
obj.headscale_api_key != null && `${obj.headscale_api_key}` !== '';
|
||||
const hasPath =
|
||||
obj.headscale_api_key_path != null && obj.headscale_api_key_path !== '';
|
||||
if (hasVal && hasPath)
|
||||
return ctx.reject(
|
||||
`Only one of "headscale_api_key" or "headscale_api_key_path" may be set.`,
|
||||
);
|
||||
if (!hasVal && !hasPath)
|
||||
return ctx.reject(
|
||||
`Either "headscale_api_key" or "headscale_api_key_path" must be provided.`,
|
||||
);
|
||||
return true;
|
||||
})
|
||||
.onDeepUndeclaredKey('reject');
|
||||
|
||||
const partialOidcConfig = type({
|
||||
issuer: 'string.url?',
|
||||
client_id: 'string?',
|
||||
client_secret: 'string?',
|
||||
client_secret_path: 'string?',
|
||||
token_endpoint_auth_method:
|
||||
'"client_secret_basic" | "client_secret_post" | "client_secret_jwt"?',
|
||||
redirect_uri: 'string.url?',
|
||||
user_storage_file: 'string = "/var/lib/headplane/users.json"',
|
||||
disable_api_key_login: stringToBool.optional(),
|
||||
headscale_api_key: 'string?',
|
||||
headscale_api_key_path: 'string?',
|
||||
strict_validation: stringToBool.default(true),
|
||||
});
|
||||
|
||||
const headscaleConfig = type({
|
||||
url: type('string.url').pipe((v) => (v.endsWith('/') ? v.slice(0, -1) : v)),
|
||||
@@ -47,26 +106,53 @@ const headscaleConfig = type({
|
||||
dns_records_path: 'string?',
|
||||
}).onDeepUndeclaredKey('reject');
|
||||
|
||||
const partialHeadscaleConfig = type({
|
||||
url: type('string.url')
|
||||
.pipe((v) => (v.endsWith('/') ? v.slice(0, -1) : v))
|
||||
.optional(),
|
||||
tls_cert_path: 'string?',
|
||||
public_url: 'string.url?',
|
||||
config_path: 'string?',
|
||||
config_strict: stringToBool.optional(),
|
||||
dns_records_path: 'string?',
|
||||
});
|
||||
|
||||
const agentConfig = type({
|
||||
enabled: stringToBool.default(false),
|
||||
host_name: 'string = "headplane-agent"',
|
||||
pre_authkey: 'string = ""',
|
||||
pre_authkey: 'string?',
|
||||
pre_authkey_path: 'string?',
|
||||
cache_ttl: 'number.integer = 180000',
|
||||
cache_path: 'string = "/var/lib/headplane/agent_cache.json"',
|
||||
executable_path: 'string = "/usr/libexec/headplane/agent"',
|
||||
work_dir: 'string = "/var/lib/headplane/agent"',
|
||||
})
|
||||
.narrow((obj: Record<string, unknown>, ctx: any) => {
|
||||
const hasVal = obj.pre_authkey != null && `${obj.pre_authkey}` !== '';
|
||||
const hasPath = obj.pre_authkey_path != null && obj.pre_authkey_path !== '';
|
||||
if (hasVal && hasPath)
|
||||
return ctx.reject(
|
||||
`Only one of "pre_authkey" or "pre_authkey_path" may be set.`,
|
||||
);
|
||||
if (!hasVal && !hasPath)
|
||||
return ctx.reject(
|
||||
`Either "pre_authkey" or "pre_authkey_path" must be provided.`,
|
||||
);
|
||||
return true;
|
||||
})
|
||||
.onDeepUndeclaredKey('reject');
|
||||
|
||||
const partialAgentConfig = type({
|
||||
enabled: stringToBool.default(false),
|
||||
host_name: 'string = "headplane-agent"',
|
||||
pre_authkey: 'string?',
|
||||
pre_authkey_path: 'string?',
|
||||
cache_ttl: 'number.integer = 180000',
|
||||
cache_path: 'string = "/var/lib/headplane/agent_cache.json"',
|
||||
executable_path: 'string = "/usr/libexec/headplane/agent"',
|
||||
work_dir: 'string = "/var/lib/headplane/agent"',
|
||||
});
|
||||
|
||||
const partialAgentConfig = type({
|
||||
enabled: stringToBool,
|
||||
host_name: 'string | undefined',
|
||||
pre_authkey: 'string | undefined',
|
||||
cache_ttl: 'number.integer | undefined',
|
||||
cache_path: 'string | undefined',
|
||||
executable_path: 'string | undefined',
|
||||
work_dir: 'string | undefined',
|
||||
}).partial();
|
||||
|
||||
const dockerConfig = type({
|
||||
enabled: stringToBool,
|
||||
container_name: 'string = ""',
|
||||
@@ -115,10 +201,10 @@ export const headplaneConfig = type({
|
||||
|
||||
export const partialHeadplaneConfig = type({
|
||||
debug: stringToBool,
|
||||
server: serverConfig.partial(),
|
||||
'oidc?': oidcConfig.partial(),
|
||||
server: partialServerConfig,
|
||||
'oidc?': partialOidcConfig,
|
||||
'integration?': partialIntegrationConfig,
|
||||
headscale: headscaleConfig.partial(),
|
||||
headscale: partialHeadscaleConfig,
|
||||
}).partial();
|
||||
|
||||
export type HeadplaneConfig = typeof headplaneConfig.infer;
|
||||
|
||||
Reference in New Issue
Block a user