Provision a dedicated service account for autonomous triggers
What does this MR do and why?
Sets autonomous_service_account_id when a catalog trigger carries only autonomous event types, so a scheduled trigger and an interactive trigger can live on the same flow. The shared account keeps composite identity and is untouched.
- The dedicated account is provisioned on demand and joins the project at the same role the flow's own account holds once enablement completes, so an unattended run has exactly the access the flow it runs has and no more.
- Owner access is deliberately excluded as an Owner-level shared account would silently hand project ownership to an autonomous account that runs with no human behind it.
- Service account creation is refused when the flow's account is not a project member. There is no role to match, and provisioning at a guessed role produces a trigger that looks configured but fails on every run until it auto-deactivates.
- The role is derived by
Ai::Catalog::ItemConsumer#active_service_account_access_level, so the value shown to users later reads the same source that provisioning uses. - Each autonomous trigger gets its own dedicated account. Revocation checks that no other trigger references the account before removing its project membership, so a shared account would survive.
- An account acting alone derives Duo entitlement from the namespace, since it cannot hold an add-on seat.
- Behind the
autonomous_service_account_executionfeature flag, which already gates the scheduled event type onmaster.
Design rationale, alternatives considered, and the path to SA+SA are in https://gitlab.com/gitlab-org/gitlab/-/work_items/617178#note_3751492736.
Follow up MR:
- Covers when the flow account's role changes: Sync dedicated SAs roles to be in line with the... (!255986 - merged)
Related: #594180 (closed)
Stack
- !251852 (merged) — column and model
- This MR — provisioning services and the entitlement policy
- !255986 (merged) — sync dedicated SAs roles to be in line with the flow's role when it changes
- !253636 (closed) —
serviceAccountAccessLevelGraphQL argument
Testing
ee/spec/services/ai/flow_triggers/dedicated_autonomous_service_account_spec.rb covers the behaviour end to end: an interactive and a scheduled trigger on one flow, composite identity on for the shared account and off for the dedicated one, the role matching the flow account and refusual when the flow account is not a project member, account reuse on update, rollback when the trigger fails validation, revocation on delete, and rejection when the flag is off.
How to set up and validate locally
Prerequisites
- Duo Enterprise enabled
- GDK running with all services up
# 1 Set up
project = Project.find(1000000) # test-status-bot-e63fd58e/test
user = User.find_by_username('root')
consumer = Ai::Catalog::ItemConsumer.find(171) # developer/v1, project-level
shared_sa = consumer.active_service_account # "duo-developer-gitlab-duo", composite=true
# Feature flags
Ability.allowed?(user, :manage_ai_flow_triggers, project) # => true
Feature.enable(:autonomous_service_account_execution, project) # => true
Feature.enable(:ai_flow_schedules) # => true
# Triggers
mention = Ai::FlowTrigger::EVENT_TYPES[:mention]
scheduled = Ai::FlowTrigger::EVENT_TYPES[:scheduled]
make = ->(types, desc) do
Ai::FlowTriggers::CreateService.new(project: project, current_user: user)
.execute(ai_catalog_item_consumer_id: consumer.id, event_types: types, description: desc)
end
# 2. Derive flow's role to match
flow_role = consumer.active_service_account_access_level(project)
{ flow_role: flow_role, human: Gitlab::Access.human_access(flow_role) } # => the flow account's project role, e.g. 30 / "Developer". Non-nil is the precondition.
# 3. Provision a scheduled trigger
sched = make.call([scheduled], 'console check nightly')
sched.success? # => true
t = sched.payload
acct = t.autonomous_service_account
acct.refresh_authorized_projects
{
dedicated: acct.present?, # => true
not_shared: acct != shared_sa, # => true
shared_intact: shared_sa.reload.composite_identity_enforced?, # => true
cleared: acct.composite_identity_enforced?, # => false
member: project.member(acct).present? # => true
}
# 4. The dedicated SA' role matches the flows
{
dedicated_role: project.member(acct).access_level,
flow_role: project.member(shared_sa).access_level,
matches: project.member(acct).access_level == project.member(shared_sa).access_level # => true
}
# 5. The dedicated account can run the flow
{
can_execute: Ability.allowed?(acct, :execute_ai_catalog_item, consumer), # => true
duo_access: Ability.allowed?(acct, :duo_workflow, project) # => true
}
# 6. Error when the flow account has no role to match
original_level = project.member(shared_sa).access_level
project.member(shared_sa).destroy! # remove flow's shared SA membership from project
shared_sa.refresh_authorized_projects
consumer.reload.active_service_account_access_level(project) # => nil
refused = make.call([scheduled], 'no flow role')
{ error: refused.error?, message: refused.message, reason: refused.reason } # => "The flow's service account is not a member of this project", reason: :validation_error
Ai::FlowTrigger.where(project: project, description: 'no flow role').exists? # => false
# restore flow's SA membership to project
project.add_member(shared_sa, original_level)
shared_sa.refresh_authorized_projects
# 7. Verify shared account keeps composite ID while the dedicated one has it cleared on the same flow (main purpose of this MR)
i = make.call([mention], 'coexist interactive')
s = make.call([scheduled], 'coexist scheduled')
i.payload.ai_catalog_item_consumer_id == s.payload.ai_catalog_item_consumer_id # => true (same flow)
i.payload.ai_catalog_item_consumer_id == s.payload.ai_catalog_item_consumer_id # => true (interactive trigger shares the same SA with the shared flow's SA)
i.payload.autonomous_service_account.nil? # => true (interactive trigger is not autonomous)
s.payload.service_account != shared_sa # => true (scheduled/autonomous trigger has its own SA and doesn't share the same SA with the shared flow's SA)
shared_sa.reload.composite_identity_enforced? # => true (untouched, shared flow's SA still has comp ID enforced)
s.payload.service_account.composite_identity_enforced? # => false (cleared, scheduled/autonomous trigger's comp ID enforcement is cleared)
# 8. Membership revocation on update
Ai::FlowTriggers::UpdateService.new(project: project, current_user: user, trigger: t)
.execute(ai_catalog_item_consumer_id: consumer.id, event_types: [mention])
t.reload.autonomous_service_account
project.member(acct) # => nil
# 9. Mixed event type rejected
make.call([mention, scheduled], 'both').message # => "Event types cannot combine autonomous event types (scheduled) with other event types. Create a separate trigger for them."
# 10. Cleanup
Ai::FlowTriggers::DestroyService.new(trigger: t, current_user: user).execute
acct.reload.destroy!
Query
Ai::FlowTrigger.autonomous_for_service_account? is the only new query. It runs from DestroyService and from the duo_workflow_available policy condition, and the policy only reaches it for a service account with composite_identity_enforced == false under the feature flag. Human users return earlier. Plans are in !251852 (merged), which adds the index.