Skip to content

Commit 4f0e275

Browse files
committed
Add client side parameter validation logic.
1 parent 7af183d commit 4f0e275

File tree

8 files changed

+103
-5
lines changed

8 files changed

+103
-5
lines changed

src/Aks/Aks/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- Additional information about change #1
1919
-->
2020
## Upcoming Release
21+
* Added client side parameter validation logic for `New-AzAksCluster`, `Set-AzAksCluster` and `New-AzAksNodePool`.
2122
* Added parameter `GenerateSshKey` for `New-AzAksCluster`.
2223
* Updated api version to 2020-06-01.
2324

src/Aks/Aks/Commands/CreateOrUpdateKubeBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public abstract class CreateOrUpdateKubeBase : KubeCmdletBase
8383
[Alias("AdminUserName")]
8484
public string LinuxProfileAdminUserName { get; set; } = "azureuser";
8585

86-
[Parameter(Mandatory = false, HelpMessage = "The DNS name prefix for the cluster.")]
86+
[Parameter(Mandatory = false, HelpMessage = "The DNS name prefix for the cluster. The length must be <= 9 if users plan to add windows container.")]
8787
public string DnsNamePrefix { get; set; }
8888

8989
[Parameter(Mandatory = false, HelpMessage = "The version of Kubernetes to use for creating the cluster.")]

src/Aks/Aks/Commands/NewAzureRmAks.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,26 @@ private void PreValidate()
8585

8686
if (this.IsParameterBound(c => c.GenerateSshKey) && this.IsParameterBound(c => c.SshKeyValue))
8787
{
88-
throw new ArgumentException(string.Format(Resources.DonotUseGenerateSshKeyWithSshKeyValue));
88+
throw new ArgumentException(Resources.DonotUseGenerateSshKeyWithSshKeyValue);
89+
}
90+
91+
if ((this.IsParameterBound(c => c.WindowsProfileAdminUserName) && !this.IsParameterBound(c => c.WindowsProfileAdminUserPassword)) ||
92+
(!this.IsParameterBound(c => c.WindowsProfileAdminUserName) && this.IsParameterBound(c => c.WindowsProfileAdminUserPassword)))
93+
{
94+
throw new ArgumentException(Resources.WindowsUserNameAndPasswordShouldAppearTogether);
95+
}
96+
97+
if (this.IsParameterBound(c => c.WindowsProfileAdminUserName))
98+
{
99+
if (!string.Equals(this.NetworkPlugin, "azure"))
100+
{
101+
throw new ArgumentException(Resources.NetworkPluginShouldBeAzure);
102+
}
103+
}
104+
if (string.Equals(this.NodeOsType, "Windows"))
105+
{
106+
if (NodeName?.Length > 6)
107+
throw new PSInvalidOperationException(Resources.WindowsNodePoolNameLengthLimitation);
89108
}
90109
}
91110

src/Aks/Aks/Commands/NewKubeBase.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using System.Management.Automation;
1818
using System.Security;
1919
using Microsoft.Azure.Commands.Aks.Properties;
20+
using Microsoft.Azure.Commands.Aks.Utils;
2021
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
2122
using Microsoft.Azure.Commands.ResourceManager.Common.Tags;
2223
using Microsoft.Azure.Management.ContainerService.Models;
@@ -97,6 +98,7 @@ public abstract class NewKubeBase : CreateOrUpdateKubeBase
9798

9899
[Parameter(Mandatory = false, HelpMessage = "The administrator password to use for Windows VMs. Password requirement:"
99100
+ "At least one lower case, one upper case, one special character !@#$%^&*(), the minimum lenth is 12.")]
101+
[ValidateSecureString(RegularExpression = "^(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%\\^&\\*\\(\\)])[a-zA-Z\\d!@#$%\\^&\\*\\(\\)]{12,123}$")]
100102
public SecureString WindowsProfileAdminUserPassword { get; set; }
101103

102104
[CmdletParameterBreakingChange("NetworkPlugin", ChangeDescription = "Default value will be changed from None to azure.")]

