Skip to content

job

Runs the whole migration as a single job that survives a crash or a restart. Instead of four commands you re-run by hand, a job moves through fixed states, saving its state to disk before each step:

queued → exporting → validating → importing → verifying → done
any step that fails ↘ failed
StateStep
exportingexport from --source into --users, then the --mapping’s export.user and ids.target, if any. Skipped when the job has no --source and starts from an existing --users file.
validatingThe same checks as validate. Any problem fails the job, unless a validate.after hook downgrades it to a warning or decides otherwise.
importingimport into --target, with organizations.cmf.jsonl and roles.cmf.jsonl read from beside --users if present.
verifyingThe same lookups as diff, with the import’s target IDs for verify.lookup: {strategy: target_id}. A user missing from the target, or with drifted attributes, fails the job, unless a verify.after hook decides otherwise. Users the import skipped on purpose aren’t looked for.

If the process dies or is interrupted (Ctrl-C and SIGTERM stop it cleanly), the job stays in the state it was in. job run picks it up there and repeats only that step and the ones after it. Steps already finished are not redone.

For an Auth0 target the import is chunked, and every chunk is checkpointed as the job goes: the Auth0 job ID as soon as the job exists, the outcome once it finishes. After a restart a finished chunk isn’t submitted again, and a chunk whose Auth0 job is still running is waited for rather than resubmitted (which would report every user in it as a duplicate). A chunk whose Auth0 job ended as failed is submitted again. If --users changed since the import started, the job fails instead of matching the old checkpoints to different users.

Kratos, Keycloak, Firebase and Cognito targets don’t checkpoint. A job interrupted during their import runs the whole import again on resume: users created before the interruption are reported as already existing, and the rest are created.

Credentials are never stored with a job. Each step reads them from the environment, so the same variables must be set for job run as for the other commands (AUTH0_DOMAIN, AUTH0_TOKEN or AUTH0_CLIENT_ID/AUTH0_CLIENT_SECRET, KRATOS_ADMIN_URL, KEYCLOAK_URL/KEYCLOAK_REALM and a username/password or client ID/secret, FIREBASE_PROJECT_ID and GOOGLE_APPLICATION_CREDENTIALS or FIREBASE_ACCESS_TOKEN, and FIREBASE_HASH_SIGNER_KEY/FIREBASE_HASH_SALT_SEPARATOR, COGNITO_USER_POOL_ID, AWS_ACCESS_KEY_ID/AWS_SECRET_ACCESS_KEY and optionally AWS_SESSION_TOKEN). job create checks that they are there, so a mistake shows up before a long export rather than after.

Terminal window
# queue a job, printing its ID
id=$(iamigrate job create --source flatfile --in users.csv --target kratos --users ./work/users.cmf.jsonl.gz)
iamigrate job run "$id" # run it, or resume it after an interruption
iamigrate job status "$id" # where it is and what each step found

When a step fails, fix the cause and retry:

Terminal window
iamigrate job retry "$id" # try the failed step again
iamigrate job retry "$id" --from exporting # go further back, e.g. a validation problem that only re-exporting fixes
iamigrate job run "$id"

retry without --from keeps the earlier steps’ results and the import’s chunk checkpoints. --from discards the results, and the checkpoints, of the state it goes back to and of every step after it.

A job can only be driven by one job run at a time; a second gets job is being run by another process.

CommandDoes
job createQueue a job and print its ID
job run <id>Run a job to completion, or resume it. Exits non-zero if the job fails or is interrupted
job retry <id>Put a failed job back in a state, ready for job run
job status <id>Show the state and the result of each finished step; --json prints the whole record
job report <id>Print, as JSON, what a finished (done or failed) job’s steps found

All take --store (default ./jobs/), where the jobs’ state is kept, one <id>.json per job (see Job stores), --api-url, and --hooks. --dir is the deprecated former name of --store.

--store is a local directory, given as a path or a file:// URI, or the URI of a remote store, for jobs that resume on another machine, a CI runner or a restarted container, and for serve replicas sharing their jobs.

Only one run drives a job at a time: the one holding its lock. In a local directory it is a file lock, which the system releases if the process dies. In a remote store it is a lease the run renews while it goes; one that crashes stops renewing, and the job can be resumed elsewhere 60 seconds later. A run that loses its lease (it stalled past it, and another took the job over) stops before its next save. Every save is also conditional on the version the run last read, so two writers never overwrite each other.

Store--store
Local directory./jobs/, file:///srv/jobs
Google Cloud Storagegs://bucket/prefix
Amazon S3s3://bucket/prefix
Azure Blob Storageazblob://container/prefix

gs://bucket/prefix keeps each job as the object prefix/<id>.json, and its lease as prefix/<id>.lease. Saves and leases are conditional on the object’s generation (ifGenerationMatch, 0 to create an object).

Credentials are a service account key file at $GOOGLE_APPLICATION_CREDENTIALS, or an access token in $GOOGLE_OAUTH_ACCESS_TOKEN (from gcloud auth print-access-token, which expires after an hour). The account needs storage.objects.get, create, delete and list on the bucket, as in the Storage Object User role. With $STORAGE_EMULATOR_HOST set, requests go to that emulator (such as fake-gcs-server) instead, with no credentials.

