feat(server): TREK_S3_* env wiring — schema, s3 namespace, all-or-nothing boot rule

This commit is contained in:
jubnl
2026-08-19 20:27:50 +02:00
parent 8ae73a2b5c
commit bc4d70986f
9 changed files with 170 additions and 3 deletions
+45 -1
View File
@@ -1,5 +1,10 @@
import { deriveAll, type AppEnv, type RawEnv } from './derive';
import { envSchema } from './env.schema';
// storage-keys is the single source of truth for key/prefix shape — deliberately
// imported here rather than re-implementing the rule for TREK_S3_KEY_PREFIX. It
// is a plain module with no imports back into app-config, so this does not
// create a cycle.
import { isValidPrefix } from '../nest/storage/storage-keys';
export type { AppEnv, RawEnv };
@@ -30,7 +35,7 @@ export function validateEnvAtBoot(raw: RawEnv = process.env as RawEnv): void {
const key = String(issue.path[0]);
return ` - ${key}=${JSON.stringify(raw[key])}: ${issue.message}`;
});
lines.push(...managedPreconditions(raw));
lines.push(...managedPreconditions(raw), ...s3Preconditions(raw));
if (lines.length === 0) return;
console.error(`Invalid environment configuration:\n${lines.join('\n')}`);
throw new Error(
@@ -68,3 +73,42 @@ function managedPreconditions(raw: RawEnv): string[] {
return problems;
}
const S3_VARS = [
'TREK_S3_ENDPOINT',
'TREK_S3_BUCKET',
'TREK_S3_ACCESS_KEY_ID',
'TREK_S3_SECRET_ACCESS_KEY',
'TREK_S3_REGION',
'TREK_S3_KEY_PREFIX',
'TREK_S3_RETRIES',
'TREK_S3_TIMEOUT_MS',
] as const;
const S3_REQUIRED = S3_VARS.slice(0, 4);
/**
* All-or-nothing: any TREK_S3_* set means the operator wants off-box storage;
* booting with a partial set would silently run without it (fail closed).
* Prefix shape delegates to storage-keys — the single source of key rules.
*/
function s3Preconditions(raw: RawEnv): string[] {
const present = (key: string): boolean => Boolean(raw[key]?.trim());
if (!S3_VARS.some(present)) return [];
const problems: string[] = [];
for (const key of S3_REQUIRED) {
if (!present(key)) {
problems.push(
` - ${key} is unset: required when any TREK_S3_* variable is set, because a ` +
'half-configured s3 backend would boot without the off-box storage the operator asked for.',
);
}
}
const prefix = raw.TREK_S3_KEY_PREFIX?.trim().replace(/^\/+|\/+$/g, '');
if (prefix && !isValidPrefix(`${prefix}/`)) {
problems.push(
` - TREK_S3_KEY_PREFIX=${JSON.stringify(raw.TREK_S3_KEY_PREFIX)}: must be plain key segments ` +
'(no dot-segments, backslashes, or control characters).',
);
}
return problems;
}