Sub topic 18
Permissions and the run-as identity
This chapter covers who can see, run, and edit a Databricks job, and which identity the job uses when it touches data. It assumes you have built a multi-task job and can read the JSON and YAML job definitions from earlier chapters. You should also know the basics of Unity Catalog grants.
Two separate questions
Job security answers two questions that are easy to confuse:
- Who can act on the job? This is controlled by job permissions, which apply to people and groups who view, run, edit, or manage the job.
- Who does the job act as? This is the run-as identity. It is the user or service principal whose credentials the tasks use when they read tables, call secrets, or start compute.
A person with permission to click Run now does not run the job as themselves. The run uses the run-as identity. That is why a person with limited data access can safely trigger a job that writes to a protected schema.
Job permission levels
A job has an access control list. Each entry gives a user, group, or service principal one permission level.
| Level | What it allows |
|---|---|
| CAN VIEW | See the job definition and its run results |
| CAN MANAGE RUN | Everything in CAN VIEW, plus Run now and canceling runs |
| CAN MANAGE | Everything in CAN MANAGE RUN, plus editing settings, deleting the job, and changing permissions |
| IS OWNER | Same abilities as CAN MANAGE. A job has exactly one owner |
Each level includes the ones above it in the table. Grant to groups instead of individuals where you can, so access follows team membership. The owner must be a single user or service principal, not a group.
Workspace admins can manage every job regardless of the access control list.
The run-as identity
By default, a job runs as the user who created it. That default causes three problems:
- If that person leaves the company and their account is removed, scheduled runs start failing.
- The job inherits all of that person's data access, which is usually broader than the job needs.
- Audit logs show a person as the actor for automated writes.
The fix is to set run as to a service principal. A service principal is an identity for automation, with no password-based login and no tie to one employee. Grant it only what the pipeline needs.
Rules for changing the run-as identity:
- To run as a service principal, you need the Service Principal User role on that principal, or you must be a workspace admin.
- To run as a different user, you generally need to be a workspace admin.
- The run-as identity must be able to read the notebooks or files that the tasks reference. For a notebook this means CAN READ.
What the run-as identity needs
Give the identity the minimum for each resource the job touches:
- Unity Catalog data: USE CATALOG, USE SCHEMA, and SELECT on sources. Add MODIFY (and CREATE TABLE if the job creates tables) on targets.
- Compute: permission to use the cluster policy that job clusters reference. For existing all-purpose clusters, it needs CAN ATTACH TO.
- Secrets: READ on any secret scope the notebooks use.
- Code: read access to notebooks, repos, or workspace files.
If a task fails with an authorization error, check the run-as identity first, not your own account. Your own access is irrelevant to the run.
Setting permissions and run-as with the API
The Jobs API takes run_as inside the job settings. A service principal is identified by its application ID. The script below updates run-as, then adds a group to the access control list. It uses PATCH on the permissions endpoint, which adds entries. PUT would replace the whole list and could remove existing access.
import os
import requests
host = os.environ["DATABRICKS_HOST"].rstrip("/")
token = os.environ["DATABRICKS_TOKEN"]
job_id = int(os.environ["JOB_ID"])
sp_application_id = os.environ["SP_APPLICATION_ID"]
headers = {"Authorization": f"Bearer {token}"}
# 1. Run the job as a service principal.
resp = requests.post(
f"{host}/api/2.1/jobs/update",
headers=headers,
json={
"job_id": job_id,
"new_settings": {"run_as": {"service_principal_name": sp_application_id}},
},
timeout=30,
)
resp.raise_for_status()
# 2. Let the on-call group start and cancel runs, without editing the job.
resp = requests.patch(
f"{host}/api/2.0/permissions/jobs/{job_id}",
headers=headers,
json={
"access_control_list": [
{"group_name": "oncall", "permission_level": "CAN_MANAGE_RUN"}
]
},
timeout=30,
)
resp.raise_for_status()
print(resp.json())The API writes permission levels with underscores, such as CAN_MANAGE_RUN. The UI shows them with spaces.
Setting both in a Databricks Asset Bundle
In a bundle, put run_as and permissions on the job resource so reviewers see them in the same pull request as the schedule and tasks. This extends the bundle from the previous chapter:
resources:
jobs:
daily_sales:
name: daily-sales-pipeline
run_as:
service_principal_name: "00000000-0000-0000-0000-000000000000"
permissions:
- level: CAN_MANAGE_RUN
group_name: oncall
- level: CAN_VIEW
group_name: analysts
schedule:
quartz_cron_expression: "0 0 6 * * ?"
timezone_id: UTC
tasks:
- task_key: ingest
notebook_task:
notebook_path: ../src/ingest.ipynbReplace the zero UUID with the real application ID of your service principal. The identity that runs databricks bundle deploy becomes the job owner, and the service principal named in run_as runs the tasks. Those can be different identities, and for production they usually should be. A common pattern is to deploy from a CI system's service principal and run as a separate, narrower one. The deploying identity still needs the Service Principal User role on the run-as principal.
Common errors
- Run now works for you but tasks fail on a table. You tested interactively with your own grants. The job uses the run-as identity, which lacks them.
- Cannot change run as. You lack the Service Principal User role on the target principal, or you tried to pick another user without being an admin.
- Scheduled runs stopped after an employee left. The job was running as that user. Move it to a service principal.
- Someone with CAN MANAGE RUN edited a job. They cannot. If they did, they hold CAN MANAGE through a group. Review inherited group membership.
- Permissions vanished after an API call. A
PUTreplaced the list. UsePATCHto add entries.
Exercises
Exercise
An analyst has CAN MANAGE RUN on a job and no SELECT grant on the source table. The job's run-as identity is a service principal that does have SELECT. The analyst clicks Run now. Does the read succeed, and why?
Show answer
Yes. The tasks run as the service principal, not as the analyst, so the service principal's grants decide whether the read succeeds. The analyst's permission only controls the ability to start and cancel the run.
Exercise
You must give a team permission to start and cancel runs of a job through the Permissions API without removing anyone's current access. Which HTTP method and permission level do you use?
Show answer
Use PATCH on /api/2.0/permissions/jobs/{jobid} with the permission level CANMANAGE_RUN. PATCH adds or updates entries, while PUT replaces the entire access control list.
Exercise
A production job runs as its creator, and that person is leaving the company. List the steps to make the job safe.
Show answer
Create or choose a service principal, grant it the needed Unity Catalog, secret scope, cluster policy, and notebook access, confirm you hold the Service Principal User role on it, then set run_as on the job to that service principal. Also make sure a group, not a departing user, holds CAN MANAGE so the job stays maintainable.