Skip to content
This repository has been archived by the owner on Apr 10, 2024. It is now read-only.

Commit

Permalink
finalize all
Browse files Browse the repository at this point in the history
  • Loading branch information
yugangw-msft committed Oct 28, 2018
1 parent b0c48f8 commit dda7a7f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
6 changes: 5 additions & 1 deletion msrestazure/azure_local_creds_prober.py
Expand Up @@ -169,6 +169,7 @@ def __init__(self, cli_path, subscription_id=None):
self.cli_path = cli_path
self.subscription_id = subscription_id
self.expires_on = None
self.resource = None

def set_token(self):
from dateutil import parser
Expand All @@ -187,12 +188,14 @@ def _invoke_cli_token_command(self):
args = [self.cli_path, 'account', 'get-access-token']
if self.subscription_id:
args.extend(['--subscription', self.subscription_id])
if self.resource:
args.extend(['--resource', self.resource])
process = Popen(args, stdout=PIPE, stderr=PIPE)
stdout, stderr = process.communicate()
process.wait()
if stderr:
raise ValueError('Retrieving acccess token failed: ' + stderr)
return json.loads(stdout)
return json.loads(stdout.decode())

def signed_session(self, session=None):
self.set_token()
Expand All @@ -209,6 +212,7 @@ def _get_creds_through_local_probing(**kwargs):
for prober in probers:
creds = prober.probe()
if creds:
creds.resource = resource
return creds, prober
raise ValueError('No credential was detected from the local machine')

Expand Down
30 changes: 14 additions & 16 deletions tests/test_local_creds_prober.py
Expand Up @@ -25,22 +25,20 @@
#--------------------------------------------------------------------------


class TestCredsLocalProbing(unittest.TestCase):
from azure.mgmt.storage import StorageManagementClient
from azure.multiapi.storage.v2018_03_28.blob.blockblobservice import BlockBlobService
from msrestazure.azure_local_creds_prober import (get_client_through_local_creds_probing,
get_creds_through_local_probing)

@unittest.skip("demo sample client code")
def test_create_client_from_probed_creds(self):
# test management plane
client = get_client_through_local_creds_probing(StorageManagementClient)
accounts = list(client.storage_accounts.list())
print('Found {} accounts'.format(len(accounts)))

from azure.mgmt.storage import StorageManagementClient
from azure.multiapi.storage.v2018_03_28.blob.blockblobservice import BlockBlobService
from msrestazure.azure_local_creds_prober import (get_client_through_local_creds_probing,
get_creds_through_local_probing)
# test storage data plane
creds = get_creds_through_local_probing(resource="https://storage.azure.com/")
test_storage_account, container, blob, file_to_upload = 'teststor12345678', 'temp', 'hey.exe', r'c:\temp\hey.exe'
storage_data_client = BlockBlobService(token_credential=creds, account_name=test_storage_account)
storage_data_client.create_blob_from_path(container, blob, file_to_upload)
print('uploaded')

# look for storage account
client = get_client_through_local_creds_probing(StorageManagementClient)
accounts = list(client.storage_accounts.list())
print('Found {} accounts'.format(len(accounts)))

# look for containers in the 1st storage account through storage preview service
creds = get_creds_through_local_probing(resource="https://storage.azure.com/")
storage_data_client2 = BlockBlobService(token_credential=creds, account_name=accounts[0].name)
print(len(list(storage_data_client2.list_containers())))

0 comments on commit dda7a7f

Please sign in to comment.