Skip to content

Commit 297fa4f

Browse files
haaghaVeryEarly
andauthored
[Compute] VirtualMachineScaleSetId Parameter for Update-Azvm and Reduced File permissions for SSH key (#23170)
* adding update-azvm changes * Adding help * tests and fix * Updated after review * Permissions change * Test fix * fix * test skip * Adding changelog * resolving merge * adding test --------- Co-authored-by: Yabo Hu <[email protected]>
1 parent 4dfccd6 commit 297fa4f

File tree

9 files changed

+5319
-17
lines changed

9 files changed

+5319
-17
lines changed

src/Compute/Compute.Test/ScenarioTests/VirtualMachineScaleSetTests.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,13 @@ public void TestVirtualMachineScaleSetSecurityTypeUpdate()
375375
TestRunner.RunTestScript("Test-VirtualMachineScaleSetSecurityTypeUpdate");
376376
}
377377

378+
[Fact]
379+
[Trait(Category.AcceptanceType, Category.CheckIn)]
380+
public void TestVirtualMachineScaleSetAttachAndDetach()
381+
{
382+
TestRunner.RunTestScript("Test-VirtualMachineScaleSetAttachAndDetach");
383+
}
384+
378385
[Fact]
379386
[Trait(Category.AcceptanceType, Category.CheckIn)]
380387
public void TestVirtualMachineScaleSetSecurityTypeDefaulting()
@@ -388,6 +395,5 @@ public void TestVirtualMachineScaleSetSecurityTypeDefaultingFromImage()
388395
{
389396
TestRunner.RunTestScript("Test-VirtualMachineScaleSetSecurityTypeDefaultingFromImage");
390397
}
391-
392398
}
393399
}

