Skip to content
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

feat(azure-storage): allow use of AzureDefaultCredential class for uploading and downloading to azure #250

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ docker run --user $(id -u):$(id -g) --rm --name grafana-backup-tool \
-e AZURE_STORAGE_CONTAINER_NAME="azure-storage-container-name" \
-e AZURE_STORAGE_CONNECTION_STRING="azure-storage-connection-string"
```
Or, using credentials configured through DefaultAzureCredential, see [here](https://learn.microsoft.com/en-us/python/api/azure-identity/azure.identity.defaultazurecredential?view=azure-python)
```
-e AZURE_STORAGE_CONTAINER_NAME="azure-storage-container-name" \
-e AZURE_STORAGE_ACCOUNT_URL="https://my-storage-account.blob.core.windows.net/" \
```

***GCS Example:*** Set GCS configurations in `-e` or `grafanaSettings.json`([example](https://github.com/ysde/grafana-backup-tool/blob/master/examples/grafana-backup.example.json))
```
Expand Down
10 changes: 8 additions & 2 deletions grafana_backup/azure_storage_download.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from azure.storage.blob import BlobServiceClient
from azure.identity import DefaultAzureCredential
import io


Expand All @@ -7,9 +8,14 @@ def main(args, settings):

azure_storage_container_name = settings.get('AZURE_STORAGE_CONTAINER_NAME')
azure_storage_connection_string = settings.get('AZURE_STORAGE_CONNECTION_STRING')

try:
blob_service_client = BlobServiceClient.from_connection_string(azure_storage_connection_string)
if not azure_storage_connection_string:
print("Azure Storage connection string is not set, using DefaultAzureCredential to authenticate")
blob_service_client = BlobServiceClient(account_url=settings.get('AZURE_STORAGE_ACCOUNT_URL'), credential=DefaultAzureCredential())
else:
blob_service_client = BlobServiceClient.from_connection_string(azure_storage_connection_string)

container_client = blob_service_client.get_blob_client(container=azure_storage_container_name, blob=arg_archive_file)
azure_storage_bytes = container_client.download_blob().readall()
azure_storage_data = io.BytesIO(azure_storage_bytes)
Expand Down
8 changes: 7 additions & 1 deletion grafana_backup/azure_storage_upload.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from azure.storage.blob import BlobServiceClient
from azure.identity import DefaultAzureCredential


def main(args, settings):
Expand All @@ -12,7 +13,12 @@ def main(args, settings):
archive_file = '{0}/{1}'.format(backup_dir, azure_file_name)

try:
blob_service_client = BlobServiceClient.from_connection_string(azure_storage_connection_string)
if not azure_storage_connection_string:
print("Azure Storage connection string is not set, using DefaultAzureCredential to authenticate")
blob_service_client = BlobServiceClient(account_url=settings.get('AZURE_STORAGE_ACCOUNT_URL'), credential=DefaultAzureCredential())
else:
blob_service_client = BlobServiceClient.from_connection_string(azure_storage_connection_string)

container_client = blob_service_client.get_blob_client(container=azure_storage_container_name, blob=azure_file_name)
with open(archive_file, 'rb') as data:
container_client.upload_blob(data)
Expand Down
3 changes: 3 additions & 0 deletions grafana_backup/grafanaSettings.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def main(config_path):
# Cloud storage settings - Azure
azure_storage_container_name = config.get('azure', {}).get('container_name', '')
azure_storage_connection_string = config.get('azure', {}).get('connection_string', '')
azure_storage_account_url = config.get('azure', {}).get('account_url', '')
# Cloud storage settings - GCP
gcp_config = config.get('gcp', {})
gcs_bucket_name = gcp_config.get('gcs_bucket_name', '')
Expand Down Expand Up @@ -70,6 +71,7 @@ def main(config_path):

AZURE_STORAGE_CONTAINER_NAME = os.getenv('AZURE_STORAGE_CONTAINER_NAME', azure_storage_container_name)
AZURE_STORAGE_CONNECTION_STRING = os.getenv('AZURE_STORAGE_CONNECTION_STRING', azure_storage_connection_string)
AZURE_STORAGE_ACCOUNT_URL = os.getenv('AZURE_STORAGE_ACCOUNT_URL', azure_storage_account_url)

GCS_BUCKET_NAME = os.getenv('GCS_BUCKET_NAME', gcs_bucket_name)
if not os.getenv('GOOGLE_APPLICATION_CREDENTIALS') and google_application_credentials:
Expand Down Expand Up @@ -176,6 +178,7 @@ def main(config_path):
config_dict['AWS_ENDPOINT_URL'] = AWS_ENDPOINT_URL
config_dict['AZURE_STORAGE_CONTAINER_NAME'] = AZURE_STORAGE_CONTAINER_NAME
config_dict['AZURE_STORAGE_CONNECTION_STRING'] = AZURE_STORAGE_CONNECTION_STRING
config_dict['AZURE_STORAGE_ACCOUNT_URL'] = AZURE_STORAGE_ACCOUNT_URL
config_dict['GCS_BUCKET_NAME'] = GCS_BUCKET_NAME
config_dict['INFLUXDB_MEASUREMENT'] = INFLUXDB_MEASUREMENT
config_dict['INFLUXDB_HOST'] = INFLUXDB_HOST
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
'azure-storage-blob',
'google-cloud-storage',
'influxdb',
'packaging'
'packaging',
'azure-identity',
]

setup(
Expand Down