Add a GraphQL API for the root namespace tracked context quota
What does this MR do and why?
This MR adds a GraphQL API for the tracked security context quota of a root namespace. It is part of the quota work in &20475.
!265368 enforces the quota for each root namespace and stores it in namespace_settings.security_tracked_context_quota. The quota is per root namespace until customers move to their own organizations (#627145). Today, only the Rails console can read or change that value. The Admin area page for quotas (#585970) needs an API to show the quota of a namespace and to change it.
This MR is stacked on !265368.
Changes
-
A new field
Group.securityTrackedContextQuota:Item Value Type SecurityTrackedContextQuota(from !239741 (merged)):limit,usage,remainingandexplicitlySet.Value The quota of the top-level group. For a subgroup, the field returns the quota of its top-level group, because the quota applies to the whole hierarchy. Visibility Users with admin_groupon the top-level group. These are the owners of the top-level group, and administrators in Admin Mode. Other users who can read the group getnull.Limit The field can be resolved for at most 10 groups in one request. Each group runs one count query. !265368 has the query plan for that count. Status Experiment, introduced in 19.6. -
A new mutation
securityUpdateNamespaceTrackedContextQuota:Item Value Arguments namespaceId(required, a top-level group or a personal namespace) andquota(required, nullableInt).quota: nullRemoves the explicit quota. The default then applies (the GitLab.com default, or no quota on self-managed and GitLab Dedicated). Authorization admin_all_resources. Only administrators in Admin Mode can use the mutation. Owners cannot change the quota of their own namespace.Fine-grained tokens The existing update_namespacepermission on the instance boundary, which only administrators can assign. The REST endpointPUT /namespaces/:idthat sets the compute minutes quota uses the same permission.Errors A subgroup or a project namespace returns the error "Tracked context quota can only be set on a root namespace." A quota below 1 returns a validation error in errorsand the unchanged quota inquotaInfo.Audit event A new event security_tracked_context_namespace_quota_updatedwith instance scope. The mutation records it only when the quota changes.Status Experiment, introduced in 19.6. -
SecurityTrackedContextQuotanow declaresskip_reason: :parent_authorizesfor fine-grained tokens. Before, it requiredread_organizationon the instance boundary. That rule is not correct for a field onGroup, because a token for a group would also need an instance permission. Each parent now authorizes the token:Parent Permission Organizationread_organization(instance)Groupread_group(group)securityUpdateNamespaceTrackedContextQuotaupdate_namespace(instance)The field descriptions of the type no longer refer only to the organization.
-
The description of
Organization.securityTrackedContextQuotanow says that only organization owners and administrators get data. This is a follow-up from !239741 (merged). -
New specs for
Organization.securityTrackedContextQuota: usage with tracked and untracked contexts, an administrator who is not a member of the organization, and GitLab.com with no explicit organization quota. These are follow-ups from !239741 (merged). -
Organization.securityTrackedContextQuotanow shows only a quota that is set explicitly on the organization. Before this MR, on GitLab.com it showed the GitLab.com default as the organization quota. That default applies to each root namespace (see !265368).Security::TrackedContextQuotachecks the organization quota only when it is set explicitly, so nothing enforced the limit that the field showed. When the organization has no explicit quota,limitandremainingare nownull.usagestill shows the count, andexplicitlySetisfalse. Enforcement does not change. !265368 says that the root namespace API follow-up makes this change. This MR is that follow-up. -
Organizations::OrganizationSetting#security_tracked_context_quota_with_defaultis removed. After item 6, no code calls it.
This MR does not add the field to personal namespaces. The mutation accepts a personal namespace, but no GraphQL field reads its quota yet. #638601 tracks this.
The security_tracked_context_quota_enforcement feature flag is not enabled for customers yet, so a quota that this API sets or shows is not enforced until we enable the flag (see !265368). Like the securityTrackedContextQuota field from !239741 (merged), the field and the mutation do not check the vulnerabilities_across_contexts feature flag.
Example
query {
group(fullPath: "gitlab-org") {
securityTrackedContextQuota { limit usage remaining explicitlySet }
}
}mutation {
securityUpdateNamespaceTrackedContextQuota(input: {
namespaceId: "gid://gitlab/Group/9970",
quota: 50
}) {
quotaInfo { limit usage remaining explicitlySet }
errors
}
}How to set up and validate locally
-
Sign in as an administrator and turn on Admin Mode.
-
Run the mutation above with the global ID of a top-level group. Check that
limitis 50 andexplicitlySetistrue. -
Run the query above as an owner of the group. Check that it returns the same quota.
-
Run the mutation again with
quota: null. Check thatexplicitlySetisfalse. -
Run the mutation as the group owner. Check that the response contains an access error.
-
Check that the audit event
security_tracked_context_namespace_quota_updatedexists in Admin > Monitoring > Audit events. -
Run the specs:
bundle exec rspec ee/spec/requests/api/graphql/mutations/security/update_namespace_tracked_context_quota_spec.rb \ ee/spec/requests/api/graphql/group/security_tracked_context_quota_spec.rb \ ee/spec/requests/api/graphql/organization/security_tracked_context_quota_spec.rb \ ee/spec/services/security/tracked_context_quota_service_spec.rb \ ee/spec/models/ee/organizations/organization_setting_spec.rb \ ee/spec/graphql/types/security/tracked_context_quota_type_spec.rb \ ee/spec/graphql/ee/types/group_type_spec.rb
MR acceptance checklist
- Field visible only to owners of the top-level group and administrators
- Mutation limited to administrators
-
quota: nullremoves the explicit quota - Audit event when the quota changes
-
Organization.securityTrackedContextQuotadoes not show the GitLab.com default - Specs for administrators, owners, maintainers, auditors, non-members, subgroups, personal namespaces, invalid values, usage, the GitLab.com default and fine-grained tokens
- GraphQL reference, introspection result, fine-grained token permissions and audit event types regenerated
Related issues
Part of &20475 (Tracked Context Quota Management)
Related to #585970, #585972 (closed) and #627145