Skip to content

fix: validating undefined connection strings #44

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

Merged
merged 22 commits into from
Aug 16, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@
# Licensed under the MIT License.

import json
import os
from typing import Union

from azure.storage.blob import BlobClient as BlobClientSdk
from azurefunctions.extensions.base import Datum, SdkType
from .utils import validate_connection_string


class BlobClient(SdkType):
def __init__(self, *, data: Union[bytes, Datum]) -> None:

# model_binding_data properties
self._data = data
self._version = None
Expand All @@ -25,7 +24,7 @@ def __init__(self, *, data: Union[bytes, Datum]) -> None:
self._source = data.source
self._content_type = data.content_type
content_json = json.loads(data.content)
self._connection = os.getenv(content_json["Connection"])
self._connection = validate_connection_string(content_json["Connection"])
self._containerName = content_json["ContainerName"]
self._blobName = content_json["BlobName"]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ class BlobClientConverter(
binding="blob",
trigger="blobTrigger",
):

@classmethod
def check_input_type_annotation(cls, pytype: type) -> bool:
return issubclass(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@
# Licensed under the MIT License.

import json
import os
from typing import Union

from azure.storage.blob import ContainerClient as ContainerClientSdk
from azurefunctions.extensions.base import Datum, SdkType
from .utils import validate_connection_string


class ContainerClient(SdkType):
def __init__(self, *, data: Union[bytes, Datum]) -> None:

# model_binding_data properties
self._data = data
self._version = ""
Expand All @@ -25,7 +24,7 @@ def __init__(self, *, data: Union[bytes, Datum]) -> None:
self._source = data.source
self._content_type = data.content_type
content_json = json.loads(data.content)
self._connection = os.getenv(content_json["Connection"])
self._connection = validate_connection_string(content_json["Connection"])
self._containerName = content_json["ContainerName"]
self._blobName = content_json["BlobName"]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@
# Licensed under the MIT License.

import json
import os
from typing import Union

from azure.storage.blob import BlobClient as BlobClientSdk
from azurefunctions.extensions.base import Datum, SdkType
from .utils import validate_connection_string


class StorageStreamDownloader(SdkType):
def __init__(self, *, data: Union[bytes, Datum]) -> None:

# model_binding_data properties
self._data = data or {}
self._version = ""
Expand All @@ -25,7 +24,7 @@ def __init__(self, *, data: Union[bytes, Datum]) -> None:
self._source = data.source
self._content_type = data.content_type
content_json = json.loads(data.content)
self._connection = os.getenv(content_json["Connection"])
self._connection = validate_connection_string(content_json["Connection"])
self._containerName = content_json["ContainerName"]
self._blobName = content_json["BlobName"]

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
import os


def validate_connection_string(connection_string: str) -> str:
"""
Validates the connection string. If the connection string is
not an App Setting, an error will be thrown.
"""
if connection_string == None:
raise ValueError(
"Storage account connection string cannot be none. "
"Please provide a connection string."
)
elif not os.getenv(connection_string):
raise ValueError(
f"Storage account connection string {connection_string} does not exist. "
f"Please make sure that it is a defined App Setting."
)
return os.getenv(connection_string)
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,55 @@ def test_input_populated(self):
self.assertIsNotNone(sdk_result)
self.assertIsInstance(sdk_result, BlobClientSdk)

def test_invalid_input_populated(self):
content = {
"Connection": "NotARealConnectionString",
"ContainerName": "test-blob",
"BlobName": "text.txt",
}

sample_mbd = MockMBD(
version="1.0",
source="AzureStorageBlobs",
content_type="application/json",
content=json.dumps(content),
)

with self.assertRaises(ValueError) as e:
datum: Datum = Datum(value=sample_mbd, type="model_binding_data")
result: BlobClient = BlobClientConverter.decode(
data=datum, trigger_metadata=None, pytype=BlobClient
)
self.assertEqual(
e.exception.args[0],
"Storage account connection string NotARealConnectionString does not exist. "
"Please make sure that it is a defined App Setting.",
)

def test_none_input_populated(self):
content = {
"Connection": None,
"ContainerName": "test-blob",
"BlobName": "text.txt",
}

sample_mbd = MockMBD(
version="1.0",
source="AzureStorageBlobs",
content_type="application/json",
content=json.dumps(content),
)

with self.assertRaises(ValueError) as e:
datum: Datum = Datum(value=sample_mbd, type="model_binding_data")
result: BlobClient = BlobClientConverter.decode(
data=datum, trigger_metadata=None, pytype=BlobClient
)
self.assertEqual(
e.exception.args[0],
"Storage account connection string cannot be none. Please provide a connection string.",
)

def test_input_invalid_pytype(self):
content = {
"Connection": "AzureWebJobsStorage",
Expand Down Expand Up @@ -169,7 +218,7 @@ def test_blob_client_invalid_creation(self):
dict_repr,
[
'{"direction": "MockBindingDirection.IN", '
'"dataType": null, "type": "blob", '
'"type": "blob", '
'"properties": '
'{"SupportsDeferredBinding": false}}'
],
Expand Down Expand Up @@ -202,7 +251,7 @@ def test_blob_client_valid_creation(self):
dict_repr,
[
'{"direction": "MockBindingDirection.IN", '
'"dataType": null, "type": "blob", '
'"type": "blob", '
'"properties": '
'{"SupportsDeferredBinding": true}}'
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,55 @@ def test_input_populated(self):
self.assertIsNotNone(sdk_result)
self.assertIsInstance(sdk_result, ContainerClientSdk)

def test_invalid_input_populated(self):
content = {
"Connection": "NotARealConnectionString",
"ContainerName": "test-blob",
"BlobName": "text.txt",
}

sample_mbd = MockMBD(
version="1.0",
source="AzureStorageBlobs",
content_type="application/json",
content=json.dumps(content),
)

with self.assertRaises(ValueError) as e:
datum: Datum = Datum(value=sample_mbd, type="model_binding_data")
result: ContainerClient = BlobClientConverter.decode(
data=datum, trigger_metadata=None, pytype=ContainerClient
)
self.assertEqual(
e.exception.args[0],
"Storage account connection string NotARealConnectionString does not exist. "
"Please make sure that it is a defined App Setting.",
)

def test_none_input_populated(self):
content = {
"Connection": None,
"ContainerName": "test-blob",
"BlobName": "text.txt",
}

sample_mbd = MockMBD(
version="1.0",
source="AzureStorageBlobs",
content_type="application/json",
content=json.dumps(content),
)

with self.assertRaises(ValueError) as e:
datum: Datum = Datum(value=sample_mbd, type="model_binding_data")
result: ContainerClient = BlobClientConverter.decode(
data=datum, trigger_metadata=None, pytype=ContainerClient
)
self.assertEqual(
e.exception.args[0],
"Storage account connection string cannot be none. Please provide a connection string.",
)

def test_input_invalid_pytype(self):
content = {
"Connection": "AzureWebJobsStorage",
Expand Down Expand Up @@ -167,7 +216,7 @@ def test_container_client_invalid_creation(self):
dict_repr,
[
'{"direction": "MockBindingDirection.IN", '
'"dataType": null, "type": "blob", '
'"type": "blob", '
'"properties": '
'{"SupportsDeferredBinding": false}}'
],
Expand Down Expand Up @@ -202,7 +251,7 @@ def test_container_client_valid_creation(self):
dict_repr,
[
'{"direction": "MockBindingDirection.IN", '
'"dataType": null, "type": "blob", '
'"type": "blob", '
'"properties": '
'{"SupportsDeferredBinding": true}}'
],
Expand Down
53 changes: 51 additions & 2 deletions azurefunctions-extensions-bindings-blob/tests/test_ssd.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,55 @@ def test_input_populated(self):
self.assertIsNotNone(sdk_result)
self.assertIsInstance(sdk_result, SSDSdk)

def test_invalid_input_populated(self):
content = {
"Connection": "NotARealConnectionString",
"ContainerName": "test-blob",
"BlobName": "text.txt",
}

sample_mbd = MockMBD(
version="1.0",
source="AzureStorageBlobs",
content_type="application/json",
content=json.dumps(content),
)

with self.assertRaises(ValueError) as e:
datum: Datum = Datum(value=sample_mbd, type="model_binding_data")
result: StorageStreamDownloader = BlobClientConverter.decode(
data=datum, trigger_metadata=None, pytype=StorageStreamDownloader
)
self.assertEqual(
e.exception.args[0],
"Storage account connection string NotARealConnectionString does not exist. "
"Please make sure that it is a defined App Setting.",
)

def test_none_input_populated(self):
content = {
"Connection": None,
"ContainerName": "test-blob",
"BlobName": "text.txt",
}

sample_mbd = MockMBD(
version="1.0",
source="AzureStorageBlobs",
content_type="application/json",
content=json.dumps(content),
)

with self.assertRaises(ValueError) as e:
datum: Datum = Datum(value=sample_mbd, type="model_binding_data")
result: StorageStreamDownloader = BlobClientConverter.decode(
data=datum, trigger_metadata=None, pytype=StorageStreamDownloader
)
self.assertEqual(
e.exception.args[0],
"Storage account connection string cannot be none. Please provide a connection string.",
)

def test_input_invalid_pytype(self):
content = {
"Connection": "AzureWebJobsStorage",
Expand Down Expand Up @@ -172,7 +221,7 @@ def test_ssd_invalid_creation(self):
dict_repr,
[
'{"direction": "MockBindingDirection.IN", '
'"dataType": null, "type": "blob", '
'"type": "blob", '
'"properties": '
'{"SupportsDeferredBinding": false}}'
],
Expand Down Expand Up @@ -207,7 +256,7 @@ def test_ssd_valid_creation(self):
dict_repr,
[
'{"direction": "MockBindingDirection.IN", '
'"dataType": null, "type": "blob", '
'"type": "blob", '
'"properties": '
'{"SupportsDeferredBinding": true}}'
],
Expand Down
Loading