.
This commit is contained in:
1 parent
1453433b62
commit
da76af3826
65 files changed
+11089
-143
No files matched your search
@@ -34,6 +34,13 @@ function resolveEnvFile(mode: NodeEnvMode): string | null {
|
||||
return null;
|
||||
}
|
||||
|
||||
function loadDotenvFile(filePath: string, label: string): void {
|
||||
const result = dotenv.config({ path: filePath, override: false });
|
||||
if (result.error) {
|
||||
logger.warn(`[Env] Failed to load ${label} (${filePath}): ${result.error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
export function loadEnvironmentFromNodeEnv(): void {
|
||||
if (loaded) return;
|
||||
loaded = true;
|
||||
@@ -47,9 +54,5 @@ export function loadEnvironmentFromNodeEnv(): void {
|
||||
return;
|
||||
}
|
||||
|
||||
const result = dotenv.config({ path: envFile, override: false });
|
||||
if (result.error) {
|
||||
logger.warn(`[Env] Failed to load ${envFile}: ${result.error.message}`);
|
||||
return;
|
||||
}
|
||||
loadDotenvFile(envFile, `env-vars/${mode}.env`);
|
||||
}
|
||||
@@ -188,6 +188,14 @@ export class GarnetClient {
|
||||
await this.getClient().hSet(key, fields);
|
||||
}
|
||||
|
||||
async expireKey(key: string, ttlSeconds: number): Promise<boolean> {
|
||||
if (ttlSeconds <= 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return Boolean(await this.getClient().expire(key, ttlSeconds));
|
||||
}
|
||||
|
||||
async appendStream(
|
||||
key: string,
|
||||
fields: Record<string, string>,
|
||||
@@ -207,6 +215,31 @@ export class GarnetClient {
|
||||
return this.getClient().xAdd(key, "*", fields, trimOptions);
|
||||
}
|
||||
|
||||
async readStreamRange(
|
||||
key: string,
|
||||
start: string,
|
||||
end: string,
|
||||
options?: { count?: number | undefined },
|
||||
): Promise<StreamEntry[]> {
|
||||
const rangeOptions =
|
||||
options?.count && options.count > 0
|
||||
? {
|
||||
COUNT: options.count,
|
||||
}
|
||||
: undefined;
|
||||
const messages = await this.getClient().xRange(
|
||||
key,
|
||||
start,
|
||||
end,
|
||||
rangeOptions,
|
||||
);
|
||||
|
||||
return messages.map((message) => ({
|
||||
id: String(message.id),
|
||||
message: normalizeStreamFields(message.message),
|
||||
}));
|
||||
}
|
||||
|
||||
async createConsumerGroup(
|
||||
key: string,
|
||||
group: string,
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
export const KNOWN_SECRET_KEYS = [
|
||||
"INFISICAL_CLIENT_ID",
|
||||
"INFISICAL_CLIENT_SECRET",
|
||||
"NATS_ENDPOINT",
|
||||
"NATS_PASS",
|
||||
"NATS_USER",
|
||||
|
||||
@@ -7,7 +7,13 @@ export const DISCORD_MEMBER_LOOKUP_SUBJECT =
|
||||
"lolympus.discord.lookup-members";
|
||||
|
||||
export interface AlertPayload {
|
||||
kind: "latency" | "eval-score-drop" | "llm-error-rate" | "endpoint-down";
|
||||
kind:
|
||||
| "archive-stalled"
|
||||
| "endpoint-down"
|
||||
| "eval-drift"
|
||||
| "eval-score-drop"
|
||||
| "latency"
|
||||
| "llm-error-rate";
|
||||
metric: string;
|
||||
severity: "info" | "warn" | "critical";
|
||||
suggestedAction?: string | undefined;
|
||||
|
||||
+186
-11
@@ -1,10 +1,19 @@
|
||||
import { InfisicalSDK } from "@infisical/sdk";
|
||||
import { KNOWN_SECRET_KEYS, type KnownSecretKey } from "./known-secret-keys.js";
|
||||
|
||||
export type SecretMap = Partial<Record<KnownSecretKey, string>>;
|
||||
export type RawSecretMap = Record<string, string>;
|
||||
|
||||
const INFISICAL_SITE_URL = "https://secret-manager.dahyeon.us/";
|
||||
const KNOWN_SECRET_KEY_SET = new Set<string>(KNOWN_SECRET_KEYS);
|
||||
const IMDS_TOKEN_TTL_SECONDS = 60;
|
||||
const DEFAULT_IMDS_TIMEOUT_MS = 1_500;
|
||||
const DEFAULT_LOCAL_IMDS_BASE_URL = "http://127.0.0.1:8675";
|
||||
const DEFAULT_LINK_LOCAL_IMDS_BASE_URL = "http://169.254.169.254";
|
||||
const IMDS_TOKEN_TTL_HEADER = "X-aws-ec2-metadata-token-ttl-seconds";
|
||||
const IMDS_TOKEN_HEADER = "X-aws-ec2-metadata-token";
|
||||
const INFISICAL_CLIENT_ID_KEY = "INFISICAL_CLIENT_ID";
|
||||
const INFISICAL_CLIENT_SECRET_KEY = "INFISICAL_CLIENT_SECRET";
|
||||
|
||||
const infisicalClient = new InfisicalSDK({ siteUrl: INFISICAL_SITE_URL });
|
||||
|
||||
@@ -15,18 +24,10 @@ function isKnownSecretKey(value: string): value is KnownSecretKey {
|
||||
return KNOWN_SECRET_KEY_SET.has(value);
|
||||
}
|
||||
|
||||
function getInfisicalCredentials(): {
|
||||
interface InfisicalCredentials {
|
||||
clientSecret: string;
|
||||
clientId: string;
|
||||
projectId: string;
|
||||
} | null {
|
||||
const clientSecret = process.env["INFISICAL_CLIENT_SECRET"] ?? "";
|
||||
const clientId = process.env["INFISICAL_CLIENT_ID"] ?? "";
|
||||
const projectId = process.env["INFISICAL_PROJECT_ID"] ?? "";
|
||||
if (!clientSecret || !clientId || !projectId) {
|
||||
return null;
|
||||
}
|
||||
return { clientSecret, clientId, projectId };
|
||||
}
|
||||
|
||||
function getInfisicalEnvironment(): "dev" | "staging" | "prod" {
|
||||
@@ -35,6 +36,173 @@ function getInfisicalEnvironment(): "dev" | "staging" | "prod" {
|
||||
return "dev";
|
||||
}
|
||||
|
||||
function isDisabled(value: string | undefined): boolean {
|
||||
const normalized = value?.trim().toLowerCase();
|
||||
return normalized === "1" || normalized === "true" || normalized === "yes";
|
||||
}
|
||||
|
||||
function getImdsBaseUrls(): string[] {
|
||||
if (
|
||||
isDisabled(process.env["IMDS_DISABLED"]) ||
|
||||
isDisabled(process.env["METADATA_DISABLED"])
|
||||
) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const configured =
|
||||
process.env["METADATA_BASE_URL"]?.trim() ||
|
||||
process.env["IMDS_BASE_URL"]?.trim() ||
|
||||
process.env["MACHINE_ENV_BASE_URL"]?.trim();
|
||||
if (configured) {
|
||||
return [configured.replace(/\/+$/u, "")];
|
||||
}
|
||||
|
||||
const candidates =
|
||||
process.env["NODE_ENV"] === "prod"
|
||||
? [DEFAULT_LINK_LOCAL_IMDS_BASE_URL, DEFAULT_LOCAL_IMDS_BASE_URL]
|
||||
: [DEFAULT_LOCAL_IMDS_BASE_URL, DEFAULT_LINK_LOCAL_IMDS_BASE_URL];
|
||||
return [...new Set(candidates)];
|
||||
}
|
||||
|
||||
function getImdsTimeoutMs(): number {
|
||||
const raw =
|
||||
process.env["METADATA_TIMEOUT_MS"]?.trim() ||
|
||||
process.env["IMDS_TIMEOUT_MS"]?.trim();
|
||||
if (!raw) {
|
||||
return DEFAULT_IMDS_TIMEOUT_MS;
|
||||
}
|
||||
|
||||
const parsed = Number(raw);
|
||||
return Number.isFinite(parsed) && parsed > 0
|
||||
? parsed
|
||||
: DEFAULT_IMDS_TIMEOUT_MS;
|
||||
}
|
||||
|
||||
async function fetchWithTimeout(
|
||||
url: string,
|
||||
init: RequestInit,
|
||||
): Promise<Response> {
|
||||
const controller = new AbortController();
|
||||
const timeout = setTimeout(() => controller.abort(), getImdsTimeoutMs());
|
||||
|
||||
try {
|
||||
return await fetch(url, {
|
||||
...init,
|
||||
signal: controller.signal,
|
||||
});
|
||||
} finally {
|
||||
clearTimeout(timeout);
|
||||
}
|
||||
}
|
||||
|
||||
async function fetchImdsToken(baseUrl: string): Promise<string | null> {
|
||||
const response = await fetchWithTimeout(`${baseUrl}/latest/api/token`, {
|
||||
method: "PUT",
|
||||
headers: {
|
||||
[IMDS_TOKEN_TTL_HEADER]: String(IMDS_TOKEN_TTL_SECONDS),
|
||||
},
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const token = await response.text();
|
||||
return token.trim() || null;
|
||||
}
|
||||
|
||||
async function fetchImdsText(
|
||||
baseUrl: string,
|
||||
token: string,
|
||||
path: string,
|
||||
): Promise<string | null> {
|
||||
const response = await fetchWithTimeout(`${baseUrl}${path}`, {
|
||||
method: "GET",
|
||||
headers: {
|
||||
[IMDS_TOKEN_HEADER]: token,
|
||||
},
|
||||
});
|
||||
|
||||
if (response.status === 404) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!response.ok) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return response.text();
|
||||
}
|
||||
|
||||
async function fetchInfisicalCredentialsFromImds(): Promise<
|
||||
Partial<Pick<InfisicalCredentials, "clientId" | "clientSecret">>
|
||||
> {
|
||||
for (const baseUrl of getImdsBaseUrls()) {
|
||||
try {
|
||||
const token = await fetchImdsToken(baseUrl);
|
||||
if (!token) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const [clientId, clientSecret] = await Promise.all([
|
||||
fetchImdsText(
|
||||
baseUrl,
|
||||
token,
|
||||
`/latest/meta-data/secrets/${encodeURIComponent(INFISICAL_CLIENT_ID_KEY)}`,
|
||||
),
|
||||
fetchImdsText(
|
||||
baseUrl,
|
||||
token,
|
||||
`/latest/meta-data/secrets/${encodeURIComponent(INFISICAL_CLIENT_SECRET_KEY)}`,
|
||||
),
|
||||
]);
|
||||
|
||||
const trimmedClientId = clientId?.trim();
|
||||
const trimmedClientSecret = clientSecret?.trim();
|
||||
if (trimmedClientId || trimmedClientSecret) {
|
||||
const credentials: Partial<
|
||||
Pick<InfisicalCredentials, "clientId" | "clientSecret">
|
||||
> = {};
|
||||
if (trimmedClientId) {
|
||||
credentials.clientId = trimmedClientId;
|
||||
}
|
||||
if (trimmedClientSecret) {
|
||||
credentials.clientSecret = trimmedClientSecret;
|
||||
}
|
||||
return credentials;
|
||||
}
|
||||
} catch {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
async function getInfisicalCredentials(): Promise<InfisicalCredentials | null> {
|
||||
const projectId = process.env["INFISICAL_PROJECT_ID"]?.trim() ?? "";
|
||||
if (!projectId) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const envClientId = process.env[INFISICAL_CLIENT_ID_KEY]?.trim();
|
||||
const envClientSecret = process.env[INFISICAL_CLIENT_SECRET_KEY]?.trim();
|
||||
const imdsCredentials: Partial<
|
||||
Pick<InfisicalCredentials, "clientId" | "clientSecret">
|
||||
> =
|
||||
envClientId && envClientSecret
|
||||
? {}
|
||||
: await fetchInfisicalCredentialsFromImds();
|
||||
const clientId = envClientId || imdsCredentials.clientId || "";
|
||||
const clientSecret = envClientSecret || imdsCredentials.clientSecret || "";
|
||||
|
||||
if (!clientId || !clientSecret) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return { clientId, clientSecret, projectId };
|
||||
}
|
||||
|
||||
export function getSecret(key: KnownSecretKey): string | undefined {
|
||||
return secrets?.[key] ?? process.env[key];
|
||||
}
|
||||
@@ -43,11 +211,11 @@ export function getRawSecret(key: string): string | undefined {
|
||||
return rawSecrets?.[key] ?? process.env[key];
|
||||
}
|
||||
|
||||
const fetchSecrets = async (): Promise<{
|
||||
const fetchSecretsFromInfisical = async (): Promise<{
|
||||
known: SecretMap;
|
||||
raw: RawSecretMap;
|
||||
}> => {
|
||||
const credentials = getInfisicalCredentials();
|
||||
const credentials = await getInfisicalCredentials();
|
||||
if (!credentials) {
|
||||
return { known: {}, raw: {} };
|
||||
}
|
||||
@@ -79,6 +247,13 @@ const fetchSecrets = async (): Promise<{
|
||||
}
|
||||
};
|
||||
|
||||
const fetchSecrets = async (): Promise<{
|
||||
known: SecretMap;
|
||||
raw: RawSecretMap;
|
||||
}> => {
|
||||
return fetchSecretsFromInfisical();
|
||||
};
|
||||
|
||||
const loaded = await fetchSecrets();
|
||||
secrets = loaded.known;
|
||||
rawSecrets = loaded.raw;
|
||||
Reference in new issue
Block a user