Skip to content

Commit d653e2e

Browse files
authored
adding password validations for user provided password (#7)
1 parent a08cb44 commit d653e2e

File tree

4 files changed

+81
-3
lines changed

4 files changed

+81
-3
lines changed

src/DataBoxEdge/DataBoxEdge/Az.DataBoxEdge.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# RootModule = ''
1313

1414
# Version number of this module.
15-
ModuleVersion = '0.2.1'
15+
ModuleVersion = '0.1.1'
1616

1717
# Supported PSEditions
1818
CompatiblePSEditions = 'Core', 'Desktop'

src/DataBoxEdge/DataBoxEdge/Common/Cmdlets/Users/DataBoxEdgeUserNewCmdletBase.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
using System.Security;
2020
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
2121
using Microsoft.Azure.Management.EdgeGateway;
22+
using Microsoft.Azure.PowerShell.Cmdlets.DataBoxEdge.Common.Utils;
2223
using Microsoft.Azure.PowerShell.Cmdlets.DataBoxEdge.Models;
2324
using Microsoft.Rest.Azure;
2425
using Microsoft.WindowsAzure.Commands.Common;
@@ -108,11 +109,13 @@ private bool DoesResourceExists()
108109

109110
private PSResourceModel CreateResourceModel()
110111
{
112+
var password = this.Password.ConvertToString();
113+
PasswordUtility.ValidateUserPasswordPattern(nameof(this.Password), password);
111114
var encryptedSecret =
112115
DataBoxEdgeManagementClient.Devices.GetAsymmetricEncryptedSecret(
113116
this.DeviceName,
114117
this.ResourceGroupName,
115-
this.Password.ConvertToString(),
118+
password,
116119
this.GetKeyForEncryption()
117120
);
118121
return new PSResourceModel(

src/DataBoxEdge/DataBoxEdge/Common/Cmdlets/Users/DataBoxEdgeUserSetCmdletBase.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using System.Security;
1818
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
1919
using Microsoft.Azure.Management.EdgeGateway;
20+
using Microsoft.Azure.PowerShell.Cmdlets.DataBoxEdge.Common.Utils;
2021
using Microsoft.WindowsAzure.Commands.Utilities.Common;
2122
using Microsoft.WindowsAzure.Commands.Common;
2223
using PSResourceModel = Microsoft.Azure.PowerShell.Cmdlets.DataBoxEdge.Models.PSDataBoxEdgeUser;
@@ -87,11 +88,14 @@ private string GetKeyForEncryption()
8788

8889
private PSResourceModel SetResourceModel()
8990
{
91+
var password = this.Password.ConvertToString();
92+
PasswordUtility.ValidateUserPasswordPattern(nameof(this.Password), password);
93+
9094
var encryptedSecret =
9195
DataBoxEdgeManagementClient.Devices.GetAsymmetricEncryptedSecret(
9296
this.DeviceName,
9397
this.ResourceGroupName,
94-
this.Password.ConvertToString(),
98+
password,
9599
this.GetKeyForEncryption()
96100
);
97101

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using System;
2+
using System.Linq;
3+
using System.Management.Automation;
4+
using System.Text.RegularExpressions;
5+
using System.Security.Cryptography;
6+
7+
namespace Microsoft.Azure.PowerShell.Cmdlets.DataBoxEdge.Common.Utils
8+
{
9+
public class PasswordUtility
10+
{
11+
private static int PasswordMinLength = 8;
12+
private static int PasswordMaxLength = 15;
13+
14+
private const string OneCapital = @"^.*(?=[A-Z]+).*$";
15+
private const string OneNumber = @"^.*(?=[0-9]+).*$";
16+
17+
public static string UnAllowedChars { get; } = @"[^a-zA-Z0-9@#\-$%^!+=;:_()]";
18+
private const string AllowedSymbolsString = "[`@`,`#`,`-`,`$`,`%`,`^`,`!`,`+`,`=`,`;`,`:`,`_`,`(`,`)`]";
19+
20+
private static readonly Regex OneCapitalRegex = new Regex(
21+
OneCapital, RegexOptions.Singleline
22+
);
23+
private static readonly Regex OneNumberRegex = new Regex(
24+
OneNumber, RegexOptions.Singleline
25+
);
26+
27+
private static readonly Regex UnAllowedCharsRegex = new Regex(UnAllowedChars,
28+
RegexOptions.Singleline);
29+
30+
31+
32+
/// <summary>
33+
/// Validates password strength with set of rules for password provided by user
34+
/// </summary>
35+
/// <paramref name="password">Password provided by user</param>
36+
/// <returns>true if valid pattern is provided.</returns>
37+
public static bool ValidateUserPasswordPattern(string argumentName, string password)
38+
{
39+
string error = null;
40+
if (string.IsNullOrEmpty(password))
41+
{
42+
error = "Password cannot be empty";
43+
}
44+
else if (password.Length < PasswordMinLength)
45+
{
46+
error = "Minimum length of the password can not be less than " + PasswordMinLength + " chars";
47+
}
48+
else if (password.Length > PasswordMaxLength)
49+
{
50+
error = "Maximum length of the password can not be greater than " + PasswordMaxLength + " chars";
51+
}
52+
else if (!(OneNumberRegex.IsMatch(password) && OneCapitalRegex.IsMatch(password)))
53+
{
54+
error = "Should Contain at least 1 uppercase and 1 number";
55+
}
56+
else if (UnAllowedCharsRegex.IsMatch(password))
57+
{
58+
error = "Should not contain characters outside of the set [a-z, A-Z, 0-9] and symbols" + AllowedSymbolsString;
59+
}
60+
61+
if (string.IsNullOrEmpty(error))
62+
{
63+
return true;
64+
}
65+
else
66+
{
67+
throw new PSArgumentException(argumentName + " " + error);
68+
}
69+
}
70+
}
71+
}

0 commit comments

Comments
 (0)