Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added sp context manager #400

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 67 additions & 43 deletions notebooks/Capacity Migration.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,15 @@
"source": [
"import sempy_labs as labs\n",
"import sempy_labs.admin as admin\n",
"from sempy_labs._authentication import service_principal_authentication\n",
"\n",
"azure_subscription_id = '' # Enter your Azure subscription ID\n",
"resource_group = '' # Enter the name of the resource group (to be used to create the new F skus)\n",
"\n",
"token_provider = labs.ServicePrincipalTokenProvider.from_azure_key_vault(\n",
" key_vault_uri = '', # Enter your key vault URI\n",
" key_vault_tenant_id = '', # Enter the key vault secret storig your Tenant ID\n",
" key_vault_client_id = '', # Enter the key vault secret storig your Client ID (Applciation ID)\n",
" key_vault_client_secret = '' # Enter the key vault secret storig your Client Secret\n",
")"
"key_vault_uri = '', # Enter your key vault URI\n",
"key_vault_tenant_id = '', # Enter the key vault secret storig your Tenant ID\n",
"key_vault_client_id = '', # Enter the key vault secret storig your Client ID (Applciation ID)\n",
"key_vault_client_secret = '' # Enter the key vault secret storig your Client Secret"
]
},
{
Expand All @@ -119,13 +118,18 @@
"metadata": {},
"outputs": [],
"source": [
"labs.migrate_capacities(\n",
" azure_subscription_id = azure_subscription_id,\n",
" token_provider = token_provider,\n",
" resource_group = resource_group,\n",
" capacities = 'CapacityA',\n",
" p_sku_only = True,\n",
")"
"with service_principal_authentication(\n",
" key_vault_uri=key_vault_uri, \n",
" key_vault_tenant_id=key_vault_tenant_id,\n",
" key_vault_client_id=key_vault_client_id,\n",
" key_vault_client_secret=key_vault_client_secret) as sp:\n",
"\n",
" labs.migrate_capacities(\n",
" azure_subscription_id = azure_subscription_id,\n",
" resource_group = resource_group,\n",
" capacities = 'CapacityA',\n",
" p_sku_only = True,\n",
" )"
]
},
{
Expand All @@ -144,13 +148,18 @@
"metadata": {},
"outputs": [],
"source": [
"labs.migrate_capacities(\n",
" azure_subscription_id = azure_subscription_id,\n",
" token_provider = token_provider,\n",
" resource_group = resource_group,\n",
" capacities = ['CapacityA', 'CapacityB', 'CapacityC'],\n",
" p_sku_only = True,\n",
")"
"with service_principal_authentication(\n",
" key_vault_uri=key_vault_uri, \n",
" key_vault_tenant_id=key_vault_tenant_id,\n",
" key_vault_client_id=key_vault_client_id,\n",
" key_vault_client_secret=key_vault_client_secret) as sp:\n",
"\n",
" labs.migrate_capacities(\n",
" azure_subscription_id = azure_subscription_id,\n",
" resource_group = resource_group,\n",
" capacities = ['CapacityA', 'CapacityB', 'CapacityC'],\n",
" p_sku_only = True,\n",
" )"
]
},
{
Expand All @@ -169,13 +178,18 @@
"metadata": {},
"outputs": [],
"source": [
"labs.migrate_capacities(\n",
" azure_subscription_id = azure_subscription_id,\n",
" token_provider = token_provider,\n",
" resource_group = resource_group,\n",
" capacities = None,\n",
" p_sku_only = True,\n",
")"
"with service_principal_authentication(\n",
" key_vault_uri=key_vault_uri, \n",
" key_vault_tenant_id=key_vault_tenant_id,\n",
" key_vault_client_id=key_vault_client_id,\n",
" key_vault_client_secret=key_vault_client_secret) as sp:\n",
"\n",
" labs.migrate_capacities(\n",
" azure_subscription_id = azure_subscription_id,\n",
" resource_group = resource_group,\n",
" capacities = None,\n",
" p_sku_only = True,\n",
" )"
]
},
{
Expand All @@ -200,13 +214,18 @@
" \"CapacityC\": \"ResourceGroupB\",\n",
"}\n",
"\n",
"labs.migrate_capacities(\n",
" azure_subscription_id = azure_subscription_id,\n",
" token_provider = token_provider,\n",
" resource_group = resource_group_mapping,\n",
" capacities = ['CapacityA', 'CapacityB', 'CapacityC'],\n",
" p_sku_only = True,\n",
")"
"with service_principal_authentication(\n",
" key_vault_uri=key_vault_uri, \n",
" key_vault_tenant_id=key_vault_tenant_id,\n",
" key_vault_client_id=key_vault_client_id,\n",
" key_vault_client_secret=key_vault_client_secret) as sp:\n",
"\n",
" labs.migrate_capacities(\n",
" azure_subscription_id = azure_subscription_id,\n",
" resource_group = resource_group_mapping,\n",
" capacities = ['CapacityA', 'CapacityB', 'CapacityC'],\n",
" p_sku_only = True,\n",
" )"
]
},
{
Expand Down Expand Up @@ -335,15 +354,20 @@
"metadata": {},
"outputs": [],
"source": [
"labs.migrate_fabric_trial_capacity(\n",
" azure_subscription_id = azure_subscription_id,\n",
" token_provider = token_provider,\n",
" resource_group = resource_group,\n",
" source_capacity = '', # The name of the Trial capacity.\n",
" target_capacity = '', # The name of Fabric capacity (if it does not exist it will be created).\n",
" target_capacity_sku = \"F64\", # Defaults to F64 but can specify the required SKU.\n",
" target_capacity_admin_members = None, # Setting this to None will use the same admin members as the Trial capacity.\n",
")"
"with service_principal_authentication(\n",
" key_vault_uri=key_vault_uri, \n",
" key_vault_tenant_id=key_vault_tenant_id,\n",
" key_vault_client_id=key_vault_client_id,\n",
" key_vault_client_secret=key_vault_client_secret) as sp:\n",
"\n",
" labs.migrate_fabric_trial_capacity(\n",
" azure_subscription_id = azure_subscription_id,\n",
" resource_group = resource_group,\n",
" source_capacity = '', # The name of the Trial capacity.\n",
" target_capacity = '', # The name of Fabric capacity (if it does not exist it will be created).\n",
" target_capacity_sku = \"F64\", # Defaults to F64 but can specify the required SKU.\n",
" target_capacity_admin_members = None, # Setting this to None will use the same admin members as the Trial capacity.\n",
" )"
]
}
],
Expand Down
57 changes: 31 additions & 26 deletions notebooks/Service Principal.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,14 @@
"metadata": {},
"outputs": [],
"source": [
"import sempy_labs as labs\n",
"from sempy_labs import admin\n",
"from sempy_labs.tom import connect_semantic_model\n",
"from sempy_labs._authentication import service_principal_authentication\n",
"\n",
"token_provider = labs.ServicePrincipalTokenProvider.from_azure_key_vault(\n",
" key_vault_uri = '', # Enter your key vault URI\n",
" key_vault_tenant_id = '', # Enter the key vault secret storing your Tenant ID\n",
" key_vault_client_id = '', # Enter the key vault secret storing your Client ID (Applciation ID)\n",
" key_vault_client_secret = '' # Enter the key vault secret storing your Client Secret\n",
")"
"key_vault_uri = '', # Enter your key vault URI\n",
"key_vault_tenant_id = '', # Enter the key vault secret storing your Tenant ID\n",
"key_vault_client_id = '', # Enter the key vault secret storing your Client ID (Applciation ID)\n",
"key_vault_client_secret = '' # Enter the key vault secret storing your Client Secret"
]
},
{
Expand All @@ -78,43 +76,50 @@
"metadata": {},
"outputs": [],
"source": [
"admin.list_capacities(token_provider=token_provider)"
"with service_principal_authentication(\n",
" key_vault_uri=key_vault_uri, \n",
" key_vault_tenant_id=key_vault_tenant_id,\n",
" key_vault_client_id=key_vault_client_id,\n",
" key_vault_client_secret=key_vault_client_secret) as sp:\n",
"\n",
" admin.list_capacities()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a0d58c85",
"cell_type": "markdown",
"id": "fcde09a7",
"metadata": {},
"outputs": [],
"source": [
"dataset = '' # Enter the name of the semantic model\n",
"workspace = None # Enter the name of the workspace\n",
"with connect_semantic_model(dataset=dataset, workspace=workspace, readonly=True, token_provider=token_provider) as tom:\n",
" for t in tom.model.Tables:\n",
" print(t.Name)"
"### Use a Service Principal to connect to the Tabular Object Model (also to Azure Analysis Services)"
]
},
{
"cell_type": "markdown",
"id": "27826ff4",
"cell_type": "code",
"execution_count": null,
"id": "c0bf4de4",
"metadata": {},
"outputs": [],
"source": [
"### Use the TokenProvider to connect to Azure Analysis Services"
"dataset = '' # Enter the name of the semantic model\n",
"workspace = None # Enter the name of the workspace (for Azure Analysis Serivces instance use this format: \"asazure://<region>.asazure.windows.net/<server_name>\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0c21bd7e",
"id": "a0d58c85",
"metadata": {},
"outputs": [],
"source": [
"dataset = '' # Enter the name of the semantic model\n",
"workspace = '' # Enter the name of the Azure Analysis Serivces instance (i.e. \"asazure://region.asazure...\")\n",
"with connect_semantic_model(dataset=dataset, workspace=workspace, readonly=True, token_provider=token_provider) as tom:\n",
" for t in tom.model.Tables:\n",
" print(t.Name)"
"with service_principal_authentication(\n",
" key_vault_uri=key_vault_uri, \n",
" key_vault_tenant_id=key_vault_tenant_id,\n",
" key_vault_client_id=key_vault_client_id,\n",
" key_vault_client_secret=key_vault_client_secret) as sp:\n",
"\n",
" with connect_semantic_model(dataset=dataset, workspace=workspace, readonly=True) as tom:\n",
" for t in tom.model.Tables:\n",
" print(t.Name)"
]
}
],
Expand Down
1 change: 1 addition & 0 deletions src/sempy_labs/_auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
token_provider = None
41 changes: 40 additions & 1 deletion src/sempy_labs/_authentication.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from typing import Literal, Optional
from sempy.fabric._token_provider import TokenProvider
from azure.identity import ClientSecretCredential
from sempy._utils._log import log
from contextlib import contextmanager
import sempy_labs._auth as auth


class ServicePrincipalTokenProvider(TokenProvider):
Expand Down Expand Up @@ -140,7 +143,9 @@ def __call__(

def _get_headers(
token_provider: str,
audience: Literal["pbi", "storage", "azure", "graph", "asazure", "keyvault"] = "azure",
audience: Literal[
"pbi", "storage", "azure", "graph", "asazure", "keyvault"
] = "azure",
):
"""
Generates headers for an API request.
Expand All @@ -156,3 +161,37 @@ def _get_headers(
headers["Content-Type"] = "application/json"

return headers


@log
@contextmanager
def service_principal_authentication(
key_vault_uri: str,
key_vault_tenant_id: str,
key_vault_client_id: str,
key_vault_client_secret: str,
):
"""
Establishes an authentication via Service Principal.

Parameters
----------
key_vault_uri : str
Azure Key Vault URI.
key_vault_tenant_id : str
Name of the secret in the Key Vault with the Fabric Tenant ID.
key_vault_client_id : str
Name of the secret in the Key Vault with the Service Principal Client ID.
key_vault_client_secret : str
Name of the secret in the Key Vault with the Service Principal Client Secret.
"""
try:
auth.token_provider = ServicePrincipalTokenProvider.from_azure_key_vault(
key_vault_uri=key_vault_uri,
key_vault_tenant_id=key_vault_tenant_id,
key_vault_client_id=key_vault_client_id,
key_vault_client_secret=key_vault_client_secret,
)
yield
finally:
auth.token_provider = None
Loading
Loading