Block agent tokens when billing namespace has Duo disabled
What does this MR do and why?
POST /api/v4/ai/third_party_agents/direct_access mints a token that a third-party agent uses to call the AI gateway. The only gate was the create_third_party_agent_direct_access_token ability in ee/app/policies/ee/global_policy.rb, which checks the instance-level disabled_direct_code_suggestions setting and whether the user can use the model proxy. It never checked whether the namespace being billed for the usage has GitLab Duo turned off at the group/namespace level. A user in a group with Duo disabled could still get a token and spend that group's credits.
The fix adds a guard to #direct_access_token in ee/lib/ai/third_party_agents/token_service.rb. If the governing namespace has duo_features_enabled false, the service returns an error with the message GitLab Duo features are disabled for the billing namespace. It goes through the same error_response helper as the missing-namespace check, so the denial is also reported to error tracking as a DirectAccessError. ee/lib/api/ai/third_party_agents.rb already maps an error response to HTTP 503, so the API layer is untouched.
- Why TokenService and not the policy. The ability is a global policy check with no namespace context, so there is no subject to hang the namespace off. TokenService is where
current_user.governing_namespace(project)is first resolved, and it is the shared service the REST endpoint and any future caller go through. Enforcing in shared business logic follows the usual convention. - Why the billing namespace. The governing namespace is what the AI gateway attributes usage to; it is sent as
governing_namespace_idin the gateway headers. If Duo is disabled there, no token should be issued. - Why SaaS only. The check sits inside the existing
Gitlab::Saas.feature_available?(:gitlab_duo_saas_only)conditional, so self-managed behaviour is unchanged. On self-managed,duo_default_namespace_candidates(ee/app/models/ee/user_preference.rb) is every top-level group the user can access, andduo_default_namespace_with_fallbackonly returns a namespace when the user belongs to exactly one. A self-managed user in a single Duo-disabled group would be blocked, but the same user would not be after joining a second group. That keys enforcement on group count rather than on policy. On GitLab.com the candidates are gated on active Duo add-on purchases, so the governing namespace really is the billing namespace. Group-level enforcement on self-managed can be handled separately.
The namespace_settings&.duo_features_enabled pattern matches existing use in ee/lib/ai/duo_workflow.rb:101 and ee/app/policies/ee/group_policy.rb:349.
Specs cover both layers: the service returns an error and makes no HTTP call to the gateway, and the endpoint returns 503 with the message. Both spec files pass in full (65 examples, 0 failures, 2 pre-existing pending). RuboCop is clean.
References
Screenshots or screen recordings
Backend-only change with no UI surface, so no screenshots apply.
How to set up and validate locally
The specs are the quickest check:
bundle exec rspec ee/spec/lib/ai/third_party_agents/token_service_spec.rb
bundle exec rspec ee/spec/requests/api/ai/third_party_agents_spec.rbTo exercise the endpoint by hand, the user needs a governing namespace that resolves on SaaS, which requires an active Duo add-on purchase:
-
From the Rails root, set up Duo in GitLab.com mode. This creates a
gitlab-duogroup with an add-on purchase attached:GITLAB_SIMULATE_SAAS=1 bundle exec 'rake gitlab:duo:setup' -
In
rails console, disable Duo on that group and make it the user's default Duo namespace:user = User.find_by_username('root') group = Group.find_by_full_path('gitlab-duo') group.namespace_settings.update!(duo_features_enabled: false) user.user_preference.update!(duo_default_namespace_id: group.id) -
Create a PAT with the
apiscope, then call the endpoint:curl --request POST --header "PRIVATE-TOKEN: <pat>" \ "http://gdk.test:3000/api/v4/ai/third_party_agents/direct_access" -
Expect
503with{"message":"GitLab Duo features are disabled for the billing namespace"}. -
Set
duo_features_enabled: trueon the group and confirm the request succeeds again.
MR acceptance checklist
Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.