diff --git a/.env.example b/.env.example index 2a2e71d..a714602 100644 --- a/.env.example +++ b/.env.example @@ -1,7 +1,7 @@ # Required by Fabric, Power BI, Microsoft Graph, and SharePoint examples. -TENANT_ID= -CLIENT_ID= -CLIENT_SECRET= +FABRIC_TENANT_ID= +FABRIC_CLIENT_ID= +FABRIC_CLIENT_SECRET= # Required only by examples/database.py. FABRIC_SQL_ENDPOINT=.datawarehouse.fabric.microsoft.com diff --git a/docs/dataflow_gen2_cicd_upgrade.md b/docs/dataflow_gen2_cicd_upgrade.md index c610c69..b202cd4 100644 --- a/docs/dataflow_gen2_cicd_upgrade.md +++ b/docs/dataflow_gen2_cicd_upgrade.md @@ -25,9 +25,9 @@ Fabric currently rejects a Dataflow Gen2 CI/CD refresh initiated by an SPN with poll the refresh job. The user must have Member or higher workspace access and access to every connection used by the dataflow. Connection visibility or an `Online` connection test alone does not prove that refresh will succeed. -The interactive login is requested in the configured `TENANT_ID`, which also -matters when the user is a guest whose home tenant differs from the workspace -tenant. +The interactive login is requested in the configured `FABRIC_TENANT_ID`, which +also matters when the user is a guest whose home tenant differs from the +workspace tenant. ## Prerequisites @@ -40,8 +40,9 @@ tenant. 3. Sign in as a user with Member or higher workspace access and access to all those connections. Interactive login must be possible on the machine running the script. Do not substitute an SPN token for this user token. -4. Set `TENANT_ID`, `CLIENT_ID`, and `CLIENT_SECRET` in `.env`, as described in - [the examples guide](../examples/README.md). Keep `.env` out of Git. +4. Set `FABRIC_TENANT_ID`, `FABRIC_CLIENT_ID`, and `FABRIC_CLIENT_SECRET` in + `.env`, as described in [the examples guide](../examples/README.md). Keep + `.env` out of Git. The exact-match mode currently handles literal `Sql.Database(server, database)`, `Fabric.Warehouse`, and `Lakehouse.Contents` calls. Dynamic SQL arguments and diff --git a/docs/development.md b/docs/development.md index 484544a..042fa22 100644 --- a/docs/development.md +++ b/docs/development.md @@ -31,10 +31,10 @@ it local: cp .env.example .env ``` -Set `TENANT_ID`, `CLIENT_ID`, and `CLIENT_SECRET` for Fabric, Power BI, Graph, -and SharePoint examples. The database and SharePoint scripts list their extra -variables in `.env.example`. See [Examples](../examples/README.md) for each -script's API audience and safety level. +Set `FABRIC_TENANT_ID`, `FABRIC_CLIENT_ID`, and `FABRIC_CLIENT_SECRET` for +Fabric, Power BI, Graph, and SharePoint examples. The database and SharePoint +scripts list their extra variables in `.env.example`. See +[Examples](../examples/README.md) for each script's API audience and safety level. ## Run and validate diff --git a/examples/README.md b/examples/README.md index b886f71..b3737bd 100644 --- a/examples/README.md +++ b/examples/README.md @@ -24,8 +24,10 @@ Set the values required by the script you choose. Never commit `.env`. | `sharepoint.py` | Microsoft Graph | Does not perform a file operation | `SP_HOSTNAME`, `SP_SITE_PATH` | | `database.py` | Fabric SQL endpoint | Does not perform a write | `FABRIC_SQL_ENDPOINT`, `FABRIC_DATABASE` | -Any script that reaches an API call requires `TENANT_ID`, `CLIENT_ID`, and -`CLIENT_SECRET`. +The Fabric, Power BI, Graph, and SharePoint API examples require +`FABRIC_TENANT_ID`, `FABRIC_CLIENT_ID`, and `FABRIC_CLIENT_SECRET`. The database +example uses `FABRIC_CLIENT_ID` and `FABRIC_CLIENT_SECRET` with its SQL endpoint +and database settings. ## Run diff --git a/examples/_setup.py b/examples/_setup.py index 361d59c..8c531d4 100644 --- a/examples/_setup.py +++ b/examples/_setup.py @@ -65,7 +65,7 @@ def require_configured_value(value: str, name: str) -> str: def build_auth() -> Auth: """Build the shared Azure credential used by Fabric, Graph, and SharePoint.""" tenant_id, client_id, client_secret = require_environment( - "TENANT_ID", "CLIENT_ID", "CLIENT_SECRET" + "FABRIC_TENANT_ID", "FABRIC_CLIENT_ID", "FABRIC_CLIENT_SECRET" ) return Auth(tenant_id, client_id, client_secret) @@ -113,6 +113,9 @@ def build_sharepoint_client() -> SharePointClient: def build_database_client() -> Database: """Build a Fabric SQL endpoint client without opening a connection.""" endpoint, database, client_id, client_secret = require_environment( - "FABRIC_SQL_ENDPOINT", "FABRIC_DATABASE", "CLIENT_ID", "CLIENT_SECRET" + "FABRIC_SQL_ENDPOINT", + "FABRIC_DATABASE", + "FABRIC_CLIENT_ID", + "FABRIC_CLIENT_SECRET", ) return Database(endpoint, database, client_id, client_secret) diff --git a/msdev_kit/fabric/report.py b/msdev_kit/fabric/report.py index 577929a..c519eac 100644 --- a/msdev_kit/fabric/report.py +++ b/msdev_kit/fabric/report.py @@ -564,7 +564,7 @@ def get_report_definition( return {"message": "Missing report id, please check.", "content": ""} if operations is None: - # fabric_auth = Auth(TENANT_ID, CLIENT_ID, CLIENT_SECRET) + # fabric_auth = Auth(fabric_tenant_id, fabric_client_id, fabric_client_secret) # fabric_token = fabric_auth.get_token('fabric') operations = Operations(self.token) diff --git a/tests/fabric/test_example_setup.py b/tests/fabric/test_example_setup.py new file mode 100644 index 0000000..cff555a --- /dev/null +++ b/tests/fabric/test_example_setup.py @@ -0,0 +1,69 @@ +"""Credential environment used by the runnable examples.""" + +import os +from unittest.mock import patch + +import pytest + +from examples import _setup + + +def test_build_auth_uses_fabric_prefixed_credentials(): + values = { + "FABRIC_TENANT_ID": "fabric-tenant", + "FABRIC_CLIENT_ID": "fabric-client", + "FABRIC_CLIENT_SECRET": "fabric-secret", + "TENANT_ID": "legacy-tenant", + "CLIENT_ID": "legacy-client", + "CLIENT_SECRET": "legacy-secret", + } + with ( + patch.dict(os.environ, values, clear=True), + patch.object(_setup, "Auth") as auth, + ): + _setup.build_auth() + + auth.assert_called_once_with("fabric-tenant", "fabric-client", "fabric-secret") + + +def test_build_auth_does_not_fall_back_to_legacy_credentials(): + values = { + "TENANT_ID": "legacy-tenant", + "CLIENT_ID": "legacy-client", + "CLIENT_SECRET": "legacy-secret", + } + with patch.dict(os.environ, values, clear=True): + with pytest.raises(RuntimeError, match="FABRIC_TENANT_ID"): + _setup.build_auth() + + +def test_build_database_client_uses_fabric_prefixed_credentials(): + values = { + "FABRIC_SQL_ENDPOINT": "server.example.com", + "FABRIC_DATABASE": "database", + "FABRIC_CLIENT_ID": "fabric-client", + "FABRIC_CLIENT_SECRET": "fabric-secret", + "CLIENT_ID": "legacy-client", + "CLIENT_SECRET": "legacy-secret", + } + with ( + patch.dict(os.environ, values, clear=True), + patch.object(_setup, "Database") as database, + ): + _setup.build_database_client() + + database.assert_called_once_with( + "server.example.com", "database", "fabric-client", "fabric-secret" + ) + + +def test_build_database_client_does_not_fall_back_to_legacy_credentials(): + values = { + "FABRIC_SQL_ENDPOINT": "server.example.com", + "FABRIC_DATABASE": "database", + "CLIENT_ID": "legacy-client", + "CLIENT_SECRET": "legacy-secret", + } + with patch.dict(os.environ, values, clear=True): + with pytest.raises(RuntimeError, match="FABRIC_CLIENT_ID"): + _setup.build_database_client()