Files
MikrotikManager/backend/scripts/writer-profile/source57-admission-plan.mts
T

49 lines
3.3 KiB
TypeScript

import assert from 'node:assert/strict'
import {FIXED57_ROLE_TEMPLATE} from './fixed57-build-definition.js'
import {fixedCatalogueParentPeriodData} from './application-writer-profile-catalogue-parent-policy.js'
import {reviewedMarkerFenceSql} from './marker-fence-enrollment.mjs'
import {sourceBindingBytes,sourceBindingSha256} from '../../src/db/database-source-binding.js'
import type {RegistryRoleBindingsData} from '../../src/db/application-writer-profile-registry-structure.js'
const privileges={r:'SELECT',a:'INSERT',w:'UPDATE',d:'DELETE'} as const
const identifier=(value:unknown)=>{assert(typeof value==='string'&&/^[a-z_][a-z0-9_]{0,62}$/.test(value));return '"'+value+'"'}
const qualified=(value:unknown)=>'public.'+identifier(value)
const allowedScopes=['runtime','publisher','capture'] as const
export function reviewedSource57Admission(roles:RegistryRoleBindingsData){
const sql:string[]=[]
function grants(entries:unknown,suffix:string,column?:string){
assert(Array.isArray(entries))
for(const entry of entries){
assert(typeof entry==='string')
if(entry==='@mm-role:operator@=arwdDxtm/@mm-role:operator@'||entry==='@mm-role:operator@=rwU/@mm-role:operator@')continue
const match=/^@mm-role:(runtime|publisher|capture)@=([rawd]+)\/@mm-role:operator@$/.exec(entry)
assert(match&&allowedScopes.includes(match[1] as typeof allowedScopes[number]))
const allowed=[...match[2]].map(key=>privileges[key as keyof typeof privileges]+(column?` (${column})`:'')).join(', ')
sql.push(`GRANT ${allowed} ON ${suffix} TO ${identifier(roles[match[1] as typeof allowedScopes[number]].name)}`)
}
}
for(const table of FIXED57_ROLE_TEMPLATE.tables){
const name=qualified(table.relname)
grants(table.acl,'TABLE '+name)
if(table.relname==='schema_migrations')continue
if(!fixedCatalogueParentPeriodData(String(table.relname))){
sql.push(`CREATE TRIGGER server_partition_writer_guard BEFORE INSERT OR DELETE OR UPDATE ON ${name} FOR EACH ROW EXECUTE FUNCTION public.server_partition_writer_guard()`,
`ALTER TABLE ONLY ${name} ENABLE ALWAYS TRIGGER server_partition_writer_guard`)
}
sql.push(`CREATE TRIGGER candidate_application_statement_guard BEFORE INSERT OR DELETE OR UPDATE ON ${name} FOR EACH STATEMENT EXECUTE FUNCTION public.server_partition_writer_guard()`,
`ALTER TABLE ONLY ${name} ENABLE ALWAYS TRIGGER candidate_application_statement_guard`,
`CREATE TRIGGER candidate_truncate_preadmitted BEFORE TRUNCATE ON ${name} FOR EACH STATEMENT EXECUTE FUNCTION mmapp_control.candidate_truncate_preadmitted()`,
`ALTER TABLE ONLY ${name} ENABLE ALWAYS TRIGGER candidate_truncate_preadmitted`)
}
for(const column of FIXED57_ROLE_TEMPLATE.columns){
if(column.acl!==null)grants(column.acl,'TABLE '+qualified(column.relname),identifier(column.attname))
}
for(const sequence of FIXED57_ROLE_TEMPLATE.sequences)grants(sequence.acl,'SEQUENCE '+qualified(sequence.relname))
sql.push(reviewedMarkerFenceSql)
const tables=FIXED57_ROLE_TEMPLATE.tables.map(row=>({name:row.relname,kind:row.relkind}))
const sequences=FIXED57_ROLE_TEMPLATE.sequences.map(row=>({name:row.relname}))
return {sql,tables,sequences,sha256:sourceBindingSha256(sourceBindingBytes(sql))}
}
export function source57AdmissionSqlSha256(roles:RegistryRoleBindingsData){return reviewedSource57Admission(roles).sha256}