diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..72513b9 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "python.linting.pylintEnabled": true +} \ No newline at end of file diff --git a/src/config.py b/src/config.py index bf3350e..091e66f 100644 --- a/src/config.py +++ b/src/config.py @@ -15,9 +15,13 @@ # import "config.py" in "python_quickstart_client.py " +_AUTH_MODE = 'Key' # VALUE SHOULD BE 'Key' OR 'ServicePrincipal' _BATCH_ACCOUNT_NAME ='' # Your batch account name -_BATCH_ACCOUNT_KEY = '' # Your batch account key +_BATCH_ACCOUNT_KEY = '' # Your batch account key, only used for 'Key' _AUTH_MODE _BATCH_ACCOUNT_URL = '' # Your batch account URL +_AD_CLIENT_ID = '' # Your Active Directory CLIENT_ID, only used for 'ServicePrincipal' _AUTH_MODE +_AD_TENANT = '' # Your Active Directory TENANT, only used for 'ServicePrincipal' _AUTH_MODE +_AD_SECRET = '' # Your Active Directory SECRET, only used for 'ServicePrincipal' _AUTH_MODE _STORAGE_ACCOUNT_NAME = '' # Your storage account name _STORAGE_ACCOUNT_KEY = '' # Your storage account key _POOL_ID = 'PythonQuickstartPool' # Your Pool ID diff --git a/src/python_quickstart_client.py b/src/python_quickstart_client.py index dd8197b..ee23b0f 100644 --- a/src/python_quickstart_client.py +++ b/src/python_quickstart_client.py @@ -10,6 +10,7 @@ except NameError: pass +from azure.common.credentials import ServicePrincipalCredentials import azure.storage.blob as azureblob import azure.batch.batch_service_client as batch import azure.batch.batch_auth as batch_auth @@ -330,15 +331,23 @@ def _read_stream_as_string(stream, encoding): # Create a Batch service client. We'll now be interacting with the Batch # service in addition to Storage - credentials = batch_auth.SharedKeyCredentials(config._BATCH_ACCOUNT_NAME, - config._BATCH_ACCOUNT_KEY) + print("Using {0} based authentication for Batch service".format(config._AUTH_MODE)) + + if config._AUTH_MODE == 'Key': + credentials = batch_auth.SharedKeyCredentials(config._BATCH_ACCOUNT_NAME, + config._BATCH_ACCOUNT_KEY) + elif config._AUTH_MODE == 'ServicePrincipal': + credentials = ServicePrincipalCredentials( + client_id=config._AD_CLIENT_ID, + secret=config._AD_SECRET, + tenant=config._AD_TENANT, + resource="https://batch.core.windows.net/" + ) batch_client = batch.BatchServiceClient( credentials, base_url=config._BATCH_ACCOUNT_URL) - - try: # Create the pool that will contain the compute nodes that will execute the # tasks.