MultiPaaS

Run background jobs

A job that runs with no user signed in, such as a nightly import or one that builds a shared word list, signs in as an app identity: your application's own identity. App identities and sign-in explains how it differs from a user's sign-in. Run the commands in your app's directory, after multipaas init, which Build on the generated API covers.

Create the identity

The app-identity commands in this guide need CLI 0.9.0 or later, and CLI 1.2.0 or later when the identity belongs outside the default environment. That includes a project whose test app-tenant is outside it: on CLI 1.2.0 the identity then defaults to environmentId in multipaas.json, and an older CLI creates it in the default one instead. Upgrading adds no environmentId to a multipaas.json an older init wrote, so without --env the app-identity commands, and the role commands with --app-wide, use the default environment. To use another, run init again as Environments describes, adding --api-version API_VERSION with the file's apiVersion to keep the pin (a bare init pins the newest version), or pass --env ENV_ID on those commands. --env on these commands, and its default from multipaas.json, need CLI 1.2.0 or later. Check multipaas --version first.

multipaas app-identity create --name NAME

It prints the identity's client ID and its secret. The secret is shown once. Store it as a secret in the job's environment, never in your repository.

You need permission to manage the application's sign-in, held in the environment the identity belongs to: the one you pass as --env, else environmentId in multipaas.json when that file is for the same application, else the default. With a CLI older than 1.2.0, that is the default environment.

Give it a role

An app identity can do only what its roles allow, like a person. These commands need CLI 0.11.0 or later, and --env on app-identity role needs CLI 1.2.0 or later.

  1. List the application's roles with multipaas roles list. Add --tenant APP_TENANT_ID to include that app-tenant's own roles.

  2. If none fits, list the permissions a role can carry with multipaas roles permissions, then declare a role:

    multipaas roles create --key KEY --name NAME --permission PERMISSION
    

    Repeat --permission for each permission. Write a permission for one entity as PERMISSION=ENTITY_ID.

    To change a role that already exists, use multipaas roles update --role KEY --add-permission PERMISSION --remove-permission PERMISSION, repeating either flag. It changes application roles only and needs CLI 1.1.0 or later.

  3. Give the role to the identity:

    multipaas app-identity role add CLIENT_ID --role KEY
    

    That gives it in the app-tenant in multipaas.json. Add --tenant APP_TENANT_ID for another app-tenant, or --app-wide for the whole application. An app identity belongs to one environment, the one it was created in (multipaas app-identity create --env ENV_ID), and it holds roles only there. Without --app-wide, the command works at an app-tenant, and only an app-tenant in the identity's environment finds the identity; for an identity in another environment, the test app-tenant in multipaas.json or a --tenant outside that environment is refused as not found. Use --tenant with an app-tenant of the identity's environment, or --app-wide --env ENV_ID to give the role across the application in that environment. --env works only with --app-wide, and so does its default: without --env, --app-wide uses environmentId in multipaas.json when that file is for the same application, or the default environment otherwise.

multipaas app-identity role list CLIENT_ID lists the roles the identity holds in a scope, and multipaas app-identity role remove CLIENT_ID --role KEY takes one back. Both work at an app-tenant like role add, so the same limits apply: use --tenant of an app-tenant in the identity's environment, or --app-wide --env ENV_ID.

Which role a token needs:

Get a token

POST, form-encoded, to the token endpoint that multipaas init --json printed as endpoints.token, with grant_type=client_credentials. Identify the identity one way, not both:

Add app_tenant=APP_TENANT_ID for a token that acts in one app-tenant. Leave it out for an app-wide token.

printf 'user = "%s:%s"\n' "$CLIENT_ID" "$CLIENT_SECRET" | curl -s -K - -X POST "$TOKEN_ENDPOINT" -d grant_type=client_credentials -d app_tenant="$APP_TENANT_ID"

This passes the ID and secret on standard input, so they stay off the command line. In the job's own code, send them from its HTTP client.

The answer holds access_token, token_type and expires_in. The token lasts 3600 seconds. There is no refresh token: when the token expires, ask for a new one the same way.

Error Status Meaning
invalid_client 401 The client ID or secret is wrong, the secret was revoked, the identity was deleted, or its environment was deleted and erased. It doesn't say which.
invalid_target 400 The identity holds no role in the scope asked for, or app_tenant isn't an active app-tenant of this application in the identity's environment, or its environment was deleted and not yet erased.
invalid_request 400 The request is malformed, for example it sent HTTP Basic and a client_secret form field together.

Call the API

Send the token as Authorization: Bearer <access_token>, with X-MultiPaaS-App-Api-Version set to the apiVersion in multipaas.json, as in Build on the generated API.

The token decides what the job reaches:

What an app identity can't do

An app identity's token works only on your application's generated API: its records and the me query. It can't:

The multipaas CLI and the MultiPaaS web app don't accept its token. A builder signed in to MultiPaaS does these instead.

Rotate the secret

  1. Add a secret with multipaas app-identity secret add CLIENT_ID. It prints the new secret once.
  2. Deploy the job with the new secret.
  3. Find the old secret's ID with multipaas app-identity secret list CLIENT_ID, and revoke it with multipaas app-identity secret revoke SECRET_ID.

These commands find the identity only in its own environment, so pass the identity's --env ENV_ID unless multipaas.json already names that environment. So does app-identity delete, which needs CLI 1.9.0 or later; app-identity retire is the deprecated name and still works.

A revoked secret gets no new tokens. Tokens already issued with it keep working until they expire. To stop an identity at once, delete it with multipaas app-identity delete CLIENT_ID. Its tokens stop working immediately.

More