Git Integration (Automatic PRs/MRs)

Store approved policy exceptions in Git and let Nirmata Control Hub open the pull requests or merge requests automatically.

With GitOps deployment, approved policy exceptions are stored in Git and reach your clusters through your GitOps tool, such as Argo CD or Flux. Nirmata Control Hub (NCH) can open the pull request (PR) or merge request (MR) for each approved exception automatically, so nobody has to run nctl after an approval.

This page explains how to set up Git storage for policy exceptions, choose the folders NCH writes to, and follow an exception from approval to merge.

Delivery modes

How an approved exception reaches its target is a tenant setting under Settings -> Policy Exceptions -> Deployment. It is not chosen per request: the request form is the same in every mode, and only what happens after approval differs.

ModeSettingWhat happens after approval
AutomaticAutomatic deploymentNCH applies the PolicyException to the target cluster directly. No Git is involved.
GitOps with nctlGitOps deployment, Create PRs/MRs automatically offSomeone runs nctl create pull-request, which commits the exception and opens the PR with their own Git credentials. See GitOps Integration.
GitOps with automatic PRs/MRsGitOps deployment, Create PRs/MRs automatically onNCH commits the exception file and opens the PR/MR itself. The exception takes effect when the PR/MR is merged and your GitOps tool syncs it.

Prerequisites

  • An Admin user in NCH to change the Policy Exception settings and to add Git provider accounts.
  • A GitOps repository with one branch per cluster group, synced to your clusters by your GitOps tool.
  • Credentials NCH can use to open PRs/MRs on the repository host:
    • github.com: the GitHub App integration installed on the repository.
    • GitLab (gitlab.com or self-hosted): a Git provider account with a GitLab personal, group or project access token that has the api scope. The token’s user needs at least the Developer role on the project.

Add a Git provider account

NCH uses the Git provider account whose host URL matches the GitOps repository host. Accounts are managed on the same page as the deployment settings, and the list appears only when GitOps deployment is selected.

  1. Go to Settings -> Policy Exceptions and click the Deployment tab.
  2. Select GitOps deployment.
  3. Under Git provider accounts, click Add GitLab Account.
  4. Fill in the dialog:
    • Name: a name for the account, for example gitlab-prod.
    • Host URL: the GitLab origin, for example https://gitlab.example.com. It must be an https:// origin without credentials or a path. The host URL cannot be changed later; to use another host, delete the account and add it again.
    • Access token: the GitLab access token. The token is stored encrypted and is never shown again. When you edit an account, leave the field empty to keep the stored token.
  5. Click Save.

Add GitLab account dialog

To remove an account, click its delete icon and confirm. After deletion, NCH can no longer open PRs/MRs on this host.

Policy Sets that are stored in GitLab use the same Git provider accounts.

Tenants that use Automatic deployment

The Git provider accounts list is shown only while GitOps deployment is selected, but an account is saved as soon as you click Save in its dialog, independently of the deployment settings. A tenant that keeps Automatic deployment, for example to use GitLab Policy Sets, can still add an account:

  • No active exception requests: select GitOps deployment, add the account as described above, then click Cancel at the bottom of the page. The account is kept and the deployment type stays Automatic deployment.
  • Active exception requests: the deployment type cannot be changed while requests are active, so the list cannot be shown. Add the account with the API instead, using an admin API key. Create the account, then store its token with the id returned by the first call:
curl -s -X POST https://<nch-url>/policies/api/GitProviderAccount \
  -H "Authorization: NIRMATA-API <api-key>" -H "Content-Type: application/json" \
  -d '{"name":"gitlab-prod","provider":"GitLab","hostUrl":"https://gitlab.example.com"}'

curl -s -X POST https://<nch-url>/policies/api/gitProviderAccounts/<account-id>/token \
  -H "Authorization: NIRMATA-API <api-key>" -H "Content-Type: application/json" \
  -d '{"accessToken":"<gitlab-token>"}'

