Sub topic 10
Permissions and running as a service principal
This chapter assumes you can write a databricks.yml with targets and variables, and that you have deployed a job with databricks bundle deploy. You should also know what a Databricks service principal is: a non-human identity for automation, with its own application ID.
Two separate questions
A deployed job raises two questions that are easy to mix up.
- Who can see, run, or manage it? This is controlled by
permissions. - Whose identity does it run with? This is controlled by
run_as.
A job can be managed by your team and still run as a service principal. That is the usual production setup.
Why not run production as yourself
By default, a job runs with the identity of the person who deployed it. That causes problems in production:
- The job fails if that person leaves or loses access.
- The job inherits that person's data access, which is usually broader than the job needs.
- Audit logs point to a person rather than to the pipeline.
A service principal has only the grants you give it, and it is not tied to anyone's employment.
Setting run_as per target
Put run_as in the production target only. Development stays under your own identity, which is simpler to debug.
targets:
dev:
mode: development
default: true
prod:
mode: production
workspace:
host: https://<your-prod-workspace-url>
run_as:
service_principal_name: <service-principal-application-id>The value is the service principal's application ID, not its display name. You can also use user_name to run as another user, but a service principal is the better choice for production.
Granting permissions
Use a top-level permissions list to apply the same access to every resource in the bundle. Each entry names one principal and one level.
permissions:
- group_name: data-engineers
level: CAN_MANAGE
- group_name: analysts
level: CAN_VIEWThe principal can be user_name, group_name, or service_principal_name. Levels differ by resource. Jobs use CAN_VIEW, CAN_MANAGE_RUN, CAN_MANAGE, and IS_OWNER. Check the documentation for the resource type you use.
You can also set permissions on a single resource. Put it inside the job definition to give one job different access than the rest of the bundle.
resources:
jobs:
daily_etl:
name: daily_etl
permissions:
- group_name: analysts
level: CAN_MANAGE_RUNPrefer groups over individual users. Membership changes then happen in your identity provider, not in the bundle.
What the deploying identity needs
The identity that runs databricks bundle deploy needs more than the identity the job runs as.
| Identity | Needs |
|---|---|
| Deployer (you, or the CI service principal) | Permission to create and manage jobs, and write access to the bundle's `root_path` |
| `run_as` service principal | Access to the notebooks or files, the cluster policy or compute it uses, and the data it reads and writes (for example `USE CATALOG`, `USE SCHEMA`, and `SELECT` or `MODIFY` grants in Unity Catalog) |
When run_as is set, the deploying identity must also be allowed to use that service principal. In practice this means the deployer has the Service Principal User role on it.
How the pieces fit
Production mode checks
With mode: production, the CLI validates settings that protect a shared deployment. If you set a root_path that includes ${bundle.target} and run_as, validation is satisfied. The mode warns when deployments would run under a personal identity or write to a user's home folder. Running databricks bundle validate -t prod shows these problems before you deploy.
databricks bundle validate -t prod
databricks bundle deploy -t prodExercise
Your production job deploys without errors but fails at run time with a permission error on a Unity Catalog table. The job has run_as set to a service principal. Which identity should you check first, and why?
Show answer
Check the run_as service principal. The job executes with its identity, so it needs USE CATALOG, USE SCHEMA, and SELECT or MODIFY on the tables. The deployer's access does not apply at run time.
Exercise
Write the permissions entry that lets the group analysts start runs of every job in a bundle but not edit them.
Show answer
permissions: - groupname: analysts level: CANMANAGE_RUN