forked from mtusi2026/mtusi2026lab7
111 lines
3.8 KiB
TypeScript
111 lines
3.8 KiB
TypeScript
export type Principal = "ingest" | "query";
|
|
export interface Account {
|
|
user: string;
|
|
token: string;
|
|
}
|
|
export interface AccountPorts {
|
|
create: (
|
|
path: string,
|
|
body: { email: string; first_name: string; last_name: string },
|
|
) => Promise<{ status: number; data: unknown }>;
|
|
save: (principal: Principal, account: Account) => Promise<void>;
|
|
}
|
|
export function createAccountProvisioner(ports: AccountPorts) {
|
|
if (typeof ports.create !== "function" || typeof ports.save !== "function")
|
|
throw new Error("OPENOBSERVE_ACCOUNT_PORTS_REQUIRED");
|
|
return async (principal: Principal): Promise<Account> => {
|
|
if (principal !== "ingest" && principal !== "query")
|
|
throw new Error("OPENOBSERVE_ACCOUNT_INVALID_PRINCIPAL");
|
|
const email = `vp7-${principal}@example.invalid`;
|
|
const response = await ports.create("/api/default/service_accounts", {
|
|
email,
|
|
first_name: "VP7",
|
|
last_name: principal,
|
|
});
|
|
if (response.status !== 200)
|
|
throw new Error("OPENOBSERVE_ACCOUNT_CREATE_REJECTED");
|
|
const data = response.data as (Partial<Account> & { code?: number }) | null;
|
|
if (
|
|
data?.code !== 200 ||
|
|
data.user !== email ||
|
|
typeof data.token !== "string" ||
|
|
data.token.length < 1 ||
|
|
data.token.length > 4096 ||
|
|
/[\r\n\x00]/.test(data.token)
|
|
)
|
|
throw new Error("OPENOBSERVE_ACCOUNT_INVALID_RESPONSE");
|
|
const account = { user: data.user, token: data.token };
|
|
await ports.save(principal, account);
|
|
return account;
|
|
};
|
|
}
|
|
export interface AccountWorkflowPorts {
|
|
positive: (principal: Principal, account: Account) => Promise<number>;
|
|
deny: (principal: Principal, account: Account) => Promise<number>;
|
|
publish: (accounts: Record<Principal, Account>) => Promise<void>;
|
|
}
|
|
export function validateStoredAccount(
|
|
principal: Principal,
|
|
input: unknown,
|
|
): Account {
|
|
if (principal !== "ingest" && principal !== "query")
|
|
throw new Error("OPENOBSERVE_ACCOUNT_INVALID_PRINCIPAL");
|
|
const account = input as Partial<Account> | null;
|
|
if (
|
|
!account ||
|
|
account.user !== `vp7-${principal}@example.invalid` ||
|
|
typeof account.token !== "string" ||
|
|
account.token.length < 1 ||
|
|
account.token.length > 4096 ||
|
|
!/^[A-Za-z0-9!#%+,.\/:;=?@^_~-]+$/.test(account.token)
|
|
)
|
|
throw new Error("OPENOBSERVE_ACCOUNT_INVALID_RESPONSE");
|
|
return { user: account.user, token: account.token };
|
|
}
|
|
export function createAccountWorkflow(ports: AccountWorkflowPorts) {
|
|
if (
|
|
!ports ||
|
|
typeof ports.positive !== "function" ||
|
|
typeof ports.deny !== "function" ||
|
|
typeof ports.publish !== "function"
|
|
)
|
|
throw new Error("OPENOBSERVE_ACCOUNT_WORKFLOW_PORTS_REQUIRED");
|
|
const { positive, deny, publish } = ports;
|
|
return async (accounts: Record<Principal, Account>) => {
|
|
if (
|
|
!accounts ||
|
|
JSON.stringify(Object.keys(accounts).sort()) !==
|
|
JSON.stringify(["ingest", "query"])
|
|
)
|
|
throw new Error("OPENOBSERVE_ACCOUNT_SLOTS_INVALID");
|
|
const checked = {
|
|
ingest: validateStoredAccount("ingest", accounts.ingest),
|
|
query: validateStoredAccount("query", accounts.query),
|
|
};
|
|
const controls: {
|
|
principal: Principal;
|
|
positive_http: number;
|
|
deny_http: number;
|
|
}[] = [];
|
|
for (const name of ["ingest", "query"] as const) {
|
|
const success = await positive(name, { ...checked[name] });
|
|
if (success !== 200)
|
|
throw new Error(
|
|
`OPENOBSERVE_CREDENTIAL_${name.toUpperCase()}_POSITIVE_HTTP_${success}`,
|
|
);
|
|
const rejected = await deny(name, { ...checked[name] });
|
|
if (rejected !== 403)
|
|
throw new Error(
|
|
`OPENOBSERVE_CREDENTIAL_${name.toUpperCase()}_DENY_HTTP_${rejected}`,
|
|
);
|
|
controls.push({
|
|
principal: name,
|
|
positive_http: success,
|
|
deny_http: rejected,
|
|
});
|
|
}
|
|
await publish(checked);
|
|
return controls;
|
|
};
|
|
}
|