dbt-fabric has a notebookutils auth method, and it kills your Service Principal
Running dbt from a Fabric notebook? Set authentication to notebookutils and dbt runs under the workspace identity. No Service Principal, no client secret, no Key Vault lookups.
This morning I made the discovery of the century: dbt-fabric has an authentication method called notebookutils.
Until now, every notebook that ran our dbt project needed a Service Principal. Fetch the tenant ID, client ID and client secret from Key Vault, push them into environment variables, and let dbt pick them up with authentication: env. It works, but it's one more secret to rotate and one more identity to manage.
With notebookutils you don't need any of that. dbt runs under your workspace identity.
How we run dbt on Fabric
We keep it simple. Our dbt project sits in a lakehouse, in Files/dbt-code/transform. A notebook with that lakehouse attached installs dbt-fabric and calls dbt from a cell:
%pip install -q dbt-fabric
!cd /lakehouse/default/Files/dbt-code/transform && dbt run
The annoying part was always authentication.
The profile
Here's our profiles.yml. The cli output is for local development, the notebookutils output is what the notebook uses. All it takes is authentication: notebookutils:
transform:
target: notebookutils
outputs:
cli:
type: fabric
driver: "ODBC Driver 18 for SQL Server"
host: <your-warehouse>.datawarehouse.fabric.microsoft.com
database: gold
schema: dbo
authentication: CLI
notebookutils:
type: fabric
driver: "ODBC Driver 18 for SQL Server"
host: "{{ env_var('DWH_HOST') }}"
database: gold
schema: dbo
authentication: notebookutils # was: env
The notebook
The notebook gets a lot shorter too. The only environment variable left is the SQL endpoint, which the notebook looks up from the attached lakehouse. The three Service Principal lines are gone.
%pip install -q tabulate
%pip install -q dbt-fabric
# 1) imports
import os
import subprocess
import notebookutils as nb
from tabulate import tabulate
# 2) set up params to run dbt
ctx = nb.runtime.context
wid = ctx.get("currentWorkspaceId")
env = ctx.get("currentWorkspaceName").split("-")[-1]
# find the SQL analytics endpoint of the attached lakehouse
sql_endpoint = (
nb.lakehouse.getWithProperties()["properties"]
.get("sqlEndpointProperties")
.get("connectionString")
)
print(tabulate([["wid", wid], ["env", env], ["sql", sql_endpoint]], tablefmt="rounded_grid"))
# 3) set them as env vars
os.environ["DWH_HOST"] = sql_endpoint
# no longer needed:
# os.environ["AZURE_TENANT_ID"] = "<tenant-id>"
# os.environ["AZURE_CLIENT_ID"] = nb.credentials.getSecret(f"https://kv-<env>.vault.azure.net/", "sp-client-id")
# os.environ["AZURE_CLIENT_SECRET"] = nb.credentials.getSecret(f"https://kv-<env>.vault.azure.net/", "sp-client-secret")
# 4) run dbt
project_dir = "/lakehouse/default/Files/dbt-code/transform"
subprocess.run(["dbt", "deps"], cwd=project_dir, check=True)
subprocess.run(["dbt", "run"], cwd=project_dir, check=True)
And dbt runs as before, just without a Service Principal anywhere in sight:
Running with dbt=1.12.6
Registered adapter: fabric=1.11.1
Found 74 models, 419 data tests, 40 sources, 738 macros
Concurrency: 1 threads (target='notebookutils')
1 of 74 START sql table model dbo.dim_company ...................... [RUN]
1 of 74 OK created sql table model dbo.dim_company ................. [OK in 2.21s]
2 of 74 START sql table model dbo.dim_date ......................... [RUN]
2 of 74 OK created sql table model dbo.dim_date .................... [OK in 1.90s]
...