Skip to content

Commit c27f45a

Browse files
authored
Add client side parameter validation logic. (#12905)
Co-authored-by: wyunchi-ms <[email protected]>
1 parent c27f3d3 commit c27f45a

File tree

8 files changed

+106
-6
lines changed

8 files changed

+106
-6
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: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
16+
using Microsoft.Azure.Commands.Aks.Properties;
17+
using Microsoft.WindowsAzure.Commands.Common;
18+
19+
using System;
20+
using System.Management.Automation;
21+
using System.Runtime.InteropServices;
22+
using System.Security;
23+
using System.Text.RegularExpressions;
24+
25+
namespace Microsoft.Azure.Commands.Aks.Utils
26+
{
27+
public sealed class ValidateSecureString: ValidateEnumeratedArgumentsAttribute
28+
{
29+
public string RegularExpression { get; set; }
30+
31+
protected override void ValidateElement(object element)
32+
{
33+
SecureString secureString = element as SecureString;
34+
string content = secureString.ConvertToString();
35+
Regex regex = new Regex(RegularExpression);
36+
if (!regex.IsMatch(content))
37+
{
38+
throw new ArgumentException(string.Format(Resources.SecureStringNotValid, RegularExpression));
39+
}
40+
}
41+
}
42+
}

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

Lines changed: 4 additions & 4 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

@@ -95,7 +95,7 @@ Accept wildcard characters: False
9595
```
9696
9797
### -DnsNamePrefix
98-
The DNS name prefix for the cluster.
98+
The DNS name prefix for the cluster. The length must be <= 9 if users plan to add windows container.
9999
100100
```yaml
101101
Type: System.String

0 commit comments

Comments
 (0)