src/Compute/Compute.Test/ScenarioTests/VirtualMachineScaleSetTests.ps1

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5119,4 +5119,61 @@ function Test-VirtualMachineScaleSetSecurityTypeDefaultingFromImage
51195119
# Cleanup
51205120
Clean-ResourceGroup $rgname;
51215121
}
5122-
}
5122+
}
5123+
5124+
5125+
<#
5126+
.SYNOPSIS
5127+
create a VMSS in flexible mode
5128+
Test Attach scenario of adding a vmss
5129+
Test Detach scenario of removing the VMSS
5130+
#>
5131+
function Test-VirtualMachineScaleSetAttachAndDetach
5132+
{
5133+
# Setup
5134+
$rgname = Get-ComputeTestResourceName
5135+
5136+
try
5137+
{
5138+
# Common
5139+
$loc = Get-ComputeVMLocation;
5140+
New-AzResourceGroup -Name $rgname -Location $loc -Force;
5141+
5142+
# New VMSS Parameters
5143+
$vmssName = 'vmssAttachAndDetach' + $rgname;
5144+
$vmName = 'vm' + $rgname;
5145+
5146+
$adminUsername = 'Foo12';
5147+
$adminPassword = $PLACEHOLDER;
5148+
$securePassword = ConvertTo-SecureString $adminPassword -AsPlainText -Force;
5149+
$cred = New-Object System.Management.Automation.PSCredential ($adminUsername, $securePassword);
5150+
5151+
$VmssFlex = New-AzVmss `
5152+
-ResourceGroupName $rgname `
5153+
-Name $vmssName `
5154+
-OrchestrationMode 'Flexible' `
5155+
-Location 'eastus' `
5156+
-Credential $cred `
5157+
-DomainNameLabel "scaleset-70f699" `
5158+
-SecurityType "Standard"
5159+
5160+
$vm = new-azvm -resourcegroupname $rgname -location $loc -name $vmname -credential $cred -DomainNameLabel "scaleset-70f699"
5161+
5162+
# attach
5163+
Update-Azvm -resourcegroupname $rgname -VM $vm -VirtualMachineScaleSetId $VmssFlex.id
5164+
$updatedVmWithVmss = get-azvm -resourcegroupname $rgname -Name $vmname
5165+
Assert-AreEqual $VmssFlex.id $updatedVmWithVmss.VirtualMachineScaleSet.Id
5166+
5167+
# detach
5168+
Update-Azvm -resourcegroupname $rgname -VM $updatedVmWithVmss -VirtualMachineScaleSetId $null
5169+
$updatedVm = get-azvm -resourcegroupname $rgname -Name $vmname
5170+
Assert-Null $updatedVm.VirtualMachineScaleSet.Id
5171+
5172+
5173+
}
5174+
finally
5175+
{
5176+
# Cleanup
5177+
Clean-ResourceGroup $rgname
5178+
}
5179+
}

src/Compute/Compute.Test/ScenarioTests/VirtualMachineTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ public void TestCapacityReservation()
445445
TestRunner.RunTestScript("Test-CapacityReservation");
446446
}
447447

448-
[Fact]
448+
[Fact(Skip = "Cannot run on Github Platform due to ACL API")]
449449
[Trait(Category.AcceptanceType, Category.CheckIn)]
450450
public void TestVMwithSSHKey()
451451
{

src/Compute/Compute.Test/SessionRecords/Microsoft.Azure.Commands.Compute.Test.ScenarioTests.VirtualMachineScaleSetTests/TestVirtualMachineScaleSetAttachAndDetach.json

Lines changed: 5183 additions & 0 deletions
Large diffs are not rendered by default.

src/Compute/Compute/ChangeLog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@
2525
* [Breaking change] Removed unversioned and outdated linux image aliases of `CentOS`, `RHEL`, `UbuntuLTS` and `Debian`.
2626
* [Breaking Change] Added defaulting logic for VM and VMSS creation to set SecurityType to TrustedLaunch and SecureBootEnabled and VTpmEnalbed to true when those are not set by the user.
2727
* [Breaking Change] Added defaulting logic for Disk creation to default to TrustedLaunch when able. Allows the user to turn this off by setting the SecurityType to Standard.
28+
* Added new parameters `-ElasticSanResourceId` and `-OptimizedForFrequentAttach` to `New-AzDiskConfig` cmdlet.
29+
* Added new parameter `-ElasticSanResourceId` to `New-AzSnapshotConfig` cmdlet.
30+
* Added new parameter `-VirtualMachineScaleSetId` to `Update-AzVm` cmdlet.
2831
* Fixed `New-AzVmss` and `New-Azvm` to use `SharedGalleryImageId` parameter.
32+
* Reducing File Permissions from 0644 to 0600 for SSH Private Key File in `New-AzVm`.
33+
2934

3035
## Version 6.3.0
3136
* Added `-Hibernate` switch parameter to `Stop-AzVmss` default parameter set.

src/Compute/Compute/Compute.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<ItemGroup>
1616
<PackageReference Include="Microsoft.Azure.PowerShell.AutoMapper" Version="6.2.2" />
1717
<PackageReference Include="Microsoft.Azure.Management.ResourceGraph" Version="2.1.0" />
18+
<PackageReference Include="System.IO.FileSystem.AccessControl" Version="5.0.0" />
1819
<PackageReference Include="System.Security.Permissions" Version="4.7.0" />
1920
<PackageReference Include="System.ServiceModel.Primitives" Version="4.7.0" />
2021
<PackageReference Include="WindowsAzure.Storage" Version="9.3.0" />

src/Compute/Compute/VirtualMachine/Operation/NewAzureVMCommand.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@
5151
using Microsoft.Azure.Commands.Compute;
5252
using Microsoft.Azure.PowerShell.Cmdlets.Compute.Helpers.Network.Models;
5353
using Microsoft.WindowsAzure.Commands.Common.CustomAttributes;
54+
using System.Security.AccessControl;
55+
using System.Security.Principal;
5456
using Microsoft.Azure.Commands.Common.Strategies.Compute;
5557

5658
namespace Microsoft.Azure.Commands.Compute
@@ -1704,6 +1706,23 @@ private string GenerateOrFindSshKey()
17041706
}
17051707
Console.WriteLine("Private key is saved to " + privateKeyFilePath);
17061708

1709+
FileSecurity fileSecurity = new FileSecurity(privateKeyFilePath, AccessControlSections.Access);
1710+
// Define the owner's identity
1711+
IdentityReference owner = new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null);
1712+
1713+
// Create an access rule for the owner with read and write permissions (0600)
1714+
FileSystemAccessRule rule = new FileSystemAccessRule(
1715+
owner,
1716+
FileSystemRights.Read | FileSystemRights.Write,
1717+
AccessControlType.Allow
1718+
);
1719+
1720+
// Add the access rule to the file security
1721+
fileSecurity.AddAccessRule(rule);
1722+
1723+
FileInfo fileinfo = new FileInfo(privateKeyFilePath);
1724+
fileinfo.SetAccessControl(fileSecurity);
1725+
17071726
using (StreamWriter writer = new StreamWriter(publicKeyFilePath))
17081727
{
17091728
writer.WriteLine(keypair.PublicKey);

src/Compute/Compute/VirtualMachine/Operation/UpdateAzureVMCommand.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@ public class UpdateAzureVMCommand : VirtualMachineBaseCmdlet
103103
[AllowEmptyString]
104104
public string ProximityPlacementGroupId { get; set; }
105105

106+
[Parameter(
107+
Mandatory = false,
108+
HelpMessage = "Attached Virtual Machine Scale Set Id.")]
109+
[AllowEmptyString]
110+
public string VirtualMachineScaleSetId { get; set; }
111+
106112
[Parameter(
107113
ValueFromPipelineByPropertyName = true,
108114
HelpMessage = "The Id of Host")]
@@ -223,7 +229,9 @@ public override void ExecuteCmdlet()
223229
Host = this.IsParameterBound(c => c.HostId)
224230
? new SubResource(this.HostId)
225231
: this.VM.Host,
226-
VirtualMachineScaleSet = this.VM.VirtualMachineScaleSet,
232+
VirtualMachineScaleSet = this.IsParameterBound(c => c.VirtualMachineScaleSetId)
233+
? new SubResource(this.VirtualMachineScaleSetId)
234+
: this.VM.VirtualMachineScaleSet,
227235
AdditionalCapabilities = this.VM.AdditionalCapabilities,
228236
EvictionPolicy = this.VM.EvictionPolicy,
229237
Priority = this.VM.Priority,
@@ -244,6 +252,12 @@ public override void ExecuteCmdlet()
244252
parameters.ProximityPlacementGroup.Id = null;
245253
}
246254

255+
// when vm.virtualMachineScaleSet.Id is set to null, powershell interprets it as empty so converting it back to null
256+
if (parameters.VirtualMachineScaleSet != null && string.IsNullOrWhiteSpace(parameters.VirtualMachineScaleSet.Id))
257+
{
258+
parameters.VirtualMachineScaleSet.Id = null;
259+
}
260+
247261
if (this.IsParameterBound(c => c.IdentityType))
248262
{
249263
parameters.Identity = new VirtualMachineIdentity(null, null, this.IdentityType, null);

src/Compute/Compute/help/Update-AzVM.md

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,34 @@ Updates the state of an Azure virtual machine.
1717
```
1818
Update-AzVM [-ResourceGroupName] <String> -VM <PSVirtualMachine> [-Tag <Hashtable>]
1919
[-OsDiskWriteAccelerator <Boolean>] [-UltraSSDEnabled <Boolean>] [-MaxPrice <Double>]
20-
[-EncryptionAtHost <Boolean>] [-ProximityPlacementGroupId <String>] [-HostId <String>]
21-
[-CapacityReservationGroupId <String>] [-AsJob] [-NoWait] [-UserData <String>] [-HibernationEnabled]
22-
[-vCPUCountAvailable <Int32>] [-vCPUCountPerCore <Int32>] [-SecurityType <String>] [-EnableVtpm <Boolean>]
23-
[-EnableSecureBoot <Boolean>] [-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm]
24-
[<CommonParameters>]
20+
[-EncryptionAtHost <Boolean>] [-ProximityPlacementGroupId <String>] [-VirtualMachineScaleSetId <String>]
21+
[-HostId <String>] [-CapacityReservationGroupId <String>] [-AsJob] [-NoWait] [-UserData <String>]
22+
[-HibernationEnabled] [-vCPUCountAvailable <Int32>] [-vCPUCountPerCore <Int32>] [-SecurityType <String>]
23+
[-EnableVtpm <Boolean>] [-EnableSecureBoot <Boolean>] [-DefaultProfile <IAzureContextContainer>] [-WhatIf]
24+
[-Confirm] [<CommonParameters>]
2525
```
2626

2727
### ExplicitIdentityParameterSet
2828
```
2929
Update-AzVM [-ResourceGroupName] <String> -VM <PSVirtualMachine> [-Tag <Hashtable>]
3030
-IdentityType <ResourceIdentityType> [-IdentityId <String[]>] [-OsDiskWriteAccelerator <Boolean>]
3131
[-UltraSSDEnabled <Boolean>] [-MaxPrice <Double>] [-EncryptionAtHost <Boolean>]
32-
[-ProximityPlacementGroupId <String>] [-HostId <String>] [-CapacityReservationGroupId <String>] [-AsJob]
33-
[-NoWait] [-UserData <String>] [-HibernationEnabled] [-vCPUCountAvailable <Int32>] [-vCPUCountPerCore <Int32>]
34-
[-SecurityType <String>] [-EnableVtpm <Boolean>] [-EnableSecureBoot <Boolean>]
35-
[-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm] [<CommonParameters>]
32+
[-ProximityPlacementGroupId <String>] [-VirtualMachineScaleSetId <String>] [-HostId <String>]
33+
[-CapacityReservationGroupId <String>] [-AsJob] [-NoWait] [-UserData <String>] [-HibernationEnabled]
34+
[-vCPUCountAvailable <Int32>] [-vCPUCountPerCore <Int32>] [-SecurityType <String>] [-EnableVtpm <Boolean>]
35+
[-EnableSecureBoot <Boolean>] [-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm]
36+
[<CommonParameters>]
3637
```
3738

3839
### IdParameterSetName
3940
```
4041
Update-AzVM [-Id] <String> -VM <PSVirtualMachine> [-Tag <Hashtable>] [-OsDiskWriteAccelerator <Boolean>]
4142
[-UltraSSDEnabled <Boolean>] [-MaxPrice <Double>] [-EncryptionAtHost <Boolean>]
42-
[-ProximityPlacementGroupId <String>] [-HostId <String>] [-CapacityReservationGroupId <String>] [-AsJob]
43-
[-NoWait] [-UserData <String>] [-HibernationEnabled] [-vCPUCountAvailable <Int32>] [-vCPUCountPerCore <Int32>]
44-
[-SecurityType <String>] [-EnableVtpm <Boolean>] [-EnableSecureBoot <Boolean>]
45-
[-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm] [<CommonParameters>]
43+
[-ProximityPlacementGroupId <String>] [-VirtualMachineScaleSetId <String>] [-HostId <String>]
44+
[-CapacityReservationGroupId <String>] [-AsJob] [-NoWait] [-UserData <String>] [-HibernationEnabled]
45+
[-vCPUCountAvailable <Int32>] [-vCPUCountPerCore <Int32>] [-SecurityType <String>] [-EnableVtpm <Boolean>]
46+
[-EnableSecureBoot <Boolean>] [-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm]
47+
[<CommonParameters>]
4648
```
4749

4850
## DESCRIPTION
@@ -419,6 +421,21 @@ Accept pipeline input: True (ByPropertyName)
419421
Accept wildcard characters: False
420422
```
421423
424+
### -VirtualMachineScaleSetId
425+
Id for the Virtual Machine ScaleSet that the virtual machine should be updated to.
426+
427+
```yaml
428+
Type: System.String
429+
Parameter Sets: (All)
430+
Aliases:
431+
432+
Required: False
433+
Position: Named
434+
Default value: None
435+
Accept pipeline input: False
436+
Accept wildcard characters: False
437+
```
438+
422439
### -VM
423440
Specifies a local virtual machine object.
424441
To obtain a virtual machine object, use the Get-AzVM cmdlet.

0 commit comments

Comments
 (0)