From e78022e64207e7298229bf06606ade3965b05e3e Mon Sep 17 00:00:00 2001 From: Matt Jenkinson Date: Tue, 17 Nov 2026 13:40:00 -0500 Subject: [PATCH 1/2] feat: activity timeline rules for feedback An ActivityPolicy per kind turns people's feedback writes into sentences: votes, follows, feedback and reports sent, staff decisions, hiding posts and editing sources. The service's own system: users are left out. Rules for kinds Milo audits at Metadata only use the verb and object reference; a test compiles every rule and enforces that. --- CLAUDE.md | 8 +- README.md | 11 ++- config/milo/activity/feedbackprofiles.yaml | 13 ++++ config/milo/activity/feedbacksources.yaml | 19 +++++ config/milo/activity/kustomization.yaml | 17 +++++ config/milo/activity/posts.yaml | 21 +++++ config/milo/activity/securityreports.yaml | 20 +++++ .../milo/activity/securityreporttriages.yaml | 13 ++++ config/milo/activity/submissionreviews.yaml | 14 ++++ config/milo/activity/submissions.yaml | 17 +++++ config/milo/activity/subscriptions.yaml | 17 +++++ config/milo/activity/votes.yaml | 18 +++++ config/milo/kustomization.yaml | 5 +- go.mod | 3 + go.sum | 6 ++ test/config/activity_test.go | 76 +++++++++++++++++++ 16 files changed, 271 insertions(+), 7 deletions(-) create mode 100644 config/milo/activity/feedbackprofiles.yaml create mode 100644 config/milo/activity/feedbacksources.yaml create mode 100644 config/milo/activity/kustomization.yaml create mode 100644 config/milo/activity/posts.yaml create mode 100644 config/milo/activity/securityreports.yaml create mode 100644 config/milo/activity/securityreporttriages.yaml create mode 100644 config/milo/activity/submissionreviews.yaml create mode 100644 config/milo/activity/submissions.yaml create mode 100644 config/milo/activity/subscriptions.yaml create mode 100644 config/milo/activity/votes.yaml create mode 100644 test/config/activity_test.go diff --git a/CLAUDE.md b/CLAUDE.md index 7158701..912d359 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -31,9 +31,11 @@ datum-cloud/enhancements#943. `feedback-sync`. The operator owns post `votes` and `delivery`; both use server-side apply with their own field manager. Staff can only patch `spec.hidden` on posts. -- `Submission`, `SubmissionReview`, `SecurityReport` and - `SecurityReportTriage` are audited at `Metadata` only (a Milo audit policy - rule). Security reports are never indexed, synced or sent to the AI gateway. +- `Submission`, `SubmissionReview`, `SecurityReport`, `SecurityReportTriage` + and `FeedbackDigest` are audited at `Metadata` only (a rule in Milo's audit + policy in datum-cloud/infra), so their activity rules can't read request or + response bodies. Security reports are never indexed, synced or sent to the + AI gateway. ## Commands diff --git a/README.md b/README.md index 70df3c4..b75774a 100644 --- a/README.md +++ b/README.md @@ -100,6 +100,12 @@ GET /{submissions|securityreports}/{name}/attachments/{ref} (staff) - A `Fixed` or `Disclosed` report with public credit gets a `SecurityAcknowledgement` with the name, link, month and severity. It's deleted if that stops being true. - **`move-to-security` reconciler:** carries out `MoveToSecurity` review decisions. It creates a report owned by the submitter and named after the submission, copies the attachments, then deletes the submission, which takes its review and files with it. +## Audit and activity + +Milo audits feedback writes like any other CRD. Its audit policy (in `datum-cloud/infra`) logs feedback writes at `RequestResponse`, so search can index posts from the audit log. It logs `submissions`, `submissionreviews`, `securityreports`, `securityreporttriages` and `feedbackdigests`, with their subresources, at `Metadata` only: private context and vulnerability details never reach the audit log, activity or search. + +`config/milo/activity` holds an `ActivityPolicy` per kind, so people's actions read as sentences in activity timelines: voting, following, sending feedback or a report, staff decisions, hiding posts and editing sources. The service's own components are `system:` users and are left out. `test/config` compiles every rule and checks that rules for metadata-only kinds never read request or response bodies. + ## Roles | Role | For | @@ -130,8 +136,9 @@ config/crd/ generated CRDs, installed in Milo config/base/ operator and uploads Deployments, Services, leader election RBAC config/components/ milo-integration: Milo kubeconfig, client and webhook certificates config/milo/ applied to Milo: IAM (iam/), RBAC for the service's - components (rbac/), webhook config (webhook/), and - optional role bindings (bindings/) + components (rbac/), webhook config (webhook/), + activity rules (activity/), and optional role + bindings (bindings/) config/samples/ an example of every resource test/config/ checks config/ against the CRDs ``` diff --git a/config/milo/activity/feedbackprofiles.yaml b/config/milo/activity/feedbackprofiles.yaml new file mode 100644 index 0000000..5330e49 --- /dev/null +++ b/config/milo/activity/feedbackprofiles.yaml @@ -0,0 +1,13 @@ +# A user's feedback settings. +apiVersion: activity.miloapis.com/v1alpha1 +kind: ActivityPolicy +metadata: + name: feedback.miloapis.com-feedbackprofiles +spec: + resource: + apiGroup: feedback.miloapis.com + kind: FeedbackProfile + auditRules: + - name: update + match: "!audit.user.username.startsWith('system:') && audit.verb in ['create', 'update', 'patch'] && has(audit.responseStatus.code) && audit.responseStatus.code >= 200 && audit.responseStatus.code < 300 && !(has(audit.requestURI) && audit.requestURI.contains('dryRun='))" + summary: "{{ actor }} updated their feedback settings" diff --git a/config/milo/activity/feedbacksources.yaml b/config/milo/activity/feedbacksources.yaml new file mode 100644 index 0000000..38ca7a1 --- /dev/null +++ b/config/milo/activity/feedbacksources.yaml @@ -0,0 +1,19 @@ +# Which GitHub organizations and repositories feed the roadmap. +apiVersion: activity.miloapis.com/v1alpha1 +kind: ActivityPolicy +metadata: + name: feedback.miloapis.com-feedbacksources +spec: + resource: + apiGroup: feedback.miloapis.com + kind: FeedbackSource + auditRules: + - name: create + match: "!audit.user.username.startsWith('system:') && audit.verb == 'create' && has(audit.responseStatus.code) && audit.responseStatus.code >= 200 && audit.responseStatus.code < 300 && !(has(audit.requestURI) && audit.requestURI.contains('dryRun='))" + summary: "{{ actor }} added feedback source {{ link(audit.responseObject.metadata.name, audit.objectRef) }}" + - name: update + match: "!audit.user.username.startsWith('system:') && audit.verb in ['update', 'patch'] && !has(audit.objectRef.subresource) && has(audit.responseStatus.code) && audit.responseStatus.code >= 200 && audit.responseStatus.code < 300 && !(has(audit.requestURI) && audit.requestURI.contains('dryRun='))" + summary: "{{ actor }} updated feedback source {{ link(audit.objectRef.name, audit.objectRef) }}" + - name: delete + match: "!audit.user.username.startsWith('system:') && audit.verb == 'delete' && has(audit.responseStatus.code) && audit.responseStatus.code >= 200 && audit.responseStatus.code < 300 && !(has(audit.requestURI) && audit.requestURI.contains('dryRun='))" + summary: "{{ actor }} removed feedback source {{ audit.objectRef.name }}" diff --git a/config/milo/activity/kustomization.yaml b/config/milo/activity/kustomization.yaml new file mode 100644 index 0000000..46b9b5e --- /dev/null +++ b/config/milo/activity/kustomization.yaml @@ -0,0 +1,17 @@ +# ActivityPolicies: how feedback writes read in activity timelines. Only +# people's actions appear; the service's own components are system: users. +# Kinds logged at Metadata (submissions, reviews, security reports, triage) +# have rules that use only the verb and object reference. +apiVersion: kustomize.config.k8s.io/v1alpha1 +kind: Component + +resources: + - votes.yaml + - subscriptions.yaml + - submissions.yaml + - submissionreviews.yaml + - securityreports.yaml + - securityreporttriages.yaml + - posts.yaml + - feedbacksources.yaml + - feedbackprofiles.yaml diff --git a/config/milo/activity/posts.yaml b/config/milo/activity/posts.yaml new file mode 100644 index 0000000..eb91e1a --- /dev/null +++ b/config/milo/activity/posts.yaml @@ -0,0 +1,21 @@ +# Posts are logged in full. feedback-sync and the operator are system: +# actors, so only staff hiding and showing posts appears. A merge patch that +# sets spec.hidden reads as hiding or showing; other staff edits as updates. +apiVersion: activity.miloapis.com/v1alpha1 +kind: ActivityPolicy +metadata: + name: feedback.miloapis.com-posts +spec: + resource: + apiGroup: feedback.miloapis.com + kind: Post + auditRules: + - name: hide + match: "!audit.user.username.startsWith('system:') && audit.verb in ['update', 'patch'] && !has(audit.objectRef.subresource) && type(audit.requestObject) == map && has(audit.requestObject.spec) && type(audit.requestObject.spec) == map && has(audit.requestObject.spec.hidden) && audit.requestObject.spec.hidden == true && has(audit.responseStatus.code) && audit.responseStatus.code >= 200 && audit.responseStatus.code < 300 && !(has(audit.requestURI) && audit.requestURI.contains('dryRun='))" + summary: "{{ actor }} hid post {{ link(audit.objectRef.name, audit.objectRef) }}" + - name: show + match: "!audit.user.username.startsWith('system:') && audit.verb in ['update', 'patch'] && !has(audit.objectRef.subresource) && type(audit.requestObject) == map && has(audit.requestObject.spec) && type(audit.requestObject.spec) == map && has(audit.requestObject.spec.hidden) && audit.requestObject.spec.hidden == false && has(audit.responseStatus.code) && audit.responseStatus.code >= 200 && audit.responseStatus.code < 300 && !(has(audit.requestURI) && audit.requestURI.contains('dryRun='))" + summary: "{{ actor }} showed post {{ link(audit.objectRef.name, audit.objectRef) }} again" + - name: update + match: "!audit.user.username.startsWith('system:') && audit.verb in ['update', 'patch'] && !has(audit.objectRef.subresource) && has(audit.responseStatus.code) && audit.responseStatus.code >= 200 && audit.responseStatus.code < 300 && !(has(audit.requestURI) && audit.requestURI.contains('dryRun='))" + summary: "{{ actor }} updated post {{ link(audit.objectRef.name, audit.objectRef) }}" diff --git a/config/milo/activity/securityreports.yaml b/config/milo/activity/securityreports.yaml new file mode 100644 index 0000000..820c6c5 --- /dev/null +++ b/config/milo/activity/securityreports.yaml @@ -0,0 +1,20 @@ +# Security reports are logged at Metadata only: nothing about the +# vulnerability reaches the activity log. +apiVersion: activity.miloapis.com/v1alpha1 +kind: ActivityPolicy +metadata: + name: feedback.miloapis.com-securityreports +spec: + resource: + apiGroup: feedback.miloapis.com + kind: SecurityReport + auditRules: + - name: report + match: "!audit.user.username.startsWith('system:') && audit.verb == 'create' && has(audit.responseStatus.code) && audit.responseStatus.code >= 200 && audit.responseStatus.code < 300 && !(has(audit.requestURI) && audit.requestURI.contains('dryRun='))" + summary: "{{ actor }} sent a security report" + - name: update-status + match: "!audit.user.username.startsWith('system:') && audit.verb in ['update', 'patch'] && has(audit.objectRef.subresource) && audit.objectRef.subresource == 'status' && has(audit.responseStatus.code) && audit.responseStatus.code >= 200 && audit.responseStatus.code < 300 && !(has(audit.requestURI) && audit.requestURI.contains('dryRun='))" + summary: "{{ actor }} updated security report {{ link(audit.objectRef.name, audit.objectRef) }}" + - name: delete + match: "!audit.user.username.startsWith('system:') && audit.verb == 'delete' && has(audit.responseStatus.code) && audit.responseStatus.code >= 200 && audit.responseStatus.code < 300 && !(has(audit.requestURI) && audit.requestURI.contains('dryRun='))" + summary: "{{ actor }} deleted security report {{ audit.objectRef.name }}" diff --git a/config/milo/activity/securityreporttriages.yaml b/config/milo/activity/securityreporttriages.yaml new file mode 100644 index 0000000..2bba5c3 --- /dev/null +++ b/config/milo/activity/securityreporttriages.yaml @@ -0,0 +1,13 @@ +# Triage notes are logged at Metadata only. +apiVersion: activity.miloapis.com/v1alpha1 +kind: ActivityPolicy +metadata: + name: feedback.miloapis.com-securityreporttriages +spec: + resource: + apiGroup: feedback.miloapis.com + kind: SecurityReportTriage + auditRules: + - name: update + match: "!audit.user.username.startsWith('system:') && audit.verb in ['update', 'patch'] && has(audit.responseStatus.code) && audit.responseStatus.code >= 200 && audit.responseStatus.code < 300 && !(has(audit.requestURI) && audit.requestURI.contains('dryRun='))" + summary: "{{ actor }} updated the triage notes on security report {{ audit.objectRef.name }}" diff --git a/config/milo/activity/submissionreviews.yaml b/config/milo/activity/submissionreviews.yaml new file mode 100644 index 0000000..71009a1 --- /dev/null +++ b/config/milo/activity/submissionreviews.yaml @@ -0,0 +1,14 @@ +# Reviews are logged at Metadata only. Staff decide by updating the review; +# the decision can't change after that, so an update is the decision. +apiVersion: activity.miloapis.com/v1alpha1 +kind: ActivityPolicy +metadata: + name: feedback.miloapis.com-submissionreviews +spec: + resource: + apiGroup: feedback.miloapis.com + kind: SubmissionReview + auditRules: + - name: decide + match: "!audit.user.username.startsWith('system:') && audit.verb in ['update', 'patch'] && !has(audit.objectRef.subresource) && has(audit.responseStatus.code) && audit.responseStatus.code >= 200 && audit.responseStatus.code < 300 && !(has(audit.requestURI) && audit.requestURI.contains('dryRun='))" + summary: "{{ actor }} decided on submission {{ link(audit.objectRef.name, audit.objectRef) }}" diff --git a/config/milo/activity/submissions.yaml b/config/milo/activity/submissions.yaml new file mode 100644 index 0000000..c2849c8 --- /dev/null +++ b/config/milo/activity/submissions.yaml @@ -0,0 +1,17 @@ +# Submissions are logged at Metadata only, so rules use the verb and the +# object reference and never the request or response body. +apiVersion: activity.miloapis.com/v1alpha1 +kind: ActivityPolicy +metadata: + name: feedback.miloapis.com-submissions +spec: + resource: + apiGroup: feedback.miloapis.com + kind: Submission + auditRules: + - name: send + match: "!audit.user.username.startsWith('system:') && audit.verb == 'create' && has(audit.responseStatus.code) && audit.responseStatus.code >= 200 && audit.responseStatus.code < 300 && !(has(audit.requestURI) && audit.requestURI.contains('dryRun='))" + summary: "{{ actor }} sent feedback" + - name: delete + match: "!audit.user.username.startsWith('system:') && audit.verb == 'delete' && has(audit.responseStatus.code) && audit.responseStatus.code >= 200 && audit.responseStatus.code < 300 && !(has(audit.requestURI) && audit.requestURI.contains('dryRun='))" + summary: "{{ actor }} deleted submission {{ audit.objectRef.name }}" diff --git a/config/milo/activity/subscriptions.yaml b/config/milo/activity/subscriptions.yaml new file mode 100644 index 0000000..cb0a853 --- /dev/null +++ b/config/milo/activity/subscriptions.yaml @@ -0,0 +1,17 @@ +# Follows are logged in full. Follows the operator creates when someone +# votes or their submission is decided are system: actors and don't appear. +apiVersion: activity.miloapis.com/v1alpha1 +kind: ActivityPolicy +metadata: + name: feedback.miloapis.com-subscriptions +spec: + resource: + apiGroup: feedback.miloapis.com + kind: Subscription + auditRules: + - name: follow + match: "!audit.user.username.startsWith('system:') && audit.verb == 'create' && has(audit.requestObject.spec) && has(audit.requestObject.spec.postRef) && has(audit.responseStatus.code) && audit.responseStatus.code >= 200 && audit.responseStatus.code < 300 && !(has(audit.requestURI) && audit.requestURI.contains('dryRun='))" + summary: "{{ actor }} followed {{ link(audit.requestObject.spec.postRef.name, {'apiGroup': 'feedback.miloapis.com', 'apiVersion': 'v1alpha1', 'resource': 'posts', 'name': audit.requestObject.spec.postRef.name}) }}" + - name: unfollow + match: "!audit.user.username.startsWith('system:') && audit.verb == 'delete' && has(audit.responseObject.spec) && has(audit.responseObject.spec.postRef) && has(audit.responseStatus.code) && audit.responseStatus.code >= 200 && audit.responseStatus.code < 300 && !(has(audit.requestURI) && audit.requestURI.contains('dryRun='))" + summary: "{{ actor }} stopped following {{ link(audit.responseObject.spec.postRef.name, {'apiGroup': 'feedback.miloapis.com', 'apiVersion': 'v1alpha1', 'resource': 'posts', 'name': audit.responseObject.spec.postRef.name}) }}" diff --git a/config/milo/activity/votes.yaml b/config/milo/activity/votes.yaml new file mode 100644 index 0000000..c72ba3b --- /dev/null +++ b/config/milo/activity/votes.yaml @@ -0,0 +1,18 @@ +# Votes are logged in full (RequestResponse), so rules can name the post. +# The operator's writes (status, votes it gives submitters) are system: +# actors and don't appear. +apiVersion: activity.miloapis.com/v1alpha1 +kind: ActivityPolicy +metadata: + name: feedback.miloapis.com-votes +spec: + resource: + apiGroup: feedback.miloapis.com + kind: Vote + auditRules: + - name: vote + match: "!audit.user.username.startsWith('system:') && audit.verb == 'create' && has(audit.requestObject.spec) && has(audit.requestObject.spec.postRef) && has(audit.responseStatus.code) && audit.responseStatus.code >= 200 && audit.responseStatus.code < 300 && !(has(audit.requestURI) && audit.requestURI.contains('dryRun='))" + summary: "{{ actor }} voted on {{ link(audit.requestObject.spec.postRef.name, {'apiGroup': 'feedback.miloapis.com', 'apiVersion': 'v1alpha1', 'resource': 'posts', 'name': audit.requestObject.spec.postRef.name}) }}" + - name: unvote + match: "!audit.user.username.startsWith('system:') && audit.verb == 'delete' && has(audit.responseObject.spec) && has(audit.responseObject.spec.postRef) && has(audit.responseStatus.code) && audit.responseStatus.code >= 200 && audit.responseStatus.code < 300 && !(has(audit.requestURI) && audit.requestURI.contains('dryRun='))" + summary: "{{ actor }} removed their vote on {{ link(audit.responseObject.spec.postRef.name, {'apiGroup': 'feedback.miloapis.com', 'apiVersion': 'v1alpha1', 'resource': 'posts', 'name': audit.responseObject.spec.postRef.name}) }}" diff --git a/config/milo/kustomization.yaml b/config/milo/kustomization.yaml index 95beac3..4303f2e 100644 --- a/config/milo/kustomization.yaml +++ b/config/milo/kustomization.yaml @@ -1,6 +1,6 @@ # What the service registers in Milo: ProtectedResources and Roles (iam/), -# Kubernetes RBAC for the service's own components (rbac/) and the admission -# webhooks Milo calls (webhook/). The CRDs are in config/crd. bindings/ is +# Kubernetes RBAC for the service's own components (rbac/), the admission +# webhooks Milo calls (webhook/) and activity timeline rules (activity/). The CRDs are in config/crd. bindings/ is # separate and optional: who holds the roles is the deployment's choice. apiVersion: kustomize.config.k8s.io/v1alpha1 kind: Component @@ -9,3 +9,4 @@ components: - iam/ - rbac/ - webhook/ + - activity/ diff --git a/go.mod b/go.mod index 8d294a1..bfc3c10 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ toolchain go1.27.4 require ( github.com/evanphx/json-patch/v5 v5.9.11 + github.com/google/cel-go v0.29.2 github.com/johannesboyne/gofakes3 v1.2.0 github.com/spf13/cobra v1.10.2 gocloud.dev v0.48.0 @@ -28,6 +29,7 @@ require ( cloud.google.com/go/storage v1.69.0 // indirect github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/metric v0.62.0 // indirect github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/resourcemapping v0.62.0 // indirect + github.com/antlr4-go/antlr/v4 v4.13.1 // indirect github.com/aws/aws-sdk-go-v2 v1.47.3 // indirect github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.7.22 // indirect github.com/aws/aws-sdk-go-v2/config v1.33.9 // indirect @@ -113,6 +115,7 @@ require ( go.yaml.in/yaml/v2 v2.4.4 // indirect go.yaml.in/yaml/v3 v3.0.6 // indirect golang.org/x/crypto v0.58.0 // indirect + golang.org/x/exp v0.0.0-20261014143641-f0251eb4aa3e // indirect golang.org/x/mod v0.42.0 // indirect golang.org/x/net v0.62.0 // indirect golang.org/x/oauth2 v0.37.0 // indirect diff --git a/go.sum b/go.sum index 27c1c9e..944853c 100644 --- a/go.sum +++ b/go.sum @@ -28,6 +28,8 @@ github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/resourcemapp github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/resourcemapping v0.62.0/go.mod h1:sg6Wqbl0BKKZ5zKnQj2q0qic/3ziF894SCx1LpbX37Y= github.com/Masterminds/semver/v3 v3.4.0 h1:Zog+i5UMtVoCU8oKka5P7i9q9HgrJeGzI9SA1Xbatp0= github.com/Masterminds/semver/v3 v3.4.0/go.mod h1:4V+yj/TJE1HU9XfppCwVMZq3I84lprf4nC11bSS5beM= +github.com/antlr4-go/antlr/v4 v4.13.1 h1:SqQKkuVZ+zWkMMNkjy5FZe5mr5WURWnlpmOuzYWrPrQ= +github.com/antlr4-go/antlr/v4 v4.13.1/go.mod h1:GKmUxMtwp6ZgGwZSva4eWPC5mS6vUAmOABFgjdkM7Nw= github.com/aws/aws-sdk-go-v2 v1.47.3 h1:HRoT/MkNi3bjO5xF4dCzLzvVmkvRGHj3/cLINDnJejk= github.com/aws/aws-sdk-go-v2 v1.47.3/go.mod h1:xSl+OSI5ZUw6bSMupWc218zE5Pl+65MCCu3ySWFY99A= github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.7.22 h1:oiEmPz/YMMeh/JvAE8xSxNCIsvWE6lpFe4ZPlyFykQI= @@ -148,6 +150,8 @@ github.com/go-task/slim-sprig/v3 v3.0.0 h1:sUs3vkvUymDpBKi3qH1YSqBQk9+9D/8M2mN1v github.com/go-task/slim-sprig/v3 v3.0.0/go.mod h1:W848ghGpv3Qj3dhTPRyJypKRiqCdHZiAzKg9hl15HA8= github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek= github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps= +github.com/google/cel-go v0.29.2 h1:ZtDxkeiMmz0mxbKDYiNkE5Lk7V5edMRcaaDf2jX002k= +github.com/google/cel-go v0.29.2/go.mod h1:X0bD6iVNR8pkROSOoHVdgTkzmRcosof7WQqCD6wcMc8= github.com/google/gnostic-models v0.7.0 h1:qwTtogB15McXDaNqTZdzPJRHvaVJlAl+HVQnLmJEJxo= github.com/google/gnostic-models v0.7.0/go.mod h1:whL5G0m6dmc5cPxKc5bdKdEN3UjI7OUGxBlw57miDrQ= github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= @@ -273,6 +277,8 @@ gocloud.dev v0.48.0 h1:rEPS6euyhuDMlEKH7E1sztIHHwnABeiAiQmyiaHiPW0= gocloud.dev v0.48.0/go.mod h1:uN+hz2x8ieyW5RjeMetvmpV/nRqFOr7DF0CKVY0Owp8= golang.org/x/crypto v0.58.0 h1:COkYLr4k7nDI4r6QtMML2AfzeIswWHAJthLS6K/M/54= golang.org/x/crypto v0.58.0/go.mod h1:Xh+kl5A+M0gMiWDytH2Hhr5Z7WbNCpv/eB/EFgfSk+w= +golang.org/x/exp v0.0.0-20261014143641-f0251eb4aa3e h1:HgI6W1OolKK6tcV74is+rl9SMT0njsHlp0TbQiGXI/A= +golang.org/x/exp v0.0.0-20261014143641-f0251eb4aa3e/go.mod h1:DBoxwWhjaW8DJJw/YeM4zpeOKMC1FMXSblsYy+1mFSw= golang.org/x/mod v0.42.0 h1:ICeL3t2L30ljSPHHxDnR4fqVcBt/zR2zJAb9juavmQg= golang.org/x/mod v0.42.0/go.mod h1:u5fzIrRInkL8MaO1cbRkUHT64v303YIT5KEqpNr7jRs= golang.org/x/net v0.62.0 h1:18ELalfM5+XNRwzM2nYqXsZQ9ByS1uhj2O/U7+5jqRY= diff --git a/test/config/activity_test.go b/test/config/activity_test.go new file mode 100644 index 0000000..27c9c50 --- /dev/null +++ b/test/config/activity_test.go @@ -0,0 +1,76 @@ +package config + +import ( + "regexp" + "strings" + "testing" + + "github.com/google/cel-go/cel" +) + +// MetadataOnly are the kinds Milo's audit policy logs without request or +// response bodies (datum-cloud/infra, the Milo audit policy), because they +// carry private context or vulnerability details. Activity rules for them can +// only use the verb and object reference. +var MetadataOnly = []string{"submissions", "submissionreviews", "securityreports", "securityreporttriages", "feedbackdigests"} + +var templateRE = regexp.MustCompile(`{{\s*(.*?)\s*}}`) + +// TestActivityPolicies compiles every rule's match expression and summary +// template, checks each policy names a feedback CRD, and checks that rules for +// metadata-only kinds don't read bodies that won't be there. +func TestActivityPolicies(t *testing.T) { + crds := loadCRDs(t) + kinds := map[string]string{} + for plural, c := range crds { + kinds[c.kind] = plural + } + env, err := cel.NewEnv( + cel.Variable("audit", cel.DynType), + cel.Variable("actor", cel.StringType), + cel.Function("link", cel.Overload("link_dyn_dyn", []*cel.Type{cel.DynType, cel.DynType}, cel.StringType)), + ) + if err != nil { + t.Fatal(err) + } + compile := func(where, expr string) { + if _, iss := env.Compile(expr); iss.Err() != nil { + t.Errorf("%s: %q: %v", where, expr, iss.Err()) + } + } + + policies := ofKind(load(t, "../../config/milo/activity"), "ActivityPolicy") + if len(policies) == 0 { + t.Fatal("no ActivityPolicies") + } + for _, p := range policies { + name := p.str("metadata", "name") + if p.str("spec", "resource", "apiGroup") != group { + t.Errorf("%s: not for %s", name, group) + } + plural, ok := kinds[p.str("spec", "resource", "kind")] + if !ok { + t.Errorf("%s: no CRD for kind %s", name, p.str("spec", "resource", "kind")) + continue + } + metadataOnly := false + for _, m := range MetadataOnly { + metadataOnly = metadataOnly || m == plural + } + for _, r := range p.list("spec", "auditRules") { + ro := obj(r.(map[string]any)) + where := name + "/" + ro.str("name") + match, summary := ro.str("match"), ro.str("summary") + compile(where+" match", match) + for _, sub := range templateRE.FindAllStringSubmatch(summary, -1) { + compile(where+" summary", sub[1]) + } + if !strings.Contains(match, "!audit.user.username.startsWith('system:')") { + t.Errorf("%s: doesn't leave out the service's own writes", where) + } + if metadataOnly && (strings.Contains(match+summary, "requestObject") || strings.Contains(match+summary, "responseObject")) { + t.Errorf("%s: %s is logged at Metadata only, so the rule can't read request or response bodies", where, plural) + } + } + } +} From 1bf67c566a4aa1e6e3621f6f52846673cd8d00b7 Mon Sep 17 00:00:00 2001 From: Matt Jenkinson Date: Tue, 17 Nov 2026 16:44:03 -0500 Subject: [PATCH 2/2] fix(activity): name the post on unvote/unfollow and drop security-report activity CRD deletes return a Status in the audit event, not the deleted object, so the unvote and unfollow rules that read responseObject.spec never matched. A ValidatingAdmissionPolicy with no validations now adds the post's name to vote and subscription deletes as the audit annotation post-ref.feedback.miloapis.com/post, and the rules read it there. Without the annotation the entries still appear, without the post. The activity service files entries under the actor's context with no per-entry access check, and platform-scope readers see every entry, so security report and triage entries would reveal that reports exist. Their ActivityPolicies are removed. Tests evaluate rules in the activity service's CEL environment against delete events written by an envtest API server, check no delete rule reads request or response bodies, and check no policy covers security reports. --- CLAUDE.md | 7 +- README.md | 2 +- config/milo/activity/kustomization.yaml | 16 +- config/milo/activity/post-ref.yaml | 41 +++ config/milo/activity/securityreports.yaml | 20 -- .../milo/activity/securityreporttriages.yaml | 13 - config/milo/activity/subscriptions.yaml | 14 +- config/milo/activity/votes.yaml | 16 +- test/config/activity_envtest_test.go | 166 ++++++++++++ test/config/activity_test.go | 236 +++++++++++++++++- .../testdata/audit/subscription-delete.json | 71 ++++++ test/config/testdata/audit/vote-delete.json | 71 ++++++ 12 files changed, 615 insertions(+), 58 deletions(-) create mode 100644 config/milo/activity/post-ref.yaml delete mode 100644 config/milo/activity/securityreports.yaml delete mode 100644 config/milo/activity/securityreporttriages.yaml create mode 100644 test/config/activity_envtest_test.go create mode 100644 test/config/testdata/audit/subscription-delete.json create mode 100644 test/config/testdata/audit/vote-delete.json diff --git a/CLAUDE.md b/CLAUDE.md index 912d359..22967c5 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -35,7 +35,12 @@ datum-cloud/enhancements#943. and `FeedbackDigest` are audited at `Metadata` only (a rule in Milo's audit policy in datum-cloud/infra), so their activity rules can't read request or response bodies. Security reports are never indexed, synced or sent to the - AI gateway. + AI gateway, and have no ActivityPolicy: platform-timeline readers see every + activity entry, whoever made it. +- A delete's audit event has a `Status` response, never the object. Activity + rules for deletes use `objectRef` or an audit annotation (see + `config/milo/activity/post-ref.yaml`); the activity service's CEL has no + string functions, so names can't be split. ## Commands diff --git a/README.md b/README.md index b75774a..38395ef 100644 --- a/README.md +++ b/README.md @@ -104,7 +104,7 @@ GET /{submissions|securityreports}/{name}/attachments/{ref} (staff) Milo audits feedback writes like any other CRD. Its audit policy (in `datum-cloud/infra`) logs feedback writes at `RequestResponse`, so search can index posts from the audit log. It logs `submissions`, `submissionreviews`, `securityreports`, `securityreporttriages` and `feedbackdigests`, with their subresources, at `Metadata` only: private context and vulnerability details never reach the audit log, activity or search. -`config/milo/activity` holds an `ActivityPolicy` per kind, so people's actions read as sentences in activity timelines: voting, following, sending feedback or a report, staff decisions, hiding posts and editing sources. The service's own components are `system:` users and are left out. `test/config` compiles every rule and checks that rules for metadata-only kinds never read request or response bodies. +`config/milo/activity` holds an `ActivityPolicy` per kind, so people's actions read as sentences in activity timelines: voting, following, sending feedback, staff decisions, hiding posts and editing sources. The service's own components are `system:` users and are left out. A delete's audit event carries a `Status`, not the deleted object, so a `ValidatingAdmissionPolicy` (`post-ref.yaml`) adds the post's name to vote and subscription deletes as the audit annotation `post-ref.feedback.miloapis.com/post`, and the unvote and unfollow rules read it there. Security reports and their triage have no `ActivityPolicy`: the activity service shows every entry to anyone who can read the platform timeline, with no per-entry access check, so an entry would reveal that a report exists. `test/config` compiles every rule in the activity service's CEL environment, checks that rules never read bodies a metadata-only kind or a delete won't have, renders delete events written by an envtest API server, and checks no policy covers security reports. ## Roles diff --git a/config/milo/activity/kustomization.yaml b/config/milo/activity/kustomization.yaml index 46b9b5e..9222c38 100644 --- a/config/milo/activity/kustomization.yaml +++ b/config/milo/activity/kustomization.yaml @@ -1,17 +1,25 @@ # ActivityPolicies: how feedback writes read in activity timelines. Only # people's actions appear; the service's own components are system: users. -# Kinds logged at Metadata (submissions, reviews, security reports, triage) -# have rules that use only the verb and object reference. +# Kinds logged at Metadata (submissions, reviews) have rules that use only +# the verb and object reference. post-ref.yaml is the admission policy that +# names the post in vote and subscription delete events. +# +# Security reports and their triage have no ActivityPolicy, so they make no +# activity entries at all. The activity service files an entry under the +# actor's context and offers no per-entry access check: anyone who can read +# the platform timeline sees every entry, including a reporter's own, and +# the Security Team works in the platform context. An entry would tell those +# readers that a report exists and what staff did with it, which only the +# Security Team (and the reporter) may know. The audit log keeps the record. apiVersion: kustomize.config.k8s.io/v1alpha1 kind: Component resources: + - post-ref.yaml - votes.yaml - subscriptions.yaml - submissions.yaml - submissionreviews.yaml - - securityreports.yaml - - securityreporttriages.yaml - posts.yaml - feedbacksources.yaml - feedbackprofiles.yaml diff --git a/config/milo/activity/post-ref.yaml b/config/milo/activity/post-ref.yaml new file mode 100644 index 0000000..0ee431d --- /dev/null +++ b/config/milo/activity/post-ref.yaml @@ -0,0 +1,41 @@ +# Names the post in the audit event when someone removes a vote or stops +# following a post, so the activity rules in votes.yaml and subscriptions.yaml +# can say which post it was. +# +# A delete's audit event has no object body: the API server answers a custom +# resource delete with a Status (name, group, resource and UID only) and the +# request body, if any, is DeleteOptions. The name is ., but the +# activity service's CEL has no string functions to split it. Admission sees +# the object being deleted (oldObject), and an audit annotation from a +# ValidatingAdmissionPolicy is recorded at every audit level as +# "post-ref.feedback.miloapis.com/post". +# +# The policy has no validations, so it never refuses a delete, and it runs in +# the API server, so there is no webhook to be unavailable. With +# failurePolicy Ignore, an evaluation error leaves the annotation out and the +# activity entry falls back to not naming the post. +apiVersion: admissionregistration.k8s.io/v1 +kind: ValidatingAdmissionPolicy +metadata: + name: post-ref.feedback.miloapis.com +spec: + failurePolicy: Ignore + matchConstraints: + resourceRules: + - apiGroups: ["feedback.miloapis.com"] + apiVersions: ["*"] + operations: ["DELETE"] + resources: ["votes", "subscriptions"] + auditAnnotations: + - key: post + valueExpression: "string(oldObject.spec.postRef.name)" +--- +apiVersion: admissionregistration.k8s.io/v1 +kind: ValidatingAdmissionPolicyBinding +metadata: + name: post-ref.feedback.miloapis.com +spec: + policyName: post-ref.feedback.miloapis.com + # There are no validations to act on; audit annotations are recorded + # whatever the actions are. + validationActions: ["Audit"] diff --git a/config/milo/activity/securityreports.yaml b/config/milo/activity/securityreports.yaml deleted file mode 100644 index 820c6c5..0000000 --- a/config/milo/activity/securityreports.yaml +++ /dev/null @@ -1,20 +0,0 @@ -# Security reports are logged at Metadata only: nothing about the -# vulnerability reaches the activity log. -apiVersion: activity.miloapis.com/v1alpha1 -kind: ActivityPolicy -metadata: - name: feedback.miloapis.com-securityreports -spec: - resource: - apiGroup: feedback.miloapis.com - kind: SecurityReport - auditRules: - - name: report - match: "!audit.user.username.startsWith('system:') && audit.verb == 'create' && has(audit.responseStatus.code) && audit.responseStatus.code >= 200 && audit.responseStatus.code < 300 && !(has(audit.requestURI) && audit.requestURI.contains('dryRun='))" - summary: "{{ actor }} sent a security report" - - name: update-status - match: "!audit.user.username.startsWith('system:') && audit.verb in ['update', 'patch'] && has(audit.objectRef.subresource) && audit.objectRef.subresource == 'status' && has(audit.responseStatus.code) && audit.responseStatus.code >= 200 && audit.responseStatus.code < 300 && !(has(audit.requestURI) && audit.requestURI.contains('dryRun='))" - summary: "{{ actor }} updated security report {{ link(audit.objectRef.name, audit.objectRef) }}" - - name: delete - match: "!audit.user.username.startsWith('system:') && audit.verb == 'delete' && has(audit.responseStatus.code) && audit.responseStatus.code >= 200 && audit.responseStatus.code < 300 && !(has(audit.requestURI) && audit.requestURI.contains('dryRun='))" - summary: "{{ actor }} deleted security report {{ audit.objectRef.name }}" diff --git a/config/milo/activity/securityreporttriages.yaml b/config/milo/activity/securityreporttriages.yaml deleted file mode 100644 index 2bba5c3..0000000 --- a/config/milo/activity/securityreporttriages.yaml +++ /dev/null @@ -1,13 +0,0 @@ -# Triage notes are logged at Metadata only. -apiVersion: activity.miloapis.com/v1alpha1 -kind: ActivityPolicy -metadata: - name: feedback.miloapis.com-securityreporttriages -spec: - resource: - apiGroup: feedback.miloapis.com - kind: SecurityReportTriage - auditRules: - - name: update - match: "!audit.user.username.startsWith('system:') && audit.verb in ['update', 'patch'] && has(audit.responseStatus.code) && audit.responseStatus.code >= 200 && audit.responseStatus.code < 300 && !(has(audit.requestURI) && audit.requestURI.contains('dryRun='))" - summary: "{{ actor }} updated the triage notes on security report {{ audit.objectRef.name }}" diff --git a/config/milo/activity/subscriptions.yaml b/config/milo/activity/subscriptions.yaml index cb0a853..50d1d18 100644 --- a/config/milo/activity/subscriptions.yaml +++ b/config/milo/activity/subscriptions.yaml @@ -1,5 +1,8 @@ -# Follows are logged in full. Follows the operator creates when someone -# votes or their submission is decided are system: actors and don't appear. +# Follows are logged in full, so the follow rule reads the post from the +# request. A delete's audit event has no subscription in it, so the unfollow +# rule reads the post from the audit annotation post-ref.yaml adds. Follows +# the operator creates when someone votes or their submission is decided are +# system: actors and don't appear. apiVersion: activity.miloapis.com/v1alpha1 kind: ActivityPolicy metadata: @@ -13,5 +16,8 @@ spec: match: "!audit.user.username.startsWith('system:') && audit.verb == 'create' && has(audit.requestObject.spec) && has(audit.requestObject.spec.postRef) && has(audit.responseStatus.code) && audit.responseStatus.code >= 200 && audit.responseStatus.code < 300 && !(has(audit.requestURI) && audit.requestURI.contains('dryRun='))" summary: "{{ actor }} followed {{ link(audit.requestObject.spec.postRef.name, {'apiGroup': 'feedback.miloapis.com', 'apiVersion': 'v1alpha1', 'resource': 'posts', 'name': audit.requestObject.spec.postRef.name}) }}" - name: unfollow - match: "!audit.user.username.startsWith('system:') && audit.verb == 'delete' && has(audit.responseObject.spec) && has(audit.responseObject.spec.postRef) && has(audit.responseStatus.code) && audit.responseStatus.code >= 200 && audit.responseStatus.code < 300 && !(has(audit.requestURI) && audit.requestURI.contains('dryRun='))" - summary: "{{ actor }} stopped following {{ link(audit.responseObject.spec.postRef.name, {'apiGroup': 'feedback.miloapis.com', 'apiVersion': 'v1alpha1', 'resource': 'posts', 'name': audit.responseObject.spec.postRef.name}) }}" + match: "!audit.user.username.startsWith('system:') && audit.verb == 'delete' && has(audit.annotations) && 'post-ref.feedback.miloapis.com/post' in audit.annotations && has(audit.responseStatus.code) && audit.responseStatus.code >= 200 && audit.responseStatus.code < 300 && !(has(audit.requestURI) && audit.requestURI.contains('dryRun='))" + summary: "{{ actor }} stopped following {{ link(audit.annotations['post-ref.feedback.miloapis.com/post'], {'apiGroup': 'feedback.miloapis.com', 'apiVersion': 'v1alpha1', 'resource': 'posts', 'name': audit.annotations['post-ref.feedback.miloapis.com/post']}) }}" + - name: unfollow-post-unknown + match: "!audit.user.username.startsWith('system:') && audit.verb == 'delete' && has(audit.responseStatus.code) && audit.responseStatus.code >= 200 && audit.responseStatus.code < 300 && !(has(audit.requestURI) && audit.requestURI.contains('dryRun='))" + summary: "{{ actor }} stopped following a post" diff --git a/config/milo/activity/votes.yaml b/config/milo/activity/votes.yaml index c72ba3b..b651dd9 100644 --- a/config/milo/activity/votes.yaml +++ b/config/milo/activity/votes.yaml @@ -1,6 +1,9 @@ -# Votes are logged in full (RequestResponse), so rules can name the post. -# The operator's writes (status, votes it gives submitters) are system: -# actors and don't appear. +# Votes are logged in full (RequestResponse), so the create rule reads the +# post from the request. A delete's audit event has no vote in it (the +# response is a Status, the request DeleteOptions), so the unvote rule reads +# the post from the audit annotation post-ref.yaml adds; without it, the entry +# doesn't name the post. The operator's writes (status, votes it gives +# submitters) are system: actors and don't appear. apiVersion: activity.miloapis.com/v1alpha1 kind: ActivityPolicy metadata: @@ -14,5 +17,8 @@ spec: match: "!audit.user.username.startsWith('system:') && audit.verb == 'create' && has(audit.requestObject.spec) && has(audit.requestObject.spec.postRef) && has(audit.responseStatus.code) && audit.responseStatus.code >= 200 && audit.responseStatus.code < 300 && !(has(audit.requestURI) && audit.requestURI.contains('dryRun='))" summary: "{{ actor }} voted on {{ link(audit.requestObject.spec.postRef.name, {'apiGroup': 'feedback.miloapis.com', 'apiVersion': 'v1alpha1', 'resource': 'posts', 'name': audit.requestObject.spec.postRef.name}) }}" - name: unvote - match: "!audit.user.username.startsWith('system:') && audit.verb == 'delete' && has(audit.responseObject.spec) && has(audit.responseObject.spec.postRef) && has(audit.responseStatus.code) && audit.responseStatus.code >= 200 && audit.responseStatus.code < 300 && !(has(audit.requestURI) && audit.requestURI.contains('dryRun='))" - summary: "{{ actor }} removed their vote on {{ link(audit.responseObject.spec.postRef.name, {'apiGroup': 'feedback.miloapis.com', 'apiVersion': 'v1alpha1', 'resource': 'posts', 'name': audit.responseObject.spec.postRef.name}) }}" + match: "!audit.user.username.startsWith('system:') && audit.verb == 'delete' && has(audit.annotations) && 'post-ref.feedback.miloapis.com/post' in audit.annotations && has(audit.responseStatus.code) && audit.responseStatus.code >= 200 && audit.responseStatus.code < 300 && !(has(audit.requestURI) && audit.requestURI.contains('dryRun='))" + summary: "{{ actor }} removed their vote on {{ link(audit.annotations['post-ref.feedback.miloapis.com/post'], {'apiGroup': 'feedback.miloapis.com', 'apiVersion': 'v1alpha1', 'resource': 'posts', 'name': audit.annotations['post-ref.feedback.miloapis.com/post']}) }}" + - name: unvote-post-unknown + match: "!audit.user.username.startsWith('system:') && audit.verb == 'delete' && has(audit.responseStatus.code) && audit.responseStatus.code >= 200 && audit.responseStatus.code < 300 && !(has(audit.requestURI) && audit.requestURI.contains('dryRun='))" + summary: "{{ actor }} removed a vote" diff --git a/test/config/activity_envtest_test.go b/test/config/activity_envtest_test.go new file mode 100644 index 0000000..cc57dad --- /dev/null +++ b/test/config/activity_envtest_test.go @@ -0,0 +1,166 @@ +package config + +import ( + "bufio" + "context" + "encoding/json" + "os" + "path/filepath" + "testing" + "time" + + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + "k8s.io/apimachinery/pkg/runtime" + "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/envtest" + + v1 "go.miloapis.com/feedback/pkg/apis/feedback/v1alpha1" +) + +// auditPolicy logs feedback writes at RequestResponse, as Milo's audit +// policy does for votes and subscriptions. +const auditPolicy = `apiVersion: audit.k8s.io/v1 +kind: Policy +omitStages: ["RequestReceived"] +rules: + - level: RequestResponse + resources: + - group: feedback.miloapis.com + - level: None +` + +// TestDeleteActivityInAnAPIServer deletes a vote and a subscription in an +// envtest API server with the CRDs, post-ref.yaml and audit logging, and runs +// the audit events it writes through the activity policies. It checks the +// delete event has no spec, only a Status, and that the entries name the +// post from post-ref.yaml's audit annotation. +func TestDeleteActivityInAnAPIServer(t *testing.T) { + if os.Getenv("KUBEBUILDER_ASSETS") == "" { + t.Skip("KUBEBUILDER_ASSETS not set; run with task test") + } + dir := t.TempDir() + policyFile, logFile := filepath.Join(dir, "policy.yaml"), filepath.Join(dir, "audit.log") + if err := os.WriteFile(policyFile, []byte(auditPolicy), 0o600); err != nil { + t.Fatal(err) + } + env := &envtest.Environment{ + CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")}, + ErrorIfCRDPathMissing: true, + } + env.ControlPlane.GetAPIServer().Configure(). + Set("audit-policy-file", policyFile). + Set("audit-log-path", logFile). + Set("audit-log-format", "json"). + Set("audit-log-mode", "blocking") + cfg, err := env.Start() + if err != nil { + t.Fatalf("start API server: %v", err) + } + t.Cleanup(func() { _ = env.Stop() }) + + scheme := runtime.NewScheme() + if err := v1.AddToScheme(scheme); err != nil { + t.Fatal(err) + } + ctx := context.Background() + admin, err := client.New(cfg, client.Options{Scheme: scheme}) + if err != nil { + t.Fatal(err) + } + for _, o := range load(t, "../../config/milo/activity") { + if k := o.str("kind"); k == "ValidatingAdmissionPolicy" || k == "ValidatingAdmissionPolicyBinding" { + if err := admin.Create(ctx, &unstructured.Unstructured{Object: o}); err != nil { + t.Fatal(err) + } + } + } + // A person, not a system: user, so the rules don't leave them out. + alice, err := env.AddUser(envtest.User{Name: "alice@example.com", Groups: []string{"system:masters"}}, nil) + if err != nil { + t.Fatal(err) + } + as, err := client.New(alice.Config(), client.Options{Scheme: scheme}) + if err != nil { + t.Fatal(err) + } + + const post, user = "datum-cloud.enhancements.912", "312983457298312345" + ps := policies(t) + for _, tc := range []struct { + plural, summary string + obj func() client.Object + }{ + {"votes", "alice@example.com removed their vote on " + post, func() client.Object { + return &v1.Vote{ + ObjectMeta: metaName(v1.OwnedName(post, user)), + Spec: v1.VoteSpec{PostRef: v1.PostReference{Name: post}, UserRef: v1.UserReference{Name: user}, Source: v1.ClientWebsite}, + } + }}, + {"subscriptions", "alice@example.com stopped following " + post, func() client.Object { + return &v1.Subscription{ + ObjectMeta: metaName(v1.OwnedName(post, user)), + Spec: v1.SubscriptionSpec{PostRef: v1.PostReference{Name: post}, UserRef: v1.UserReference{Name: user}, Reason: v1.SubscriptionReasonManual}, + } + }}, + } { + t.Run(tc.plural, func(t *testing.T) { + // The policy takes effect once the API server has loaded it, so + // try a few times. + var event map[string]any + for i := 0; i < 30; i++ { + o := tc.obj() + if err := as.Create(ctx, o); err != nil { + t.Fatal(err) + } + if err := as.Delete(ctx, o); err != nil { + t.Fatal(err) + } + event = lastDelete(t, logFile, tc.plural) + if event != nil && obj(event).str("annotations", postRefAnnotation) != "" { + break + } + time.Sleep(200 * time.Millisecond) + } + if event == nil { + t.Fatal("no delete audit event") + } + checkDeleteEventShape(t, event) + if got := obj(event).str("annotations", postRefAnnotation); got != post { + t.Fatalf("annotation %s = %q, want %q", postRefAnnotation, got, post) + } + got, ok := render(t, ps[tc.plural], event) + if !ok || got.summary != tc.summary { + t.Errorf("rendered %q (rule %s, matched %v), want %q", got.summary, got.rule, ok, tc.summary) + } + }) + } +} + +// lastDelete returns the last completed delete of plural in the audit log. +func lastDelete(t *testing.T, path, plural string) map[string]any { + t.Helper() + f, err := os.Open(path) + if err != nil { + t.Fatal(err) + } + defer func() { _ = f.Close() }() + var last map[string]any + sc := bufio.NewScanner(f) + sc.Buffer(make([]byte, 1<<20), 1<<20) + for sc.Scan() { + var e map[string]any + if err := json.Unmarshal(sc.Bytes(), &e); err != nil { + t.Fatal(err) + } + if obj(e).str("verb") == "delete" && obj(e).str("stage") == "ResponseComplete" && obj(e).str("objectRef", "resource") == plural { + last = e + } + } + if err := sc.Err(); err != nil { + t.Fatal(err) + } + return last +} + +func metaName(name string) metav1.ObjectMeta { return metav1.ObjectMeta{Name: name} } diff --git a/test/config/activity_test.go b/test/config/activity_test.go index 27c9c50..1947a75 100644 --- a/test/config/activity_test.go +++ b/test/config/activity_test.go @@ -1,35 +1,164 @@ package config import ( + "encoding/json" + "fmt" + "os" + "path/filepath" + "reflect" "regexp" "strings" "testing" "github.com/google/cel-go/cel" + "github.com/google/cel-go/common/types" + "github.com/google/cel-go/common/types/ref" ) // MetadataOnly are the kinds Milo's audit policy logs without request or // response bodies (datum-cloud/infra, the Milo audit policy), because they // carry private context or vulnerability details. Activity rules for them can -// only use the verb and object reference. +// only use the verb, object reference and audit annotations. var MetadataOnly = []string{"submissions", "submissionreviews", "securityreports", "securityreporttriages", "feedbackdigests"} -var templateRE = regexp.MustCompile(`{{\s*(.*?)\s*}}`) +// NoActivity are the kinds that must have no ActivityPolicy. The activity +// service files entries under the actor's context with no per-entry access +// check, so platform-timeline readers would learn a report exists and what +// staff did with it. +var NoActivity = []string{"securityreports", "securityreporttriages"} + +// postRefAnnotation is the audit annotation config/milo/activity/post-ref.yaml +// adds to vote and subscription deletes. +const postRefAnnotation = "post-ref.feedback.miloapis.com/post" + +// templateRE is the activity service's summary template pattern +// (internal/cel/policy.go in milo-os/activity). +var templateRE = regexp.MustCompile(`\{\{\s*(.+?)\s*\}\}`) + +type link struct { + text string + ref any +} + +// auditEnv is the activity service's CEL environment for audit rules +// (NewAuditEnvironment in milo-os/activity internal/cel/environment.go): the +// standard library only, so no split or substring. link records its calls in +// links when links isn't nil. +func auditEnv(links *[]link) (*cel.Env, error) { + return cel.NewEnv( + cel.Variable("audit", cel.MapType(cel.StringType, cel.DynType)), + cel.Variable("actor", cel.StringType), + cel.Variable("actorRef", cel.MapType(cel.StringType, cel.DynType)), + cel.Variable("kind", cel.StringType), + cel.Function("link", + cel.Overload("link_string_dyn", []*cel.Type{cel.StringType, cel.DynType}, cel.StringType, + cel.BinaryBinding(func(text, r ref.Val) ref.Val { + if links != nil { + native, err := r.ConvertToNative(reflect.TypeOf(map[string]any{})) + if err != nil { + native = r.Value() + } + *links = append(*links, link{fmt.Sprint(text.Value()), native}) + } + return types.String(fmt.Sprint(text.Value())) + }), + ), + ), + ) +} + +// auditVars is the activity service's BuildAuditVars: absent nested maps +// become empty ones so has() on their fields is safe. +func auditVars(event map[string]any) map[string]any { + a := map[string]any{} + for k, v := range event { + a[k] = v + } + for _, f := range []string{"objectRef", "user", "responseStatus", "responseObject", "requestObject"} { + if _, ok := a[f]; !ok { + a[f] = map[string]any{} + } + } + user := obj(a).str("user", "username") + return map[string]any{ + "audit": a, + "actor": user, + "actorRef": map[string]any{"type": "user", "name": user}, + "kind": obj(a).str("objectRef", "resource"), + } +} + +// rendered is what the activity service makes of an audit event. +type rendered struct { + rule, summary string + links []link +} + +// render evaluates a policy's audit rules against event the way the activity +// processor does: top to bottom, first match wins, then the summary +// template. ok is false when no rule matches. +func render(t *testing.T, policy obj, event map[string]any) (r rendered, ok bool) { + t.Helper() + vars := auditVars(event) + eval := func(env *cel.Env, expr string) ref.Val { + ast, iss := env.Compile(expr) + if iss.Err() != nil { + t.Fatalf("%q: %v", expr, iss.Err()) + } + prg, err := env.Program(ast) + if err != nil { + t.Fatalf("%q: %v", expr, err) + } + out, _, err := prg.Eval(vars) + if err != nil { + t.Fatalf("%q: %v", expr, err) + } + return out + } + env, err := auditEnv(&r.links) + if err != nil { + t.Fatal(err) + } + for _, rule := range policy.list("spec", "auditRules") { + ro := obj(rule.(map[string]any)) + if eval(env, ro.str("match")).Value() != true { + continue + } + r.rule = ro.str("name") + r.summary = templateRE.ReplaceAllStringFunc(ro.str("summary"), func(m string) string { + return fmt.Sprint(eval(env, templateRE.FindStringSubmatch(m)[1]).Value()) + }) + return r, true + } + return r, false +} + +// policies returns the ActivityPolicies by the plural of the kind they cover. +func policies(t *testing.T) map[string]obj { + t.Helper() + plurals := map[string]string{} + for plural, c := range loadCRDs(t) { + plurals[c.kind] = plural + } + out := map[string]obj{} + for _, p := range ofKind(load(t, "../../config/milo/activity"), "ActivityPolicy") { + out[plurals[p.str("spec", "resource", "kind")]] = p + } + return out +} // TestActivityPolicies compiles every rule's match expression and summary -// template, checks each policy names a feedback CRD, and checks that rules for -// metadata-only kinds don't read bodies that won't be there. +// template in the activity service's CEL environment, checks each policy +// names a feedback CRD, and checks that rules don't read bodies that won't be +// there: none for metadata-only kinds, and none for deletes, whose audit +// event has a Status response and a DeleteOptions request. func TestActivityPolicies(t *testing.T) { crds := loadCRDs(t) kinds := map[string]string{} for plural, c := range crds { kinds[c.kind] = plural } - env, err := cel.NewEnv( - cel.Variable("audit", cel.DynType), - cel.Variable("actor", cel.StringType), - cel.Function("link", cel.Overload("link_dyn_dyn", []*cel.Type{cel.DynType, cel.DynType}, cel.StringType)), - ) + env, err := auditEnv(nil) if err != nil { t.Fatal(err) } @@ -53,6 +182,11 @@ func TestActivityPolicies(t *testing.T) { t.Errorf("%s: no CRD for kind %s", name, p.str("spec", "resource", "kind")) continue } + for _, n := range NoActivity { + if n == plural { + t.Errorf("%s: %s must have no activity: platform-timeline readers would learn reports exist", name, plural) + } + } metadataOnly := false for _, m := range MetadataOnly { metadataOnly = metadataOnly || m == plural @@ -68,9 +202,91 @@ func TestActivityPolicies(t *testing.T) { if !strings.Contains(match, "!audit.user.username.startsWith('system:')") { t.Errorf("%s: doesn't leave out the service's own writes", where) } - if metadataOnly && (strings.Contains(match+summary, "requestObject") || strings.Contains(match+summary, "responseObject")) { + readsBody := strings.Contains(match+summary, "requestObject") || strings.Contains(match+summary, "responseObject") + if metadataOnly && readsBody { t.Errorf("%s: %s is logged at Metadata only, so the rule can't read request or response bodies", where, plural) } + if strings.Contains(match, "'delete'") && readsBody { + t.Errorf("%s: a delete's audit event has a Status response and DeleteOptions request, not the object", where) + } } } } + +// TestDeleteActivityRenders runs vote and subscription delete audit events +// through the policies. The events in testdata/audit were written by +// envtest's kube-apiserver (TestDeleteActivityInAnAPIServer), with the user +// changed to a person in their own context in Milo: the request is +// DeleteOptions, the response a Status, and neither has the object's spec. +// The post comes from post-ref.yaml's audit annotation. +func TestDeleteActivityRenders(t *testing.T) { + ps := policies(t) + for _, tc := range []struct { + file, plural, rule, summary, post string + noAnnotation bool + }{ + {file: "vote-delete.json", plural: "votes", rule: "unvote", + summary: "alice@example.com removed their vote on datum-cloud.enhancements.912", post: "datum-cloud.enhancements.912"}, + {file: "subscription-delete.json", plural: "subscriptions", rule: "unfollow", + summary: "alice@example.com stopped following datum-cloud.enhancements.912", post: "datum-cloud.enhancements.912"}, + // Without the annotation (post-ref.yaml not installed yet, or its + // expression failed) the entry still appears, without the post. + {file: "vote-delete.json", plural: "votes", rule: "unvote-post-unknown", summary: "alice@example.com removed a vote", noAnnotation: true}, + {file: "subscription-delete.json", plural: "subscriptions", rule: "unfollow-post-unknown", summary: "alice@example.com stopped following a post", noAnnotation: true}, + } { + t.Run(fmt.Sprintf("%s annotation=%v", tc.file, !tc.noAnnotation), func(t *testing.T) { + event := readEvent(t, filepath.Join("testdata", "audit", tc.file)) + if tc.noAnnotation { + delete(event["annotations"].(map[string]any), postRefAnnotation) + } + checkDeleteEventShape(t, event) + got, ok := render(t, ps[tc.plural], event) + if !ok { + t.Fatal("no rule matched") + } + if got.rule != tc.rule || got.summary != tc.summary { + t.Errorf("got rule %s %q, want %s %q", got.rule, got.summary, tc.rule, tc.summary) + } + if tc.post != "" { + var ref map[string]any + if len(got.links) == 1 { + ref, _ = got.links[0].ref.(map[string]any) + } + if obj(ref).str("name") != tc.post || obj(ref).str("resource") != "posts" { + t.Errorf("links %v, want one to post %s", got.links, tc.post) + } + } + }) + } +} + +// readEvent reads an audit.k8s.io/v1 Event as the activity processor sees +// it: JSON decoded into a map. +func readEvent(t *testing.T, path string) map[string]any { + t.Helper() + data, err := os.ReadFile(path) + if err != nil { + t.Fatal(err) + } + var event map[string]any + if err := json.Unmarshal(data, &event); err != nil { + t.Fatalf("%s: %v", path, err) + } + return event +} + +// checkDeleteEventShape checks event is a custom resource delete as the API +// server logs it, so the rules are tested against what really arrives. +func checkDeleteEventShape(t *testing.T, event map[string]any) { + t.Helper() + e := obj(event) + if e.str("verb") != "delete" || e.str("responseObject", "kind") != "Status" || e.str("responseObject", "status") != "Success" { + t.Errorf("not a successful delete with a Status response: verb %q, response %v", e.str("verb"), e.get("responseObject")) + } + if e.get("responseObject", "spec") != nil || e.get("requestObject", "spec") != nil { + t.Errorf("a delete's audit event has no spec") + } + if k := e.str("requestObject", "kind"); k != "" && k != "DeleteOptions" { + t.Errorf("request is %s, want DeleteOptions or none", k) + } +} diff --git a/test/config/testdata/audit/subscription-delete.json b/test/config/testdata/audit/subscription-delete.json new file mode 100644 index 0000000..38f7b1a --- /dev/null +++ b/test/config/testdata/audit/subscription-delete.json @@ -0,0 +1,71 @@ +{ + "annotations": { + "authorization.k8s.io/decision": "allow", + "authorization.k8s.io/reason": "", + "post-ref.feedback.miloapis.com/post": "datum-cloud.enhancements.912" + }, + "apiVersion": "audit.k8s.io/v1", + "auditID": "ff5e6b43-f1bb-4699-9213-8b3808558241", + "kind": "Event", + "level": "RequestResponse", + "objectRef": { + "apiGroup": "feedback.miloapis.com", + "apiVersion": "v1alpha1", + "name": "datum-cloud.enhancements.912.312983457298312345", + "resource": "subscriptions" + }, + "requestObject": { + "apiVersion": "meta.k8s.io/__internal", + "kind": "DeleteOptions" + }, + "requestReceivedTimestamp": "2026-11-17T21:41:44.923712Z", + "requestURI": "/apis/feedback.miloapis.com/v1alpha1/subscriptions/datum-cloud.enhancements.912.312983457298312345", + "responseObject": { + "apiVersion": "v1", + "details": { + "group": "feedback.miloapis.com", + "kind": "subscriptions", + "name": "datum-cloud.enhancements.912.312983457298312345", + "uid": "b07b6034-b036-4738-866f-7b0ce3590c18" + }, + "kind": "Status", + "metadata": {}, + "status": "Success" + }, + "responseStatus": { + "code": 200, + "details": { + "group": "feedback.miloapis.com", + "kind": "subscriptions", + "name": "datum-cloud.enhancements.912.312983457298312345", + "uid": "b07b6034-b036-4738-866f-7b0ce3590c18" + }, + "metadata": {}, + "status": "Success" + }, + "sourceIPs": [ + "10.0.0.12" + ], + "stage": "ResponseComplete", + "stageTimestamp": "2026-11-17T21:41:44.924673Z", + "user": { + "username": "alice@example.com", + "uid": "312983457298312345", + "groups": [ + "system:authenticated" + ], + "extra": { + "iam.miloapis.com/parent-api-group": [ + "iam.miloapis.com" + ], + "iam.miloapis.com/parent-type": [ + "User" + ], + "iam.miloapis.com/parent-name": [ + "312983457298312345" + ] + } + }, + "userAgent": "feedback-website", + "verb": "delete" +} diff --git a/test/config/testdata/audit/vote-delete.json b/test/config/testdata/audit/vote-delete.json new file mode 100644 index 0000000..1e3b28c --- /dev/null +++ b/test/config/testdata/audit/vote-delete.json @@ -0,0 +1,71 @@ +{ + "annotations": { + "authorization.k8s.io/decision": "allow", + "authorization.k8s.io/reason": "", + "post-ref.feedback.miloapis.com/post": "datum-cloud.enhancements.912" + }, + "apiVersion": "audit.k8s.io/v1", + "auditID": "e2757309-ccd9-41ce-9135-5284f7ae1777", + "kind": "Event", + "level": "RequestResponse", + "objectRef": { + "apiGroup": "feedback.miloapis.com", + "apiVersion": "v1alpha1", + "name": "datum-cloud.enhancements.912.312983457298312345", + "resource": "votes" + }, + "requestObject": { + "apiVersion": "meta.k8s.io/__internal", + "kind": "DeleteOptions" + }, + "requestReceivedTimestamp": "2026-11-17T21:41:44.910172Z", + "requestURI": "/apis/feedback.miloapis.com/v1alpha1/votes/datum-cloud.enhancements.912.312983457298312345", + "responseObject": { + "apiVersion": "v1", + "details": { + "group": "feedback.miloapis.com", + "kind": "votes", + "name": "datum-cloud.enhancements.912.312983457298312345", + "uid": "18063293-a607-4f35-b02b-d88bf899cf69" + }, + "kind": "Status", + "metadata": {}, + "status": "Success" + }, + "responseStatus": { + "code": 200, + "details": { + "group": "feedback.miloapis.com", + "kind": "votes", + "name": "datum-cloud.enhancements.912.312983457298312345", + "uid": "18063293-a607-4f35-b02b-d88bf899cf69" + }, + "metadata": {}, + "status": "Success" + }, + "sourceIPs": [ + "10.0.0.12" + ], + "stage": "ResponseComplete", + "stageTimestamp": "2026-11-17T21:41:44.912283Z", + "user": { + "username": "alice@example.com", + "uid": "312983457298312345", + "groups": [ + "system:authenticated" + ], + "extra": { + "iam.miloapis.com/parent-api-group": [ + "iam.miloapis.com" + ], + "iam.miloapis.com/parent-type": [ + "User" + ], + "iam.miloapis.com/parent-name": [ + "312983457298312345" + ] + } + }, + "userAgent": "feedback-website", + "verb": "delete" +}