|
| 1 | +import { Attribute, AttributeRuleType, KeyAccessServer, Value } from './attributes.js'; |
| 2 | + |
| 3 | +export type KeySplitStep = { |
| 4 | + kas: KeyAccessServer; |
| 5 | + sid?: string; |
| 6 | +}; |
| 7 | + |
| 8 | +type AttributeClause = { |
| 9 | + def: Attribute; |
| 10 | + values: string[]; |
| 11 | +}; |
| 12 | + |
| 13 | +type AndClause = { |
| 14 | + op: 'allOf'; |
| 15 | + kases: string[]; |
| 16 | +}; |
| 17 | + |
| 18 | +type HeirarchyClause = { |
| 19 | + op: 'hierarchy'; |
| 20 | + kases: string[]; |
| 21 | +}; |
| 22 | + |
| 23 | +type OrClause = { |
| 24 | + op: 'anyOf'; |
| 25 | + kases: string[]; |
| 26 | +}; |
| 27 | + |
| 28 | +type BooleanClause = AndClause | OrClause | HeirarchyClause; |
| 29 | + |
| 30 | +type BooleanOperator = BooleanClause['op']; |
| 31 | + |
| 32 | +type ComplexBooleanClause = { |
| 33 | + op: BooleanOperator; |
| 34 | + children: BooleanClause[]; |
| 35 | +}; |
| 36 | + |
| 37 | +export function booleanOperatorFor(rule?: AttributeRuleType): BooleanOperator { |
| 38 | + if (!rule) { |
| 39 | + return 'allOf'; |
| 40 | + } |
| 41 | + switch (rule) { |
| 42 | + case 'ATTRIBUTE_RULE_TYPE_ENUM_UNSPECIFIED': |
| 43 | + case 'ATTRIBUTE_RULE_TYPE_ENUM_ALL_OF': |
| 44 | + return 'allOf'; |
| 45 | + case 'ATTRIBUTE_RULE_TYPE_ENUM_ANY_OF': |
| 46 | + return 'anyOf'; |
| 47 | + case 'ATTRIBUTE_RULE_TYPE_ENUM_HIERARCHY': |
| 48 | + return 'hierarchy'; |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +export function plan(dataAttrs: Value[]): KeySplitStep[] { |
| 53 | + // KASes by value |
| 54 | + const grants: Record<string, Set<string>> = {}; |
| 55 | + // KAS detail by KAS url |
| 56 | + const kasInfo: Record<string, KeyAccessServer> = {}; |
| 57 | + // Attribute definitions in use |
| 58 | + const prefixes: Set<string> = new Set(); |
| 59 | + // Values grouped by normalized attribute prefix |
| 60 | + const allClauses: Record<string, AttributeClause> = {}; |
| 61 | + // Values by normalized FQN |
| 62 | + const allValues: Record<string, Value> = {}; |
| 63 | + |
| 64 | + const addGrants = (val: string, gs?: KeyAccessServer[]): boolean => { |
| 65 | + if (!gs?.length) { |
| 66 | + if (!(val in grants)) { |
| 67 | + grants[val] = new Set(); |
| 68 | + } |
| 69 | + return false; |
| 70 | + } |
| 71 | + for (const g of gs) { |
| 72 | + if (val in grants) { |
| 73 | + grants[val].add(g.uri); |
| 74 | + } else { |
| 75 | + grants[val] = new Set([g.uri]); |
| 76 | + } |
| 77 | + kasInfo[g.uri] = g; |
| 78 | + } |
| 79 | + return true; |
| 80 | + }; |
| 81 | + |
| 82 | + for (const v of dataAttrs) { |
| 83 | + const { attribute, fqn } = v; |
| 84 | + if (!attribute) { |
| 85 | + throw new Error(`attribute not defined for [${fqn}]`); |
| 86 | + } |
| 87 | + const valFqn = fqn.toLowerCase(); |
| 88 | + const attrFqn = attribute.fqn.toLowerCase(); |
| 89 | + if (!prefixes.has(attrFqn)) { |
| 90 | + prefixes.add(attrFqn); |
| 91 | + allClauses[attrFqn] = { |
| 92 | + def: attribute, |
| 93 | + values: [], |
| 94 | + }; |
| 95 | + } |
| 96 | + allClauses[attrFqn].values.push(valFqn); |
| 97 | + allValues[valFqn] = v; |
| 98 | + if (!addGrants(valFqn, v.grants)) { |
| 99 | + if (!addGrants(valFqn, attribute.grants)) { |
| 100 | + addGrants(valFqn, attribute.namespace?.grants); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + const kcs: ComplexBooleanClause[] = []; |
| 105 | + for (const attrClause of Object.values(allClauses)) { |
| 106 | + const ccv: BooleanClause[] = []; |
| 107 | + for (const term of attrClause.values) { |
| 108 | + const grantsForTerm = Array.from(grants[term] || []); |
| 109 | + if (grantsForTerm?.length) { |
| 110 | + ccv.push({ |
| 111 | + op: 'anyOf', |
| 112 | + kases: grantsForTerm, |
| 113 | + }); |
| 114 | + } |
| 115 | + } |
| 116 | + const op = booleanOperatorFor(attrClause.def.rule); |
| 117 | + kcs.push({ |
| 118 | + op, |
| 119 | + children: ccv, |
| 120 | + }); |
| 121 | + } |
| 122 | + return simplify(kcs, kasInfo); |
| 123 | +} |
| 124 | + |
| 125 | +function simplify( |
| 126 | + clauses: ComplexBooleanClause[], |
| 127 | + kasInfo: Record<string, KeyAccessServer> |
| 128 | +): KeySplitStep[] { |
| 129 | + const conjunction: Record<string, string[]> = {}; |
| 130 | + function keyFor(kases: string[]): string { |
| 131 | + const k = Array.from(new Set([kases])).sort(); |
| 132 | + return k.join('|'); |
| 133 | + } |
| 134 | + for (const { op, children } of clauses) { |
| 135 | + if (!children) { |
| 136 | + continue; |
| 137 | + } |
| 138 | + if (op === 'anyOf') { |
| 139 | + const anyKids = []; |
| 140 | + for (const bc of children) { |
| 141 | + if (bc.op != 'anyOf') { |
| 142 | + throw new Error('inversion'); |
| 143 | + } |
| 144 | + if (!bc.kases?.length) { |
| 145 | + continue; |
| 146 | + } |
| 147 | + anyKids.push(...bc.kases); |
| 148 | + } |
| 149 | + if (!anyKids?.length) { |
| 150 | + continue; |
| 151 | + } |
| 152 | + const k = keyFor(anyKids); |
| 153 | + conjunction[k] = anyKids; |
| 154 | + } else { |
| 155 | + for (const bc of children) { |
| 156 | + if (bc.op != 'anyOf') { |
| 157 | + throw new Error('inversion'); |
| 158 | + } |
| 159 | + if (!bc.kases?.length) { |
| 160 | + continue; |
| 161 | + } |
| 162 | + const k = keyFor(bc.kases); |
| 163 | + conjunction[k] = bc.kases; |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | + const t: KeySplitStep[] = []; |
| 168 | + let i = 0; |
| 169 | + for (const k of Object.keys(conjunction).sort()) { |
| 170 | + if (!conjunction[k]) { |
| 171 | + continue; |
| 172 | + } |
| 173 | + i += 1; |
| 174 | + const sid = '' + i; |
| 175 | + for (const kas of conjunction[k]) { |
| 176 | + t.push({ sid, kas: kasInfo[kas] }); |
| 177 | + } |
| 178 | + } |
| 179 | + return t; |
| 180 | +} |
0 commit comments