Skip to content

Commit 83c93d7

Browse files
authored
[Network] Fix #33520: az network vpn-connection create: --shared-key is optional with certificate-based authentication (#33523)
1 parent 7e5a7b6 commit 83c93d7

2 files changed

Lines changed: 50 additions & 1 deletion

File tree

src/azure-cli/azure/cli/command_modules/network/_validators.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,8 @@ def _validate_name_or_id(value, resource_type):
668668

669669
_normalize_shared_key_fields(namespace)
670670

671-
if (namespace.local_gateway2 or namespace.vnet_gateway2) and not namespace.shared_key:
671+
has_gateway = any([namespace.local_gateway2, namespace.vnet_gateway2])
672+
if has_gateway and not (namespace.shared_key or auth == 'certificate'):
672673
raise CLIError('--shared-key is required for VNET-to-VNET or Site-to-Site connections.')
673674

674675
if namespace.express_route_circuit2 and namespace.shared_key:

src/azure-cli/azure/cli/command_modules/network/tests/latest/test_network_unit_tests.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,5 +77,53 @@ def test_network_upsert(self):
7777
self.assertEqual(result[1].value, 'noodle')
7878

7979

80+
class TestVpnConnectionCertAuthNoSharedKey(unittest.TestCase):
81+
"""Unit test to verify that --shared-key is not required when --auth-type Certificate is used."""
82+
83+
def _build_namespace(self, auth_type=None, shared_key=None):
84+
namespace = mock.MagicMock()
85+
namespace.resource_group_name = 'test-rg'
86+
namespace.vnet_gateway1 = ('/subscriptions/00000000-0000-0000-0000-000000000000/'
87+
'resourceGroups/test-rg/providers/Microsoft.Network/'
88+
'virtualNetworkGateways/gw1')
89+
namespace.local_gateway2 = ('/subscriptions/00000000-0000-0000-0000-000000000000/'
90+
'resourceGroups/test-rg/providers/Microsoft.Network/'
91+
'localNetworkGateways/lgw2')
92+
namespace.vnet_gateway2 = None
93+
namespace.express_route_circuit2 = None
94+
namespace.shared_key = shared_key
95+
namespace.shared_key_keyvault_id = None
96+
namespace.auth_type = auth_type
97+
namespace.tags = None
98+
namespace.location = 'eastus'
99+
return namespace
100+
101+
def test_cert_auth_without_shared_key_should_not_raise(self):
102+
"""--auth-type Certificate should not require --shared-key."""
103+
from azure.cli.command_modules.network._validators import process_vpn_connection_create_namespace
104+
105+
cmd = mock.MagicMock()
106+
namespace = self._build_namespace(auth_type='Certificate', shared_key=None)
107+
108+
try:
109+
process_vpn_connection_create_namespace(cmd, namespace)
110+
except CLIError as e:
111+
if '--shared-key is required' in str(e):
112+
self.fail(
113+
'Raised CLIError for missing --shared-key even though '
114+
'--auth-type Certificate was specified.')
115+
116+
def test_no_shared_key_without_cert_auth_should_raise(self):
117+
"""without --auth-type Certificate, missing --shared-key must raise CLIError."""
118+
from azure.cli.command_modules.network._validators import process_vpn_connection_create_namespace
119+
120+
cmd = mock.MagicMock()
121+
namespace = self._build_namespace(auth_type=None, shared_key=None)
122+
123+
with self.assertRaises(CLIError) as ctx:
124+
process_vpn_connection_create_namespace(cmd, namespace)
125+
self.assertIn('--shared-key is required', str(ctx.exception))
126+
127+
80128
if __name__ == '__main__':
81129
unittest.main()

0 commit comments

Comments
 (0)