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

[Compute] VirtualMachineScaleSetId Parameter for Update-Azvm and Reduced File permissions for SSH key #23170

Merged
merged 14 commits into from
Nov 8, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,13 @@ public void TestVirtualMachineScaleSetSecurityTypeUpdate()
TestRunner.RunTestScript("Test-VirtualMachineScaleSetSecurityTypeUpdate");
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
haagha marked this conversation as resolved.
Show resolved Hide resolved
public void TestVirtualMachineScaleSetAttachAndDetach()
{
TestRunner.RunTestScript("Test-VirtualMachineScaleSetAttachAndDetach");
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestVirtualMachineScaleSetSecurityTypeDefaulting()
Expand All @@ -388,6 +395,5 @@ public void TestVirtualMachineScaleSetSecurityTypeDefaultingFromImage()
{
TestRunner.RunTestScript("Test-VirtualMachineScaleSetSecurityTypeDefaultingFromImage");
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5119,4 +5119,61 @@ function Test-VirtualMachineScaleSetSecurityTypeDefaultingFromImage
# Cleanup
Clean-ResourceGroup $rgname;
}
}
}


<#
.SYNOPSIS
create a VMSS in flexible mode
Test Attach scenario of adding a vmss
Test Detach scenario of removing the VMSS
#>
function Test-VirtualMachineScaleSetAttachAndDetach
{
# Setup
$rgname = Get-ComputeTestResourceName

try
{
# Common
$loc = Get-ComputeVMLocation;
New-AzResourceGroup -Name $rgname -Location $loc -Force;

# New VMSS Parameters
$vmssName = 'vmssAttachAndDetach' + $rgname;
$vmName = 'vm' + $rgname;

$adminUsername = 'Foo12';
$adminPassword = $PLACEHOLDER;
$securePassword = ConvertTo-SecureString $adminPassword -AsPlainText -Force;
$cred = New-Object System.Management.Automation.PSCredential ($adminUsername, $securePassword);

$VmssFlex = New-AzVmss `
-ResourceGroupName $rgname `
-Name $vmssName `
-OrchestrationMode 'Flexible' `
-Location 'eastus' `
-Credential $cred `
-DomainNameLabel "scaleset-70f699" `
-SecurityType "Standard"

$vm = new-azvm -resourcegroupname $rgname -location $loc -name $vmname -credential $cred -DomainNameLabel "scaleset-70f699"

# attach
Update-Azvm -resourcegroupname $rgname -VM $vm -VirtualMachineScaleSetId $VmssFlex.id
$updatedVmWithVmss = get-azvm -resourcegroupname $rgname -Name $vmname
Assert-AreEqual $VmssFlex.id $updatedVmWithVmss.VirtualMachineScaleSet.Id

# detach
Update-Azvm -resourcegroupname $rgname -VM $updatedVmWithVmss -VirtualMachineScaleSetId $null
$updatedVm = get-azvm -resourcegroupname $rgname -Name $vmname
Assert-Null $updatedVm.VirtualMachineScaleSet.Id


}
finally
{
# Cleanup
Clean-ResourceGroup $rgname
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ public void TestCapacityReservation()
TestRunner.RunTestScript("Test-CapacityReservation");
}

[Fact]
[Fact(Skip = "Cannot run on Github Platform due to ACL API")]
haagha marked this conversation as resolved.
Show resolved Hide resolved
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestVMwithSSHKey()
{
Expand Down

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions src/Compute/Compute/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@
* [Breaking change] Removed unversioned and outdated linux image aliases of `CentOS`, `RHEL`, `UbuntuLTS` and `Debian`.
* [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.
* [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.
* Added new parameters `-ElasticSanResourceId` and `-OptimizedForFrequentAttach` to `New-AzDiskConfig` cmdlet.
* Added new parameter `-ElasticSanResourceId` to `New-AzSnapshotConfig` cmdlet.
* Added new parameter `-VirtualMachineScaleSetId` to `Update-AzVm` cmdlet.
* Fixed `New-AzVmss` and `New-Azvm` to use `SharedGalleryImageId` parameter.
* Reducing File Permissions from 0644 to 0600 for SSH Private Key File in `New-AzVm`.


## Version 6.3.0
* Added `-Hibernate` switch parameter to `Stop-AzVmss` default parameter set.
Expand Down
1 change: 1 addition & 0 deletions src/Compute/Compute/Compute.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.Azure.PowerShell.AutoMapper" Version="6.2.2" />
<PackageReference Include="Microsoft.Azure.Management.ResourceGraph" Version="2.1.0" />
<PackageReference Include="System.IO.FileSystem.AccessControl" Version="5.0.0" />
haagha marked this conversation as resolved.
Show resolved Hide resolved
<PackageReference Include="System.Security.Permissions" Version="4.7.0" />
<PackageReference Include="System.ServiceModel.Primitives" Version="4.7.0" />
<PackageReference Include="WindowsAzure.Storage" Version="9.3.0" />
Expand Down
19 changes: 19 additions & 0 deletions src/Compute/Compute/VirtualMachine/Operation/NewAzureVMCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
using Microsoft.Azure.Commands.Compute;
using Microsoft.Azure.PowerShell.Cmdlets.Compute.Helpers.Network.Models;
using Microsoft.WindowsAzure.Commands.Common.CustomAttributes;
using System.Security.AccessControl;
using System.Security.Principal;
using Microsoft.Azure.Commands.Common.Strategies.Compute;

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

FileSecurity fileSecurity = new FileSecurity(privateKeyFilePath, AccessControlSections.Access);
haagha marked this conversation as resolved.
Show resolved Hide resolved
// Define the owner's identity
IdentityReference owner = new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null);

// Create an access rule for the owner with read and write permissions (0600)
FileSystemAccessRule rule = new FileSystemAccessRule(
owner,
FileSystemRights.Read | FileSystemRights.Write,
AccessControlType.Allow
);

// Add the access rule to the file security
fileSecurity.AddAccessRule(rule);

FileInfo fileinfo = new FileInfo(privateKeyFilePath);
fileinfo.SetAccessControl(fileSecurity);

using (StreamWriter writer = new StreamWriter(publicKeyFilePath))
{
writer.WriteLine(keypair.PublicKey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ public class UpdateAzureVMCommand : VirtualMachineBaseCmdlet
[AllowEmptyString]
public string ProximityPlacementGroupId { get; set; }

[Parameter(
Mandatory = false,
HelpMessage = "Attached Virtual Machine Scale Set Id.")]
[AllowEmptyString]
public string VirtualMachineScaleSetId { get; set; }

[Parameter(
ValueFromPipelineByPropertyName = true,
HelpMessage = "The Id of Host")]
Expand Down Expand Up @@ -223,7 +229,9 @@ public override void ExecuteCmdlet()
Host = this.IsParameterBound(c => c.HostId)
? new SubResource(this.HostId)
: this.VM.Host,
VirtualMachineScaleSet = this.VM.VirtualMachineScaleSet,
VirtualMachineScaleSet = this.IsParameterBound(c => c.VirtualMachineScaleSetId)
? new SubResource(this.VirtualMachineScaleSetId)
haagha marked this conversation as resolved.
Show resolved Hide resolved
: this.VM.VirtualMachineScaleSet,
AdditionalCapabilities = this.VM.AdditionalCapabilities,
EvictionPolicy = this.VM.EvictionPolicy,
Priority = this.VM.Priority,
Expand All @@ -244,6 +252,12 @@ public override void ExecuteCmdlet()
parameters.ProximityPlacementGroup.Id = null;
}

// when vm.virtualMachineScaleSet.Id is set to null, powershell interprets it as empty so converting it back to null
if (parameters.VirtualMachineScaleSet != null && string.IsNullOrWhiteSpace(parameters.VirtualMachineScaleSet.Id))
haagha marked this conversation as resolved.
Show resolved Hide resolved
{
parameters.VirtualMachineScaleSet.Id = null;
}

if (this.IsParameterBound(c => c.IdentityType))
{
parameters.Identity = new VirtualMachineIdentity(null, null, this.IdentityType, null);
Expand Down
43 changes: 30 additions & 13 deletions src/Compute/Compute/help/Update-AzVM.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,34 @@ Updates the state of an Azure virtual machine.
```
Update-AzVM [-ResourceGroupName] <String> -VM <PSVirtualMachine> [-Tag <Hashtable>]
[-OsDiskWriteAccelerator <Boolean>] [-UltraSSDEnabled <Boolean>] [-MaxPrice <Double>]
[-EncryptionAtHost <Boolean>] [-ProximityPlacementGroupId <String>] [-HostId <String>]
[-CapacityReservationGroupId <String>] [-AsJob] [-NoWait] [-UserData <String>] [-HibernationEnabled]
[-vCPUCountAvailable <Int32>] [-vCPUCountPerCore <Int32>] [-SecurityType <String>] [-EnableVtpm <Boolean>]
[-EnableSecureBoot <Boolean>] [-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm]
[<CommonParameters>]
[-EncryptionAtHost <Boolean>] [-ProximityPlacementGroupId <String>] [-VirtualMachineScaleSetId <String>]
[-HostId <String>] [-CapacityReservationGroupId <String>] [-AsJob] [-NoWait] [-UserData <String>]
[-HibernationEnabled] [-vCPUCountAvailable <Int32>] [-vCPUCountPerCore <Int32>] [-SecurityType <String>]
[-EnableVtpm <Boolean>] [-EnableSecureBoot <Boolean>] [-DefaultProfile <IAzureContextContainer>] [-WhatIf]
[-Confirm] [<CommonParameters>]
```

### ExplicitIdentityParameterSet
```
Update-AzVM [-ResourceGroupName] <String> -VM <PSVirtualMachine> [-Tag <Hashtable>]
-IdentityType <ResourceIdentityType> [-IdentityId <String[]>] [-OsDiskWriteAccelerator <Boolean>]
[-UltraSSDEnabled <Boolean>] [-MaxPrice <Double>] [-EncryptionAtHost <Boolean>]
[-ProximityPlacementGroupId <String>] [-HostId <String>] [-CapacityReservationGroupId <String>] [-AsJob]
[-NoWait] [-UserData <String>] [-HibernationEnabled] [-vCPUCountAvailable <Int32>] [-vCPUCountPerCore <Int32>]
[-SecurityType <String>] [-EnableVtpm <Boolean>] [-EnableSecureBoot <Boolean>]
[-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm] [<CommonParameters>]
[-ProximityPlacementGroupId <String>] [-VirtualMachineScaleSetId <String>] [-HostId <String>]
[-CapacityReservationGroupId <String>] [-AsJob] [-NoWait] [-UserData <String>] [-HibernationEnabled]
[-vCPUCountAvailable <Int32>] [-vCPUCountPerCore <Int32>] [-SecurityType <String>] [-EnableVtpm <Boolean>]
[-EnableSecureBoot <Boolean>] [-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm]
[<CommonParameters>]
```

### IdParameterSetName
```
Update-AzVM [-Id] <String> -VM <PSVirtualMachine> [-Tag <Hashtable>] [-OsDiskWriteAccelerator <Boolean>]
[-UltraSSDEnabled <Boolean>] [-MaxPrice <Double>] [-EncryptionAtHost <Boolean>]
[-ProximityPlacementGroupId <String>] [-HostId <String>] [-CapacityReservationGroupId <String>] [-AsJob]
[-NoWait] [-UserData <String>] [-HibernationEnabled] [-vCPUCountAvailable <Int32>] [-vCPUCountPerCore <Int32>]
[-SecurityType <String>] [-EnableVtpm <Boolean>] [-EnableSecureBoot <Boolean>]
[-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm] [<CommonParameters>]
[-ProximityPlacementGroupId <String>] [-VirtualMachineScaleSetId <String>] [-HostId <String>]
[-CapacityReservationGroupId <String>] [-AsJob] [-NoWait] [-UserData <String>] [-HibernationEnabled]
[-vCPUCountAvailable <Int32>] [-vCPUCountPerCore <Int32>] [-SecurityType <String>] [-EnableVtpm <Boolean>]
[-EnableSecureBoot <Boolean>] [-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm]
[<CommonParameters>]
```

## DESCRIPTION
Expand Down Expand Up @@ -419,6 +421,21 @@ Accept pipeline input: True (ByPropertyName)
Accept wildcard characters: False
```

### -VirtualMachineScaleSetId
Id for the Virtual Machine ScaleSet that the virtual machine should be updated to.
haagha marked this conversation as resolved.
Show resolved Hide resolved

```yaml
Type: System.String
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```

### -VM
Specifies a local virtual machine object.
To obtain a virtual machine object, use the Get-AzVM cmdlet.
Expand Down