Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
+499
@@ -0,0 +1,499 @@
|
||||
import { getReadDbClient, getWriteDbClient, type DbClient } from "./client.server.js";
|
||||
import {
|
||||
PORTAL_RLS_CROSS_ROLE_CAPABILITY_CONTRACT,
|
||||
PORTAL_RLS_COLUMN_PRIVILEGE_CONTRACT,
|
||||
PORTAL_RLS_POLICY_CONTRACT,
|
||||
PORTAL_RLS_PRIVATE_DEFINER_HELPER_SIGNATURES,
|
||||
PORTAL_RLS_PRIVATE_HELPER_SIGNATURES,
|
||||
PORTAL_RLS_RESOLVER_SIGNATURES,
|
||||
PORTAL_RLS_RUNTIME_ROLE,
|
||||
PORTAL_RLS_TABLE_PRIVILEGE_CONTRACT,
|
||||
} from "./portal-rls-runtime-contract.js";
|
||||
import { resolveRlsRuntimeMode, type RlsRuntimeMode } from "./rls-runtime-readiness.server.js";
|
||||
|
||||
type PortalRlsProbeClient = Pick<DbClient, "$queryRawUnsafe">;
|
||||
|
||||
export type PortalRlsRuntimeProbe = {
|
||||
sessionRoleName: string;
|
||||
roleName: string;
|
||||
roleExists: boolean;
|
||||
canLogin: boolean;
|
||||
isSuperuser: boolean;
|
||||
canCreateDb: boolean;
|
||||
canCreateRole: boolean;
|
||||
inherits: boolean;
|
||||
replicates: boolean;
|
||||
bypassesRls: boolean;
|
||||
membershipCount: number;
|
||||
hasSchemaUsage: boolean;
|
||||
hasSchemaCreate: boolean;
|
||||
expectedRlsMismatchCount: number;
|
||||
expectedPolicyMismatchCount: number;
|
||||
unexpectedPortalPolicyCount: number;
|
||||
expectedPrivilegeMismatchCount: number;
|
||||
unexpectedTablePrivilegeCount: number;
|
||||
expectedColumnPrivilegeMismatchCount: number;
|
||||
unexpectedColumnPrivilegeCount: number;
|
||||
resolverMismatchCount: number;
|
||||
unexpectedResolverCount: number;
|
||||
unexpectedDefinerExecuteCount: number;
|
||||
tenantContext: string | null;
|
||||
userContext: string | null;
|
||||
portalUserContext: string | null;
|
||||
publicKnowledgeBaseContext: string | null;
|
||||
platformAnalyticsContext: string | null;
|
||||
};
|
||||
|
||||
export type PortalRlsRuntimeReadiness = {
|
||||
mode: RlsRuntimeMode;
|
||||
ready: boolean;
|
||||
diagnostics: string[];
|
||||
probes: PortalRlsRuntimeProbe[];
|
||||
};
|
||||
|
||||
function sqlLiteral(value: string): string {
|
||||
return `'${value.replaceAll("'", "''")}'`;
|
||||
}
|
||||
|
||||
const EXPECTED_POLICY_VALUES_SQL = PORTAL_RLS_POLICY_CONTRACT.map(
|
||||
({ table, policy, command, usingHash, withCheckHash }) =>
|
||||
`(${sqlLiteral(table)}, ${sqlLiteral(policy)}, ${sqlLiteral(command)}, ${usingHash === null ? "NULL" : sqlLiteral(usingHash)}, ${withCheckHash === null ? "NULL" : sqlLiteral(withCheckHash)})`
|
||||
).join(",\n ");
|
||||
const EXPECTED_PRIVILEGE_ROWS_SQL = PORTAL_RLS_TABLE_PRIVILEGE_CONTRACT.flatMap(({ table, privileges }) =>
|
||||
privileges.map((privilege) => `(${sqlLiteral(table)}, ${sqlLiteral(privilege)})`)
|
||||
).join(",\n ");
|
||||
const EXPECTED_PRIVILEGE_VALUES_SQL = EXPECTED_PRIVILEGE_ROWS_SQL
|
||||
? `VALUES\n ${EXPECTED_PRIVILEGE_ROWS_SQL}`
|
||||
: "SELECT NULL::text, NULL::text WHERE false";
|
||||
const EXPECTED_COLUMN_PRIVILEGE_VALUES_SQL = PORTAL_RLS_COLUMN_PRIVILEGE_CONTRACT.flatMap(({ table, privilege, columns }) =>
|
||||
columns.map((column) => `(${sqlLiteral(table)}, ${sqlLiteral(column)}, ${sqlLiteral(privilege)})`)
|
||||
).join(",\n ");
|
||||
const EXPECTED_RESOLVER_VALUES_SQL = PORTAL_RLS_RESOLVER_SIGNATURES.map((signature) => `(${sqlLiteral(signature)})`).join(",\n ");
|
||||
const EXPECTED_PRIVATE_HELPER_VALUES_SQL = PORTAL_RLS_PRIVATE_HELPER_SIGNATURES.map((signature) => `(${sqlLiteral(signature)})`).join(",\n ");
|
||||
const EXPECTED_PRIVATE_DEFINER_HELPER_VALUES_SQL = PORTAL_RLS_PRIVATE_DEFINER_HELPER_SIGNATURES.map(
|
||||
(signature) => `(${sqlLiteral(signature)})`,
|
||||
).join(",\n ");
|
||||
const EXPECTED_CROSS_ROLE_CAPABILITY_VALUES_SQL = PORTAL_RLS_CROSS_ROLE_CAPABILITY_CONTRACT.map(
|
||||
({ signature, runtimeRole }) => `(${sqlLiteral(signature)}, ${sqlLiteral(runtimeRole)})`,
|
||||
).join(",\n ");
|
||||
|
||||
export async function verifyPortalRlsRuntimeReadiness(
|
||||
clients: readonly PortalRlsProbeClient[] = uniqueClients(getWriteDbClient(), getReadDbClient()),
|
||||
env: NodeJS.ProcessEnv = process.env
|
||||
): Promise<PortalRlsRuntimeReadiness> {
|
||||
const mode = resolveRlsRuntimeMode(env);
|
||||
if (mode === "off") return { mode, ready: true, diagnostics: [], probes: [] };
|
||||
|
||||
const probes = await Promise.all(clients.map(readPortalRlsRuntimeProbe));
|
||||
const diagnostics = probes.flatMap(diagnosePortalProbe);
|
||||
return { mode, ready: diagnostics.length === 0, diagnostics, probes };
|
||||
}
|
||||
|
||||
export async function assertPortalRlsRuntimeReady(
|
||||
clients?: readonly PortalRlsProbeClient[],
|
||||
env: NodeJS.ProcessEnv = process.env
|
||||
): Promise<PortalRlsRuntimeReadiness> {
|
||||
const result = await verifyPortalRlsRuntimeReadiness(clients, env);
|
||||
if (result.mode === "required" && !result.ready) {
|
||||
throw new Error(`Portal RLS runtime readiness failed: ${result.diagnostics.join(", ")}`);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
async function readPortalRlsRuntimeProbe(client: PortalRlsProbeClient): Promise<PortalRlsRuntimeProbe> {
|
||||
const rows = await client.$queryRawUnsafe<
|
||||
Array<{
|
||||
session_role_name: string;
|
||||
role_name: string;
|
||||
role_exists: boolean;
|
||||
can_login: boolean;
|
||||
is_superuser: boolean;
|
||||
can_create_db: boolean;
|
||||
can_create_role: boolean;
|
||||
inherits: boolean;
|
||||
replicates: boolean;
|
||||
bypasses_rls: boolean;
|
||||
membership_count: number;
|
||||
has_schema_usage: boolean;
|
||||
has_schema_create: boolean;
|
||||
expected_rls_mismatch_count: number;
|
||||
expected_policy_mismatch_count: number;
|
||||
unexpected_portal_policy_count: number;
|
||||
expected_privilege_mismatch_count: number;
|
||||
unexpected_table_privilege_count: number;
|
||||
expected_column_privilege_mismatch_count: number;
|
||||
unexpected_column_privilege_count: number;
|
||||
resolver_mismatch_count: number;
|
||||
unexpected_resolver_count: number;
|
||||
unexpected_definer_execute_count: number;
|
||||
tenant_context: string | null;
|
||||
user_context: string | null;
|
||||
portal_user_context: string | null;
|
||||
public_knowledge_base_context: string | null;
|
||||
platform_analytics_context: string | null;
|
||||
}>
|
||||
>(`
|
||||
WITH expected_policy(table_name, policy_name, command, using_hash, with_check_hash) AS (
|
||||
VALUES
|
||||
${EXPECTED_POLICY_VALUES_SQL}
|
||||
),
|
||||
expected_table(table_name) AS (
|
||||
SELECT DISTINCT table_name FROM expected_policy
|
||||
),
|
||||
expected_privilege(table_name, privilege_type) AS (
|
||||
${EXPECTED_PRIVILEGE_VALUES_SQL}
|
||||
),
|
||||
expected_column_privilege(table_name, column_name, privilege_type) AS (
|
||||
VALUES
|
||||
${EXPECTED_COLUMN_PRIVILEGE_VALUES_SQL}
|
||||
),
|
||||
expected_resolver(signature) AS (
|
||||
VALUES
|
||||
${EXPECTED_RESOLVER_VALUES_SQL}
|
||||
),
|
||||
expected_private_helper(signature) AS (
|
||||
VALUES
|
||||
${EXPECTED_PRIVATE_HELPER_VALUES_SQL}
|
||||
),
|
||||
expected_private_definer_helper(signature) AS (
|
||||
VALUES
|
||||
${EXPECTED_PRIVATE_DEFINER_HELPER_VALUES_SQL}
|
||||
),
|
||||
expected_cross_role_capability(signature, runtime_role) AS (
|
||||
VALUES
|
||||
${EXPECTED_CROSS_ROLE_CAPABILITY_VALUES_SQL}
|
||||
)
|
||||
SELECT
|
||||
session_user::text AS session_role_name,
|
||||
current_user::text AS role_name,
|
||||
EXISTS (SELECT 1 FROM pg_roles WHERE rolname = '${PORTAL_RLS_RUNTIME_ROLE}') AS role_exists,
|
||||
COALESCE((SELECT rolcanlogin FROM pg_roles WHERE rolname = '${PORTAL_RLS_RUNTIME_ROLE}'), false) AS can_login,
|
||||
COALESCE((SELECT rolsuper FROM pg_roles WHERE rolname = '${PORTAL_RLS_RUNTIME_ROLE}'), false) AS is_superuser,
|
||||
COALESCE((SELECT rolcreatedb FROM pg_roles WHERE rolname = '${PORTAL_RLS_RUNTIME_ROLE}'), false) AS can_create_db,
|
||||
COALESCE((SELECT rolcreaterole FROM pg_roles WHERE rolname = '${PORTAL_RLS_RUNTIME_ROLE}'), false) AS can_create_role,
|
||||
COALESCE((SELECT rolinherit FROM pg_roles WHERE rolname = '${PORTAL_RLS_RUNTIME_ROLE}'), false) AS inherits,
|
||||
COALESCE((SELECT rolreplication FROM pg_roles WHERE rolname = '${PORTAL_RLS_RUNTIME_ROLE}'), false) AS replicates,
|
||||
COALESCE((SELECT rolbypassrls FROM pg_roles WHERE rolname = '${PORTAL_RLS_RUNTIME_ROLE}'), false) AS bypasses_rls,
|
||||
(
|
||||
SELECT COUNT(*)::int
|
||||
FROM pg_auth_members AS membership
|
||||
JOIN pg_roles AS granted ON granted.oid = membership.roleid
|
||||
JOIN pg_roles AS member ON member.oid = membership.member
|
||||
WHERE granted.rolname = '${PORTAL_RLS_RUNTIME_ROLE}' OR member.rolname = '${PORTAL_RLS_RUNTIME_ROLE}'
|
||||
) AS membership_count,
|
||||
has_schema_privilege('${PORTAL_RLS_RUNTIME_ROLE}', 'public', 'USAGE') AS has_schema_usage,
|
||||
has_schema_privilege('${PORTAL_RLS_RUNTIME_ROLE}', 'public', 'CREATE') AS has_schema_create,
|
||||
(
|
||||
SELECT COUNT(*)::int
|
||||
FROM expected_table AS expected
|
||||
LEFT JOIN pg_class AS class
|
||||
ON class.relname = expected.table_name
|
||||
AND class.relnamespace = 'public'::regnamespace
|
||||
WHERE class.oid IS NULL
|
||||
OR class.relkind NOT IN ('r', 'p')
|
||||
OR class.relrowsecurity IS DISTINCT FROM true
|
||||
OR class.relforcerowsecurity IS DISTINCT FROM true
|
||||
) AS expected_rls_mismatch_count,
|
||||
(
|
||||
SELECT COUNT(*)::int
|
||||
FROM expected_policy AS expected
|
||||
LEFT JOIN pg_class AS class ON class.relname = expected.table_name AND class.relnamespace = 'public'::regnamespace
|
||||
LEFT JOIN pg_policy AS policy
|
||||
ON policy.polrelid = class.oid
|
||||
AND policy.polname = expected.policy_name
|
||||
AND policy.polcmd::text = expected.command
|
||||
WHERE policy.oid IS NULL
|
||||
OR policy.polpermissive IS DISTINCT FROM true
|
||||
OR ARRAY(
|
||||
SELECT CASE
|
||||
WHEN policy_role.role_oid = 0 THEN 'PUBLIC'
|
||||
ELSE role.rolname::text
|
||||
END
|
||||
FROM unnest(policy.polroles) AS policy_role(role_oid)
|
||||
LEFT JOIN pg_roles AS role ON role.oid = policy_role.role_oid
|
||||
ORDER BY 1
|
||||
) IS DISTINCT FROM ARRAY['${PORTAL_RLS_RUNTIME_ROLE}']::text[]
|
||||
OR md5(regexp_replace(pg_get_expr(policy.polqual, policy.polrelid, false), '[[:space:]]+', '', 'g'))
|
||||
IS DISTINCT FROM expected.using_hash
|
||||
OR md5(regexp_replace(pg_get_expr(policy.polwithcheck, policy.polrelid, false), '[[:space:]]+', '', 'g'))
|
||||
IS DISTINCT FROM expected.with_check_hash
|
||||
) AS expected_policy_mismatch_count,
|
||||
(
|
||||
SELECT COUNT(*)::int
|
||||
FROM pg_policy AS policy
|
||||
JOIN pg_class AS class ON class.oid = policy.polrelid
|
||||
WHERE class.relnamespace = 'public'::regnamespace
|
||||
AND (
|
||||
COALESCE((SELECT oid FROM pg_roles WHERE rolname = '${PORTAL_RLS_RUNTIME_ROLE}'), 0::oid) = ANY (policy.polroles)
|
||||
OR (
|
||||
0::oid = ANY (policy.polroles)
|
||||
AND EXISTS (SELECT 1 FROM expected_table AS expected WHERE expected.table_name = class.relname)
|
||||
)
|
||||
)
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM expected_policy AS expected
|
||||
WHERE expected.table_name = class.relname
|
||||
AND expected.policy_name = policy.polname
|
||||
AND expected.command = policy.polcmd::text
|
||||
)
|
||||
) AS unexpected_portal_policy_count,
|
||||
(
|
||||
SELECT COUNT(*)::int
|
||||
FROM expected_privilege AS expected
|
||||
WHERE NOT has_table_privilege(
|
||||
'${PORTAL_RLS_RUNTIME_ROLE}',
|
||||
format('%I.%I', 'public', expected.table_name),
|
||||
expected.privilege_type
|
||||
)
|
||||
) AS expected_privilege_mismatch_count,
|
||||
(
|
||||
SELECT COUNT(*)::int
|
||||
FROM pg_class AS class
|
||||
CROSS JOIN (VALUES ('SELECT'), ('INSERT'), ('UPDATE'), ('DELETE'), ('TRUNCATE'), ('REFERENCES'), ('TRIGGER')) AS candidate(privilege_type)
|
||||
WHERE class.relnamespace = 'public'::regnamespace
|
||||
AND class.relkind IN ('r', 'p', 'v', 'm', 'f')
|
||||
AND has_table_privilege('${PORTAL_RLS_RUNTIME_ROLE}', class.oid, candidate.privilege_type)
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM expected_privilege AS expected
|
||||
WHERE expected.table_name = class.relname
|
||||
AND expected.privilege_type = candidate.privilege_type
|
||||
)
|
||||
) AS unexpected_table_privilege_count,
|
||||
(
|
||||
SELECT COUNT(*)::int
|
||||
FROM expected_column_privilege AS expected
|
||||
JOIN pg_class AS class
|
||||
ON class.relname = expected.table_name
|
||||
AND class.relnamespace = 'public'::regnamespace
|
||||
JOIN pg_attribute AS attribute
|
||||
ON attribute.attrelid = class.oid
|
||||
AND attribute.attname = expected.column_name
|
||||
AND attribute.attnum > 0
|
||||
AND NOT attribute.attisdropped
|
||||
WHERE NOT has_column_privilege(
|
||||
'${PORTAL_RLS_RUNTIME_ROLE}',
|
||||
class.oid,
|
||||
attribute.attnum,
|
||||
expected.privilege_type
|
||||
)
|
||||
) AS expected_column_privilege_mismatch_count,
|
||||
(
|
||||
SELECT COUNT(*)::int
|
||||
FROM pg_class AS class
|
||||
JOIN pg_attribute AS attribute
|
||||
ON attribute.attrelid = class.oid
|
||||
AND attribute.attnum > 0
|
||||
AND NOT attribute.attisdropped
|
||||
CROSS JOIN (VALUES ('SELECT'), ('INSERT'), ('UPDATE'), ('REFERENCES')) AS candidate(privilege_type)
|
||||
WHERE class.relnamespace = 'public'::regnamespace
|
||||
AND class.relkind IN ('r', 'p', 'v', 'm', 'f')
|
||||
AND has_column_privilege('${PORTAL_RLS_RUNTIME_ROLE}', class.oid, attribute.attnum, candidate.privilege_type)
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM expected_privilege AS expected
|
||||
WHERE expected.table_name = class.relname
|
||||
AND expected.privilege_type = candidate.privilege_type
|
||||
)
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM expected_column_privilege AS expected
|
||||
WHERE expected.table_name = class.relname
|
||||
AND expected.column_name = attribute.attname
|
||||
AND expected.privilege_type = candidate.privilege_type
|
||||
)
|
||||
) AS unexpected_column_privilege_count,
|
||||
(
|
||||
SELECT (
|
||||
SELECT COUNT(*)::int
|
||||
FROM expected_resolver AS expected
|
||||
LEFT JOIN pg_proc AS procedure ON procedure.oid = to_regprocedure(expected.signature)
|
||||
LEFT JOIN pg_roles AS owner ON owner.oid = procedure.proowner
|
||||
WHERE procedure.oid IS NULL
|
||||
OR owner.rolname IS DISTINCT FROM 'hivegrid_rls_definer'
|
||||
OR procedure.prokind IS DISTINCT FROM 'f'
|
||||
OR procedure.prosecdef IS DISTINCT FROM true
|
||||
OR NOT (procedure.proconfig @> ARRAY['search_path=pg_catalog, public', 'row_security=off']::text[])
|
||||
OR NOT has_function_privilege('${PORTAL_RLS_RUNTIME_ROLE}', procedure.oid, 'EXECUTE')
|
||||
OR EXISTS (
|
||||
SELECT 1
|
||||
FROM aclexplode(COALESCE(procedure.proacl, acldefault('f', procedure.proowner))) AS access
|
||||
LEFT JOIN pg_roles AS grantee ON grantee.oid = access.grantee
|
||||
WHERE access.privilege_type = 'EXECUTE'
|
||||
AND access.grantee <> procedure.proowner
|
||||
AND (access.grantee = 0 OR grantee.rolname <> '${PORTAL_RLS_RUNTIME_ROLE}')
|
||||
)
|
||||
) + (
|
||||
SELECT COUNT(*)::int
|
||||
FROM expected_private_helper AS expected
|
||||
LEFT JOIN pg_proc AS procedure ON procedure.oid = to_regprocedure(expected.signature)
|
||||
LEFT JOIN pg_roles AS owner ON owner.oid = procedure.proowner
|
||||
WHERE procedure.oid IS NULL
|
||||
OR owner.rolname IS DISTINCT FROM 'hivegrid_rls_definer'
|
||||
OR procedure.prokind IS DISTINCT FROM 'f'
|
||||
OR procedure.prosecdef IS DISTINCT FROM false
|
||||
OR has_function_privilege('${PORTAL_RLS_RUNTIME_ROLE}', procedure.oid, 'EXECUTE')
|
||||
) + (
|
||||
SELECT COUNT(*)::int
|
||||
FROM expected_private_definer_helper AS expected
|
||||
LEFT JOIN pg_proc AS procedure ON procedure.oid = to_regprocedure(expected.signature)
|
||||
LEFT JOIN pg_roles AS owner ON owner.oid = procedure.proowner
|
||||
WHERE procedure.oid IS NULL
|
||||
OR owner.rolname IS DISTINCT FROM 'hivegrid_rls_definer'
|
||||
OR procedure.prokind IS DISTINCT FROM 'f'
|
||||
OR procedure.prosecdef IS DISTINCT FROM true
|
||||
OR NOT (procedure.proconfig @> ARRAY['search_path=pg_catalog, public', 'row_security=off']::text[])
|
||||
OR has_function_privilege('${PORTAL_RLS_RUNTIME_ROLE}', procedure.oid, 'EXECUTE')
|
||||
OR EXISTS (
|
||||
SELECT 1
|
||||
FROM aclexplode(COALESCE(procedure.proacl, acldefault('f', procedure.proowner))) AS access
|
||||
WHERE access.privilege_type = 'EXECUTE'
|
||||
AND access.grantee <> procedure.proowner
|
||||
)
|
||||
) + (
|
||||
SELECT COUNT(*)::int
|
||||
FROM expected_cross_role_capability AS expected
|
||||
LEFT JOIN pg_proc AS procedure ON procedure.oid = to_regprocedure(expected.signature)
|
||||
LEFT JOIN pg_roles AS owner ON owner.oid = procedure.proowner
|
||||
WHERE procedure.oid IS NULL
|
||||
OR owner.rolname IS DISTINCT FROM 'hivegrid_rls_definer'
|
||||
OR procedure.prokind IS DISTINCT FROM 'f'
|
||||
OR procedure.prosecdef IS DISTINCT FROM true
|
||||
OR NOT (procedure.proconfig @> ARRAY['search_path=pg_catalog, public', 'row_security=off']::text[])
|
||||
OR has_function_privilege('${PORTAL_RLS_RUNTIME_ROLE}', procedure.oid, 'EXECUTE')
|
||||
OR NOT has_function_privilege(expected.runtime_role, procedure.oid, 'EXECUTE')
|
||||
OR EXISTS (
|
||||
SELECT 1
|
||||
FROM aclexplode(COALESCE(procedure.proacl, acldefault('f', procedure.proowner))) AS access
|
||||
LEFT JOIN pg_roles AS grantee ON grantee.oid = access.grantee
|
||||
WHERE access.privilege_type = 'EXECUTE'
|
||||
AND access.grantee <> procedure.proowner
|
||||
AND (access.grantee = 0 OR grantee.rolname IS DISTINCT FROM expected.runtime_role)
|
||||
)
|
||||
)
|
||||
) AS resolver_mismatch_count,
|
||||
(
|
||||
SELECT COUNT(*)::int
|
||||
FROM pg_proc AS procedure
|
||||
JOIN pg_namespace AS namespace ON namespace.oid = procedure.pronamespace
|
||||
WHERE namespace.nspname = 'public'
|
||||
AND procedure.proname LIKE 'hivegrid_portal_%'
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM expected_resolver AS expected WHERE procedure.oid = to_regprocedure(expected.signature)
|
||||
)
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM expected_private_helper AS expected WHERE procedure.oid = to_regprocedure(expected.signature)
|
||||
)
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM expected_private_definer_helper AS expected WHERE procedure.oid = to_regprocedure(expected.signature)
|
||||
)
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM expected_cross_role_capability AS expected WHERE procedure.oid = to_regprocedure(expected.signature)
|
||||
)
|
||||
) AS unexpected_resolver_count,
|
||||
(
|
||||
SELECT COUNT(*)::int
|
||||
FROM pg_proc AS procedure
|
||||
JOIN pg_roles AS owner ON owner.oid = procedure.proowner
|
||||
WHERE owner.rolname = 'hivegrid_rls_definer'
|
||||
AND has_function_privilege('${PORTAL_RLS_RUNTIME_ROLE}', procedure.oid, 'EXECUTE')
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM expected_resolver AS expected WHERE procedure.oid = to_regprocedure(expected.signature)
|
||||
)
|
||||
) AS unexpected_definer_execute_count,
|
||||
NULLIF(current_setting('app.tenant_id', true), '') AS tenant_context,
|
||||
NULLIF(current_setting('app.user_id', true), '') AS user_context,
|
||||
NULLIF(current_setting('app.portal_user_id', true), '') AS portal_user_context,
|
||||
NULLIF(current_setting('app.public_knowledge_base_id', true), '') AS public_knowledge_base_context,
|
||||
NULLIF(current_setting('app.platform_analytics', true), '') AS platform_analytics_context
|
||||
`);
|
||||
|
||||
const row = rows[0];
|
||||
if (
|
||||
!row ||
|
||||
typeof row.session_role_name !== "string" ||
|
||||
typeof row.role_name !== "string" ||
|
||||
!Number.isInteger(row.membership_count) ||
|
||||
!Number.isInteger(row.expected_rls_mismatch_count) ||
|
||||
!Number.isInteger(row.expected_policy_mismatch_count) ||
|
||||
!Number.isInteger(row.unexpected_portal_policy_count) ||
|
||||
!Number.isInteger(row.expected_privilege_mismatch_count) ||
|
||||
!Number.isInteger(row.unexpected_table_privilege_count) ||
|
||||
!Number.isInteger(row.expected_column_privilege_mismatch_count) ||
|
||||
!Number.isInteger(row.unexpected_column_privilege_count) ||
|
||||
!Number.isInteger(row.resolver_mismatch_count) ||
|
||||
!Number.isInteger(row.unexpected_resolver_count) ||
|
||||
!Number.isInteger(row.unexpected_definer_execute_count)
|
||||
) {
|
||||
throw new Error("Portal RLS runtime readiness returned an invalid database probe");
|
||||
}
|
||||
|
||||
return {
|
||||
sessionRoleName: row.session_role_name,
|
||||
roleName: row.role_name,
|
||||
roleExists: row.role_exists,
|
||||
canLogin: row.can_login,
|
||||
isSuperuser: row.is_superuser,
|
||||
canCreateDb: row.can_create_db,
|
||||
canCreateRole: row.can_create_role,
|
||||
inherits: row.inherits,
|
||||
replicates: row.replicates,
|
||||
bypassesRls: row.bypasses_rls,
|
||||
membershipCount: row.membership_count,
|
||||
hasSchemaUsage: row.has_schema_usage,
|
||||
hasSchemaCreate: row.has_schema_create,
|
||||
expectedRlsMismatchCount: row.expected_rls_mismatch_count,
|
||||
expectedPolicyMismatchCount: row.expected_policy_mismatch_count,
|
||||
unexpectedPortalPolicyCount: row.unexpected_portal_policy_count,
|
||||
expectedPrivilegeMismatchCount: row.expected_privilege_mismatch_count,
|
||||
unexpectedTablePrivilegeCount: row.unexpected_table_privilege_count,
|
||||
expectedColumnPrivilegeMismatchCount: row.expected_column_privilege_mismatch_count,
|
||||
unexpectedColumnPrivilegeCount: row.unexpected_column_privilege_count,
|
||||
resolverMismatchCount: row.resolver_mismatch_count,
|
||||
unexpectedResolverCount: row.unexpected_resolver_count,
|
||||
unexpectedDefinerExecuteCount: row.unexpected_definer_execute_count,
|
||||
tenantContext: row.tenant_context,
|
||||
userContext: row.user_context,
|
||||
portalUserContext: row.portal_user_context,
|
||||
publicKnowledgeBaseContext: row.public_knowledge_base_context,
|
||||
platformAnalyticsContext: row.platform_analytics_context,
|
||||
};
|
||||
}
|
||||
|
||||
function diagnosePortalProbe(probe: PortalRlsRuntimeProbe, index: number): string[] {
|
||||
const prefix = `portal_database_${index + 1}`;
|
||||
return [
|
||||
...(probe.sessionRoleName === PORTAL_RLS_RUNTIME_ROLE ? [] : [`${prefix}:runtime_session_role_mismatch`]),
|
||||
...(probe.roleName === PORTAL_RLS_RUNTIME_ROLE ? [] : [`${prefix}:runtime_role_mismatch`]),
|
||||
...(probe.sessionRoleName === probe.roleName ? [] : [`${prefix}:runtime_role_switch_detected`]),
|
||||
...(probe.roleExists ? [] : [`${prefix}:portal_role_missing`]),
|
||||
...(probe.canLogin ? [] : [`${prefix}:portal_role_no_login`]),
|
||||
...(!probe.isSuperuser ? [] : [`${prefix}:portal_role_superuser`]),
|
||||
...(!probe.canCreateDb ? [] : [`${prefix}:portal_role_createdb`]),
|
||||
...(!probe.canCreateRole ? [] : [`${prefix}:portal_role_createrole`]),
|
||||
...(!probe.inherits ? [] : [`${prefix}:portal_role_inherit`]),
|
||||
...(!probe.replicates ? [] : [`${prefix}:portal_role_replication`]),
|
||||
...(!probe.bypassesRls ? [] : [`${prefix}:portal_role_bypassrls`]),
|
||||
...(probe.membershipCount === 0 ? [] : [`${prefix}:portal_role_membership`]),
|
||||
...(probe.hasSchemaUsage ? [] : [`${prefix}:portal_schema_usage_missing`]),
|
||||
...(!probe.hasSchemaCreate ? [] : [`${prefix}:portal_schema_create`]),
|
||||
...(probe.expectedRlsMismatchCount === 0 ? [] : [`${prefix}:portal_rls_table_mismatch`]),
|
||||
...(probe.expectedPolicyMismatchCount === 0 ? [] : [`${prefix}:portal_policy_mismatch`]),
|
||||
...(probe.unexpectedPortalPolicyCount === 0 ? [] : [`${prefix}:portal_unexpected_policy`]),
|
||||
...(probe.expectedPrivilegeMismatchCount === 0 ? [] : [`${prefix}:portal_privilege_missing`]),
|
||||
...(probe.unexpectedTablePrivilegeCount === 0 ? [] : [`${prefix}:portal_unexpected_table_privilege`]),
|
||||
...(probe.expectedColumnPrivilegeMismatchCount === 0 ? [] : [`${prefix}:portal_column_privilege_missing`]),
|
||||
...(probe.unexpectedColumnPrivilegeCount === 0 ? [] : [`${prefix}:portal_unexpected_column_privilege`]),
|
||||
...(probe.resolverMismatchCount === 0 ? [] : [`${prefix}:portal_resolver_mismatch`]),
|
||||
...(probe.unexpectedResolverCount === 0 ? [] : [`${prefix}:portal_unexpected_resolver`]),
|
||||
...(probe.unexpectedDefinerExecuteCount === 0 ? [] : [`${prefix}:portal_unexpected_definer_execute`]),
|
||||
...(probe.tenantContext === null &&
|
||||
probe.userContext === null &&
|
||||
probe.portalUserContext === null &&
|
||||
probe.publicKnowledgeBaseContext === null &&
|
||||
probe.platformAnalyticsContext === null
|
||||
? []
|
||||
: [`${prefix}:leaked_rls_context`]),
|
||||
];
|
||||
}
|
||||
|
||||
function uniqueClients(...clients: PortalRlsProbeClient[]): PortalRlsProbeClient[] {
|
||||
return [...new Set(clients)];
|
||||
}
|
||||
Reference in new issue
Block a user