Skip to content

Commit 278164a

Browse files
jsjmrjsj
authored andcommitted
allow AZURE_STORAGE_TOKEN auth
1 parent a631106 commit 278164a

File tree

3 files changed

+40
-18
lines changed

3 files changed

+40
-18
lines changed

src/msfabricutils/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
from msfabricutils.common.fabric_duckdb_connection import FabricDuckDBConnection
44
from msfabricutils.core import (
5+
get_azure_storage_access_token,
56
get_fabric_bearer_token,
6-
get_onelake_access_token,
77
get_workspace,
88
get_workspace_lakehouse_tables,
99
get_workspace_lakehouses,
@@ -13,7 +13,7 @@
1313
__all__ = (
1414
"FabricDuckDBConnection",
1515
"get_fabric_bearer_token",
16-
"get_onelake_access_token",
16+
"get_azure_storage_access_token",
1717
"get_workspace",
1818
"get_workspace_lakehouse_tables",
1919
"get_workspace_lakehouses",

src/msfabricutils/core/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
from .auth import get_fabric_bearer_token, get_onelake_access_token
1+
from .auth import get_azure_storage_access_token, get_fabric_bearer_token, get_onelake_access_token
22
from .lakehouse import get_workspace_lakehouse_tables, get_workspace_lakehouses
33
from .workspace import get_workspace, get_workspaces
44

55
__all__ = (
6+
"get_azure_storage_access_token",
67
"get_workspace",
78
"get_workspaces",
89
"get_workspace_lakehouses",

src/msfabricutils/core/auth.py

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import os
2+
3+
from azure.core.exceptions import ClientAuthenticationError
14
from azure.identity import DefaultAzureCredential
25

36

@@ -24,19 +27,38 @@ def get_access_token(audience: str) -> str:
2427

2528
def get_onelake_access_token() -> str:
2629
"""
27-
Retrieves an access token for OneLake storage.
30+
Alias for `get_azure_storage_access_token`
31+
"""
32+
return get_azure_storage_access_token()
33+
34+
def get_azure_storage_access_token() -> str:
35+
"""
36+
Retrieves an access token for Azure Storage.
2837

2938
This function attempts to obtain an access token for accessing Azure storage.
30-
It first checks if the code is running in a Microsoft Fabric notebook environment
31-
and attempts to use the `notebookutils` library to get the token. If the library
32-
is not available, it falls back to using the `DefaultAzureCredential` from the Azure SDK
33-
to fetch the token.
39+
It first checks if the `AZURE_STORAGE_TOKEN` environment variable is set.
40+
Otherwise, it tries to get the token using `notebookutils.credentials.getToken`.
41+
Lastly, it falls back to using the `DefaultAzureCredential`.
3442

3543
Returns:
36-
The access token used for authenticating requests to Azure OneLake storage.
44+
The access token used for authenticating requests to Azure Storage.
3745
"""
38-
audience = "https://storage.azure.com"
39-
return get_access_token(audience)
46+
47+
token = os.environ.get("AZURE_STORAGE_TOKEN")
48+
if token:
49+
return token
50+
51+
try:
52+
audience = "https://storage.azure.com"
53+
return get_access_token(audience)
54+
except ClientAuthenticationError as e:
55+
raise ClientAuthenticationError(
56+
f"{str(e)}\n\n"
57+
"Additional troubleshooting steps:\n"
58+
"1. Ensure you can use any of the credentials methods to get an access token\n"
59+
"2. Set the `AZURE_STORAGE_TOKEN` environment variable with a valid access token"
60+
) from e
61+
4062

4163

4264
def get_fabric_bearer_token() -> str:
@@ -71,14 +93,13 @@ def get_azure_devops_access_token() -> str:
7193

7294
def get_storage_options() -> dict[str, str]:
7395
"""
74-
Retrieves storage options including a bearer token for OneLake storage.
96+
Retrieves storage options including a bearer token for Azure Storage.
7597

76-
This function calls `get_onelake_access_token` to obtain a bearer token
77-
and returns a dictionary containing the token and a flag indicating
78-
whether to use the Fabric endpoint.
98+
This function calls `get_azure_storage_access_token` to obtain a bearer token
99+
and returns a dictionary containing the token.
79100

80101
Returns:
81-
A dictionary containing the storage options for OneLake.
102+
A dictionary containing the storage options for Azure Storage.
82103

83104
Example:
84105
**Retrieve storage options**
@@ -87,7 +108,7 @@ def get_storage_options() -> dict[str, str]:
87108

88109
options = get_storage_options()
89110
options
90-
{'bearer_token': 'your_token_here', 'use_fabric_endpoint': 'true'}
111+
{"bearer_token": "your_token_here"}
91112
```
92113
"""
93-
return {"bearer_token": get_onelake_access_token(), "use_fabric_endpoint": "true"}
114+
return {"bearer_token": get_azure_storage_access_token()}

0 commit comments

Comments
 (0)