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

  1. A new field Group.securityTrackedContextQuota:

    Item Value
    Type SecurityTrackedContextQuota (from !239741 (merged)): limit, usage, remaining and explicitlySet.
    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_group on 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 get null.
    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.
  2. A new mutation securityUpdateNamespaceTrackedContextQuota:

    Item Value
    Arguments namespaceId (required, a top-level group or a personal namespace) and quota (required, nullable Int).
    quota: null Removes 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_namespace permission on the instance boundary, which only administrators can assign. The REST endpoint PUT /namespaces/:id that 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 errors and the unchanged quota in quotaInfo.
    Audit event A new event security_tracked_context_namespace_quota_updated with instance scope. The mutation records it only when the quota changes.
    Status Experiment, introduced in 19.6.
  3. SecurityTrackedContextQuota now declares skip_reason: :parent_authorizes for fine-grained tokens. Before, it required read_organization on the instance boundary. That rule is not correct for a field on Group, because a token for a group would also need an instance permission. Each parent now authorizes the token:

    Parent Permission
    Organization read_organization (instance)
    Group read_group (group)
    securityUpdateNamespaceTrackedContextQuota update_namespace (instance)

    The field descriptions of the type no longer refer only to the organization.

  4. The description of Organization.securityTrackedContextQuota now says that only organization owners and administrators get data. This is a follow-up from !239741 (merged).

  5. 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).

  6. Organization.securityTrackedContextQuota now 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::TrackedContextQuota checks 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, limit and remaining are now null. usage still shows the count, and explicitlySet is false. Enforcement does not change. !265368 says that the root namespace API follow-up makes this change. This MR is that follow-up.

  7. Organizations::OrganizationSetting#security_tracked_context_quota_with_default is 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

  1. Sign in as an administrator and turn on Admin Mode.

  2. Run the mutation above with the global ID of a top-level group. Check that limit is 50 and explicitlySet is true.

  3. Run the query above as an owner of the group. Check that it returns the same quota.

  4. Run the mutation again with quota: null. Check that explicitlySet is false.

  5. Run the mutation as the group owner. Check that the response contains an access error.

  6. Check that the audit event security_tracked_context_namespace_quota_updated exists in Admin > Monitoring > Audit events.

  7. 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: null removes the explicit quota
  • Audit event when the quota changes
  • Organization.securityTrackedContextQuota does 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

Part of &20475 (Tracked Context Quota Management)

Related to #585970, #585972 (closed) and #627145

Merge request reports

Loading
Loading