Policies (ULTIMATE)

Policies in GitLab provide security teams a way to require scans of their choice to be run whenever a project pipeline runs according to the configuration specified. Security teams can therefore be confident that the scans they set up have not been changed, altered, or disabled. You can access these by navigating to your project's Security & Compliance > Policies page.

GitLab supports the following security policies:

Security policy project

All security policies are stored as YAML in a separate security policy project that gets linked to the development project. This association can be a one-to-many relationship, allowing one security policy project to apply to multiple development projects. Linked projects are not required to be in the same group as the development projects to which they are linked.

Security Policy Project Linking Diagram

Although it is possible to have one project linked to itself and to serve as both the development project and the security policy project, this is not recommended. Keeping the security policy project separate from the development project allows for complete separation of duties between security/compliance teams and development teams.

All security policies are stored in the .gitlab/security-policies/policy.yml YAML file inside the linked security policy project. The format for this YAML is specific to the type of policy that is stored there. Examples and schema information are available for the following policy types:

Policies created in this project are applied through a background job that runs once every 10 minutes. Allow up to 10 minutes for any policy changes committed to this project to take effect.

Managing the linked security policy project

NOTE: Only project Owners have the permissions to select, edit, and unlink a security policy project.

As a project owner, take the following steps to create or edit an association between your current project and a project that you would like to designate as the security policy project:

  1. On the top bar, select Menu > Projects and find your project.
  2. On the left sidebar, select Security & Compliance > Policies.
  3. Select Edit Policy Project, and search for and select the project you would like to link from the dropdown menu.
  4. Select Save.

To unlink a security policy project, follow the same steps but instead select the trash can icon in the modal.

Security Policy Project

Viewing the linked security policy project

All users who have access to the project policy page and are not project owners will instead view a button linking out to the associated security policy project. If no security policy project has been associated then the linking button does not appear.

Policy management

The Policies page displays deployed policies for all available environments. You can check a policy's information (for example, description or enforcement status), and create and edit deployed policies:

  1. On the top bar, select Menu > Projects and find your project.
  2. On the left sidebar, select Security & Compliance > Policies.

Policies List Page

Network policies are fetched directly from the selected environment's deployment platform while other policies are fetched from the project's security policy project. Changes performed outside of this tab are reflected upon refresh.

By default, the policy list contains predefined network policies in a disabled state. Once enabled, a predefined policy deploys to the selected environment's deployment platform and you can manage it like the regular policies.

Note that if you're using Auto DevOps and change a policy in this section, your auto-deploy-values.yaml file doesn't update. Auto DevOps users must make changes by following the Container Network Policy documentation.

Policy editor

Introduced in GitLab 13.4.

You can use the policy editor to create, edit, and delete policies:

  1. On the top bar, select Menu > Projects and find your group.
  2. On the left sidebar, select Security & Compliance > Policies.
    • To create a new policy, select New policy which is located in the Policies page's header. You can then select which type of policy to create.
    • To edit an existing policy, select Edit policy in the selected policy drawer.

The policy editor has two modes:

  • The visual Rule mode allows you to construct and preview policy rules using rule blocks and related controls.

    Policy Editor Rule Mode

  • YAML mode allows you to enter a policy definition in .yaml format and is aimed at expert users and cases that the Rule mode doesn't support.

    Policy Editor YAML Mode

You can use both modes interchangeably and switch between them at any time. If a YAML resource is incorrect or contains data not supported by the Rule mode, Rule mode is automatically disabled. If the YAML is incorrect, you must use YAML mode to fix your policy before Rule mode is available again.

When you finish creating or editing your policy, save and apply it by selecting the Configure with a merge request button and then merging the resulting merge request. When you press this button, the policy YAML is validated and any resulting errors are displayed. Additionally, if you are a project owner and a security policy project has not been previously associated with this project, then a new project is created and associated automatically at the same time that the first policy merge request is created.

Scan execution policies

See Scan execution policies.

Scan result policy editor

See Scan result policies.

Container Network Policy

WARNING: Container Network Policy is in its end-of-life process. It's deprecated in GitLab 14.8, and planned for removal in GitLab 15.0.

The Container Network Policy section provides packet flow metrics for your application's Kubernetes namespace. This section has the following prerequisites:

If you're using custom Helm values for Cilium, you must enable Hubble with flow metrics for each namespace by adding the following lines to your Cilium values:

hubble:
  enabled: true
  metrics:
    enabled:
      - 'flow:sourceContext=namespace;destinationContext=namespace'

The Container Network Policy section displays the following information about your packet flow:

  • The total amount of the inbound and outbound packets
  • The proportion of packets dropped according to the configured policies
  • The per-second average rate of the forwarded and dropped packets accumulated over time window for the requested time interval

If a significant percentage of packets is dropped, you should investigate it for potential threats by examining the Cilium logs:

kubectl -n gitlab-managed-apps logs -l k8s-app=cilium -c cilium-monitor

Change the status

To change a network policy's status:

  • Select the network policy you want to update.
  • Select Edit policy.
  • Select the Policy status toggle to update the selected policy.
  • Select Save changes to deploy network policy changes.

Disabled network policies have the network-policy.gitlab.com/disabled_by: gitlab selector inside the podSelector block. This narrows the scope of such a policy and as a result it doesn't affect any pods. The policy itself is still deployed to the corresponding deployment namespace.

Container Network Policy editor

The policy editor only supports the CiliumNetworkPolicy specification. Regular Kubernetes NetworkPolicy resources aren't supported.

Rule mode supports the following rule types:

Once your policy is complete, save it by selecting Save policy at the bottom of the editor. Existing policies can also be removed from the editor interface by selecting Delete policy at the bottom of the editor.

Configure a Network Policy Alert

You can use policy alerts to track your policy's impact. Alerts are only available if you've installed and configured an agent for this project.

There are two ways to create policy alerts:

  • In the policy editor UI, by clicking Add alert.

  • In the policy editor's YAML mode, through the metadata.annotations property:

    metadata:
      annotations:
        app.gitlab.com/alert: 'true'

Once added, the UI updates and displays a warning about the dangers of too many alerts.

Roadmap

See the Category Direction page for more information on the product direction of security policies within GitLab.