Sub topic 10
Permissions and running as a service principal
This chapter covers who can see and run a deployed bundle, and which identity the jobs execute as. It assumes you can read a databricks.yml with targets and variables, you know what a Databricks job is, and you have used databricks bundle deploy. It also assumes you know that a workspace has users, groups, and service principals.
Two separate questions
A deployed job involves two identity questions, and bundles answer them with two settings.
- Who may act on the job? This covers viewing it, triggering a run, and editing it. The
permissionsmapping controls this. - Who does the job run as? This is the identity whose access rights apply to every table, secret, and cluster the job touches. The
run_asmapping controls this.
Without run_as, a job runs as the person who deployed it. That is convenient in development. In production it creates a fragile setup. The job breaks if that person leaves, and it holds that person's data access instead of a narrow set of rights.
A service principal is a workspace identity for automation. It has no password and no personal mailbox. It authenticates with credentials issued to it, and you grant it only the access its jobs need. Running production jobs as a service principal is the standard practice for that reason.
The deploy identity and the run_as identity can be the same or different. Keeping them separate lets you give the pipeline the right to create jobs without giving it the right to read production data.
Setting permissions
Use the permissions mapping to grant access. Each entry names one principal and one level. A principal is a user_name, a group_name, or a service_principal_name.
permissions:
- level: CAN_VIEW
group_name: analysts
- level: CAN_RUN
group_name: data-engineers
- level: CAN_MANAGE
service_principal_name: <service-principal-application-id>When you write permissions at the top level of the bundle, it applies to every resource in the bundle that supports permissions. The top-level levels are CAN_VIEW, CAN_RUN, and CAN_MANAGE. The CLI also uses the same entries for the bundle's folder in the workspace. A group that can run the job can therefore read the deployed notebooks. Check the folder after your first deployment to confirm it.
You can also set permissions on one resource when different jobs need different audiences:
resources:
jobs:
daily_etl:
name: daily_etl
permissions:
- level: CAN_MANAGE_RUN
group_name: data-engineers
- level: CAN_VIEW
group_name: analystsJob permission levels are CAN_VIEW, CAN_MANAGE_RUN, CAN_MANAGE, and IS_OWNER. The set of levels differs by resource type. A pipeline, for example, uses CAN_RUN, and a model serving endpoint uses CAN_QUERY. If validation rejects a level, check the list for that resource type in the reference documentation.
Prefer groups
Grant to groups, not individual users. Group membership changes without a code change, and your YAML stays stable when people join or leave. Reserve user_name entries for cases like a personal development deployment.
What `permissions` does not cover
The permissions mapping controls access to the workspace objects that the bundle creates. It does not grant access to data. If the job reads prod_catalog.sales.orders, the identity in run_as needs USE CATALOG, USE SCHEMA, and SELECT on those objects in Unity Catalog. Those grants live in Unity Catalog, not in the bundle's permissions section. Missing data grants are one of the most common reasons a job works in dev and fails in prod.
Setting `run_as`
The run_as mapping takes either a user_name or a service_principal_name. For a service principal, use its application ID, which is a UUID. Do not use its display name.
run_as:
service_principal_name: <service-principal-application-id>At the top level, run_as applies to the jobs and pipelines in the bundle. You normally put it inside a target so that dev stays simple and prod is locked down:
bundle:
name: my_project
include:
- resources/*.yml
targets:
dev:
mode: development
default: true
workspace:
host: https://<your-dev-workspace-url>
prod:
mode: production
workspace:
host: https://<your-prod-workspace-url>
root_path: /Workspace/Shared/.bundle/${bundle.name}/${bundle.target}
run_as:
service_principal_name: <service-principal-application-id>
permissions:
- level: CAN_VIEW
group_name: analysts
- level: CAN_MANAGE
group_name: data-engineersIn dev, nothing sets run_as, so the job runs as you. That is intended. You want development runs to use your own access, and you want mistakes to be traceable to you.
Rights the deploying identity needs
Setting run_as to a service principal is not automatic for every user. The identity that runs bundle deploy must be allowed to use that service principal. In the workspace, this is the Service Principal User role on the service principal, and workspace admins have it implicitly. If the role is missing, the deployment fails with a permission error when it creates the job.
The service principal also needs these rights:
- Read access to the files in the bundle's workspace folder, so the job can load notebooks and other files.
- Permission to use the compute the job requests. Creating a job cluster usually needs cluster creation rights on the workspace entitlement, or a cluster policy that allows it.
- The Unity Catalog grants described earlier.
- Access to any secrets scopes the code reads.
How the production check helps
With mode: production, validation checks that your configuration is safe for a shared deployment. It reports problems such as a deployment path under a single user's folder when no run_as is set. The check does not know your data grants, so a clean validate does not prove the job will have the access it needs at run time. Run the job once in a staging environment as the same service principal before you rely on it.
Authenticating as the service principal
There are two ways to arrange the identities, and the trade-offs differ.
| Setup | How it works | Strength | Weakness |
|---|---|---|---|
| CI deploys as a person or a deploy identity, jobs run as a service principal via `run_as` | The pipeline holds one credential. The job's identity is set in YAML. | Deploy rights and data rights are separate. | The deploy identity needs the Service Principal User role. |
| CI authenticates as the service principal itself, and `run_as` names the same principal | The pipeline and the job use one identity. | Simple to configure and audit. | A leaked CI credential gives access to production data. |
For the second setup, the pipeline authenticates with OAuth machine-to-machine credentials. You create an OAuth secret for the service principal and give the CLI three environment variables:
export DATABRICKS_HOST="https://<your-prod-workspace-url>"
export DATABRICKS_CLIENT_ID="<service-principal-application-id>"
export DATABRICKS_CLIENT_SECRET="<oauth-secret-from-your-secret-store>"
databricks bundle validate -t prod
databricks bundle deploy -t prodStore the secret in your CI system's secret store. Never put it in databricks.yml, in a variable default, or in the repository. The CLI reads these environment variables itself, so the bundle file needs no credentials at all.
Common errors
- The deploy fails with a permission error on the service principal. The deploying identity lacks the Service Principal User role on it. Ask a workspace admin to grant the role.
- The job is created but fails at run time with
PERMISSION_DENIEDon a table. Therun_asidentity is missing a Unity Catalog grant. Grant it on the catalog, schema, and table. Do not widenpermissions. - The notebook cannot be found at run time. The
run_asidentity cannot read the bundle's workspace folder. Check that the target'spermissionslist covers the identity that runs the job, and look at the folder's permissions in the workspace. - Validation complains about the root path. In production mode, a
root_pathunder/Users/<you>/is flagged. Use a shared path such as/Workspace/Shared/.bundle/${bundle.name}/${bundle.target}, as the earlier chapters did. - You used a display name in
service_principal_name. The field expects the application ID. A display name fails to resolve. - A group cannot see the job. Group names are case-sensitive and must match exactly. Confirm the group exists in the workspace, not only in your identity provider.
Putting it together
A sound production setup follows these steps:
- Create a service principal for the project and note its application ID.
- Grant it the Unity Catalog privileges the job needs, and nothing more.
- Add
run_asandpermissionsto theprodtarget only. - Authenticate CI with OAuth credentials from a secret store.
- Validate, deploy, and run once in a staging target using the same service principal.
- Check the "Run as" field and the permission list in the workspace.
Dev deployments keep the default behavior, so each developer runs and tests with their own identity and does not touch production resources.
Exercise
A job deployed to prod with run_as set to a service principal starts, then fails with PERMISSIONDENIED when reading `prodcatalog.sales.orders. Adding the data-engineers group with CAN_MANAGE to the bundle's permissions` did not help. Why, and what fixes it?
Show answer
The permissions mapping controls access to the job and the bundle folder, not to data. The job runs as the service principal, so that principal needs Unity Catalog grants: USE CATALOG on prod_catalog, USE SCHEMA on the sales schema, and SELECT on the orders table. Granting those to the service principal fixes the error.
Exercise
You want developers to run and test against dev as themselves, but prod jobs to run as a service principal and be visible to the analysts group. Which parts of the bundle do you change, and where do you put them?
Show answer
Add run_as with service_principal_name (the application ID) and a permissions entry with level: CAN_VIEW and group_name: analysts under the prod target. Leave run_as out of the dev target so jobs there run as the deploying user.
Exercise
Why does bundle deploy -t prod fail with a permission error when run_as names a service principal, even though your YAML is valid?
Show answer
The identity running the deployment must be allowed to use that service principal. It needs the Service Principal User role on the principal, or workspace admin rights. Validation checks the configuration, not whether the deploying identity holds that role.