forked from mtusi2026/mtusi2026lab7
179 lines
5.0 KiB
TypeScript
179 lines
5.0 KiB
TypeScript
import { readFile, writeFile } from "node:fs/promises";
|
|
import { resolve } from "node:path";
|
|
import { root, local, requireThat } from "../scripts/runtime";
|
|
import { containerIP } from "../scripts/runtime-inspect";
|
|
import { tlsProbe, expectDenied } from "./transport";
|
|
const http = (path: string) =>
|
|
`GET ${path} HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n`;
|
|
export async function auditMatrix(
|
|
scope: "full" | "cached" | "openobserve" = "full",
|
|
) {
|
|
const authorization = async (name: string) => {
|
|
const value = await readFile(resolve(local, "config", name), "utf8");
|
|
requireThat(
|
|
value.length <= 4096 && /^Basic [A-Za-z0-9+/]+={0,2}$/.test(value),
|
|
"INVALID_OPENOBSERVE_AUTH_HEADER_FILE",
|
|
);
|
|
return value;
|
|
};
|
|
const ingest =
|
|
scope !== "cached" ? await authorization("openobserve_ingest_auth") : "";
|
|
const query =
|
|
scope !== "cached" ? await authorization("openobserve_query_auth") : "";
|
|
const cases: {
|
|
service: string;
|
|
port: number;
|
|
role: string;
|
|
wrong: string;
|
|
data: string;
|
|
expected: RegExp;
|
|
}[] = [
|
|
{
|
|
service: "api1-gateway",
|
|
port: 8443,
|
|
role: "lb",
|
|
wrong: "worker",
|
|
data: http("/health/ready"),
|
|
expected: /HTTP\/1\.[01] 200/,
|
|
},
|
|
{
|
|
service: "api2-gateway",
|
|
port: 8443,
|
|
role: "lb",
|
|
wrong: "worker",
|
|
data: http("/health/ready"),
|
|
expected: /HTTP\/1\.[01] 200/,
|
|
},
|
|
{
|
|
service: "broker-gateway",
|
|
port: 6379,
|
|
role: "api",
|
|
wrong: "lb",
|
|
data: "*1\r\n$4\r\nPING\r\n",
|
|
expected: /NOAUTH/,
|
|
},
|
|
{
|
|
service: "collector-gateway",
|
|
port: 4318,
|
|
role: "api",
|
|
wrong: "lb",
|
|
data: http("/v1/traces"),
|
|
expected: /HTTP\/1\.[01] (405|404)/,
|
|
},
|
|
{
|
|
service: "collector-gateway",
|
|
port: 4318,
|
|
role: "worker",
|
|
wrong: "forbidden",
|
|
data: http("/v1/traces"),
|
|
expected: /HTTP\/1\.[01] (405|404)/,
|
|
},
|
|
{
|
|
service: "s3-gateway",
|
|
port: 9000,
|
|
role: "worker",
|
|
wrong: "lb",
|
|
data: http("/messenger-attachments"),
|
|
expected: /HTTP\/1\.[01] 403/,
|
|
},
|
|
{
|
|
service: "openobserve-gateway",
|
|
port: 5082,
|
|
role: "grafana",
|
|
wrong: "worker",
|
|
data: `GET /api/default/streams HTTP/1.1\r\nHost: localhost\r\nAuthorization: ${query}\r\nConnection: close\r\n\r\n`,
|
|
expected: /HTTP\/1\.[01] 200/,
|
|
},
|
|
{
|
|
service: "openobserve-gateway",
|
|
port: 5080,
|
|
role: "collector",
|
|
wrong: "grafana",
|
|
data: `POST /api/default/v1/traces HTTP/1.1\r\nHost: localhost\r\nAuthorization: ${ingest}\r\nContent-Type: application/x-protobuf\r\nContent-Length: 0\r\nConnection: close\r\n\r\n`,
|
|
expected: /HTTP\/1\.[01] 200/,
|
|
},
|
|
];
|
|
const results = [];
|
|
for (const c of cases.filter(
|
|
(c) =>
|
|
scope === "full" ||
|
|
(scope === "openobserve"
|
|
? c.service === "openobserve-gateway"
|
|
: [
|
|
"api1-gateway",
|
|
"api2-gateway",
|
|
"broker-gateway",
|
|
"s3-gateway",
|
|
].includes(c.service)),
|
|
)) {
|
|
const ip = await containerIP(c.service);
|
|
const positive = async () => {
|
|
const result = await tlsProbe(ip, c.port, c.service, c.role, c.data);
|
|
requireThat(
|
|
result.authorized && c.expected.test(result.data),
|
|
"MATRIX_POSITIVE_" + c.service.toUpperCase(),
|
|
);
|
|
};
|
|
await positive();
|
|
const missing = await expectDenied(
|
|
() => tlsProbe(ip, c.port, c.service, null, c.data),
|
|
"client",
|
|
);
|
|
const foreign = await expectDenied(
|
|
() => tlsProbe(ip, c.port, c.service, "foreign-lb", c.data),
|
|
"client",
|
|
);
|
|
const identity = await expectDenied(
|
|
() => tlsProbe(ip, c.port, c.service, c.wrong, c.data),
|
|
"role",
|
|
);
|
|
const wrongName = await expectDenied(
|
|
() => tlsProbe(ip, c.port, "invalid.example", c.role, c.data),
|
|
"trust",
|
|
);
|
|
const wrongCA = await expectDenied(
|
|
() => tlsProbe(ip, c.port, c.service, c.role, c.data, "foreign-lb"),
|
|
"trust",
|
|
);
|
|
await positive();
|
|
results.push({
|
|
service: c.service,
|
|
port: c.port,
|
|
permitted_role: c.role,
|
|
missing_client: missing,
|
|
foreign_client: foreign,
|
|
wrong_role: identity,
|
|
wrong_hostname: wrongName,
|
|
foreign_trust: wrongCA,
|
|
});
|
|
}
|
|
await writeFile(
|
|
resolve(
|
|
root,
|
|
scope === "openobserve"
|
|
? "evidence/openobserve/tls-matrix-" + crypto.randomUUID() + ".json"
|
|
: scope === "full"
|
|
? "evidence/security/tls-matrix.json"
|
|
: "evidence/preflight/tls-matrix.json",
|
|
),
|
|
JSON.stringify(
|
|
{
|
|
scope,
|
|
results,
|
|
positive_controls: "before and after every negative set",
|
|
timeouts_counted_as_denial: false,
|
|
},
|
|
null,
|
|
2,
|
|
) + "\n",
|
|
scope === "openobserve" ? { flag: "wx" } : {},
|
|
);
|
|
console.log(
|
|
scope === "openobserve"
|
|
? "PASS_OPENOBSERVE_INGEST_QUERY_MTLS_IDENTITIES_AND_SERVER_TRUST"
|
|
: scope === "full"
|
|
? "PASS_ALL_MATRIX_CLIENT_IDENTITIES_AND_SERVER_TRUST"
|
|
: "PASS_CACHED_CORE_MATRIX_ONLY",
|
|
);
|
|
}
|