Service accounts setup guide
Available since: version 8.0
Overview
Section titled “Overview”A service account lets an integration use the MetalCloud API with short-lived tokens from your own identity provider, with no long-lived secret stored in MetalCloud. The integration gets a token for itself from your OIDC provider, exchanges it for a MetalCloud session, and signs in again before that session ends.
A service account is a MetalCloud user marked as a service account. Its role decides what it can do, it owns what it creates, and people can share their infrastructures with it, as with any user. It is linked to one client in your identity provider through that client’s subject, the sub claim of the tokens the provider issues to it.
Each sign-in gives a session that ends no later than the provider token it came from. Suspending or archiving the service account refuses its next request.
How sign-in works:
- The integration asks your identity provider for a token with the client-credentials grant.
- The provider returns a signed JWT, the provider token.
- The integration sends the provider token to MetalCloud’s sign-in route.
- MetalCloud checks it against the provider’s issuer and published signing keys, which it caches.
- MetalCloud returns a session token and the time it expires.
- The integration calls the API with the session token, and starts again at step 1 before the session expires or after a 401.
Before you start
Section titled “Before you start”Service accounts need MetalCloud 8.0 or later and OIDC sign-in already set up on the installation.
- OIDC sign-in is enabled in MetalCloud’s authentication settings, with your provider’s issuer URL. Service accounts use that same provider. Installations that sign people in only through SAML or LDAP can’t use service accounts.
- Your provider issues JWT access tokens for the client-credentials grant, not opaque tokens. They must be signed with an asymmetric algorithm (RS256, RS384, RS512, PS256, PS384, PS512, ES256, ES384, ES512 or EdDSA) and carry
expandsub. - The token’s issuer matches discovery. The
issclaim must equal the issuer in the provider’s discovery document (/.well-known/openid-configuration), the same one MetalCloud’s OIDC sign-in uses. - MetalCloud can reach the provider to read its discovery document and its signing keys (JWKS). If it can’t, sign-in fails with 503 until it can.
- The integration can reach both the provider’s token endpoint and the MetalCloud API.
Step 1: set up your identity provider
Section titled “Step 1: set up your identity provider”Create one client per integration that gets tokens with the client-credentials grant, and make its access tokens carry an audience reserved for MetalCloud.
- Choose the audience. Pick the value that names the MetalCloud API in your provider, for example
metalcloud. Use a value different from the client ID that MetalCloud’s OIDC sign-in for people uses; otherwise people’s ID tokens would also carry it. - Create a client for the integration. A confidential client with the client-credentials grant and its own secret or certificate, which only the integration holds. Use one client per integration, so each can be disabled on its own.
- Add the audience to its access tokens.
- Read the client’s subject. Request one token and decode its payload (the middle part of the JWT, base64url-encoded). Check
iss,audandexp, and notesub: MetalCloud needs it in step 2.
| Provider | Audience | Subject (sub) | Notes |
|---|---|---|---|
| Keycloak | Add an Audience mapper to the client, with Included Custom Audience set to your value and Add to access token on. | The ID of the client’s service-account user (a UUID). | Turn on Client authentication and Service account roles for the client. |
| Microsoft Entra ID | Register the MetalCloud API as its own app; the integration requests APP_ID_URI/.default. In v2 tokens, aud is that API app’s application (client) ID. | The object ID of the integration’s service principal (also in oid). | Set accessTokenAcceptedVersion to 2 in the API app’s manifest, so tokens carry the v2 issuer https://login.microsoftonline.com/TENANT_ID/v2.0. v1 tokens carry https://sts.windows.net/TENANT_ID/ and are refused. |
| Okta | A custom authorization server whose Audience is your value. | The client ID. | The org authorization server issues opaque tokens for client credentials, which MetalCloud can’t verify; use a custom authorization server. |
Whatever the provider, the decoded token is the final word: the iss must match the issuer MetalCloud’s OIDC settings use, and aud must contain the audience you set in step 2.
Step 2: set up MetalCloud
Section titled “Step 2: set up MetalCloud”Turn service-account sign-in on by setting the audience, then create one service account per integration.
- Set the audience. Go to Configuration › Global Configurations › Authentication. In the OIDC settings, enter your value in Service account audience and save. Service-account sign-in stays off while this field is empty; clearing it turns sign-in off again.
- Pick or create a role. The role decides what the integration can do. It must include the API access permission (
api_key_use), the same one API tokens need. - Create the service account. Go to Users & Permissions › Users and add a user. Turn on Service account, enter the client’s
subin Subject, and give it a name, an email address and the role.- The subject must match the token’s
subexactly, including letter case; spaces around it are removed. Each subject can belong to only one service account. - The email address must be unique and pass the usual allowed-domain check. Use an address of its own, such as a team mailbox: a person whose email matches a service account’s can’t sign in with OIDC.
- A service account has no password, gets no invitation or verification email, and its email counts as verified.
- The subject must match the token’s
Administrators can also create one from the API (POST /api/v2/users with serviceAccountSubject) or the CLI (see Using metalcloud-cli). A service account can’t be created with a password or with an account of its own: a request that sends password, or createWithAccount: true, together with serviceAccountSubject answers 400 (A service account cannot be created with a password or with an account.). Leaving createWithAccount out, or sending false, is fine.
Step 3: sign in from the integration
Section titled “Step 3: sign in from the integration”The integration exchanges a provider token for a MetalCloud session token, sends that as a Bearer token, and signs in again before it ends.
-
Get a provider token with the client-credentials grant. For Keycloak:
Terminal window curl -s -X POST https://idp.example.com/realms/REALM/protocol/openid-connect/token \-d grant_type=client_credentials -d client_id=CLIENT_ID -d client_secret=CLIENT_SECRETWith Entra ID, also send
scope=APP_ID_URI/.default. -
Sign in to MetalCloud. The route needs no other credential. The token is at most 16,384 characters (400 if longer) and the body at most 32 KB (413 if larger), and requests are rate-limited per IP address.
POST /api/v2/service-accounts/actions/sign-inContent-Type: application/json{"token": "PROVIDER_ACCESS_TOKEN"}A successful sign-in answers 200:
{"token": "SESSION_TOKEN", "expiresTimestamp": "2026-11-24T17:24:10Z"} -
Call the API with
Authorization: Bearer SESSION_TOKEN. -
Sign in again with a fresh provider token before
expiresTimestamp, or whenever a call answers 401. The session ends at the earliest of:- the provider token’s expiry;
- MetalCloud’s usual session validity;
- the inactivity timeout, counted from the last call;
- the maximum session length.
-
End the session when done with
POST /api/v2/logoutand the same Bearer token.
Limits and behaviour:
- 100 active sessions per service account. A sign-in beyond that ends the oldest session. Several copies of an integration may sign in at once, and one provider token may sign in more than once while it is valid.
- Key rotation. When the provider signs with a new key, MetalCloud fetches the new keys on the first token that uses it. It fetches at most once every 30 seconds, so a sign-in right after another fetch may get 401: retry after 30 seconds.
Using metalcloud-cli as a service account
Section titled “Using metalcloud-cli as a service account”Point metalcloud-cli at a file that holds the provider token, and every command signs in as the service account on its own, with no login step and no browser.
export METALCLOUD_ENDPOINT=https://metalcloud.example.comexport METALCLOUD_SERVICE_ACCOUNT_TOKEN_FILE=/var/run/secrets/metalcloud/tokenmetalcloud-cli site listThe token file can also be given with --service-account-token-file or the service-account-token-file key in metalcloud.yaml. metalcloud-cli login --service-account-token-file PATH signs in once and saves the session.
- Keep the file current yourself. Your tooling writes a fresh token from your OIDC provider there, for example a CI job step or a sidecar that renews it. A Kubernetes service-account token won’t work: the cluster issues it, not your provider. The CLI reads the file at each sign-in and never caches its content.
- Saved sessions. The session is saved in
$HOME/.metalcloud/sessions.json, readable only by the user, one per endpoint and token file. Two service accounts can share one home folder with different token files. A person’s own saved session for the same endpoint is ignored, not removed. - When it signs in. A command signs in from the file when there is no saved session, or the saved one ends within 60 seconds. If MetalCloud ended the session earlier, the command’s first calls get 401, and the CLI signs in once and repeats them.
- 401 during a command. The command fails with
The service account's session ended; the next command signs in again from the token file., and the next command signs in again. - Read-only home folder. If the session can’t be saved, the command still runs and prints a warning; each command then signs in again.
- Precedence.
--api_keyorMETALCLOUD_API_KEYfirst, then the token file, then a person’s saved session. - Logout.
metalcloud-cli logoutremoves only a person’s session, not the service account’s.
Administrators create a service account from the CLI with --service-account-subject instead of a password:
metalcloud-cli user create --email robot@company.com --service-account-subject SUB --access-level useruser get shows the provider; user get -f json also shows serviceAccountSubject.
Managing service accounts
Section titled “Managing service accounts”Service accounts are managed like other users, with a few differences listed here.
| Change | How | Effect |
|---|---|---|
| Replace the integration’s client | Edit Subject on the service account’s page (needs permission to edit users). | Saving asks for confirmation and ends all the service account’s sessions. |
| Change what it can do | Change its role, as for a built-in user. | Applies from the next request; removing api_key_use from the role refuses the next request. |
| Stop it | Suspend, block or archive it. | Its next request answers 401 and new sign-ins are refused. Unsuspending lets it sign in again. |
| Remove it | Delete it. | Archives it, removes its personal data and frees its subject. |
| Turn the feature off | Clear Service account audience, or turn off OIDC sign-in. | New sign-ins answer 403. Open sessions run until they end, at most until their provider token expires. |
What a service account can’t do, by design:
- sign in to the MetalCloud UI, or through any other sign-in method;
- use password, two-factor authentication, email change or re-authentication;
- create or manage API tokens, or approve CLI sign-ins, so it can never give itself a long-lived credential;
- change its own user (A service account can’t change its own user.).
Nobody can turn a person into a service account or back. Separately, no user can change their own sign-in provider any more (You can’t change your own sign-in provider.); administrators still change other users’ providers.
Auditing. Each sign-in updates the service account’s last login time and records the event user_service_account_signed_in, with the client IP and the token’s sub, iss and aud. It also sends the same “user logged in” event as a person’s login, with provider service-account. Whatever the integration does is recorded with the service account as the actor.
Troubleshooting
Section titled “Troubleshooting”Sign-in gives the same 401 for every token problem, so that an outsider can’t tell whether a subject exists; the MetalCloud auth service’s log names the actual reason and the subject, never the token.
| Message | Status | Meaning | What to check |
|---|---|---|---|
Service accounts can't sign in on this MetalCloud. | 403 at sign-in | Service-account sign-in is off. | OIDC sign-in is enabled and Service account audience is set. |
The token isn't accepted. | 401 at sign-in | The token or the service account was refused. | Decode the token: iss equals the issuer in MetalCloud’s OIDC settings, aud contains the service account audience, exp is in the future, sub equals the service account’s subject exactly. The service account isn’t suspended, blocked or archived, and its role has api_key_use. Right after a key rotation, retry after 30 seconds. |
Unable to verify the token at this time. | 503 at sign-in | MetalCloud can’t reach the provider’s discovery document or signing keys. | Network access from MetalCloud to the provider, and the provider’s availability. |
A service account cannot be created with a password or with an account. | 400 on POST /api/v2/users | The request combined serviceAccountSubject with password or createWithAccount: true. | Drop password, and leave createWithAccount out or set it to false. |
This service account is not allowed to use the API. | 401 on an API call | The role no longer has api_key_use. | The service account’s role. |
This route does not accept a service account session. | 403 on an API call | The route needs a signed-in person (passwords, two-factor authentication, API tokens, CLI approval). | Use a person’s account for that task. |
A service account can't change its own user. | 403 on an API call | A service account tried to edit itself. | An administrator makes the change. |
metalcloud-cli shows its own texts for sign-in failures:
| CLI message | Cause |
|---|---|
The provider token wasn't accepted; check the token file and the service account's subject. | Sign-in answered 401. |
This MetalCloud doesn't accept service accounts. | Sign-in answered 403. |
This MetalCloud doesn't support signing in from the CLI; use an API token. | The MetalCloud version has no service-account sign-in. |
The service account's session ended; the next command signs in again from the token file. | A call during the command answered 401. |