# Service accounts

A gate waits for people with the right roles to approve it. Sometimes the one who should decide is not a person: an
agent that checks a commit's tests passed before a release, or a bot that runs your release checklist. A **service
account** is a principal for that. It belongs to one workspace, holds roles like a member, and votes on gates through
the API with its own key. It can never sign in to Mocco, and every vote it casts is in the audit log under its name.

## Create one

On the workspace's **Settings** page, owners and admins see **Service accounts**. Type a name that says what it is for,
such as `deploy agent`, and choose **Create service account**. The list shows each service account and whether it is
active or disabled. To rename one, change its name in the list and choose **Save**; the audit log keeps the old name in
the rename entry.

## Give it a role

A service account votes only on gates that require a role it holds. On the **Access** page, add it to a role the same
way you add a person: the member picker lists the workspace's active service accounts after its people, marked
**(service account)**, and the role's member list shows them with a **Service account** badge.

The usual policy still applies: a gate that needs two approvals from `deployer` needs two distinct principals in that
role, so a service account and a person, or two service accounts. Each principal votes once.

## Issue it a key

Under the service account, choose **Issue key**, pick the project whose runs it acts on, and choose its scopes:

- `runs:read` reads the project's runs, their steps and their gates (`GET /v1/runs`).
- `approvals:write` resumes or rejects their gates as the service account.

The key is a secret key (`mk_sec_…`). Mocco shows its token once, right after you issue it: store it where your agent
reads its secrets, such as a GitHub Actions secret. A person's or a project's API key can't hold `approvals:write`;
only a service account's can. **Revoke** stops one key at once.

## Resume or reject a gate

With the key, the agent votes on the gate a run is paused at. `index` is the gate's position in the pipeline's `steps`,
as `GET /v1/runs/{runId}` reports it (`currentIndex` on a paused run).

```bash
curl -X POST "https://www.mocco.club/api/ext/v1/runs/$RUN_ID/gates/$INDEX/resume" \
  -H "Authorization: Bearer $MOCCO_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "reason": "required checks are green" }'
```

`/reject` takes the same path and needs a `reason`; one rejection stops the run. The answer is the run and the gate's
state after the vote: `resumed` when every role has enough approvals, `pending` while the gate waits for more.

Mocco refuses the vote with `403` when the service account holds none of the gate's roles, `400` when the gate requires
a reason and none was given, `409` when it already voted on this gate, and `404` when the run belongs to another
project or the run isn't paused at that gate.

## Disable it

**Disable** ends a service account for good: every key it has is revoked at once and it gets no new ones. Its roles
stop mattering because nothing can act as it, and its past votes stay in the audit log. Disabling can't be undone;
create a new service account instead.

## What is recorded

The [audit log](https://www.mocco.club/docs/en/articles/2yiscf-audit-log) records each service account being created, renamed and disabled, each key
issued and revoked, and every gate it resumes or rejects (`gate.resumed`, `gate.rejected`), naming the service account
and marking it as one (`principalKind: service`).

Agents connected over MCP can do all of this too, with the `mocco_service_accounts_*` tools. See
[Connect Mocco to your agent](https://www.mocco.club/docs/en/articles/frhj3w-connect).