Terminal window
iamigrate job --store gs://acme-migrations/jobs create --target kratos --users /srv/work/users.cmf.jsonl
iamigrate job --store gs://acme-migrations/jobs run "$id"

s3://bucket/prefix keeps each job as the object prefix/<id>.json, and its lease as prefix/<id>.lease. Saves and leases are conditional writes (If-None-Match: * to create an object, If-Match on its ETag to replace it). An S3-compatible store that ignores them is refused when the store is opened, with an error saying so.

Credentials and settings come from the environment variables the AWS tools read: AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_SESSION_TOKEN for temporary credentials (aws configure export-credentials --format env prints them for a profile), AWS_REGION (or AWS_DEFAULT_REGION, default us-east-1), and AWS_ENDPOINT_URL_S3 (or AWS_ENDPOINT_URL) for an S3-compatible store such as MinIO, which is then addressed path-style. The credentials need s3:GetObject, s3:PutObject, s3:DeleteObject and s3:ListBucket on the bucket and prefix.

Terminal window
iamigrate job --store s3://acme-migrations/jobs create --target kratos --users /srv/work/users.cmf.jsonl
iamigrate job --store s3://acme-migrations/jobs run "$id"

azblob://container/prefix keeps each job as the blob prefix/<id>.json. Saves are conditional on the blob’s ETag (If-Match, or If-None-Match: * to create one), and a job’s lock is a native blob lease on prefix/<id>.lease, renewed while the job runs. Azure leases last between 15 and 60 seconds, so a crashed run’s job can be resumed elsewhere a minute later at most.

Credentials come from $AZURE_STORAGE_CONNECTION_STRING, or $AZURE_STORAGE_ACCOUNT with either $AZURE_STORAGE_KEY (the account key) or $AZURE_STORAGE_SAS_TOKEN (a shared access signature allowing read, write, delete and list on the container). For Azurite, AZURE_STORAGE_CONNECTION_STRING=UseDevelopmentStorage=true.

Terminal window
iamigrate job --store azblob://migrations/jobs create --target kratos --users /srv/work/users.cmf.jsonl
iamigrate job --store azblob://migrations/jobs run "$id"

With --hooks hooks.yaml, the jobs run in this process fire hooks: job.before_run and the state events around every step, the export, validate, import and verify events of the steps, and the handlers of the --mapping’s strategies.

Terminal window
iamigrate job --hooks hooks.yaml run "$id"

Hooks aren’t stored with a job: pass --hooks to every job run, a resumed one included. The job remembers a fingerprint of the file, and job status lists a run under a different one as changed. A resumed job fires its events again, with the same event IDs. A hook call that vetoed a step, failed, or failed and was ignored is listed by job status, under hooks, and validate’s downgraded problems under warnings. --hooks can’t be combined with --api-url: a server’s jobs fire the hooks the server was started with.

With --api-url (or $IAMIGRATE_API_URL) the commands act on the jobs of a running iamigrate serve instead of running them in process, and --store is ignored:

Terminal window
id=$(iamigrate job --api-url http://127.0.0.1:8080 create --target kratos --admin-url http://kratos:4434 --users /srv/work/users.cmf.jsonl)
iamigrate job --api-url http://127.0.0.1:8080 run "$id" # waits for the job to finish
iamigrate job --api-url http://127.0.0.1:8080 report "$id"

The server starts a job as soon as it is created, so job run only waits for it and polls its state; interrupting it leaves the job running. The server’s own environment supplies the credentials, and --users and the other file paths are paths on the server. $IAMIGRATE_API_TOKEN is sent as the bearer token. job retry works the same way: the server runs the job again as soon as it is put back, and job run waits for it.

FlagDescription
--targetTarget connector: auth0, kratos, keycloak, firebase or cognito (required)
--usersCMF users file, written by the export and read by the other steps; gzipped if it ends in .gz (required)
--sourceSource connector: flatfile, kratos, keycloak, firebase, auth0 or azure-ad-b2c. Without it there’s no export and --users must already exist
--mappingmapping.yaml path
--upsertAllow re-running the import against existing users
--connection-id, --connectionAuth0 database connection, by ID or name (default: the tenant’s only one). Mutually exclusive
--connection-configAuth0 connection JSON, to tighten validate’s username rules
--in, --format, --source-mappingInput of a flatfile (--in, --format csv|json, --source-mapping) or keycloak (--in) source, as for export
--admin-url, --schema-id, --schema-fileKratos Admin API URL (or $KRATOS_ADMIN_URL), identity schema ID, and schema JSON for validate
--export-connection-id, --export-fields, --export-limitAuth0 source options (--export-limit also applies to azure-ad-b2c), as for export
--tenant, --filter, --extension-attributesAzure AD B2C source options, as for export; the client ID and secret come from the environment
--hash-rounds, --hash-mem-costFirebase project’s scrypt parameters; the signer key and salt separator come from the environment
--user-pool-id, --region, --endpoint-urlCognito user pool ID (or $COGNITO_USER_POOL_ID), AWS region (default: the pool ID’s) and API URL for an emulator; the AWS keys come from the environment