-
Notifications
You must be signed in to change notification settings - Fork 200
Expand file tree
/
Copy path_azure.py
More file actions
407 lines (307 loc) · 13 KB
/
Copy path_azure.py
File metadata and controls
407 lines (307 loc) · 13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
# SPDX-License-Identifier: MIT
# SPDX-FileCopyrightText: Copyright (c) 2024 Development Seed
from collections.abc import Coroutine
from datetime import datetime
from typing import Any, Protocol, Self, TypeAlias, TypedDict, Unpack
from typing_extensions import override
from .._lib import store as _store # pyright: ignore[reportMissingModuleSource]
from ._client import ClientConfig
from ._retry import RetryConfig
class AzureConfig(TypedDict, total=False):
"""Configuration parameters for AzureStore."""
account_name: str
"""The name of the azure storage account. (Required.)
**Environment variable**: ``AZURE_STORAGE_ACCOUNT_NAME``.
"""
account_key: str
"""Master key for accessing storage account.
**Environment variables**:
- ``AZURE_STORAGE_ACCOUNT_KEY``
- ``AZURE_STORAGE_ACCESS_KEY``
- ``AZURE_STORAGE_MASTER_KEY``
"""
client_id: str
"""The client id for use in client secret or k8s federated credential flow.
**Environment variables**:
- ``AZURE_STORAGE_CLIENT_ID``
- ``AZURE_CLIENT_ID``
"""
client_secret: str
"""The client secret for use in client secret flow.
**Environment variables**:
- ``AZURE_STORAGE_CLIENT_SECRET``
- ``AZURE_CLIENT_SECRET``
"""
tenant_id: str
"""The tenant id for use in client secret or k8s federated credential flow.
**Environment variables**:
- ``AZURE_STORAGE_TENANT_ID``
- ``AZURE_STORAGE_AUTHORITY_ID``
- ``AZURE_TENANT_ID``
- ``AZURE_AUTHORITY_ID``
"""
authority_host: str
"""Sets an alternative authority host for OAuth based authorization.
Defaults to ``https://login.microsoftonline.com``.
Common hosts for azure clouds are:
- Azure China: ``"https://login.chinacloudapi.cn"``
- Azure Germany: ``"https://login.microsoftonline.de"``
- Azure Government: ``"https://login.microsoftonline.us"``
- Azure Public: ``"https://login.microsoftonline.com"``
**Environment variables**:
- ``AZURE_STORAGE_AUTHORITY_HOST``
- ``AZURE_AUTHORITY_HOST``
"""
sas_key: str
"""
Shared access signature.
The signature is expected to be percent-encoded, much like they are provided in
the azure storage explorer or azure portal.
**Environment variables**:
- ``AZURE_STORAGE_SAS_KEY``
- ``AZURE_STORAGE_SAS_TOKEN``
"""
token: str
"""A static bearer token to be used for authorizing requests.
**Environment variable**: ``AZURE_STORAGE_TOKEN``.
"""
use_emulator: bool
"""Set if the Azure emulator should be used (defaults to ``False``).
**Environment variable**: ``AZURE_STORAGE_USE_EMULATOR``.
"""
use_fabric_endpoint: bool
"""Set if Microsoft Fabric url scheme should be used (defaults to ``False``).
When disabled the url scheme used is ``https://{account}.blob.core.windows.net``.
When enabled the url scheme used is ``https://{account}.dfs.fabric.microsoft.com``.
.. note::
``endpoint`` will take precedence over this option.
"""
endpoint: str
"""Override the endpoint used to communicate with blob storage.
Defaults to ``https://{account}.blob.core.windows.net``.
By default, only HTTPS schemes are enabled. To connect to an HTTP endpoint, enable
``allow_http`` in the client options.
**Environment variables**:
- ``AZURE_STORAGE_ENDPOINT``
- ``AZURE_ENDPOINT``
"""
msi_endpoint: str
"""Endpoint to request a imds managed identity token.
**Environment variables**:
- ``AZURE_MSI_ENDPOINT``
- ``AZURE_IDENTITY_ENDPOINT``
"""
object_id: str
"""Object id for use with managed identity authentication.
**Environment variable**: ``AZURE_OBJECT_ID``.
"""
msi_resource_id: str
"""Msi resource id for use with managed identity authentication.
**Environment variable**: ``AZURE_MSI_RESOURCE_ID``.
"""
federated_token_file: str
"""Sets a file path for acquiring azure federated identity token in k8s.
Requires ``client_id`` and ``tenant_id`` to be set.
**Environment variable**: ``AZURE_FEDERATED_TOKEN_FILE``.
"""
use_azure_cli: bool
"""Set if the Azure Cli should be used for acquiring access token.
<https://learn.microsoft.com/en-us/cli/azure/account?view=azure-cli-latest#az-account-get-access-token>.
**Environment variable**: ``AZURE_USE_AZURE_CLI``.
"""
skip_signature: bool
"""If enabled, ``AzureStore`` will not fetch credentials and will not sign requests.
This can be useful when interacting with public containers.
**Environment variable**: ``AZURE_SKIP_SIGNATURE``.
"""
container_name: str
"""Container name.
**Environment variable**: ``AZURE_CONTAINER_NAME``.
"""
disable_tagging: bool
"""If set to ``True`` will ignore any tags provided to uploads.
**Environment variable**: ``AZURE_DISABLE_TAGGING``.
"""
fabric_token_service_url: str
"""Service URL for Fabric OAuth2 authentication.
**Environment variable**: ``AZURE_FABRIC_TOKEN_SERVICE_URL``.
"""
fabric_workload_host: str
"""Workload host for Fabric OAuth2 authentication.
**Environment variable**: ``AZURE_FABRIC_WORKLOAD_HOST``.
"""
fabric_session_token: str
"""Session token for Fabric OAuth2 authentication.
**Environment variable**: ``AZURE_FABRIC_SESSION_TOKEN``.
"""
fabric_cluster_identifier: str
"""Cluster identifier for Fabric OAuth2 authentication.
**Environment variable**: ``AZURE_FABRIC_CLUSTER_IDENTIFIER``.
"""
class AzureAccessKey(TypedDict):
"""A shared Azure Storage Account Key.
See `Authorize with Shared Key <https://learn.microsoft.com/en-us/rest/api/storageservices/authorize-with-shared-key>`_.
"""
access_key: str
"""Access key value."""
expires_at: datetime | None
"""Expiry datetime of credential. The datetime should have time zone set.
If None, the credential will never expire.
"""
class AzureSASToken(TypedDict):
"""A shared access signature.
See `Shared Access Signatures <https://learn.microsoft.com/en-us/rest/api/storageservices/delegate-access-with-shared-access-signature>`_.
"""
sas_token: str | list[tuple[str, str]]
"""SAS token."""
expires_at: datetime | None
"""Expiry datetime of credential. The datetime should have time zone set.
If None, the credential will never expire.
"""
class AzureBearerToken(TypedDict):
"""An authorization token.
See `Authorize with Azure AD <https://learn.microsoft.com/en-us/rest/api/storageservices/authorize-with-azure-active-directory>`_.
"""
token: str
"""Bearer token."""
expires_at: datetime | None
"""Expiry datetime of credential. The datetime should have time zone set.
If None, the credential will never expire.
"""
AzureCredential: TypeAlias = AzureAccessKey | AzureSASToken | AzureBearerToken
"""A type alias for supported azure credentials to be returned from ``AzureCredentialProvider``.
"""
class AzureCredentialProvider(Protocol):
"""A type hint for a synchronous or asynchronous callback to provide custom Azure credentials.
This should be passed into the ``credential_provider`` parameter of ``AzureStore``.
"""
def __call__(self) -> AzureCredential | Coroutine[Any, Any, AzureCredential]: # pyright: ignore[reportExplicitAny]
"""Return an ``AzureCredential``."""
...
class AzureStore(_store.AzureStore):
"""Interface to a Microsoft Azure Blob Storage container.
All constructors will check for environment variables. Refer to
:class:`~vortex.store.AzureConfig` for valid environment variables.
"""
def __new__( # type: ignore[misc] # Overlap between argument names and ** TypedDict items: "container_name"
cls,
container_name: str | None = None,
*,
prefix: str | None = None,
config: AzureConfig | None = None,
client_options: ClientConfig | None = None,
retry_config: RetryConfig | None = None,
credential_provider: AzureCredentialProvider | None = None,
**kwargs: Unpack[AzureConfig], # pyright: ignore[reportGeneralTypeIssues]
) -> Self:
"""Construct a new AzureStore.
Args:
container_name: the name of the container.
Keyword Args:
prefix: A prefix within the bucket to use for all operations.
config: Azure Configuration. Values in this config will override values inferred from
the url. Defaults to None.
client_options: HTTP Client options. Defaults to None.
retry_config: Retry configuration. Defaults to None.
credential_provider: A callback to provide custom Azure credentials.
kwargs: Azure configuration values. Supports the same values as ``config``, but as named
keyword args.
Returns:
AzureStore
"""
return super().__new__( # pyright: ignore[reportUnknownVariableType]
cls,
container_name,
prefix=prefix,
config=config,
client_options=client_options,
retry_config=retry_config,
credential_provider=credential_provider,
**kwargs, # pyright: ignore[reportCallIssue]
)
@override
@classmethod
def from_url(
cls,
url: str,
*,
prefix: str | None = None,
config: AzureConfig | None = None,
client_options: ClientConfig | None = None,
retry_config: RetryConfig | None = None,
credential_provider: AzureCredentialProvider | None = None,
**kwargs: Unpack[AzureConfig],
) -> Self:
"""Construct a new AzureStore with values populated from a well-known storage URL.
Any path on the URL will be assigned as the ``prefix`` for the store. So if you
pass ``https://<account>.blob.core.windows.net/<container>/path/to/directory``,
the store will be created with a prefix of ``path/to/directory``, and all further
operations will use paths relative to that prefix.
The supported url schemes are:
- ``abfs[s]://<container>/<path>`` (according to `fsspec <https://github.com/fsspec/adlfs>`_)
- ``abfs[s]://<file_system>@<account_name>.dfs.core.windows.net/<path>``
- ``abfs[s]://<file_system>@<account_name>.dfs.fabric.microsoft.com/<path>``
- ``az://<container>/<path>`` (according to `fsspec <https://github.com/fsspec/adlfs>`_)
- ``adl://<container>/<path>`` (according to `fsspec <https://github.com/fsspec/adlfs>`_)
- ``azure://<container>/<path>`` (custom)
- ``https://<account>.dfs.core.windows.net``
- ``https://<account>.blob.core.windows.net``
- ``https://<account>.blob.core.windows.net/<container>``
- ``https://<account>.dfs.fabric.microsoft.com``
- ``https://<account>.dfs.fabric.microsoft.com/<container>``
- ``https://<account>.blob.fabric.microsoft.com``
- ``https://<account>.blob.fabric.microsoft.com/<container>``
Args:
url: well-known storage URL.
Keyword Args:
prefix: A prefix within the bucket to use for all operations.
config: Azure Configuration. Values in this config will override values inferred from the
url. Defaults to None.
client_options: HTTP Client options. Defaults to None.
retry_config: Retry configuration. Defaults to None.
credential_provider: A callback to provide custom Azure credentials.
kwargs: Azure configuration values. Supports the same values as ``config``, but as named keyword
args.
Returns:
AzureStore
"""
return super().from_url(
url,
prefix=prefix,
config=config,
client_options=client_options,
retry_config=retry_config,
credential_provider=credential_provider,
**kwargs,
)
@override
def __eq__(self, value: object) -> bool:
return super().__eq__(value)
@override
def __getnewargs_ex__(self): # pyright: ignore[reportUnknownParameterType]
return super().__getnewargs_ex__() # pyright: ignore[reportUnknownVariableType, reportUnknownMemberType]
@property
@override
def prefix(self) -> str | None:
"""Get the prefix applied to all operations in this store, if any."""
return super().prefix
@property
@override
def config(self) -> AzureConfig:
"""Get the underlying Azure config parameters."""
return super().config
@property
@override
def client_options(self) -> ClientConfig | None:
"""Get the store's client configuration."""
return super().client_options
@property
@override
def credential_provider(self) -> AzureCredentialProvider | None:
"""Get the store's credential provider."""
return super().credential_provider
@property
@override
def retry_config(self) -> RetryConfig | None:
"""Get the store's retry configuration."""
return super().retry_config