Skip to content

Commit 917a4e2

Browse files
committed
use standard pip and add nsg rule
1 parent e71a202 commit 917a4e2

File tree

6 files changed

+156
-9
lines changed

6 files changed

+156
-9
lines changed

lisa/sut_orchestrator/azure/arm_template.bicep

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ param use_ipv6 bool = false
5555
@description('whether to enable network outbound access')
5656
param enable_vm_nat bool
5757

58+
@description('The source IP address prefixes allowed in NSG')
59+
param source_address_prefixes array
60+
5861
var vnet_id = virtual_network_name_resource.id
5962
var node_count = length(nodes)
6063
var availability_set_name_value = 'lisa-availabilitySet'
@@ -253,9 +256,65 @@ resource virtual_network_name_resource 'Microsoft.Network/virtualNetworks@2024-0
253256
use_ipv6 ? ['2001:db8:${j}::/64'] : []
254257
)
255258
defaultOutboundAccess: enable_vm_nat
259+
networkSecurityGroup: {
260+
id: resourceId('Microsoft.Network/networkSecurityGroups', '${toLower(virtual_network_name)}-nsg')
261+
}
256262
}
257263
}]
258264
}
265+
dependsOn: [
266+
nsg
267+
]
268+
}
269+
270+
resource nsg 'Microsoft.Network/networkSecurityGroups@2024-05-01' = {
271+
name: '${toLower(virtual_network_name)}-nsg'
272+
location: location
273+
properties: {
274+
securityRules: [
275+
{
276+
name: 'LISASSH'
277+
properties: {
278+
priority: 100
279+
direction: 'Inbound'
280+
access: 'Allow'
281+
protocol: 'Tcp'
282+
sourcePortRange: '*'
283+
destinationPortRange: '22'
284+
sourceAddressPrefixes: source_address_prefixes
285+
destinationAddressPrefix: '*'
286+
}
287+
}
288+
{
289+
name: 'LISAKVMSSH'
290+
properties: {
291+
description: 'Allows nested VM SSH traffic'
292+
protocol: 'Tcp'
293+
sourcePortRange: '*'
294+
destinationPortRange: '60020-60030'
295+
destinationAddressPrefix: '*'
296+
sourceAddressPrefixes: source_address_prefixes
297+
access: 'Allow'
298+
priority: 206
299+
direction: 'Inbound'
300+
}
301+
}
302+
{
303+
name: 'LISALIBVIRTSSH'
304+
properties: {
305+
description: 'Allows SSH traffic to Libvirt Platform Guests'
306+
protocol: 'Tcp'
307+
sourcePortRange: '*'
308+
destinationPortRange: '49152-49352'
309+
destinationAddressPrefix: '*'
310+
sourceAddressPrefixes: source_address_prefixes
311+
access: 'Allow'
312+
priority: 208
313+
direction: 'Inbound'
314+
}
315+
}
316+
]
317+
}
259318
}
260319

261320
resource availability_set 'Microsoft.Compute/availabilitySets@2019-07-01' = if (use_availability_set) {
@@ -273,11 +332,11 @@ resource nodes_public_ip 'Microsoft.Network/publicIPAddresses@2020-05-01' = [for
273332
tags: tags
274333
name: '${nodes[i].name}-public-ip'
275334
properties: {
276-
publicIPAllocationMethod: ((is_ultradisk || use_availability_zones || use_ipv6) ? 'Static' : 'Dynamic')
335+
publicIPAllocationMethod: 'Static'
277336
ipTags: (empty(ip_tags) ? null : ip_tags)
278337
}
279338
sku: {
280-
name: ((is_ultradisk || use_availability_zones || use_ipv6) ? 'Standard' : 'Basic')
339+
name: 'Standard'
281340
}
282341
zones: (use_availability_zones ? availability_zones : null)
283342
}]

