-
Notifications
You must be signed in to change notification settings - Fork 4
build: add user-based managed identity #122
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
Open
hallvictoria
wants to merge
9
commits into
dev
Choose a base branch
from
hallvictoria/USER-MI
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f9276bd
Initial changes for supporting user-based MI
hallvictoria 6044123
merge
hallvictoria a9d99f3
Merge branch 'dev' of https://github.com/Azure/azure-functions-python…
hallvictoria 11d0cbf
fix tests
hallvictoria 2a8ea48
fix user assigned check & tests
hallvictoria 76d84f0
Working user-assigned MI
hallvictoria 6fb0f72
add unit tests
hallvictoria 9ce2fe1
update dependabot
hallvictoria afa806d
feedback
hallvictoria File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 10 additions & 47 deletions
57
...efunctions-extensions-bindings-blob/azurefunctions/extensions/bindings/blob/blobClient.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,22 @@ | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
|
||
import json | ||
from azurefunctions.extensions.base import Datum | ||
from .blobSdkType import BlobSdkType | ||
|
||
from azure.identity import DefaultAzureCredential | ||
from azure.storage.blob import BlobServiceClient | ||
from azurefunctions.extensions.base import Datum, SdkType | ||
from .utils import get_connection_string, using_managed_identity | ||
|
||
|
||
class BlobClient(SdkType): | ||
class BlobClient(BlobSdkType): | ||
def __init__(self, *, data: Datum) -> None: | ||
# model_binding_data properties | ||
self._data = data | ||
self._using_managed_identity = False | ||
self._version = None | ||
self._source = None | ||
self._content_type = None | ||
self._connection = None | ||
self._containerName = None | ||
self._blobName = None | ||
if self._data: | ||
self._version = data.version | ||
self._source = data.source | ||
self._content_type = data.content_type | ||
content_json = json.loads(data.content) | ||
self._connection = get_connection_string(content_json.get("Connection")) | ||
self._using_managed_identity = using_managed_identity( | ||
content_json.get("Connection") | ||
) | ||
self._containerName = content_json.get("ContainerName") | ||
self._blobName = content_json.get("BlobName") | ||
super().__init__(data=data) | ||
|
||
# Returns a BlobClient | ||
def get_sdk_type(self): | ||
""" | ||
When using Managed Identity, the only way to create a BlobClient is | ||
through a BlobServiceClient. There are two ways to create a | ||
BlobServiceClient: | ||
1. Through the constructor: this is the only option when using Managed Identity | ||
2. Through from_connection_string: this is the only option when | ||
not using Managed Identity | ||
|
||
We track if Managed Identity is being used through a flag. | ||
""" | ||
if self._data: | ||
blob_service_client = ( | ||
BlobServiceClient( | ||
account_url=self._connection, credential=DefaultAzureCredential() | ||
) | ||
if self._using_managed_identity | ||
else BlobServiceClient.from_connection_string(self._connection) | ||
) | ||
blob_service_client = super().get_sdk_type() | ||
try: | ||
return blob_service_client.get_blob_client( | ||
container=self._containerName, | ||
blob=self._blobName, | ||
) | ||
else: | ||
raise ValueError(f"Unable to create {self.__class__.__name__} SDK type.") | ||
except Exception as e: | ||
raise ValueError(f"Unable to create {self.__class__.__name__} SDK type." | ||
f"Exception: {e}") |
47 changes: 47 additions & 0 deletions
47
...functions-extensions-bindings-blob/azurefunctions/extensions/bindings/blob/blobSdkType.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
import json | ||
|
||
from azurefunctions.extensions.base import Datum, SdkType | ||
from .utils import (validate_connection_name, | ||
service_client_factory) | ||
|
||
|
||
class BlobSdkType(SdkType): | ||
def __init__(self, *, data: Datum) -> None: | ||
# model_binding_data properties | ||
self._data = data | ||
self._version = None | ||
self._source = None | ||
self._content_type = None | ||
self._connection = None | ||
self._containerName = None | ||
self._blobName = None | ||
if self._data: | ||
self._version = data.version | ||
self._source = data.source | ||
self._content_type = data.content_type | ||
content_json = json.loads(data.content) | ||
self._connection = validate_connection_name( | ||
content_json.get("Connection")) | ||
self._containerName = content_json.get("ContainerName") | ||
self._blobName = content_json.get("BlobName") | ||
|
||
def get_sdk_type(self): | ||
""" | ||
When using Managed Identity, the only way to create a BlobClient is | ||
through a BlobServiceClient. There are two ways to create a | ||
BlobServiceClient: | ||
1. Through the constructor: this is the only option when using Managed Identity | ||
1a. If system-based MI, the credential is DefaultAzureCredential | ||
1b. If user-based MI, the credential is ManagedIdentityCredential | ||
2. Through from_connection_string: this is the only option when | ||
not using Managed Identity | ||
|
||
We track if Managed Identity is being used through a flag. | ||
""" | ||
if self._data: | ||
blob_service_client = service_client_factory(self._connection) | ||
return blob_service_client | ||
else: | ||
raise ValueError("Unable to create Blob Service Client type.") |
46 changes: 9 additions & 37 deletions
46
...tions-extensions-bindings-blob/azurefunctions/extensions/bindings/blob/containerClient.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,21 @@ | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
|
||
import json | ||
from azurefunctions.extensions.base import Datum | ||
from .blobSdkType import BlobSdkType | ||
|
||
from azure.identity import DefaultAzureCredential | ||
from azure.storage.blob import BlobServiceClient | ||
from azurefunctions.extensions.base import Datum, SdkType | ||
from .utils import get_connection_string, using_managed_identity | ||
|
||
|
||
class ContainerClient(SdkType): | ||
class ContainerClient(BlobSdkType): | ||
def __init__(self, *, data: Datum) -> None: | ||
# model_binding_data properties | ||
self._data = data | ||
self._using_managed_identity = False | ||
self._version = "" | ||
self._source = "" | ||
self._content_type = "" | ||
self._connection = "" | ||
self._containerName = "" | ||
self._blobName = "" | ||
if self._data: | ||
self._version = data.version | ||
self._source = data.source | ||
self._content_type = data.content_type | ||
content_json = json.loads(data.content) | ||
self._connection = get_connection_string(content_json.get("Connection")) | ||
self._using_managed_identity = using_managed_identity( | ||
content_json.get("Connection") | ||
) | ||
self._containerName = content_json.get("ContainerName") | ||
self._blobName = content_json.get("BlobName") | ||
super().__init__(data=data) | ||
|
||
# Returns a ContainerClient | ||
def get_sdk_type(self): | ||
if self._data: | ||
blob_service_client = ( | ||
BlobServiceClient( | ||
account_url=self._connection, credential=DefaultAzureCredential() | ||
) | ||
if self._using_managed_identity | ||
else BlobServiceClient.from_connection_string(self._connection) | ||
) | ||
blob_service_client = super().get_sdk_type() | ||
try: | ||
return blob_service_client.get_container_client( | ||
container=self._containerName | ||
) | ||
else: | ||
raise ValueError(f"Unable to create {self.__class__.__name__} SDK type.") | ||
except Exception as e: | ||
raise ValueError(f"Unable to create {self.__class__.__name__} SDK type." | ||
f"Exception: {e}") |
48 changes: 10 additions & 38 deletions
48
...tensions-bindings-blob/azurefunctions/extensions/bindings/blob/storageStreamDownloader.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,23 @@ | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
|
||
import json | ||
from azurefunctions.extensions.base import Datum | ||
from .blobSdkType import BlobSdkType | ||
|
||
from azure.identity import DefaultAzureCredential | ||
from azure.storage.blob import BlobServiceClient | ||
from azurefunctions.extensions.base import Datum, SdkType | ||
from .utils import get_connection_string, using_managed_identity | ||
|
||
|
||
class StorageStreamDownloader(SdkType): | ||
class StorageStreamDownloader(BlobSdkType): | ||
def __init__(self, *, data: Datum) -> None: | ||
# model_binding_data properties | ||
self._data = data | ||
self._using_managed_identity = False | ||
self._version = "" | ||
self._source = "" | ||
self._content_type = "" | ||
self._connection = "" | ||
self._containerName = "" | ||
self._blobName = "" | ||
if self._data: | ||
self._version = data.version | ||
self._source = data.source | ||
self._content_type = data.content_type | ||
content_json = json.loads(data.content) | ||
self._connection = get_connection_string(content_json.get("Connection")) | ||
self._using_managed_identity = using_managed_identity( | ||
content_json.get("Connection") | ||
) | ||
self._containerName = content_json.get("ContainerName") | ||
self._blobName = content_json.get("BlobName") | ||
super().__init__(data=data) | ||
|
||
# Returns a StorageStreamDownloader | ||
def get_sdk_type(self): | ||
if self._data: | ||
blob_service_client = ( | ||
BlobServiceClient( | ||
account_url=self._connection, credential=DefaultAzureCredential() | ||
) | ||
if self._using_managed_identity | ||
else BlobServiceClient.from_connection_string(self._connection) | ||
) | ||
# download_blob() returns a StorageStreamDownloader object | ||
blob_service_client = super().get_sdk_type() | ||
|
||
try: | ||
return blob_service_client.get_blob_client( | ||
container=self._containerName, | ||
blob=self._blobName, | ||
).download_blob() | ||
else: | ||
raise ValueError(f"Unable to create {self.__class__.__name__} SDK type.") | ||
except Exception as e: | ||
raise ValueError(f"Unable to create {self.__class__.__name__} SDK type." | ||
f"Exception: {e}") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.