Skip to content

Added support for Service Principal authentication #13

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
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
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"python.linting.pylintEnabled": true
}
6 changes: 5 additions & 1 deletion src/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 13 additions & 4 deletions src/python_quickstart_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down