Skip to content

Commit

Permalink
Validate ip-address parameter + tests (#12312)
Browse files Browse the repository at this point in the history
  • Loading branch information
madsd authored Feb 28, 2020
1 parent 77fcc2b commit 2242523
Show file tree
Hide file tree
Showing 7 changed files with 1,650 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/azure-cli/azure/cli/command_modules/appservice/_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from ._completers import get_hostname_completion_list
from ._constants import FUNCTIONS_VERSIONS_FUNCTIONAPP, RUNTIME_TO_IMAGE_FUNCTIONAPP
from ._validators import (validate_timeout_value, validate_site_create, validate_asp_create,
validate_add_vnet, validate_front_end_scale_factor, validate_ase_create)
validate_add_vnet, validate_front_end_scale_factor, validate_ase_create, validate_ip_address)


AUTH_TYPES = {
Expand Down Expand Up @@ -556,7 +556,7 @@ def load_arguments(self, _):
c.argument('description', help='Description of the access restriction rule')
c.argument('action', arg_type=get_enum_type(ACCESS_RESTRICTION_ACTION_TYPES),
help="Allow or deny access")
c.argument('ip_address', help="IP address or CIDR range")
c.argument('ip_address', help="IP address or CIDR range", validator=validate_ip_address)
c.argument('vnet_name', help="vNet name")
c.argument('subnet', help="Subnet name (requires vNet name) or subnet resource id")
c.argument('ignore_missing_vnet_service_endpoint',
Expand All @@ -570,7 +570,7 @@ def load_arguments(self, _):
c.argument('name', arg_type=webapp_name_arg_type)
c.argument('rule_name', options_list=['--rule-name', '-r'],
help='Name of the access restriction to remove')
c.argument('ip_address', help="IP address or CIDR range")
c.argument('ip_address', help="IP address or CIDR range", validator=validate_ip_address)
c.argument('vnet_name', help="vNet name")
c.argument('subnet', help="Subnet name (requires vNet name) or subnet resource id")
c.argument('scm_site', help='True if access restriction should be removed from scm site',
Expand Down
20 changes: 20 additions & 0 deletions src/azure-cli/azure/cli/command_modules/appservice/_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,23 @@ def validate_asp_sku(cmd, namespace):
if res.get('properties').get('hostingEnvironment') is not None:
raise CLIError("Only pricing tier 'Isolated' is allowed in this app service plan. Use this link to "
"learn more: https://docs.microsoft.com/en-us/azure/app-service/overview-hosting-plans")


def validate_ip_address(namespace):
if namespace.ip_address is not None:
# IPv6
if ':' in namespace.ip_address:
if namespace.ip_address.count(':') > 1:
if '/' not in namespace.ip_address:
namespace.ip_address = namespace.ip_address + '/128'
return
return
# IPv4
elif '.' in namespace.ip_address:
if namespace.ip_address.count('.') == 3:
if '/' not in namespace.ip_address:
namespace.ip_address = namespace.ip_address + '/32'
return
return

raise CLIError('Invalid IP address')
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def add_webapp_access_restriction(
configs = get_site_configs(cmd, resource_group_name, name, slot)

if (ip_address and subnet) or (not ip_address and not subnet):
raise CLIError('Usage error: --subnet | --ip_address')
raise CLIError('Usage error: --subnet | --ip-address')

# get rules list
access_rules = configs.scm_ip_security_restrictions if scm_site else configs.ip_security_restrictions
Expand Down
Loading

0 comments on commit 2242523

Please sign in to comment.