Sub topic 14
Handling credentials safely
This chapter assumes you can already authenticate with the CLI (OAuth or a personal access token), use profiles and environment variables, and call the REST API with a bearer token. Here you will learn how to keep those credentials out of places where they leak, and how to limit the damage if one does.
Where credentials leak
Most leaks come from a few habits:
- Pasting a token into a script or notebook and committing it to Git.
- Passing a token as a command-line argument, which shows up in shell history and in process listings.
- Printing request headers or environment variables in CI logs.
- Sharing one long-lived token across a whole team.
The fix in each case is the same. Keep the secret outside the code, give it the narrowest permissions that work, and make it short-lived or easy to revoke.
Prefer short-lived credentials
OAuth is the safer default. A user signing in with databricks auth login gets a token that expires within about an hour and is refreshed by the CLI. A personal access token (PAT) can live for months, so a leaked PAT stays dangerous for a long time.
If you must use a PAT, set an expiry when you create it. Revoke it as soon as it is no longer needed.
Keep secrets out of code
Read secrets from the environment, never from string literals. The CLI, SDK, and curl examples in this tutorial all follow this pattern.
# Set in your shell or injected by the CI system, never committed
export DATABRICKS_HOST="https://<workspace-host>"
export DATABRICKS_TOKEN="$(cat /run/secrets/databricks_token)"
curl -sS "$DATABRICKS_HOST/api/2.1/jobs/list" \
-H "Authorization: Bearer $DATABRICKS_TOKEN"Reading the token from a file or a secret manager keeps it out of your shell history. The token does not appear in the command text. Avoid --token style flags, because arguments are visible to other users through ps.
Add local files to .gitignore if they could hold secrets. This includes .env files. ~/.databrickscfg lives in your home directory and is not part of a repository, but never copy it into one.
.env
*.token
.databrickscfgUse service principals for automation
A service principal is an identity for a program rather than a person. Automation should run as one, because it does not break when an employee leaves and its permissions can be set separately from any user's.
Service principals authenticate with OAuth machine-to-machine (M2M). You create a client ID and client secret, and the tools exchange them for a short-lived access token. The CLI and SDK read these from environment variables:
export DATABRICKS_HOST="https://<workspace-host>"
export DATABRICKS_CLIENT_ID="<service-principal-application-id>"
export DATABRICKS_CLIENT_SECRET="<injected-by-your-secret-store>"
databricks current-user meThe last command should print the service principal, not a person. Run it first to confirm which identity the automation uses.
Apply least privilege
Least privilege means granting only the access a task needs. For a deployment pipeline, that typically means:
- Give the service principal permission to manage only the jobs or folders it deploys, not workspace admin rights.
- Use a separate service principal for each environment, so a leak in dev cannot touch production.
- Rotate secrets on a schedule, and immediately after any suspected exposure.
Credential flow in a pipeline
Summary of choices
| Situation | Better choice | Avoid |
|---|---|---|
| Interactive work on a laptop | OAuth login with a profile | Long-lived PAT in a script |
| Scheduled or CI automation | Service principal with OAuth M2M | A personal user's token |
| Passing the secret to a tool | Environment variable from a secret store | Command-line flag or hardcoded string |
| Multiple environments | One identity per environment | One shared credential |
Exercise
A teammate's deployment script contains databricks jobs list --token dapi123... and runs under their personal account in CI. Name two problems and the fix for each.
Show answer
First, the token is a command-line argument, so it is visible in process listings and logs. Pass it through the DATABRICKS_TOKEN environment variable, injected from a secret store. Second, the pipeline depends on a person's identity and long-lived PAT. Replace it with a service principal using OAuth M2M, with only the permissions the pipeline needs.