Skip to content

Commit f04c251

Browse files
committed
add validation to parameter
1 parent c3014a3 commit f04c251

File tree

1 file changed

+56
-2
lines changed

1 file changed

+56
-2
lines changed

src/Network/Network/Models/AzureFirewall/PSAzureFirewallThreatIntelWhitelist.cs

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,64 @@
1515

1616
namespace Microsoft.Azure.Commands.Network.Models
1717
{
18+
using System;
19+
using System.Management.Automation;
20+
using System.Net;
21+
using System.Text.RegularExpressions;
22+
1823
public class PSAzureFirewallThreatIntelWhitelist
1924
{
20-
public string[] FQDNs { get; set; }
25+
private string[] fqdns;
26+
27+
private string[] ipAddresses;
28+
29+
public string[] FQDNs {
30+
get { return this.fqdns; }
31+
set
32+
{
33+
fqdns = value;
34+
ValidateFqdns();
35+
}
36+
}
37+
38+
public string[] IpAddresses
39+
{
40+
get { return this.ipAddresses; }
41+
set
42+
{
43+
ipAddresses = value;
44+
ValidateIpAddresses();
45+
}
46+
}
47+
48+
private const string SecureGatewayThreatIntelFqdnRegex = "^\\*?([a-zA-Z0-9\\-\\.]?[a-zA-Z0-9])*$";
49+
50+
private void ValidateFqdns()
51+
{
52+
if (fqdns == null)
53+
return;
54+
foreach (var fqdn in fqdns)
55+
{
56+
var matchingRegEx = new Regex(SecureGatewayThreatIntelFqdnRegex);
57+
if (!matchingRegEx.IsMatch(fqdn))
58+
{
59+
throw new PSArgumentException(String.Format("\'{0}\' is not a valid threat intel whitelist FQDN", fqdn));
60+
}
61+
}
62+
}
2163

22-
public string[] IpAddresses { get; set; }
64+
private void ValidateIpAddresses()
65+
{
66+
if (IpAddresses == null)
67+
return;
68+
foreach (var ip in IpAddresses)
69+
{
70+
IPAddress ipVal;
71+
if (!IPAddress.TryParse(ip, out ipVal) || ipVal.AddressFamily != System.Net.Sockets.AddressFamily.InterNetwork)
72+
{
73+
throw new PSArgumentException(String.Format("\'{0}\' is not a valid threat intel whitelist Ip Address", ip));
74+
}
75+
}
76+
}
2377
}
2478
}

0 commit comments

Comments
 (0)