Audit service-account credentials
The CodeNOW API lists the credentials of the service accounts you manage — CodeNOW API keys, Git HTTPS tokens and Git SSH keys — with their state, expiry and last use. Use it to audit them automatically: which credentials expire soon, which nobody uses any more, which were created outside CodeNOW.
The calls are read-only and return metadata only (names, states, dates, scopes, fingerprints) — never a secret or a token value. Credentials are created, suspended and revoked in the CodeNOW UI, on the service account's KEYS & TOKENS tab.
Before you start
- Use an API key of a person who manages the service accounts: an account Owner or Admin (sees every service account), the owner of a service account, or an Admin / Service Account Manager of the owning team. Create it under Profile › KEYS & TOKENS › CodeNOW API keys — see Authentication.
- A service account's own API key doesn't work for an audit. A service account never manages service accounts, so it sees none of them (
404on its own credentials, an empty search). - Keep the API key in an environment variable, for example
CODENOW_API_KEY, never in a script.
The examples use https://api.cloud.codenow.com. Replace it with your installation's API host if it differs.
The calls
| Call | Returns |
|---|---|
GET /service-accounts/{idOrUsername}/credentials | The credentials of one service account, identified by its UUID or username (sa-<account>_<name>). |
GET /service-account-credentials | The credentials of every service account you manage, each with its serviceAccount. |
GET /service-accounts | The service accounts you manage, with their lifecycle state. |
Both credential calls need the headers X-Codenow-Api-Key and Accept: application/vnd.codenow.v1+json. Full reference: List the credentials of a service account, Search the credentials of all managed service accounts.
curl -s https://api.cloud.codenow.com/service-accounts/sa-acme_ci/credentials \
-H "X-Codenow-Api-Key: $CODENOW_API_KEY" \
-H "Accept: application/vnd.codenow.v1+json"
A service account you don't manage — or that doesn't exist — is answered with 404.
Filters
| Parameter | Values | Default | Calls |
|---|---|---|---|
kind | API_KEY, GIT_TOKEN, SSH_KEY | all kinds | both |
state | ACTIVE, SUSPENDED, EXPIRED, REVOKED | ACTIVE, SUSPENDED, EXPIRED | both |
origin | CODENOW (created through CodeNOW), EXTERNAL (created directly in GitLab) | both | search |
serviceAccount | UUIDs or usernames | all you manage | search |
expiresWithinDays | 0–3650 | — | search |
unusedForDays | 0–3650 | — | search |
- Several values: repeat the parameter (
kind=API_KEY&kind=GIT_TOKEN) or give one comma-separated value (kind=API_KEY,GIT_TOKEN) — not both in one parameter; mixing them is rejected with400. - Filters combine with AND; several values of one filter match any of them.
expiresWithinDays— credentials that expire after now and at most this many days from now. Expired credentials and credentials that never expire are left out.unusedForDays— API keys and HTTPS tokens not used in the last N days, including those never used. GitLab reports no last use for SSH keys, so SSH keys are left out whenever this filter is set.- A
serviceAccountyou don't manage, or that doesn't exist, simply matches nothing.
An invalid value is answered with 400 as application/problem+json, naming the parameter:
{
"type": "https://api.codenow.com/problem/illegal-argument",
"title": "Illegal Argument",
"status": 400,
"detail": "Invalid value 'abc' for unusedForDays."
}
The response
{
"credentials": [
{
"id": "4182",
"kind": "API_KEY",
"name": "nightly-audit",
"state": "ACTIVE",
"origin": "CODENOW",
"serviceAccount": { "id": "0b6c2f0e-6a43-4c1e-9b7e-2f1d6a0c9e11", "username": "sa-acme_ci", "name": "ci", "state": "ACTIVE" },
"createdAt": "2026-03-02T09:14:00Z",
"createdBy": { "type": "USER", "id": "jnovak" },
"expiresAt": "2027-03-01T00:00:00Z",
"lastUsedAt": "2026-06-30T17:20:00Z"
},
{
"id": "5c0b7e21-44d9-5a3f-9e12-8b6d0f3c7a55",
"kind": "GIT_TOKEN",
"name": "mirror-sync",
"state": "ACTIVE",
"origin": "CODENOW",
"scopes": ["read_repository"],
"serviceAccount": { "id": "9e4a1b77-0c3d-4f62-a8e5-1d7b3c9f0a22", "username": "sa-acme_mirror", "name": "mirror", "state": "ACTIVE" },
"createdAt": "2026-05-20T10:00:00Z",
"expiresAt": "2026-12-01T00:00:00Z"
}
]
}
| Field | Meaning |
|---|---|
id + kind | Identify a credential; they stay the same across calls, so results can be compared between runs. |
state | ACTIVE; SUSPENDED (API keys only); EXPIRED; REVOKED (Git credentials only — pass state=REVOKED to see them). |
origin | CODENOW or EXTERNAL. API keys are always CODENOW. |
serviceAccount | The service account (id, username, name, state) — in search results only; the per-account call has it once at the top. |
scopes | GitLab scopes of an HTTPS token, for example read_repository, write_repository, read_api, api. |
fingerprint, keyType | An SSH key's fingerprint and type, for example ssh-ed25519. |
createdAt, createdBy | When and by whom it was created. createdAt may be missing for EXTERNAL credentials. |
expiresAt | When it stops working; omitted when it never expires. |
lastUsedAt | Last use — see Last use. Omitted when never used; never present for SSH keys. |
revokedAt, revokedBy | When and by whom a Git credential was revoked through CodeNOW. |
unverified | true when GitLab couldn't be reached and the Git credential is shown as CodeNOW last recorded it. |
- Fields without a value are omitted, not sent as
null— test for a missing key. - People are ids only:
createdByandrevokedByare{type, id}, wheretypeisUSERorSERVICE_ACCOUNTandidis the CodeNOW username, the service account's username, orcodenowfor the platform. Resolve names yourself if you need them. - Sort order: service account name, kind (
API_KEY,GIT_TOKEN,SSH_KEY), credential name, id. GET /service-accountsadds each service account's lifecyclestate:ACTIVE,DISABLING,DISABLED,ENABLINGorDELETING. OnlyACTIVEservice accounts can authenticate.
Expiry times
Git tokens and SSH keys, new API keys and new CI keys stop working at 00:00 UTC on their expiry date, so expiresAt is a midnight time such as 2027-03-01T00:00:00Z. API keys created earlier keep the time they were created with and may show an end-of-day time, for example 2026-12-01T22:59:59.999Z. Compare expiresAt as a timestamp, not as a date string.
Last use
- API keys: recorded by CodeNOW, precise to about 5 minutes. Use is recorded only since the account 25.0.5 release — older use isn't known. Until N days have passed since that release,
unusedForDays=Nlists every API key that hasn't been used since the release, including keys used before it. - HTTPS tokens: as reported by GitLab; it can lag by a few minutes.
- SSH keys: GitLab reports no last use, so there is none.
Warnings and the cap
The response has a warnings array when the result may be incomplete — always check it. It is omitted when there is nothing to report.
code | Meaning | What to do |
|---|---|---|
GIT_UNAVAILABLE | GitLab couldn't be reached for a service account. The call still succeeds: its Git credentials come from CodeNOW's records with unverified: true. The warning names the serviceAccount. When more than 20 service accounts are affected, a single GIT_UNAVAILABLE warning without serviceAccount gives their number instead (the affected credentials still carry unverified: true). | Treat the Git part as possibly stale; run the audit again later. |
TRUNCATED | More than 1000 credentials matched. The first 1000 in sort order are returned. | Narrow the search — for example by kind or serviceAccount — and call again. |
UNKNOWN_CREDENTIAL | Credentials this API version can't represent were left out; the message gives their number. The audit never shrinks silently. | Report it; check the service account in the CodeNOW UI. |
There is no paging. Each warning has a human-readable message:
"warnings": [
{
"code": "GIT_UNAVAILABLE",
"serviceAccount": { "id": "0b6c2f0e-6a43-4c1e-9b7e-2f1d6a0c9e11", "username": "sa-acme_ci", "name": "ci", "state": "ACTIVE" },
"message": "GitLab could not be reached; Git credentials are shown as CodeNOW last recorded them."
}
]
Recipes
- Shell + jq
- Python
API=https://api.cloud.codenow.com # your installation's API host
audit() {
curl -sf "$API/service-account-credentials?$1" \
-H "X-Codenow-Api-Key: $CODENOW_API_KEY" \
-H "Accept: application/vnd.codenow.v1+json"
}
# Credentials that expire within 14 days
audit 'expiresWithinDays=14' |
jq -r '.credentials[] | [.serviceAccount.username, .kind, .name, .expiresAt] | @tsv'
# API keys and HTTPS tokens not used for 90 days (or never)
audit 'unusedForDays=90' |
jq -r '.credentials[] | [.serviceAccount.username, .kind, .name, (.lastUsedAt // "never")] | @tsv'
# Git credentials created outside CodeNOW
audit 'origin=EXTERNAL' |
jq -r '.credentials[] | [.serviceAccount.username, .kind, .name, (.createdAt // "?")] | @tsv'
# Fail the job when the result may be incomplete
audit 'expiresWithinDays=14' | jq -e '(.warnings // []) | length == 0' > /dev/null \
|| { echo "Audit incomplete: check warnings" >&2; exit 1; }
import os
import requests
API = "https://api.cloud.codenow.com" # your installation's API host
HEADERS = {
"X-Codenow-Api-Key": os.environ["CODENOW_API_KEY"],
"Accept": "application/vnd.codenow.v1+json",
}
def audit(**params):
r = requests.get(f"{API}/service-account-credentials",
headers=HEADERS, params=params, timeout=60)
r.raise_for_status()
body = r.json()
for w in body.get("warnings", []):
print(f"WARNING {w['code']}: {w['message']}")
return body["credentials"]
# A list value is sent as repeated parameters: kind=API_KEY&kind=GIT_TOKEN
for c in audit(expiresWithinDays=14, kind=["API_KEY", "GIT_TOKEN"]):
print(c["serviceAccount"]["username"], c["kind"], c["name"], c["expiresAt"])
for c in audit(unusedForDays=90):
print(c["serviceAccount"]["username"], c["kind"], c["name"], c.get("lastUsedAt", "never"))
for c in audit(origin="EXTERNAL"):
print(c["serviceAccount"]["username"], c["kind"], c["name"])
Through the MCP server
The CodeNOW MCP server offers the same calls as read-only tools, so an AI assistant can run the audit for you:
| Tool | Call |
|---|---|
service_account_credential_list | GET /service-accounts/{idOrUsername}/credentials |
service_account_credential_search | GET /service-account-credentials (same filters; list values as arrays) |
service_account_list / service_account_get | GET /service-accounts, GET /service-accounts/{idOrUsername} |
Ask, for example: "Which service-account credentials expire within 14 days? Mention any warnings." The tools act with the permissions of the connected user, so connect as someone who manages the service accounts.
Not included
- Per-deployment CI tokens — the tokens CodeNOW issues to deployments (the Deployment tokens line on the service account) are not part of the audit.
- Deleted API keys — a deleted API key is not kept, so it no longer appears. (Revoked Git credentials are kept as
REVOKED.) - SSH-key last use — GitLab doesn't report it.
- A credential deleted directly in GitLab drops out of the result; one created through CodeNOW and revoked there shows as
REVOKED.EXTERNALcredentials keep stable ids while they exist.