Skip to content
Merged
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
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ python:
- "3.5"
install:
- pip install azure==2.0.0a1
- pip install mock==1.3.0
script:
- export PYTHONPATH=$PATHONPATH:./src
- python -m unittest discover -s src/azure/cli/tests
10 changes: 9 additions & 1 deletion azure-cli.pyproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,23 @@
<Compile Include="azure\cli\commands\account.py" />
<Compile Include="azure\cli\commands\login.py" />
<Compile Include="azure\cli\commands\logout.py" />
<Compile Include="azure\cli\commands\resourcegroup.py" />
<Compile Include="azure\cli\commands\storage.py" />
<Compile Include="azure\cli\commands\__init__.py" />
<Compile Include="azure\cli\main.py" />
<Compile Include="azure\cli\tests\test_argparse.py">
<SubType>Code</SubType>
</Compile>
<Compile Include="azure\cli\tests\test_connection_verify.py">
<SubType>Code</SubType>
</Compile>
<Compile Include="azure\cli\tests\test_profile.py" />
<Compile Include="azure\cli\_argparse.py" />
<Compile Include="azure\cli\commands\_command_creation.py">
<SubType>Code</SubType>
</Compile>
<Compile Include="azure\cli\_debug.py">
<SubType>Code</SubType>
</Compile>
<Compile Include="azure\cli\_logging.py" />
<Compile Include="azure\cli\_profile.py" />
<Compile Include="azure\cli\_session.py">
Expand Down
12 changes: 12 additions & 0 deletions src/azure/cli/_debug.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import os
from ._logging import logger

DISABLE_VERIFY_VARIABLE_NAME = "AZURE_CLI_DISABLE_CONNECTION_VERIFICATION"

def allow_debug_connection(client):
if should_disable_connection_verify():
logger.warn("Connection verification disabled by environment variable %s", DISABLE_VERIFY_VARIABLE_NAME)
client.config.connection.verify = False

def should_disable_connection_verify():
return bool(os.environ.get(DISABLE_VERIFY_VARIABLE_NAME))
10 changes: 10 additions & 0 deletions src/azure/cli/commands/_command_creation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from .._profile import Profile
import azure.cli._debug as _debug
import azure.cli as cli

def get_service_client(client_type, config_type):
profile = Profile()
client = client_type(config_type(*profile.get_login_credentials()))
_debug.allow_debug_connection(client)
client.config.add_user_agent("AZURECLI_{}".format(cli.__version__))
return client
3 changes: 2 additions & 1 deletion src/azure/cli/commands/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from .._profile import Profile
from ..commands import command, description, option
from .._debug import should_disable_connection_verify

CLIENT_ID = '04b07795-8ddb-461a-bbee-02f9e1bf7b46'

Expand All @@ -21,7 +22,7 @@ def login(args, unexpected):
import getpass
password = getpass.getpass(_('Password: '))

credentials = UserPassCredentials(username, password, client_id=CLIENT_ID)
credentials = UserPassCredentials(username, password, client_id=CLIENT_ID, verify=not should_disable_connection_verify())
client = SubscriptionClient(SubscriptionClientConfiguration(credentials))
subscriptions = client.subscriptions.list()

Expand Down
9 changes: 3 additions & 6 deletions src/azure/cli/commands/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from ..main import SESSION
from ..commands import command, description, option
from .._profile import Profile
from ._command_creation import get_service_client

@command('storage account list')
@description(_('List storage accounts'))
Expand All @@ -13,10 +13,7 @@ def list_accounts(args, unexpected):
from azure.mgmt.storage.models import StorageAccount
from msrestazure.azure_active_directory import UserPassCredentials

profile = Profile()
smc = StorageManagementClient(StorageManagementClientConfiguration(
*profile.get_login_credentials()
))
smc = get_service_client(StorageManagementClient, StorageManagementClientConfiguration)

group = args.get('resource-group')
if group:
Expand All @@ -32,7 +29,7 @@ def list_accounts(args, unexpected):
def checkname(args, unexpected):
from azure.mgmt.storage import StorageManagementClient, StorageManagementClientConfiguration

smc = StorageManagementClient(StorageManagementClientConfiguration())
smc = get_service_client(StorageManagementClient, StorageManagementClientConfiguration)
logger.warn(smc.storage_accounts.check_name_availability(args.account_name))


39 changes: 39 additions & 0 deletions src/azure/cli/tests/test_connection_verify.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import os
import unittest
try:
from unittest.mock import MagicMock
except ImportError:
from mock import MagicMock

from azure.cli._argparse import ArgumentParser, IncorrectUsageError
from azure.cli._logging import logger
import logging
import azure.cli._debug as _debug


class Test_argparse(unittest.TestCase):
@classmethod
def setUpClass(cls):
# Ensure initialization has occurred correctly
import azure.cli.main
logging.basicConfig(level=logging.DEBUG)

@classmethod
def tearDownClass(cls):
logging.shutdown()

def test_verify_client_connection(self):
os.environ[_debug.DISABLE_VERIFY_VARIABLE_NAME] = ""
self.assertFalse(_debug.should_disable_connection_verify())

os.environ[_debug.DISABLE_VERIFY_VARIABLE_NAME] = "1"
self.assertTrue(_debug.should_disable_connection_verify())

clientMock = MagicMock()
clientMock.config.connection.verify = True
_debug.allow_debug_connection(clientMock)
self.assertFalse(clientMock.config.connection.verify)


if __name__ == '__main__':
unittest.main()