lisa/sut_orchestrator/azure/autogen_arm_template.json

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"_generator": {
77
"name": "bicep",
88
"version": "0.33.93.31351",
9-
"templateHash": "15877188685327638829"
9+
"templateHash": "14741729812904551283"
1010
}
1111
},
1212
"functions": [
@@ -530,6 +530,12 @@
530530
"metadata": {
531531
"description": "whether to enable network outbound access"
532532
}
533+
},
534+
"source_address_prefixes": {
535+
"type": "array",
536+
"metadata": {
537+
"description": "The source IP address prefixes allowed in NSG"
538+
}
533539
}
534540
},
535541
"variables": {
@@ -574,14 +580,71 @@
574580
"name": "[format('{0}{1}', parameters('subnet_prefix'), range(0, parameters('subnet_count'))[copyIndex('subnets')])]",
575581
"properties": {
576582
"addressPrefixes": "[concat(createArray(format('10.0.{0}.0/24', range(0, parameters('subnet_count'))[copyIndex('subnets')])), if(parameters('use_ipv6'), createArray(format('2001:db8:{0}::/64', range(0, parameters('subnet_count'))[copyIndex('subnets')])), createArray()))]",
577-
"defaultOutboundAccess": "[parameters('enable_vm_nat')]"
583+
"defaultOutboundAccess": "[parameters('enable_vm_nat')]",
584+
"networkSecurityGroup": {
585+
"id": "[resourceId('Microsoft.Network/networkSecurityGroups', format('{0}-nsg', toLower(parameters('virtual_network_name'))))]"
586+
}
578587
}
579588
}
580589
}
581590
],
582591
"addressSpace": {
583592
"addressPrefixes": "[concat(createArray('10.0.0.0/16'), if(parameters('use_ipv6'), createArray('2001:db8::/32'), createArray()))]"
584593
}
594+
},
595+
"dependsOn": [
596+
"nsg"
597+
]
598+
},
599+
"nsg": {
600+
"type": "Microsoft.Network/networkSecurityGroups",
601+
"apiVersion": "2024-05-01",
602+
"name": "[format('{0}-nsg', toLower(parameters('virtual_network_name')))]",
603+
"location": "[parameters('location')]",
604+
"properties": {
605+
"securityRules": [
606+
{
607+
"name": "LISASSH",
608+
"properties": {
609+
"priority": 100,
610+
"direction": "Inbound",
611+
"access": "Allow",
612+
"protocol": "Tcp",
613+
"sourcePortRange": "*",
614+
"destinationPortRange": "22",
615+
"sourceAddressPrefixes": "[parameters('source_address_prefixes')]",
616+
"destinationAddressPrefix": "*"
617+
}
618+
},
619+
{
620+
"name": "LISAKVMSSH",
621+
"properties": {
622+
"description": "Allows nested VM SSH traffic",
623+
"protocol": "Tcp",
624+
"sourcePortRange": "*",
625+
"destinationPortRange": "60020-60030",
626+
"destinationAddressPrefix": "*",
627+
"sourceAddressPrefixes": "[parameters('source_address_prefixes')]",
628+
"access": "Allow",
629+
"priority": 206,
630+
"direction": "Inbound"
631+
}
632+
},
633+
{
634+
"name": "LISALIBVIRTSSH",
635+
"properties": {
636+
"description": "Allows SSH traffic to Libvirt Platform Guests",
637+
"protocol": "Tcp",
638+
"sourcePortRange": "*",
639+
"destinationPortRange": "49152-49352",
640+
"destinationAddressPrefix": "*",
641+
"sourceAddressPrefixes": "[parameters('source_address_prefixes')]",
642+
"access": "Allow",
643+
"priority": 208,
644+
"direction": "Inbound"
645+
}
646+
}
647+
]
585648
}
586649
},
587650
"availability_set": {
@@ -607,11 +670,11 @@
607670
"location": "[parameters('location')]",
608671
"tags": "[parameters('tags')]",
609672
"properties": {
610-
"publicIPAllocationMethod": "[if(or(or(parameters('is_ultradisk'), variables('use_availability_zones')), parameters('use_ipv6')), 'Static', 'Dynamic')]",
673+
"publicIPAllocationMethod": "Static",
611674
"ipTags": "[if(empty(variables('ip_tags')), null(), variables('ip_tags'))]"
612675
},
613676
"sku": {
614-
"name": "[if(or(or(parameters('is_ultradisk'), variables('use_availability_zones')), parameters('use_ipv6')), 'Standard', 'Basic')]"
677+
"name": "Standard"
615678
},
616679
"zones": "[if(variables('use_availability_zones'), variables('availability_zones'), null())]"
617680
},

lisa/sut_orchestrator/azure/common.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,6 +1209,7 @@ class AzureArmParameter:
12091209
is_ultradisk: bool = False
12101210
use_ipv6: bool = False
12111211
enable_vm_nat: bool = False
1212+
source_address_prefixes: List[str] = field(default_factory=list)
12121213

12131214
def __post_init__(self, *args: Any, **kwargs: Any) -> None:
12141215
add_secret(self.admin_username, PATTERN_HEADTAIL)

