In this lesson, we’ll explore how to securely manage and use sensitive information, such as application IDs, tenant IDs, and secret keys, in your Databricks notebooks. We’ll cover the `DBUtils.secrets.get` method and the use of secret scopes with Azure Key Vault to keep your secrets safe and hidden.
When working with sensitive information in Databricks, it’s crucial to ensure that values like application IDs and secret keys are not directly exposed in your notebooks. Storing these values securely helps prevent unauthorized access and potential security breaches.
Let’s start by assigning sensitive information to variables:
application_id = "your_application_id" tenant_id = "your_tenant_id" secret = "your_secret_value" container_name = "your_container_name" account_name = "your_account_name" mount_point = "/mnt/your_mount_point"
Instead of hardcoding these values, you can use Python’s F-strings to securely reference variables within strings. For example:
url = f"https://{account_name}.blob.core.windows.net/{container_name}" #This approach allows you to dynamically construct URLs and other strings without directly displaying sensitive information.
Databricks provides the `DBUtils.secrets.get` method, which retrieves secret values stored in a secure location like Azure Key Vault. This method ensures that your secrets are used securely without exposing them in your code.
To securely store your secrets, follow these steps:
3. Create a Secret Scope in Databricks:
With the secret scope in place, you can now access your secrets securely in your Databricks notebooks:
application_id = dbutils.secrets.get(scope="databricks-secrets-639", key="application-id") tenant_id = dbutils.secrets.get(scope="databricks-secrets-639", key="tenant-id") secret = dbutils.secrets.get(scope="databricks-secrets-639", key="secret")
When you run the cell, the values are retrieved securely, and the actual values are not displayed in the notebook, keeping them safe from exposure.
Now that your secrets are securely stored, you can use them to mount a storage container in Databricks:
mount_point = "/mnt/bronze" dbutils.fs.mount( source = f"wasbs://{container_name}@{account_name}.blob.core.windows.net", mount_point = mount_point, extra_configs = { f"fs.azure.account.key.{account_name}.blob.core.windows.net": secret } )
After running the command, the storage container is mounted, and the secret values remain secure.
If needed, you can unmount the storage container:
dbutils.fs.unmount(mount_point)
This step ensures that the mount point is cleared, and you can confirm it by checking the Databricks File System (DBFS).
By using Azure Key Vault in combination with secret scopes and the `DBUtils.secrets.get` method, you can securely manage and use sensitive information in your Databricks notebooks. This approach helps prevent accidental exposure of credentials and other critical data, enhancing the security of your data workflows.