Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions packages/renderers/runtime-policy/src/main.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
import '@githubnext/projections-renderer-kit/react.css'
import './styles.css'
import {followContext} from '@githubnext/projections-renderer-kit'
import {mountRenderer} from '@githubnext/projections-renderer-kit/react'
import {RuntimePolicyView} from './view.js'
import {runtimePolicyFacts, runtimePolicyNouns} from './summary.js'
import {accepts} from './accepts.js'
import {runtimePolicyContext} from './scope.js'

mountRenderer(RuntimePolicyView, {
name: 'runtime-policy',
// The Changes scope lays out only the changed policies and their context, each whole: a changed selector or probe
// keeps the probe and policy that declare it and the hook it attaches to, where it is drawn.
scope: {
layout: 'subset',
parts: 'whole',
context: (all, changes) =>
followContext(all, changes, {incoming: kind => kind === 'declares' || kind === 'filters', outgoing: kind => kind === 'attaches'}),
},
// The Changes scope lays out only the changed policies and their context, each whole (see `runtimePolicyContext`).
scope: {layout: 'subset', parts: 'whole', context: runtimePolicyContext},
accepts,
// Which workloads a policy applies to is shown as chips inside the policy, not as drawn relationships.
capabilities: {changeHighlighting: 'supported', comparisonModes: ['side-by-side', 'diff'], drawsRelationships: false},
Expand Down
19 changes: 19 additions & 0 deletions packages/renderers/runtime-policy/src/scope.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {followContext} from '@githubnext/projections-renderer-kit'
import type {ComparisonModel} from '@githubnext/projections-renderer-kit'

/** Relationships to what a policy or rule applies to, matches or excludes: drawn as chips inside it, not as rows. */
const subjects = new Set(['applies-to', 'matches', 'excludes'])

/**
* What the Changes scope keeps beside the changes so each can be read, each laid out whole: a changed selector or
* probe keeps the probe and policy that declare it and the hook it attaches to, where it is drawn. Chips name what
* they point at, changed or not: every policy and rule shown keeps the subjects it applies to, matches or excludes, so
* an unchanged scope reads as itself rather than as no workloads, and a workload keeps the policies covering it, so it
* doesn't read as covered by no policy.
*/
export function runtimePolicyContext(all: ComparisonModel, changes: ReadonlySet<string>): Set<string> {
return followContext(all, changes, {
incoming: kind => kind === 'declares' || kind === 'filters' || kind === 'covers',
outgoing: kind => kind === 'attaches' || subjects.has(kind),
})
}
82 changes: 81 additions & 1 deletion packages/renderers/runtime-policy/test/runtime-policy.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {readFileSync} from 'node:fs'
import {createElement} from 'react'
import {renderToStaticMarkup} from 'react-dom/server'
import {createNativeEngine} from '@githubnext/projections-engine/native'
import {comparisonModel, extractionModel, stepEmphasis} from '@githubnext/projections-renderer-kit'
import {changesScope, comparisonModel, extractionModel, scopedModel, stepEmphasis} from '@githubnext/projections-renderer-kit'
import {createRulesRuntime} from '@githubnext/projections-comparator-runtime'
import {badgeIconNames} from '@githubnext/projections-renderer-sdk/protocol'
import {plainProse} from '@githubnext/projections-schemas'
Expand All @@ -13,6 +13,7 @@ import {chipPrefix, conditionLines, effectProse, warnings} from '../node_modules
import {findingLabels, interpretationIcons} from '../node_modules/.cache/test/model.js'

import {RuntimePolicyView} from '../node_modules/.cache/test/view.js'
import {runtimePolicyContext} from '../node_modules/.cache/test/scope.js'
import {plain} from '../../kit/test/markup.mjs'

const engine = createNativeEngine()
Expand Down Expand Up @@ -319,3 +320,82 @@ test('in the diff, changed rows marked the same way join into one change run, as
/<section class="rp-lane pk-region pk-region-run"[^>]*data-entity-id="hook:kprobe:sys_execve"[^>]*data-change="added" data-run-start="" data-run-end=""/,
)
})

