Skip to content

Commit a5c337a

Browse files
authored
Refactoring (Azure#2937)
1 parent a598633 commit a5c337a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+898
-711
lines changed

src/PSRule.Rules.Azure/Common/JsonExtensions.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,7 @@ internal static void ReplaceProperty(this JObject o, string propertyName, int va
274274
internal static void RemoveProperty(this JObject o, string propertyName)
275275
{
276276
var p = o.Property(propertyName, StringComparison.OrdinalIgnoreCase);
277-
if (p != null)
278-
p.Remove();
277+
p?.Remove();
279278
}
280279

281280
/// <summary>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using Newtonsoft.Json;
5+
6+
namespace PSRule.Rules.Azure.Data
7+
{
8+
9+
/// <summary>
10+
/// An Azure Availability Zone mapping between location and zones.
11+
/// </summary>
12+
public sealed class AvailabilityZoneMapping
13+
{
14+
/// <summary>
15+
/// The location/ region where AZ is available.
16+
/// </summary>
17+
[JsonProperty(PropertyName = "l")]
18+
public string Location { get; set; }
19+
20+
/// <summary>
21+
/// The zone names available at the location.
22+
/// </summary>
23+
[JsonProperty(PropertyName = "z")]
24+
public string[] Zones { get; set; }
25+
}
26+
}

src/PSRule.Rules.Azure/Data/CloudEnvironment.cs

-53
Original file line numberDiff line numberDiff line change
@@ -129,57 +129,4 @@ public sealed class CloudEnvironmentAuthentication
129129
[JsonProperty(PropertyName = "identityProvider")]
130130
public string identityProvider { get; set; }
131131
}
132-
133-
/// <summary>
134-
/// Endpoints for Azure cloud environments.
135-
/// </summary>
136-
public sealed class CloudEnvironmentSuffixes
137-
{
138-
/// <summary>
139-
/// The suffix for ACR login.
140-
/// Defaults to <c>.azurecr.io</c>.
141-
/// </summary>
142-
[JsonProperty(PropertyName = "acrLoginServer")]
143-
public string acrLoginServer { get; set; }
144-
145-
/// <summary>
146-
/// Defaults to <c>azuredatalakeanalytics.net</c>.
147-
/// </summary>
148-
[JsonProperty(PropertyName = "azureDatalakeAnalyticsCatalogAndJob")]
149-
public string azureDatalakeAnalyticsCatalogAndJob { get; set; }
150-
151-
/// <summary>
152-
/// Defaults to <c>azuredatalakestore.net</c>.
153-
/// </summary>
154-
[JsonProperty(PropertyName = "azureDatalakeStoreFileSystem")]
155-
public string azureDatalakeStoreFileSystem { get; set; }
156-
157-
/// <summary>
158-
/// The suffix for Front Door endpoints.
159-
/// Defaults to <c>azurefd.net</c>.
160-
/// </summary>
161-
[JsonProperty(PropertyName = "azureFrontDoorEndpointSuffix")]
162-
public string azureFrontDoorEndpointSuffix { get; set; }
163-
164-
/// <summary>
165-
/// The suffix for Key Vaults.
166-
/// Defaults to <c>.vault.azure.net</c>.
167-
/// </summary>
168-
[JsonProperty(PropertyName = "keyvaultDns")]
169-
public string keyvaultDns { get; set; }
170-
171-
/// <summary>
172-
/// The suffix for Azure SQL Database logical servers.
173-
/// Defaults to <c>.database.windows.net</c>.
174-
/// </summary>
175-
[JsonProperty(PropertyName = "sqlServerHostname")]
176-
public string sqlServerHostname { get; set; }
177-
178-
/// <summary>
179-
/// The base suffix for Azure Storage services.
180-
/// Defaults to <c>core.windows.net</c>.
181-
/// </summary>
182-
[JsonProperty(PropertyName = "storage")]
183-
public string storage { get; set; }
184-
}
185132
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using Newtonsoft.Json;
5+
6+
namespace PSRule.Rules.Azure.Data
7+
{
8+
/// <summary>
9+
/// Endpoints for Azure cloud environments.
10+
/// </summary>
11+
public sealed class CloudEnvironmentSuffixes
12+
{
13+
/// <summary>
14+
/// The suffix for ACR login.
15+
/// Defaults to <c>.azurecr.io</c>.
16+
/// </summary>
17+
[JsonProperty(PropertyName = "acrLoginServer")]
18+
public string acrLoginServer { get; set; }
19+
20+
/// <summary>
21+
/// Defaults to <c>azuredatalakeanalytics.net</c>.
22+
/// </summary>
23+
[JsonProperty(PropertyName = "azureDatalakeAnalyticsCatalogAndJob")]
24+
public string azureDatalakeAnalyticsCatalogAndJob { get; set; }
25+
26+
/// <summary>
27+
/// Defaults to <c>azuredatalakestore.net</c>.
28+
/// </summary>
29+
[JsonProperty(PropertyName = "azureDatalakeStoreFileSystem")]
30+
public string azureDatalakeStoreFileSystem { get; set; }
31+
32+
/// <summary>
33+
/// The suffix for Front Door endpoints.
34+
/// Defaults to <c>azurefd.net</c>.
35+
/// </summary>
36+
[JsonProperty(PropertyName = "azureFrontDoorEndpointSuffix")]
37+
public string azureFrontDoorEndpointSuffix { get; set; }
38+
39+
/// <summary>
40+
/// The suffix for Key Vaults.
41+
/// Defaults to <c>.vault.azure.net</c>.
42+
/// </summary>
43+
[JsonProperty(PropertyName = "keyvaultDns")]
44+
public string keyvaultDns { get; set; }
45+
46+
/// <summary>
47+
/// The suffix for Azure SQL Database logical servers.
48+
/// Defaults to <c>.database.windows.net</c>.
49+
/// </summary>
50+
[JsonProperty(PropertyName = "sqlServerHostname")]
51+
public string sqlServerHostname { get; set; }
52+
53+
/// <summary>
54+
/// The base suffix for Azure Storage services.
55+
/// Defaults to <c>core.windows.net</c>.
56+
/// </summary>
57+
[JsonProperty(PropertyName = "storage")]
58+
public string storage { get; set; }
59+
}
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using Newtonsoft.Json;
7+
8+
namespace PSRule.Rules.Azure.Data
9+
{
10+
internal sealed class HashSetConverter : JsonConverter<HashSet<string>>
11+
{
12+
private readonly IEqualityComparer<string> _Comparer;
13+
14+
public HashSetConverter(IEqualityComparer<string> comparer)
15+
{
16+
_Comparer = comparer;
17+
}
18+
19+
public override HashSet<string> ReadJson(JsonReader reader, Type objectType, HashSet<string> existingValue, bool hasExistingValue, JsonSerializer serializer)
20+
{
21+
if (reader == null || serializer == null || reader.TokenType != JsonToken.StartArray)
22+
return null;
23+
24+
var d = new HashSet<string>(_Comparer);
25+
serializer.Populate(reader, d);
26+
return d;
27+
}
28+
29+
public override void WriteJson(JsonWriter writer, HashSet<string> value, JsonSerializer serializer)
30+
{
31+
throw new NotImplementedException();
32+
}
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
namespace PSRule.Rules.Azure.Data.Network
5+
{
6+
/// <summary>
7+
/// The result after evaluating a rule.
8+
/// </summary>
9+
public enum Access
10+
{
11+
/// <summary>
12+
/// The result is denies or allowed based on NSG defaults.
13+
/// A specific NSG rule has not been configured to allow or deny.
14+
/// </summary>
15+
Default = 0,
16+
17+
/// <summary>
18+
/// Access has been permitted.
19+
/// </summary>
20+
Allow = 1,
21+
22+
/// <summary>
23+
/// Access has been denied.
24+
/// </summary>
25+
Deny = 2
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
namespace PSRule.Rules.Azure.Data.Network
5+
{
6+
/// <summary>
7+
/// Evaluates NSG rules to determine resulting access.
8+
/// </summary>
9+
public interface INetworkSecurityGroupEvaluator
10+
{
11+
/// <summary>
12+
/// Determine the resulting outbound access after evaluating NSG rules.
13+
/// </summary>
14+
Access Outbound(string prefix, int port);
15+
}
16+
}

src/PSRule.Rules.Azure/Data/Network/NetworkSecurityGroupEvaluator.cs

-33
Original file line numberDiff line numberDiff line change
@@ -7,39 +7,6 @@
77

88
namespace PSRule.Rules.Azure.Data.Network
99
{
10-
/// <summary>
11-
/// Evaluates NSG rules to determine resulting access.
12-
/// </summary>
13-
public interface INetworkSecurityGroupEvaluator
14-
{
15-
/// <summary>
16-
/// Determine the resulting outbound access after evaluating NSG rules.
17-
/// </summary>
18-
Access Outbound(string prefix, int port);
19-
}
20-
21-
/// <summary>
22-
/// The result after evaluating a rule.
23-
/// </summary>
24-
public enum Access
25-
{
26-
/// <summary>
27-
/// The result is denies or allowed based on NSG defaults.
28-
/// A specific NSG rule has not been configured to allow or deny.
29-
/// </summary>
30-
Default = 0,
31-
32-
/// <summary>
33-
/// Access has been permitted.
34-
/// </summary>
35-
Allow = 1,
36-
37-
/// <summary>
38-
/// Access has been denied.
39-
/// </summary>
40-
Deny = 2
41-
}
42-
4310
/// <summary>
4411
/// A basic implementation of an evaluator for checking NSG rules.
4512
/// </summary>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using Newtonsoft.Json;
5+
6+
namespace PSRule.Rules.Azure.Data
7+
{
8+
internal sealed class PolicyIgnoreEntry
9+
{
10+
[JsonProperty("i")]
11+
public string[] PolicyDefinitionIds { get; set; }
12+
13+
[JsonProperty("r")]
14+
public PolicyIgnoreReason Reason { get; set; }
15+
16+
[JsonProperty("v", NullValueHandling = NullValueHandling.Ignore)]
17+
public string Value { get; set; }
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
namespace PSRule.Rules.Azure.Data
5+
{
6+
internal enum PolicyIgnoreReason
7+
{
8+
/// <summary>
9+
/// The policy is excluded because it was duplicated with an existing rule.
10+
/// </summary>
11+
Duplicate = 1,
12+
13+
/// <summary>
14+
/// The policy is excluded because it is not testable or not applicable for IaC.
15+
/// </summary>
16+
NotApplicable = 2,
17+
18+
/// <summary>
19+
/// An exclusion configured by the user.
20+
/// </summary>
21+
Configured = 3
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using System.Collections.Generic;
5+
6+
namespace PSRule.Rules.Azure.Data
7+
{
8+
internal sealed class PolicyIgnoreResult
9+
{
10+
public PolicyIgnoreReason Reason { get; set; }
11+
12+
public List<string> Value { get; set; }
13+
}
14+
}

src/PSRule.Rules.Azure/Data/PolicyIgnore.cs src/PSRule.Rules.Azure/Data/PolicyIgnoreResultConverter.cs

-37
Original file line numberDiff line numberDiff line change
@@ -7,43 +7,6 @@
77

88
namespace PSRule.Rules.Azure.Data
99
{
10-
internal sealed class PolicyIgnoreResult
11-
{
12-
public PolicyIgnoreReason Reason { get; set; }
13-
14-
public List<string> Value { get; set; }
15-
}
16-
17-
internal sealed class PolicyIgnoreEntry
18-
{
19-
[JsonProperty("i")]
20-
public string[] PolicyDefinitionIds { get; set; }
21-
22-
[JsonProperty("r")]
23-
public PolicyIgnoreReason Reason { get; set; }
24-
25-
[JsonProperty("v", NullValueHandling = NullValueHandling.Ignore)]
26-
public string Value { get; set; }
27-
}
28-
29-
internal enum PolicyIgnoreReason
30-
{
31-
/// <summary>
32-
/// The policy is excluded because it was duplicated with an existing rule.
33-
/// </summary>
34-
Duplicate = 1,
35-
36-
/// <summary>
37-
/// The policy is excluded because it is not testable or not applicable for IaC.
38-
/// </summary>
39-
NotApplicable = 2,
40-
41-
/// <summary>
42-
/// An exclusion configured by the user.
43-
/// </summary>
44-
Configured = 3
45-
}
46-
4710
internal sealed class PolicyIgnoreResultConverter : JsonConverter
4811
{
4912
public override bool CanConvert(Type objectType)

src/PSRule.Rules.Azure/Data/ProviderData.cs

-13
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,6 @@
99

1010
namespace PSRule.Rules.Azure.Data
1111
{
12-
/// <summary>
13-
/// Defines extension methods for Azure resource provider data.
14-
/// </summary>
15-
internal static class ProviderDataExtensions
16-
{
17-
public static bool TryResourceType(this ProviderData data, string resourceType, out ResourceProviderType type)
18-
{
19-
type = null;
20-
return ResourceHelper.TryResourceProviderFromType(resourceType, out var provider, out var typeName) &&
21-
data.TryResourceType(provider, typeName, out type);
22-
}
23-
}
24-
2512
/// <summary>
2613
/// Defines a datastore of Azure resource provider data.
2714
/// </summary>

0 commit comments

Comments
 (0)