lisa/sut_orchestrator/azure/platform_.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
get_first_combination,
8181
get_matched_str,
8282
get_or_generate_key_pairs,
83+
get_public_ip,
8384
get_public_key_data,
8485
is_unittest,
8586
plugin_manager,
@@ -301,6 +302,7 @@ class AzurePlatformSchema:
301302
# will be retired. It's recommended to disable outbound access to
302303
# enforce explicit connectivity rules.
303304
enable_vm_nat: bool = field(default=False)
305+
source_address_prefixes: Optional[List[str]] = field(default=None)
304306

305307
virtual_network_resource_group: str = field(default="")
306308
virtual_network_name: str = field(default=AZURE_VIRTUAL_NETWORK_NAME)
@@ -356,6 +358,7 @@ def __post_init__(self, *args: Any, **kwargs: Any) -> None:
356358
"use_public_address",
357359
"use_ipv6",
358360
"enable_vm_nat",
361+
"source_address_prefixes",
359362
],
360363
)
361364

@@ -939,6 +942,7 @@ def _initialize(self, *args: Any, **kwargs: Any) -> None:
939942
self.subscription_id = azure_runbook.subscription_id
940943
self.cloud = azure_runbook.cloud
941944
self.resource_group_managed_by = azure_runbook.resource_group_managed_by
945+
self._cached_ip_address: List[str] = []
942946

943947
self._initialize_credential()
944948

@@ -956,6 +960,15 @@ def _initialize(self, *args: Any, **kwargs: Any) -> None:
956960
self.credential, self.subscription_id, self.cloud
957961
)
958962

963+
def _get_ip_addresses(self) -> List[str]:
964+
if self._cached_ip_address:
965+
return self._cached_ip_address
966+
if self._azure_runbook.source_address_prefixes:
967+
self._cached_ip_address = self._azure_runbook.source_address_prefixes
968+
else:
969+
self._cached_ip_address = [get_public_ip()]
970+
return self._cached_ip_address
971+
959972
def _initialize_credential(self) -> None:
960973
azure_runbook = self._azure_runbook
961974
if azure_runbook.credential:
@@ -1254,6 +1267,8 @@ def _create_deployment_parameters(
12541267
self._azure_runbook.shared_resource_group_name
12551268
)
12561269
arm_parameters.enable_vm_nat = self._azure_runbook.enable_vm_nat
1270+
arm_parameters.source_address_prefixes = self._get_ip_addresses()
1271+
12571272
# the arm template may be updated by the hooks, so make a copy to avoid
12581273
# the original template is modified.
12591274
template = deepcopy(self._load_template())

lisa/util/__init__.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Copyright (c) Microsoft Corporation.
22
# Licensed under the MIT license.
3+
import ipaddress
34
import random
45
import re
56
import string
@@ -28,9 +29,11 @@
2829

2930
import paramiko
3031
import pluggy
32+
import requests
3133
from assertpy import assert_that
3234
from dataclasses_json import config
3335
from marshmallow import fields
36+
from retry import retry
3437
from semver import VersionInfo
3538

3639
from lisa import secret
@@ -947,3 +950,11 @@ def to_bool(value: Union[str, bool, int]) -> bool:
947950
raise TypeError(
948951
f"Unsupported type for conversion to boolean: {type(value).__name__}"
949952
)
953+
954+
955+
@retry(tries=10, delay=0.5)
956+
def get_public_ip() -> str:
957+
response = requests.get("https://api.ipify.org/", timeout=5)
958+
result = response.text
959+
ipaddress.ip_address(result)
960+
return str(result)

pyproject.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ dependencies = [
2727
"spurplus ~= 2.3.5",
2828
"websockets ~= 10.3",
2929
"charset_normalizer ~= 2.1.1",
30+
"requests ~= 2.32.0",
3031
]
3132
dynamic = ["version"]
3233
license = {text = "MIT"}
@@ -58,13 +59,11 @@ azure = [
5859
"cachetools ~= 5.2.0",
5960
"Pillow <= 11.1.0",
6061
"PyGObject <= 3.50.0; platform_system == 'Linux'",
61-
"requests ~= 2.32.0",
6262
"pycdlib ~= 1.12.0",
6363
]
6464

6565
ado = [
6666
"azure-devops ~= 7.1.0b3",
67-
"requests ~= 2.32.0",
6867
]
6968

7069
black = [
@@ -105,7 +104,6 @@ baremetal = [
105104
"pysmb ~= 1.2.9.1",
106105
"redfish ~= 3.2.1",
107106
"azure-devops ~= 7.1.0b3",
108-
"requests ~= 2.32.0",
109107
]
110108

111109
mypy = [

0 commit comments

Comments
 (0)