Configure storage and folders

  1. Go to Settings -> Policy Exceptions and click the Deployment tab.
  2. Select GitOps deployment.
  3. Fill in the fields described in the table below.
  4. Under Cluster Expression to Branch Mappings, map each cluster to the branch it syncs from. See GitOps Integration for the mapping options.
  5. Click Save.

GitOps deployment settings with automatic PRs/MRs

FieldDescription
Git repository for policy exceptionsURL of the GitOps repository.
Folder for policy exception filesFolder for cluster exception files, relative to the repository root, for example policy-exceptions. The folder must exist on each mapped branch. Leave empty to use the repository root.
Create PRs/MRs automaticallyWhen on, NCH opens the PR/MR after a policy exception is approved, using the Git account configured for the repository host. GitHub repositories use the GitHub App integration.
Git accountThe Git provider account NCH uses to open PRs/MRs. Leave empty for github.com repositories, which use the GitHub App integration.
ReviewersShown only when Create PRs/MRs automatically is on. Git usernames requested as reviewers on the PRs/MRs NCH opens, at most 20. Unknown users are skipped and never block the PR/MR.
Write repository exceptions toWhere exceptions for Git repository scans are stored: Central GitOps repository (default) or The scanned repository. Repository exceptions apply to scans only after their PR/MR is merged.
Repository exceptions folderCentral GitOps repository only. Folder in the GitOps repository for approved repository-scan exceptions. Leave empty to keep them only in NCH. Avoid the folder your policy sets sync to clusters.
Repository exceptions branchCentral GitOps repository only. Leave empty to use the default branch of the repository.
Folder in each repositoryThe scanned repository only. Folder committed on the branch the exception targets. Default: .nirmata/policy-exceptions. NCH needs write access to each scanned repository through the Git account above, or through the GitHub App integration for github.com repositories.

Repository exceptions written to the scanned repository

Folder paths must be relative and must not contain spaces or . or .. segments.

NOTE: The deployment type cannot be changed while there are active policy exception requests. Either wait for these requests to expire, or delete them and then change the type.

What NCH writes to Git

NCH writes each approved exception as a Kyverno PolicyException YAML file and opens a PR/MR that adds it. NCH never pushes to the base branch and never force-pushes.

Cluster exceptionRepository exception
RepositoryGit repository for policy exceptionsThe central repository, or the scanned repository (per Write repository exceptions to)
FolderFolder for policy exception filesRepository exceptions folder or Folder in each repository
File name<request name>.yaml, or <request name>-<n>.yaml when the request has several targets<request name>-<exceptionKey>.yaml, for example allow-root-checkout-ff3cbd93.yaml
Base branchFrom Cluster Expression to Branch MappingsRepository exceptions branch, or the branch the exception targets in the scanned repository; else the default branch
Head branchnirmata/polex/<request name>-<id>-<add|revoke|expiry|delete>nirmata/polex-repo/...
Applied byYour GitOps tool, after the PR/MR is mergedRepository scans, using NCH’s approved copy

The cluster file is the same file that nctl create pull-request writes. Example:

apiVersion: kyverno.io/v2
kind: PolicyException
metadata:
  name: allow-privileged-ingress-proxy-0
  namespace: kyverno
spec:
  exceptions:
  - policyName: disallow-privileged-containers
    ruleNames:
    - privileged-containers
    - autogen-privileged-containers
  match:
    any:
    - resources:
        kinds:
        - Deployment
        names:
        - ingress-proxy
        namespaces:
        - ingress
    - resources:
        kinds:
        - ReplicaSet
        - Pod
        names:
        - ingress-proxy-*
        namespaces:
        - ingress

Repository exception files also carry a policies.nirmata.io/exception-metadata annotation. Its JSON value links the file to the NCH request (requestId, specId, exceptionKey, and expiresAt when the exception expires). Do not edit the annotation.

