Add security_policy event type to flow triggers
Why
Security policies need to start Duo flows that have no person behind them, so they need a dedicated service account, like scheduled triggers.
This adds a security_policy flow trigger event type that only the system can set, so those runs get that account.
The follow-up Dispatch scheduled business logic scans from sc... (!257271 - merged) creates these triggers; until then, none exist.
What changes
security_policyis added toAi::FlowTrigger::EVENT_TYPESandAUTONOMOUS_EVENT_TYPE_IDS.- Project members cannot add, remove or deactivate it, through the services or GraphQL. They can still delete the trigger.
- GraphQL gains
SECURITY_POLICY(experiment) to describe existing triggers.aiFlowTriggers(kind: EVENT)does not return these triggers, so they are not in the Automations "Event based" tab.
How to test
-
In the project, enable a foundational flow (for example Code Review) so it has a service account.
-
Create a
security_policytrigger withPROJECT=group/project bin/rails runner create_sp_trigger.rb, using this script:project = Project.find_by_full_path(ENV.fetch('PROJECT')) admin = User.admins.first consumer = Ai::Catalog::ItemConsumer.where(project: project).includes(:item) .find { |c| c.item&.foundational_flow && c.active_service_account } abort 'Enable a foundational flow in this project first' unless consumer account = Ai::FlowTriggers::ProvisionAutonomousServiceAccountService.new( project: project, current_user: admin, item_consumer: consumer, description: 'Scan execution policy', access_level: consumer.active_service_account_access_level(project) ).execute.payload.fetch(:service_account) trigger = project.ai_flow_triggers.new( ai_catalog_item_consumer: consumer, autonomous_service_account: account, active: true, event_types: [Ai::FlowTrigger::EVENT_TYPES[:security_policy]], description: 'Scan execution policy' ) # Only the follow-up MR lists security_policy in a flow's supported_events, so skip validation here. trigger.save!(validate: false) puts "Flow: #{consumer.item.name}", "Trigger: #{trigger.to_global_id}" -
As a project Maintainer, in
/-/graphql-explorer(eventTypestakes integers;SECURITY_POLICYis10):Mutation Result aiFlowTriggerUpdate(input: { id: "<trigger id>", active: false })"Security policy triggers cannot be deactivated. Exclude the project in the security policy instead." aiFlowTriggerUpdate(input: { id: "<trigger id>", eventTypes: [0] })"Security policy triggers are managed by security policies and cannot be added or removed" aiFlowTriggerUpdate(input: { id: "<mention trigger id>", eventTypes: [0, 10] })Same error aiFlowTriggerCreate(input: { projectPath: "group/project", description: "x", eventTypes: [10], aiCatalogItemConsumerId: "<consumer id>" })Same error -
Mention triggers still deactivate and reactivate as before.
-
In the UI,
/-/automate/triggers:- with the
duo_automationsflag off, the trigger is listed; - with it on, it is in neither the "Scheduled (beta)" nor the "Event based" tab, and is only counted in the total.
- with the
Verified in a GDK with this MR alone on master.
With this MR alone, any edit to that trigger is refused: no flow lists security_policy until the follow-up MR.
Database
aiFlowTriggers(kind: EVENT) gains one filter, NOT (event_types @> '{10}'), on rows already scoped by project_id:
SELECT "ai_flow_triggers".* FROM "ai_flow_triggers"
WHERE "ai_flow_triggers"."project_id" = 278964
AND NOT ((event_types @> ('{9}')))
AND NOT ((event_types @> ('{10}')))
ORDER BY "ai_flow_triggers"."created_at" DESC, "ai_flow_triggers"."id" DESC
LIMIT 101Database Lab on gitlab-production-main, project 278964: the same index scan on index_ai_flow_triggers_on_project_id, 40 rows and 59 buffers, as before the change.
- With this MR (warm): https://console.postgres.ai/gitlab/projects/gitlab-production-main/sessions/88812/commands/195179
- Before this MR: https://console.postgres.ai/gitlab/projects/gitlab-production-main/sessions/88812/commands/195177
Context
- Design and limitations: https://gitlab.com/gitlab-org/gitlab/-/work_items/638792+
- Part 1 of 3, split from Dispatch scheduled business logic scans from sc... (!257271 - merged). Issue: https://gitlab.com/gitlab-org/gitlab/-/work_items/630266+