Skip to content

A repository policy

A repository’s committed .moat/policy.yaml adds deny and review rules for everyone who works in it, whatever agent they use; its allow rules apply only for a person who trusted that exact file, or where a key they trusted signed it. How it combines with each person’s policy: Repository policies.

  1. Add a policy to the repository

    Create .moat/policy.yaml at the root of the repository and commit it:

    version: 1
    deny:
    - id: no-prod-config
    reason: production settings stay with the release pipeline
    fs.write: ["${project}/deploy/prod/**"]
    review:
    - id: migrations
    reason: database migrations change shared data
    shell: ["npm run migrate*"]

    Only version, deny, review and allow are accepted (ask, the name before 0.14.0, is still read); defaults, executables, sandbox and any other key make the file invalid, and every call in the project is then denied (kernel-error) until it is fixed. The agent cannot write the file (kernel-self denies writes under .moat/): edit it yourself.

  2. Everyone gets the deny and review rules at once

    They apply to everyone who works in the repository, whatever agent they use, and show up as repo:<id>, such as repo:no-prod-config. Per action the stricter of the two policies wins: from 0.16.0 a repository’s review rule never softens a deny your own policy gives, so review: [{net: ['*']}] cannot turn the hosts your net: deny default refuses into reviews a person could approve.

  3. Each person trusts the allow rules

    A repository is untrusted input, so by itself it can only make your policy stricter. Its allow rules apply after you review and trust that exact file:

    Terminal window
    openmoat trust # in the repository; prints the allow rules it lets in
    openmoat trust --revoke

    It asks Trust this file? [y/N] before it trusts anything. openmoat allowed lists the repositories you trusted. What it prints: Use it in your projects.

Terminal window
openmoat policy check "npm run migrate" --cwd ~/work/app # decides with the repository's policy, as the hook does
openmoat status # in the repository: the file, its digest and which form applies

Leave out --policy: with it, only the file you name decides, without your policy or the repository’s. openmoat policy lint .moat/policy.yaml checks the rule syntax, but not the keys only a repository may not set; openmoat status in the project reports those.

A pull request that edits .moat/policy.yaml drops the file back to deny and review only, for everyone who trusted it, until each trusts it again. To review it:

Terminal window
git diff origin/main -- .moat/policy.yaml # what the change adds
openmoat status # in the project: whether its allow rules apply
openmoat trust # shows the file, its digest and every rule it adds, then asks

openmoat trust asks Trust this file? [y/N] before it trusts anything; openmoat allowed lists the repositories you trusted, and openmoat trust --revoke takes it back. Under require_signed_repo_policy: true (Signed policy packs), openmoat status says trusted: signed by alice@example.com, or not signed by a trusted key: tightening only, allow rules ignored (…).

openmoat policy check exits 0 for allow or defer, 2 for deny and 3 for an unresolved review, so a CI job can assert what the repository’s rules decide. On a runner, openmoat init without a terminal writes the default policy to ~/.moat and changes no agent’s files:

Terminal window
openmoat init # default policy in ~/.moat; no agent is changed
openmoat policy check "npm run migrate" --cwd . # exits 3: review, repo:migrations
openmoat policy check "$PWD/deploy/prod/app.yaml" --kind fs-write --cwd . # exits 2: deny, repo:no-prod-config

Install openmoat on the runner as in the Quick start. A job that runs these in a shell with set -e stops at the first non-zero exit: check the code yourself, for example openmoat policy check "npm run migrate" --cwd . || test $? -eq 3. The runner decides with the default policy, not each person’s, so it checks the repository’s rules, not what a given person gets.

To check every pull request for changes to CI workflows, git hooks, agent settings, .env files or added tokens, with the repository’s rules read as the base branch has them, use openmoat policy check-diff and the GitHub Action: Check a pull request.