The PR/MR description links to the request in NCH and states the reason, who requested the exception and who approved it.

Guidance for PR/MR reviewers

  • Review the exception, then merge or close the PR/MR. Merging puts the exception in force. Closing it leaves the request approved but not in force until someone retries in NCH.
  • Do not edit the exception in Git. NCH stays the source of approvals, and edits made in Git are not imported.
  • Do not add exception files by hand, and do not delete a file to revoke an exception. Request and revoke exceptions in NCH.

After approval

When a request is approved, NCH opens the PR/MR within about a minute. Follow its progress in the Deploy Status column under Policy -> Policy Exceptions -> Exception Requests. NCH names the change the way the provider does: MR for GitLab, PR for GitHub.

Cluster exceptions

Deploy StatusMeaning
Opening MRApproved; NCH is opening the MR.
Merge MR !<n>The MR is open and waiting for review. The status links to the MR.
MR failedNCH could not open the MR. Open the status to see the error.
MR closed · not deployedThe MR was closed without merging. The exception is not deployed.
CompletedThe MR was merged and the cluster reports the exception.

Repository exceptions

Deploy StatusMeaning
Waiting for merge · MR !<n>Repository exceptions apply to repository scans only after their PR/MR is merged. Until then, scans still report the finding.
Scan OnlyThe MR was merged; repository scans apply the exception.
Revoke pending · in force until mergedRevoked; the removal MR is open. The exception stays in force until it is merged.

Click the status to open the details drawer. It shows the status and when it was last updated, the Merge request (or Pull request) link, the File, the Branch (target and head branch), and any Error.

Retry a closed or failed MR

If an MR was closed without merging or could not be opened, click Retry MR (Retry PR for GitHub) in the row’s action menu or in the details drawer. NCH opens a new one.

Failed attempts are also retried automatically with increasing delay, from 1 minute up to 6 hours. Failures caused by the settings or the token, such as a missing branch mapping or missing permission, are retried after the settings or the Git account change.

Revoke and expiry

When you revoke an approved exception, the confirmation reads: “NCH will open a merge request that removes this exception from Git. The exception stays in effect until it is merged.”

  • NCH opens a PR/MR that deletes the file from the repository, path and branch where it was added.
  • If the PR/MR that adds the exception is still open, NCH closes it instead.
  • When an exception expires, NCH opens the same kind of removal PR/MR.

Using nctl with automatic PRs/MRs

  • When Create PRs/MRs automatically is on, nctl create pull-request prints a message that NCH opens the PRs/MRs for the tenant and exits with code 0, so existing CI jobs do not fail. It does not need Git credentials from nctl login git. This requires nctl v4.10.33 or later.
  • nctl scan repository (v4.10.33 or later) reads exception files from .nirmata/policy-exceptions/ in the scanned repository. Use --repo-exceptions-path when you set a different Folder in each repository; an empty value turns reading off. nctl matches each file to an NCH request by the specId in its policies.nirmata.io/exception-metadata annotation. A file is only reported, never applied itself: what applies is NCH’s approved copy of an exception that is in force. Files without the annotation, hand-written files and edits made in Git are reported but not applied.

Troubleshooting

SymptomCause and fix
MR failed, error mentions permissionThe access token lacks permission. GitLab tokens need the api scope and the Developer role on the project. Update the token in the Git provider account.
MR failed: “No branch in clusterBranchMappingExpressions matches the exception’s cluster”Add a mapping for the cluster under Cluster Expression to Branch Mappings. NCH does not fall back to the default branch.
MR failed: “… already contains …; rename the request”A file with the same name that NCH did not write for this request already exists. Rename the request.
MR closed · not deployedThe MR was closed without merging. Click Retry MR to open a new one.
No PR/MR for a github.com repositoryCheck that the GitHub App integration is installed on the repository and that Git account is empty.
Repository exceptions are not written to GitRepository exceptions folder is empty. Set a folder, or select The scanned repository.