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_policy is added to Ai::FlowTrigger::EVENT_TYPES and AUTONOMOUS_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

  1. In the project, enable a foundational flow (for example Code Review) so it has a service account.

  2. Create a security_policy trigger with PROJECT=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}"
  3. As a project Maintainer, in /-/graphql-explorer (eventTypes takes integers; SECURITY_POLICY is 10):

    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
  4. Mention triggers still deactivate and reactivate as before.

  5. In the UI, /-/automate/triggers:

    • with the duo_automations flag 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.

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 101

Database 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.

Context

Edited by Meir Benayoun

Merge request reports

Loading
Loading