Skip to content

Commit 8c8da57

Browse files
Merge branch 'dev' into agent-assist/azure-azure-cli-issue-34063-338c19fac314
2 parents a38b606 + ab6f154 commit 8c8da57

2 files changed

Lines changed: 62 additions & 3 deletions

File tree

‎src/azure-cli/azure/cli/command_modules/storage/_validators.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ def validate_client_parameters(cmd, namespace):
132132
if not n.account_name:
133133
if hasattr(n, 'account_url') and not n.account_url:
134134
n.account_name = get_config_value(cmd, 'storage', 'account', None)
135-
n.account_url = get_config_value(cmd, 'storage', 'account_url', None)
135+
n.account_url = (get_config_value(cmd, 'storage', 'service_endpoint', None) or
136+
get_config_value(cmd, 'storage', 'account_url', None))
136137
else:
137138
n.account_name = get_config_value(cmd, 'storage', 'account', None)
138139
if auth_mode == 'login':
@@ -168,7 +169,8 @@ def validate_client_parameters(cmd, namespace):
168169
if not n.account_name:
169170
if hasattr(n, 'account_url') and not n.account_url:
170171
n.account_name = get_config_value(cmd, 'storage', 'account', None)
171-
n.account_url = get_config_value(cmd, 'storage', 'account_url', None)
172+
n.account_url = (get_config_value(cmd, 'storage', 'service_endpoint', None) or
173+
get_config_value(cmd, 'storage', 'account_url', None))
172174
else:
173175
n.account_name = get_config_value(cmd, 'storage', 'account', None)
174176
if not n.account_key and not n.sas_token:

‎src/azure-cli/azure/cli/command_modules/storage/tests/latest/test_storage_validators.py‎

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
validate_encryption_source, validate_source_uri,
2222
validate_source_url,
2323
validate_encryption_services, as_user_validator,
24-
get_not_none_validator, validate_upload_blob)
24+
get_not_none_validator, validate_upload_blob,
25+
validate_client_parameters)
2526

2627

2728
class MockCLI(CLI):
@@ -254,6 +255,62 @@ def test_validate_upload_blob(self):
254255
validate_upload_blob(Namespace(file_path=None, data=None))
255256

256257

258+
class TestClientParametersValidator(unittest.TestCase):
259+
def setUp(self):
260+
self.cli = MockCLI()
261+
self.local_url = 'http://localhost:10000/devstoreaccount1'
262+
self.legacy_url = 'http://legacy:10000/devstoreaccount1'
263+
264+
def test_service_endpoint_env_var_used(self):
265+
# AZURE_STORAGE_SERVICE_ENDPOINT is the documented env var, should set account_url
266+
ns = self._create_namespace(auth_mode='key')
267+
self._validate(ns, AZURE_STORAGE_SERVICE_ENDPOINT=self.local_url)
268+
self.assertEqual(ns.account_url, self.local_url)
269+
270+
def test_account_url_env_var_still_used(self):
271+
# AZURE_STORAGE_ACCOUNT_URL is the legacy env var, should keep working
272+
ns = self._create_namespace(auth_mode='key')
273+
self._validate(ns, AZURE_STORAGE_ACCOUNT_URL=self.legacy_url)
274+
self.assertEqual(ns.account_url, self.legacy_url)
275+
276+
def test_service_endpoint_env_var_wins_over_account_url(self):
277+
# both env vars set, documented one takes precedence
278+
ns = self._create_namespace(auth_mode='key')
279+
self._validate(ns, AZURE_STORAGE_SERVICE_ENDPOINT=self.local_url,
280+
AZURE_STORAGE_ACCOUNT_URL=self.legacy_url)
281+
self.assertEqual(ns.account_url, self.local_url)
282+
283+
def test_no_endpoint_env_var(self):
284+
# no endpoint env var, account_url stays empty so client factory falls back to public cloud
285+
ns = self._create_namespace(auth_mode='key')
286+
self._validate(ns)
287+
self.assertIsNone(ns.account_url)
288+
289+
def test_endpoint_argument_wins_over_env_var(self):
290+
# --blob-endpoint given explicitly, env var is ignored
291+
ns = self._create_namespace(auth_mode='key', account_url='https://flag.example.com')
292+
self._validate(ns, AZURE_STORAGE_SERVICE_ENDPOINT=self.local_url)
293+
self.assertEqual(ns.account_url, 'https://flag.example.com')
294+
295+
def test_service_endpoint_env_var_used_without_auth_mode(self):
296+
# commands without --auth-mode go through the second lookup block in the validator
297+
ns = self._create_namespace()
298+
self._validate(ns, AZURE_STORAGE_SERVICE_ENDPOINT=self.local_url)
299+
self.assertEqual(ns.account_url, self.local_url)
300+
301+
def _create_namespace(self, **kwargs):
302+
ns = Namespace(account_name=None, account_url=None, account_key=None, sas_token=None,
303+
connection_string=None)
304+
for key in kwargs:
305+
setattr(ns, key, kwargs[key])
306+
return ns
307+
308+
def _validate(self, ns, **env):
309+
env.update(AZURE_STORAGE_ACCOUNT='devstoreaccount1', AZURE_STORAGE_KEY='fake_key')
310+
with mock.patch.dict('os.environ', env, clear=True):
311+
validate_client_parameters(MockCmd(self.cli), ns)
312+
313+
257314
class TestEncryptionValidators(unittest.TestCase):
258315
def setUp(self):
259316
self.cli = MockCLI()

0 commit comments

Comments
 (0)