|
| 1 | +import { |
| 2 | + type ResourcePolicyConditionEvaluationFacts, |
| 3 | + requireResourcePolicyConditionDefinition, |
| 4 | +} from '@/lib/resource-policies/conditions' |
| 5 | +import { |
| 6 | + matchResourcePolicyPrincipal, |
| 7 | + type ResourcePolicyPrincipalEvaluationFacts, |
| 8 | +} from '@/lib/resource-policies/principals' |
| 9 | +import { getResourcePolicyDefinition } from '@/lib/resource-policies/registry' |
| 10 | +import type { |
| 11 | + ResourcePolicyAction, |
| 12 | + ResourcePolicyDocument, |
| 13 | + ResourcePolicyResourceType, |
| 14 | + ResourcePolicyStatement, |
| 15 | +} from '@/lib/resource-policies/types' |
| 16 | + |
| 17 | +export type ResourcePolicyDecision = |
| 18 | + | { decision: 'allow'; statementSid: string } |
| 19 | + | { decision: 'deny'; statementSid: string } |
| 20 | + | { decision: 'implicit_deny' } |
| 21 | + |
| 22 | +export interface ResourcePolicyEvaluationFacts |
| 23 | + extends ResourcePolicyPrincipalEvaluationFacts, |
| 24 | + ResourcePolicyConditionEvaluationFacts {} |
| 25 | + |
| 26 | +interface EvaluateResourcePolicyInput<ResourceType extends ResourcePolicyResourceType> { |
| 27 | + document: ResourcePolicyDocument<ResourceType> |
| 28 | + action: ResourcePolicyAction |
| 29 | + facts: ResourcePolicyEvaluationFacts |
| 30 | +} |
| 31 | + |
| 32 | +function conditionMatches( |
| 33 | + statement: ResourcePolicyStatement, |
| 34 | + definition: ReturnType<typeof getResourcePolicyDefinition>, |
| 35 | + facts: ResourcePolicyConditionEvaluationFacts |
| 36 | +): boolean { |
| 37 | + if (!statement.condition) return true |
| 38 | + const operators = Object.entries(statement.condition) |
| 39 | + if (operators.length === 0) throw new Error('Resource policy condition must not be empty') |
| 40 | + |
| 41 | + for (const [operator, entries] of operators) { |
| 42 | + if (!entries || Object.keys(entries).length === 0) { |
| 43 | + throw new Error(`Resource policy condition operator ${operator} must not be empty`) |
| 44 | + } |
| 45 | + for (const [key, expected] of Object.entries(entries)) { |
| 46 | + const condition = requireResourcePolicyConditionDefinition(key) |
| 47 | + if (!definition.conditionKeys.includes(condition.key)) { |
| 48 | + throw new Error(`Condition key ${key} does not apply to this resource`) |
| 49 | + } |
| 50 | + if (!condition.operators.includes(operator as 'StringEquals')) { |
| 51 | + throw new Error(`Condition operator ${operator} does not apply to ${key}`) |
| 52 | + } |
| 53 | + if (typeof expected !== 'string') { |
| 54 | + throw new Error(`Condition ${key} requires a string value`) |
| 55 | + } |
| 56 | + if (condition.resolve(facts) !== expected) return false |
| 57 | + } |
| 58 | + } |
| 59 | + return true |
| 60 | +} |
| 61 | + |
| 62 | +function statementMatches<ResourceType extends ResourcePolicyResourceType>( |
| 63 | + statement: ResourcePolicyStatement, |
| 64 | + input: EvaluateResourcePolicyInput<ResourceType> |
| 65 | +): boolean { |
| 66 | + const definition = getResourcePolicyDefinition(input.document.resource.type) |
| 67 | + for (const action of statement.actions) { |
| 68 | + if (!definition.actions.includes(action)) { |
| 69 | + throw new Error(`Action ${action} does not apply to ${input.document.resource.type}`) |
| 70 | + } |
| 71 | + } |
| 72 | + if (!statement.actions.includes(input.action)) return false |
| 73 | + if (statement.principals.length === 0) { |
| 74 | + throw new Error(`Resource policy statement ${statement.sid} must contain a principal`) |
| 75 | + } |
| 76 | + const principalMatches = statement.principals.some((principal) => { |
| 77 | + if (!definition.principalTypes.includes(principal.type)) { |
| 78 | + throw new Error(`Principal ${principal.type} does not apply to this resource`) |
| 79 | + } |
| 80 | + return matchResourcePolicyPrincipal(principal, input.facts) |
| 81 | + }) |
| 82 | + return principalMatches && conditionMatches(statement, definition, input.facts) |
| 83 | +} |
| 84 | + |
| 85 | +export function evaluateResourcePolicy<ResourceType extends ResourcePolicyResourceType>( |
| 86 | + input: EvaluateResourcePolicyInput<ResourceType> |
| 87 | +): ResourcePolicyDecision { |
| 88 | + const definition = getResourcePolicyDefinition(input.document.resource.type) |
| 89 | + if (!definition.actions.includes(input.action)) { |
| 90 | + throw new Error(`Action ${input.action} does not apply to ${input.document.resource.type}`) |
| 91 | + } |
| 92 | + for (const statement of input.document.statements) { |
| 93 | + if (statement.effect !== 'allow' && statement.effect !== 'deny') { |
| 94 | + throw new Error(`Resource policy statement ${statement.sid} has an invalid effect`) |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + for (const statement of input.document.statements) { |
| 99 | + if (statement.effect === 'deny' && statementMatches(statement, input)) { |
| 100 | + return { decision: 'deny', statementSid: statement.sid } |
| 101 | + } |
| 102 | + } |
| 103 | + for (const statement of input.document.statements) { |
| 104 | + if (statement.effect === 'allow' && statementMatches(statement, input)) { |
| 105 | + return { decision: 'allow', statementSid: statement.sid } |
| 106 | + } |
| 107 | + } |
| 108 | + return { decision: 'implicit_deny' } |
| 109 | +} |
0 commit comments