Skip to content

[Aks]Add client side parameter validation logic. #12905

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

Merged
merged 1 commit into from
Sep 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Aks/Aks/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- Additional information about change #1
-->
## Upcoming Release
* Added client side parameter validation logic for `New-AzAksCluster`, `Set-AzAksCluster` and `New-AzAksNodePool`.
* Added parameter `GenerateSshKey` for `New-AzAksCluster`.
* Updated api version to 2020-06-01.

Expand Down
2 changes: 1 addition & 1 deletion src/Aks/Aks/Commands/CreateOrUpdateKubeBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public abstract class CreateOrUpdateKubeBase : KubeCmdletBase
[Alias("AdminUserName")]
public string LinuxProfileAdminUserName { get; set; } = "azureuser";

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

[Parameter(Mandatory = false, HelpMessage = "The version of Kubernetes to use for creating the cluster.")]
Expand Down
21 changes: 20 additions & 1 deletion src/Aks/Aks/Commands/NewAzureRmAks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,26 @@ private void PreValidate()

if (this.IsParameterBound(c => c.GenerateSshKey) && this.IsParameterBound(c => c.SshKeyValue))
{
throw new ArgumentException(string.Format(Resources.DonotUseGenerateSshKeyWithSshKeyValue));
throw new ArgumentException(Resources.DonotUseGenerateSshKeyWithSshKeyValue);
}

if ((this.IsParameterBound(c => c.WindowsProfileAdminUserName) && !this.IsParameterBound(c => c.WindowsProfileAdminUserPassword)) ||
(!this.IsParameterBound(c => c.WindowsProfileAdminUserName) && this.IsParameterBound(c => c.WindowsProfileAdminUserPassword)))
{
throw new ArgumentException(Resources.WindowsUserNameAndPasswordShouldAppearTogether);
}

if (this.IsParameterBound(c => c.WindowsProfileAdminUserName))
{
if (!string.Equals(this.NetworkPlugin, "azure"))
{
throw new ArgumentException(Resources.NetworkPluginShouldBeAzure);
}
}
if (string.Equals(this.NodeOsType, "Windows"))
{
if (NodeName?.Length > 6)
throw new PSInvalidOperationException(Resources.WindowsNodePoolNameLengthLimitation);
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/Aks/Aks/Commands/NewKubeBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using System.Management.Automation;
using System.Security;
using Microsoft.Azure.Commands.Aks.Properties;
using Microsoft.Azure.Commands.Aks.Utils;
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
using Microsoft.Azure.Commands.ResourceManager.Common.Tags;
using Microsoft.Azure.Management.ContainerService.Models;
Expand Down Expand Up @@ -97,6 +98,7 @@ public abstract class NewKubeBase : CreateOrUpdateKubeBase

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

[CmdletParameterBreakingChange("NetworkPlugin", ChangeDescription = "Default value will be changed from None to azure.")]
Expand Down
27 changes: 27 additions & 0 deletions src/Aks/Aks/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions src/Aks/Aks/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -381,4 +381,13 @@
<data name="UpdatingNodePoolMode" xml:space="preserve">
<value>Updating NodePoolMode.</value>
</data>
<data name="SecureStringNotValid" xml:space="preserve">
<value>Plain text of SecureString should match the patten {0}.</value>
</data>
<data name="WindowsUserNameAndPasswordShouldAppearTogether" xml:space="preserve">
<value>WindowsProfileAdminUser and WindowsProfileAdminUserPassword must appear together.</value>
</data>
<data name="NetworkPluginShouldBeAzure" xml:space="preserve">
<value>NetworkPlugin must be azure if you want to use Windows.</value>
</data>
</root>
42 changes: 42 additions & 0 deletions src/Aks/Aks/Utils/ValidateSecureString.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------


using Microsoft.Azure.Commands.Aks.Properties;
using Microsoft.WindowsAzure.Commands.Common;

using System;
using System.Management.Automation;
using System.Runtime.InteropServices;
using System.Security;
using System.Text.RegularExpressions;

namespace Microsoft.Azure.Commands.Aks.Utils
{
public sealed class ValidateSecureString: ValidateEnumeratedArgumentsAttribute
{
public string RegularExpression { get; set; }

protected override void ValidateElement(object element)
{
SecureString secureString = element as SecureString;
string content = secureString.ConvertToString();
Regex regex = new Regex(RegularExpression);
if (!regex.IsMatch(content))
{
throw new ArgumentException(string.Format(Resources.SecureStringNotValid, RegularExpression));
}
}
}
}
8 changes: 4 additions & 4 deletions src/Aks/Aks/help/New-AzAksCluster.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ Create a new managed Kubernetes cluster.
## SYNTAX

```
New-AzAksCluster [-Force] [-NodeVmSetType <String>] [-NodeVnetSubnetID <String>] [-NodeMaxPodCount <Int32>]
[-NodeOsType <String>] [-NodeSetPriority <String>] [-NodePoolMode <String>]
New-AzAksCluster [-Force] [-GenerateSshKey] [-NodeVmSetType <String>] [-NodeVnetSubnetID <String>]
[-NodeMaxPodCount <Int32>] [-NodeOsType <String>] [-NodeSetPriority <String>] [-NodePoolMode <String>]
[-NodeScaleSetEvictionPolicy <String>] [-AcrNameToAttach <String>] [-EnableRbac]
[-WindowsProfileAdminUserName <String>] [-WindowsProfileAdminUserPassword <SecureString>]
[-NetworkPlugin <String>] [-LoadBalancerSku <String>] [-ResourceGroupName] <String> [-Name] <String>
[[-ServicePrincipalIdAndSecret] <PSCredential>] [-Location <String>] [-LinuxProfileAdminUserName <String>]
[-DnsNamePrefix <String>] [-KubernetesVersion <String>] [-NodeName <String>] [-NodeMinCount <Int32>]
[-NodeMaxCount <Int32>] [-EnableNodeAutoScaling] [-NodeCount <Int32>] [-NodeOsDiskSize <Int32>]
[-NodeVmSize <String>] [-SshKeyValue <String>] [-GenerateSshKey] [-AsJob] [-Tag <Hashtable>]
[-NodeVmSize <String>] [-SshKeyValue <String>] [-AsJob] [-Tag <Hashtable>]
[-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm] [<CommonParameters>]
```

Expand Down Expand Up @@ -95,7 +95,7 @@ Accept wildcard characters: False
```
### -DnsNamePrefix
The DNS name prefix for the cluster.
The DNS name prefix for the cluster. The length must be <= 9 if users plan to add windows container.
```yaml
Type: System.String
Expand Down