/** The diff in the Changes scope, as the shell narrows it: the changes, their context, each laid out whole. */
function changesView(model, metadata) {
const scope = changesScope(model, {query: '', changesOnly: true, ids: null})
const shown = new Set([...scope.shown, ...runtimePolicyContext(model, scope.changes)])
return buildModel(scopedModel(model, shown, 'whole').model, 'diff', metadata)
}

const chipNames = items =>
Object.fromEntries(items.map(item => [item.entity.label, (item.scope ?? item.coveredBy).map(chip => chip.subject.entity.label)]))

test("the Changes scope keeps what a changed policy's chips name, so an unchanged Applies to still reads", async () => {
// Only a rule changes: the policy's scope is the same in both revisions.
const edited = structuredClone(before)
const rule = edited.entities.find(entity => entity.kind === 'selector')
rule.data = {...rule.data, matchArgs: [{index: 0, operator: 'Prefix', values: ['/var/run/secrets/']}]}
const comparison = await interpreted(before, edited)
const model = comparisonModel(before, edited, comparison)
const full = changesView(model, comparison.metadata)
const all = buildModel(model, 'diff', comparison.metadata)
assert.ok(full.policies.length > 0)
const shown = chipNames(full.policies)
assert.deepEqual(shown, Object.fromEntries(Object.keys(shown).map(label => [label, chipNames(all.policies)[label]])))
assert.ok(
Object.values(shown).every(names => names.length > 0),
'no policy reads as applying to no workloads',
)
})
Comment thread
agorischek marked this conversation as resolved.
Resolved

test('the Changes scope keeps the policies covering a changed workload', async () => {
// Only a covered workload changes: the policies covering it don't.
const a = fixture('deployment/before.expected.json')
const b = structuredClone(a)
const covered = new Set(b.relationships.filter(relationship => relationship.kind === 'covers').map(relationship => relationship.to))
const workload = b.entities.find(entity => entity.kind === 'workload' && covered.has(entity.id))
workload.data = {...workload.data, replicas: 7}
const comparison = await interpreted(a, b)
const model = comparisonModel(a, b, comparison)
const scoped = chipNames(changesView(model, comparison.metadata).workloads)
const all = chipNames(buildModel(model, 'diff', comparison.metadata).workloads)
assert.deepEqual(Object.keys(scoped), [workload.label])
assert.deepEqual(scoped[workload.label], all[workload.label])
assert.ok(scoped[workload.label].length > 0, 'the workload reads as covered')
})

test("the Changes scope keeps a changed rule's matched and excluded subjects, so its binaries still read", async () => {
// Two rules change their conditions only: one matches binaries under a prefix, one excludes a binary.
const edited = structuredClone(library)
const ruled = new Set(edited.relationships.filter(item => ['matches', 'excludes'].includes(item.kind)).map(item => item.from))
assert.equal(ruled.size, 2)
for (const rule of edited.entities.filter(entity => ruled.has(entity.id)))
rule.data = {
...rule.data,
conditions: [
...(rule.data.conditions ?? []),
{display: '`arg1` (`int`) = `4`', filter: 'matchArgs', index: 1, operator: 'Equal', values: ['4']},
],
}
const comparison = await interpreted(library, edited)
const model = comparisonModel(library, edited, comparison)
const rules = view =>
new Map(
view.categories.flatMap(category =>
category.hooks.flatMap(hook =>
hook.probes.flatMap(probe =>
probe.selectors.map(rule => [rule.row.id, rule.chips.map(chip => [chip.kind, chip.subject.entity.label])]),
),
),
),
)
const scoped = rules(changesView(model, comparison.metadata))
const all = rules(buildModel(model, 'diff', comparison.metadata))
const changed = [...ruled].map(id => model.entityRow.after.get(id))
for (const id of changed) {
assert.ok(scoped.get(id)?.length, `${id} shows its binaries`)
assert.deepEqual(scoped.get(id), all.get(id), id)
}
assert.deepEqual(new Set(changed.flatMap(id => scoped.get(id).map(([kind]) => kind))), new Set(['matches', 'excludes']))
})
Loading