-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig.ts
More file actions
172 lines (155 loc) · 6.06 KB
/
config.ts
File metadata and controls
172 lines (155 loc) · 6.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import * as v from "valibot";
import { randomBytes } from "node:crypto";
import { existsSync, readFileSync } from "node:fs";
import { mkdir, writeFile } from "node:fs/promises";
import { homedir } from "node:os";
import { dirname, join } from "node:path";
const EnvSchema = v.object({
XDG_CONFIG_HOME: v.optional(v.pipe(v.string(), v.minLength(1))),
XDG_STATE_HOME: v.optional(v.pipe(v.string(), v.minLength(1))),
});
export type OpsHarborControlPlaneEnv = v.InferOutput<typeof EnvSchema>;
export type OpsHarborControlPlaneConfig = {
port: number;
dbPath: string;
githubAppId?: string;
githubPrivateKey?: string;
githubWebhookSecret?: string;
githubTunnelDisabled: boolean;
githubTunnelHost?: string;
internalApiToken?: string;
githubApiUrl: string;
defaultAuthor?: string;
};
export type StoredOpsHarborControlPlaneConfig = {
port?: number;
dbPath?: string;
githubAppId?: string;
githubPrivateKey?: string;
githubWebhookSecret?: string;
githubTunnelDisabled?: boolean;
githubTunnelHost?: string;
internalApiToken?: string;
githubApiUrl?: string;
defaultAuthor?: string;
};
const StoredConfigSchema = v.object({
port: v.optional(v.pipe(v.number(), v.integer(), v.minValue(1), v.maxValue(65_535))),
dbPath: v.optional(v.pipe(v.string(), v.minLength(1))),
githubAppId: v.optional(v.pipe(v.string(), v.minLength(1))),
githubPrivateKey: v.optional(v.pipe(v.string(), v.minLength(1))),
githubWebhookSecret: v.optional(v.pipe(v.string(), v.minLength(1))),
githubTunnelDisabled: v.optional(v.boolean()),
githubTunnelHost: v.optional(v.pipe(v.string(), v.minLength(1))),
internalApiToken: v.optional(v.pipe(v.string(), v.minLength(1))),
githubApiUrl: v.optional(v.pipe(v.string(), v.minLength(1))),
defaultAuthor: v.optional(v.pipe(v.string(), v.minLength(1))),
});
export function readProcessEnv(env: NodeJS.ProcessEnv = process.env): OpsHarborControlPlaneEnv {
return v.parse(EnvSchema, env);
}
function configPath(env: OpsHarborControlPlaneEnv): string {
return join(
env.XDG_CONFIG_HOME ?? join(homedir(), ".config"),
"ops-harbor",
"control-plane.json",
);
}
function normalizeStoredConfig(
config: v.InferOutput<typeof StoredConfigSchema>,
): StoredOpsHarborControlPlaneConfig {
return {
...(config.port !== undefined ? { port: config.port } : {}),
...(config.dbPath ? { dbPath: config.dbPath } : {}),
...(config.githubAppId ? { githubAppId: config.githubAppId } : {}),
...(config.githubPrivateKey ? { githubPrivateKey: config.githubPrivateKey } : {}),
...(config.githubWebhookSecret ? { githubWebhookSecret: config.githubWebhookSecret } : {}),
...(config.githubTunnelDisabled !== undefined
? { githubTunnelDisabled: config.githubTunnelDisabled }
: {}),
...(config.githubTunnelHost ? { githubTunnelHost: config.githubTunnelHost } : {}),
...(config.internalApiToken ? { internalApiToken: config.internalApiToken } : {}),
...(config.githubApiUrl ? { githubApiUrl: config.githubApiUrl } : {}),
...(config.defaultAuthor ? { defaultAuthor: config.defaultAuthor } : {}),
};
}
export function loadStoredConfig(
env: OpsHarborControlPlaneEnv = readProcessEnv(),
): StoredOpsHarborControlPlaneConfig {
const path = configPath(env);
if (!existsSync(path)) {
return {};
}
try {
return normalizeStoredConfig(
v.parse(StoredConfigSchema, JSON.parse(readFileSync(path, "utf-8"))),
);
} catch (e) {
// ENOENT is guarded by existsSync above. Remaining errors are JSON parse
// or schema failures from a corrupt config file. Log and return defaults
// so the control plane can still start and write a fresh config.
console.error("[ops-harbor-control-plane] loadStoredConfig: failed to parse config:", e);
return {};
}
}
function generateWebhookSecret(): string {
return randomBytes(32).toString("hex");
}
function shouldProvisionWebhookSecret(config: StoredOpsHarborControlPlaneConfig): boolean {
return Boolean(config.githubAppId && config.githubPrivateKey);
}
export async function saveStoredConfig(
config: StoredOpsHarborControlPlaneConfig,
env: OpsHarborControlPlaneEnv = readProcessEnv(),
): Promise<StoredOpsHarborControlPlaneConfig> {
const current = loadStoredConfig(env);
const parsed = normalizeStoredConfig(v.parse(StoredConfigSchema, config));
const resolved: StoredOpsHarborControlPlaneConfig = {
...parsed,
...(shouldProvisionWebhookSecret(parsed)
? {
githubWebhookSecret:
parsed.githubWebhookSecret ?? current.githubWebhookSecret ?? generateWebhookSecret(),
}
: {}),
};
const path = configPath(env);
await mkdir(dirname(path), { recursive: true });
await writeFile(path, `${JSON.stringify(resolved, null, 2)}\n`, {
encoding: "utf-8",
mode: 0o600,
});
return resolved;
}
export async function ensureStoredConfigSecrets(
env: OpsHarborControlPlaneEnv = readProcessEnv(),
): Promise<StoredOpsHarborControlPlaneConfig> {
const stored = loadStoredConfig(env);
if (!shouldProvisionWebhookSecret(stored) || stored.githubWebhookSecret) {
return stored;
}
return saveStoredConfig(stored, env);
}
export function readConfig(
env: OpsHarborControlPlaneEnv = readProcessEnv(),
): OpsHarborControlPlaneConfig {
const stored = loadStoredConfig(env);
return {
port: stored.port ?? 4130,
dbPath:
stored.dbPath ??
join(
env.XDG_STATE_HOME ?? join(homedir(), ".local", "state"),
"ops-harbor",
"control-plane.db",
),
githubApiUrl: stored.githubApiUrl ?? "https://api.github.com",
githubTunnelDisabled: stored.githubTunnelDisabled ?? false,
...(stored.githubAppId ? { githubAppId: stored.githubAppId } : {}),
...(stored.githubPrivateKey ? { githubPrivateKey: stored.githubPrivateKey } : {}),
...(stored.githubWebhookSecret ? { githubWebhookSecret: stored.githubWebhookSecret } : {}),
...(stored.githubTunnelHost ? { githubTunnelHost: stored.githubTunnelHost } : {}),
...(stored.internalApiToken ? { internalApiToken: stored.internalApiToken } : {}),
...(stored.defaultAuthor ? { defaultAuthor: stored.defaultAuthor } : {}),
};
}