Skip to content

Commit

Permalink
[Azure Storage] Honor host/port from config (#685)
Browse files Browse the repository at this point in the history
  • Loading branch information
rzvoncek authored Nov 22, 2023
1 parent dca04bc commit 3fd53d9
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 18 deletions.
15 changes: 6 additions & 9 deletions docs/azure_blobs_setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,7 @@ Create a new storage account or use an existing one which will be used to store
"key": "YOUR_KEY"
}
```
If you need to set a different host for Azure (for example the host for Azure Gov is `<storageAccount>.blob.core.usgovcloudapi.net`), please ADDITIONALLY set these two fields in the JSON file (the connection string can be found with the key):

```
"host": "YOUR_HOST"
"connection_string": "YOUR_CONNECTION_STRING"
```

Place this file on all Apache Cassandra™ nodes running medusa under `/etc/medusa/`and set the rigths appropriately so that onyl users running Medusa can read/modify it.
Place this file on all Apache Cassandra™ nodes running medusa under `/etc/medusa/`and set the rights appropriately so that only users running Medusa can read/modify it.

### Create a container

Expand All @@ -36,3 +28,8 @@ key_file = /etc/medusa/medusa-azure-credentials

Medusa should now be able to access the bucket and perform all required operations.

If you need to set a different host for Azure (for example the host for Azure Gov is `<storageAccount>.blob.core.usgovcloudapi.net`), please use the `host` parameter in the `[storage]` section of `/etc/medusa/medusa.ini`:

```
"host": "usgovcloudapi.net"
```
12 changes: 11 additions & 1 deletion medusa/storage/azure_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,25 @@ def __init__(self, config):
self.account_name = self.credentials.named_key.name
self.bucket_name = config.bucket_name

self.azure_blob_service_url = self._make_blob_service_url(self.account_name, config)

# disable chatty loggers
logging.getLogger('azure.core.pipeline.policies.http_logging_policy').setLevel(logging.WARNING)
logging.getLogger('chardet.universaldetector').setLevel(logging.WARNING)

super().__init__(config)

def _make_blob_service_url(self, account_name, config):
domain = 'windows.net' if config.host is None else config.host
if config.port is None:
url = f"https://{account_name}.blob.core.{domain}/"
else:
url = f"https://{account_name}.blob.core.{domain}:{config.port}/"
return url

def connect(self):
self.azure_blob_service = BlobServiceClient(
account_url=f"https://{self.account_name}.blob.core.windows.net/",
account_url=self.azure_blob_service_url,
credential=self.credentials
)
self.azure_container_client = self.azure_blob_service.get_container_client(self.bucket_name)
Expand Down
8 changes: 7 additions & 1 deletion tests/storage/abstract_storage_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@
from medusa.storage.abstract_storage import AbstractStorage


class S3StorageTest(unittest.TestCase):
class AttributeDict(dict):
__slots__ = ()
__getattr__ = dict.__getitem__
__setattr__ = dict.__setitem__


class AbstractStorageTest(unittest.TestCase):

def test_convert_human_friendly_size_to_bytes(self):
self.assertEqual(50, AbstractStorage._human_size_to_bytes('50B'))
Expand Down
79 changes: 79 additions & 0 deletions tests/storage/azure_storage_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import tempfile
import unittest

from medusa.storage.azure_storage import AzureStorage
from tests.storage.abstract_storage_test import AttributeDict


class AzureStorageTest(unittest.TestCase):

credentials_file_content = """
{
"storage_account": "medusa-unit-test",
"key": "randomString=="
}
"""

def test_make_connection_url(self):
with tempfile.NamedTemporaryFile() as credentials_file:
credentials_file.write(self.credentials_file_content.encode())
credentials_file.flush()
config = AttributeDict({
'region': 'region-from-config',
'storage_provider': 'azure_blobs',
'key_file': credentials_file.name,
'bucket_name': 'bucket-from-config',
'concurrent_transfers': '1',
'host': None,
'port': None,
})
azure_storage = AzureStorage(config)
self.assertEqual(
'https://medusa-unit-test.blob.core.windows.net/',
azure_storage.azure_blob_service_url
)

def test_make_connection_url_with_custom_host(self):
with tempfile.NamedTemporaryFile() as credentials_file:
credentials_file.write(self.credentials_file_content.encode())
credentials_file.flush()
config = AttributeDict({
'region': 'region-from-config',
'storage_provider': 'azure_blobs',
'key_file': credentials_file.name,
'bucket_name': 'bucket-from-config',
'concurrent_transfers': '1',
'host': 'custom.host.net',
'port': None,
})
azure_storage = AzureStorage(config)
self.assertEqual(
'https://medusa-unit-test.blob.core.custom.host.net/',
azure_storage.azure_blob_service_url
)

def test_make_connection_url_with_custom_host_port(self):
with tempfile.NamedTemporaryFile() as credentials_file:
credentials_file.write(self.credentials_file_content.encode())
credentials_file.flush()
config = AttributeDict({
'region': 'region-from-config',
'storage_provider': 'azure_blobs',
'key_file': credentials_file.name,
'bucket_name': 'bucket-from-config',
'concurrent_transfers': '1',
'host': 'custom.host.net',
'port': 123,
})
azure_storage = AzureStorage(config)
self.assertEqual(
'https://medusa-unit-test.blob.core.custom.host.net:123/',
azure_storage.azure_blob_service_url
)
2 changes: 1 addition & 1 deletion tests/storage/google_storage_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from medusa.storage.google_storage import _group_by_parent, _is_in_folder


class RestoreNodeTest(unittest.TestCase):
class GoogleStorageTest(unittest.TestCase):

def test_is_in_folder(self):
folder = Path('foo/bar')
Expand Down
7 changes: 1 addition & 6 deletions tests/storage/s3_storage_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,7 @@
from unittest.mock import patch, MagicMock

from medusa.storage.s3_base_storage import S3BaseStorage


class AttributeDict(dict):
__slots__ = ()
__getattr__ = dict.__getitem__
__setattr__ = dict.__setitem__
from tests.storage.abstract_storage_test import AttributeDict


class S3StorageTest(unittest.TestCase):
Expand Down

0 comments on commit 3fd53d9

Please sign in to comment.