Secret Scope in Azure Databricks

What is Secret Scope and Why to Use It

When running notebooks in Databricks, we may need to provide sensitive data, such as the client secret of a service principal. For example, below is the example code snippet from Mounting cloud object storage on Azure Databricks – Azure Databricks | Microsoft Learn to mount an Azure Storage container in Databricks:

configs = {
  "fs.azure.account.auth.type": "OAuth",
  "fs.azure.account.oauth.provider.type": "org.apache.hadoop.fs.azurebfs.oauth2.ClientCredsTokenProvider",
  "fs.azure.account.oauth2.client.id": "<application-id>",
  "fs.azure.account.oauth2.client.secret": dbutils.secrets.get(scope="<scope-name>", key="<service-credential-key-name>"),
  "fs.azure.account.oauth2.client.endpoint": "https://login.microsoftonline.com/<directory-id>/oauth2/token"}

# Optionally, you can add <directory-name> to the source URI of your mount point.
dbutils.fs.mount(
  source = "abfss://<container-name>@<storage-account-name>.dfs.core.windows.net/",
  mount_point = "/mnt/<mount-name>",
  extra_configs = configs)

Instead of putting the client secret as plain text in the code, here we retrieve the secret from the Secret Scope <scope-name> with the key name <service-credential-key-name>:

dbutils.secrets.get(scope="<scope-name>", key="<service-credential-key-name>")

This is the purpose of Secret Scope: a collection of secrets identified by a name. We create a Secret Scope to store sensitive data such as credentials, API keys, or connection strings.

How to Create Secret Scope in Databricks Workspace

There are two types of secret scope: Azure Key Vault-backed and Databricks-backed.

Azure Key Vault-backed Scope

Azure Databricks workspace does not provide navigation or a menu for creating scope. To create a secret scope, you need to go directly to the URL below and that will directs you to the form:

https://<databricks-instance>#secrets/createScope

Here, you must provide the scope name, manage principal (typically All workspace users), and the key vault’s DNS name (Vault URI) and resource ID. For key vault data, you can find them from Azure Portal (clicking JSON View to see the resource ID)

Hit the Create button, and that’s it! For more details, you can refer to Create an Azure Key Vault-backed secret scope

Databricks-backed Scope

Databricks-backed secret scope is stored in (backed by) an encrypted database owned and managed by Databricks. Databricks-backed scope can only be created with Databricks CLI. You can find the steps to create such Scopes on Azure and AWS:

Secret Management: Create, List, Update and Delete

With Azure key vault-backed scope, we can directly create, list, update and delete the secrets from the key vault. This is one of the user-friendly features when you have the secret scopes backed by Azure key vaults. Secret management can also be performed with Databricks CLI or the Secrets API, which is cloud agnostic and works on AWS, Azure, and GCP:

Find the Referred Key Vault of Secret Scope

One common request for Azure key vault-backed scope is to identify the referred key vault. Unfortunately, Databricks workspace does not provide a straightforward UI for such queries. In Databricks notebook, you can leverage dbutils.secrets to guess and narrow down the key vault behind a scope:

# List all scopes in the current workspace
dbutils.secrets.listScopes() 
 
# List all secrets in the scope. 
# This may help you refresh your memory and match the key vault
dbutils.secrets.list("<scope-name>")

Using Secret API

To my knowledge, the fastest way to identify the key vault that a scope points to is using Secret API. First, we need a personal access token (PAT). In the Databricks workspace, go to Settings → Developer → Manage Access tokens to generate a PAT.

Then, you can run a curl command in the terminal to retrieve the details of scopes:

curl -X GET -H "Authorization: Bearer <your-PAT> \
https://<databricks-host>/api/2.0/secrets/scopes/list

# response
{
  "scopes": [
    {
      "name": "<scope-name",
      "backend_type": "AZURE_KEYVAULT",
      "keyvault_metadata": {
        "resource_id": "<resource-id>",
        "dns_name": "<dns-name>"
      }
    },
    ...
}

Reference:

Jim
Jim

Site reliability engineer working in a global gaming and entertainment company

Articles: 18

Leave a Reply

Your email address will not be published. Required fields are marked *