src/Aks/Aks/Properties/Resources.Designer.cs

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Aks/Aks/Properties/Resources.resx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,4 +381,13 @@
381381
<data name="UpdatingNodePoolMode" xml:space="preserve">
382382
<value>Updating NodePoolMode.</value>
383383
</data>
384+
<data name="SecureStringNotValid" xml:space="preserve">
385+
<value>Plain text of SecureString should match the patten {0}.</value>
386+
</data>
387+
<data name="WindowsUserNameAndPasswordShouldAppearTogether" xml:space="preserve">
388+
<value>WindowsProfileAdminUser and WindowsProfileAdminUserPassword must appear together.</value>
389+
</data>
390+
<data name="NetworkPluginShouldBeAzure" xml:space="preserve">
391+
<value>NetworkPlugin must be azure if you want to use Windows.</value>
392+
</data>
384393
</root>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using Microsoft.Azure.Commands.Aks.Properties;
2+
3+
using System;
4+
using System.Management.Automation;
5+
using System.Runtime.InteropServices;
6+
using System.Security;
7+
using System.Text.RegularExpressions;
8+
9+
namespace Microsoft.Azure.Commands.Aks.Utils
10+
{
11+
public sealed class ValidateSecureString: ValidateEnumeratedArgumentsAttribute
12+
{
13+
public string RegularExpression { get; set; }
14+
15+
protected override void ValidateElement(object element)
16+
{
17+
SecureString secureString = element as SecureString;
18+
string content = SecureStringToString(secureString);
19+
Regex regex = new Regex(RegularExpression);
20+
if (!regex.IsMatch(content))
21+
{
22+
throw new ArgumentException(string.Format(Resources.SecureStringNotValid, RegularExpression));
23+
}
24+
}
25+
26+
private string SecureStringToString(SecureString value)
27+
{
28+
IntPtr valuePtr = IntPtr.Zero;
29+
try
30+
{
31+
valuePtr = Marshal.SecureStringToGlobalAllocUnicode(value);
32+
return Marshal.PtrToStringUni(valuePtr);
33+
}
34+
finally
35+
{
36+
Marshal.ZeroFreeGlobalAllocUnicode(valuePtr);
37+
}
38+
}
39+
}
40+
}

src/Aks/Aks/help/New-AzAksCluster.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ Create a new managed Kubernetes cluster.
1313
## SYNTAX
1414

1515
```
16-
New-AzAksCluster [-Force] [-NodeVmSetType <String>] [-NodeVnetSubnetID <String>] [-NodeMaxPodCount <Int32>]
17-
[-NodeOsType <String>] [-NodeSetPriority <String>] [-NodePoolMode <String>]
16+
New-AzAksCluster [-Force] [-GenerateSshKey] [-NodeVmSetType <String>] [-NodeVnetSubnetID <String>]
17+
[-NodeMaxPodCount <Int32>] [-NodeOsType <String>] [-NodeSetPriority <String>] [-NodePoolMode <String>]
1818
[-NodeScaleSetEvictionPolicy <String>] [-AcrNameToAttach <String>] [-EnableRbac]
1919
[-WindowsProfileAdminUserName <String>] [-WindowsProfileAdminUserPassword <SecureString>]
2020
[-NetworkPlugin <String>] [-LoadBalancerSku <String>] [-ResourceGroupName] <String> [-Name] <String>
2121
[[-ServicePrincipalIdAndSecret] <PSCredential>] [-Location <String>] [-LinuxProfileAdminUserName <String>]
2222
[-DnsNamePrefix <String>] [-KubernetesVersion <String>] [-NodeName <String>] [-NodeMinCount <Int32>]
2323
[-NodeMaxCount <Int32>] [-EnableNodeAutoScaling] [-NodeCount <Int32>] [-NodeOsDiskSize <Int32>]
24-
[-NodeVmSize <String>] [-SshKeyValue <String>] [-GenerateSshKey] [-AsJob] [-Tag <Hashtable>]
24+
[-NodeVmSize <String>] [-SshKeyValue <String>] [-AsJob] [-Tag <Hashtable>]
2525
[-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm] [<CommonParameters>]
2626
```
2727

0 commit comments

Comments
 (0)