Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[KeyVault] az keyvault create: support --network-acls, --network-acls-ips and --network-acls-vnets for specifying network rules while creating vault #12716

Merged
merged 10 commits into from
Mar 25, 2020

Conversation

bim-msft
Copy link
Contributor

@bim-msft bim-msft commented Mar 23, 2020

Support for AB#1150

Usage:

Use JSON string directly, for the vnet field, {vnet_name}/{subnet_name} and {subnet_id} are both supported:

az keyvault create --network-acls "{
     \"ip\":  [\"1.2.3.4\", \"2.3.4.0/24\"], 
     \"vnet\": [
           \"vnet_name_1/subnet_name1\", 
           \"/subscriptions/000000-0000-0000/resourceGroups/MyResourceGroup/providers/Microsoft.Network/virtualNetworks/vnet2/subnets/subnet2\"
     ]
}"

Use JSON file:

az keyvault create --network-acls network-acls-example.json

Use flattened parameters (IP rule):

az keyvault create --network-acls-ips 1.2.3.4 2.3.4.0/24

Virtual network rule:

az keyvault create --network-acls-vnets vnet_name_1/subnet_name1 /subscriptions/000000-0000-0000/resourceGroups/MyResourceGroup/providers/Microsoft.Network/virtualNetworks/vnet2/subnets/subnet2

Also, the three parameters --network-acls, --network-acls-ips and --network-acls-vnets are all optional and they can be used together:

az keyvault create --network-acls ... --network-acls-ips ... --network-acls-vnets ...

This checklist is used to make sure that common guidelines for a pull request are followed.

@bim-msft bim-msft requested a review from fengzhou-msft as a code owner March 23, 2020 12:58
@bim-msft bim-msft requested review from yungezz, qwordy and arrownj March 23, 2020 12:58
network_acls_json = json.load(f)

try:
network_acls_json = json.loads(json.dumps(network_acls_json))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why load again?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why load again?

@yungezz Good point, actually I added this sentence in order to make sure the input is a valid JSON. But later I modified the code that makes this part useless... I will remove this part.

for vnet_rule in network_acls_json.get('vnet', []):
items = vnet_rule.split('/')
if len(items) != 2:
raise CLIError('Invalid VNet rule: {}. Format: {{vnet}}/{{subnet}}'.format(vnet_rule))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can user input resource id directly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can user input resource id directly?

@yungezz It should be supported, I will update the code.

@yonzhan
Copy link
Collaborator

yonzhan commented Mar 23, 2020

keyvault

@bim-msft bim-msft force-pushed the bim_kv_networkrule branch from 2ecfd43 to f00c115 Compare March 24, 2020 08:44
Copy link
Member

@yungezz yungezz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, one comment left

az keyvault create --location westus2 --name MyKeyVault --resource-group MyResourceGroup --network-acls network-acls-example.json
- name: Create a key vault with network ACLs specified (use --network-acls, --network-acls-ips and --network-acls-vnets at the same time).
text: |
az keyvault create --location westus2 --name MyKeyVault --resource-group MyResourceGroup --network-acls network-acls-example.json --network-acls-ips 3.4.5.0/24 4.5.6.0/24 --network-acls-vnets vnet1/subnet1 vnet2/subnet2 /subscriptions/000000-0000-0000/resourceGroups/MyResourceGroup/providers/Microsoft.Network/virtualNetworks/vnet3/subnets/subnet3
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks @bim-msft for quick change. Should we fail validation when json and flatten params from user at same time?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks @bim-msft for quick change. Should we fail validation when json and flatten params from user at same time?

@yungezz I think it's safe to pass validation for this case which all three params are specified at the same time. As far as I can see, there is one scenario may benefits from this: user has one basic network rules json file, and this file is always applied to a new created vault, also there are different additional rules for each new vault.

Copy link
Member

@yungezz yungezz Mar 25, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:) I would think user would like to put all configure rules in one place either one json file or params value. anyway, the validation could be added later after get some feedback

@bim-msft bim-msft changed the title [KeyVault] az keyvault create: support --network-acls for specifying network rules while creating vault [KeyVault] az keyvault create: support --network-acls, --network-acls-ips and --network-acls-vnets for specifying network rules while creating vault Mar 25, 2020

- name: Create a key vault with network ACLs specified (use --network-acls to specify IP and VNet rules by using a JSON file).
text: |
az keyvault create --location westus2 --name MyKeyVault --resource-group MyResourceGroup --network-acls network-acls-example.json
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No @ prefix for file?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No @ prefix for file?

@fengzhou-msft Is @ a convention? I think in this case users don't need to specify the marker explicitly, the program can detect whether it's a JSON string or a JSON filename.

Copy link
Member

@fengzhou-msft fengzhou-msft Mar 25, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems json str or @file.json is a convention, and type=validate_file_or_dict can be used. @Juliehzl @myronfanqiu any guideline for this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested with validate_file_or_dict, it can support json string and file path with or without @ prefix.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fengzhou-msft That's good, I will use type=validate_file_or_dict to refine the code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fengzhou-msft For the help messages and examples, I decided not to add @ as a prefix since this is not mandatory and may cause confusion.

@bim-msft bim-msft requested a review from fengzhou-msft March 25, 2020 09:45
@bim-msft bim-msft merged commit bfb3067 into Azure:dev Mar 25, 2020
@bim-msft bim-msft deleted the bim_kv_networkrule branch March 25, 2020 10:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants