Skip to content
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

Bump AzureIPNetworks from 1.6.0 to 1.7.0 #203

Merged
merged 2 commits into from
Jan 24, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ namespace Tingle.AspNetCore.Authorization;
/// An <see cref="IAuthorizationRequirement"/> that contains IP networks
/// </summary>
/// <param name="networks">the networks allowed</param>
#if NET8_0_OR_GREATER
public sealed class ApprovedIPNetworkRequirement(IList<IPNetwork> networks) : IAuthorizationRequirement
#else
public sealed class ApprovedIPNetworkRequirement(IList<IPNetwork2> networks) : IAuthorizationRequirement
#endif
{
/// <summary>
/// Checks is an instance of <see cref="IPAddress"/> is approved
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@ public static class AuthorizationPolicyBuilderExtensions
/// </summary>
/// <param name="builder">The instance to add to</param>
/// <param name="networks">The allowed networks</param>
public static AuthorizationPolicyBuilder RequireApprovedNetworks(this AuthorizationPolicyBuilder builder,
IList<IPNetwork> networks)
#if NET8_0_OR_GREATER
public static AuthorizationPolicyBuilder RequireApprovedNetworks(this AuthorizationPolicyBuilder builder, IList<IPNetwork> networks)
#else
public static AuthorizationPolicyBuilder RequireApprovedNetworks(this AuthorizationPolicyBuilder builder, IList<IPNetwork2> networks)
#endif
{
// if there are no networks just return
if (!networks.Any()) return builder;

// reduce the networks where possible (referred to as supernetting)
#if !NET8_0_OR_GREATER
networks = IPNetwork.Supernet([.. networks]);
networks = IPNetwork2.Supernet([.. networks]);
#endif

// add the requirement
Expand All @@ -41,10 +44,13 @@ public static AuthorizationPolicyBuilder RequireApprovedNetworks(this Authorizat
/// </summary>
/// <param name="builder">The instance to add to</param>
/// <param name="networks">The allowed networks</param>
public static AuthorizationPolicyBuilder RequireApprovedNetworks(this AuthorizationPolicyBuilder builder,
params string[] networks)
public static AuthorizationPolicyBuilder RequireApprovedNetworks(this AuthorizationPolicyBuilder builder, params string[] networks)
{
#if NET8_0_OR_GREATER
var parsed = networks.Select(a => IPNetwork.Parse(a)).ToList();
#else
var parsed = networks.Select(a => IPNetwork2.Parse(a)).ToList();
#endif
return builder.RequireApprovedNetworks(parsed);
}

Expand All @@ -55,8 +61,11 @@ public static AuthorizationPolicyBuilder RequireApprovedNetworks(this Authorizat
/// </summary>
/// <param name="builder">The instance to add to</param>
/// <param name="networks">The allowed networks</param>
public static AuthorizationPolicyBuilder RequireApprovedNetworks(this AuthorizationPolicyBuilder builder,
params IPNetwork[] networks)
#if NET8_0_OR_GREATER
public static AuthorizationPolicyBuilder RequireApprovedNetworks(this AuthorizationPolicyBuilder builder, params IPNetwork[] networks)
#else
public static AuthorizationPolicyBuilder RequireApprovedNetworks(this AuthorizationPolicyBuilder builder, params IPNetwork2[] networks)
#endif
{
return builder.RequireApprovedNetworks(networks.ToList());
}
Expand Down Expand Up @@ -122,10 +131,13 @@ public static AuthorizationPolicyBuilder RequireAzureIPNetworks(this Authorizati
/// A list of Fully Qualified Domain Names.
/// Each of them will be resolved to list of IP addresses using <see cref="Dns.GetHostAddresses(string)"/>
/// </param>
public static AuthorizationPolicyBuilder RequireNetworkFromDns(this AuthorizationPolicyBuilder builder,
params string[] fqdns)
public static AuthorizationPolicyBuilder RequireNetworkFromDns(this AuthorizationPolicyBuilder builder, params string[] fqdns)
{
#if NET8_0_OR_GREATER
var networks = new List<IPNetwork>();
#else
var networks = new List<IPNetwork2>();
#endif

// work on each FQDN
foreach (var f in fqdns)
Expand All @@ -136,7 +148,11 @@ public static AuthorizationPolicyBuilder RequireNetworkFromDns(this Authorizatio
var ips = Dns.GetHostAddresses(f);

// parse the IP addresses into IP networks
#if NET8_0_OR_GREATER
var rawNetworks = ips?.Select(ip => new IPNetwork(ip, (byte)(ip.AddressFamily is AddressFamily.InterNetwork ? 32 : 128)));
#else
var rawNetworks = ips?.Select(ip => new IPNetwork2(ip, (byte)(ip.AddressFamily is AddressFamily.InterNetwork ? 32 : 128)));
#endif

// add networks into the list if there are any
if (rawNetworks?.Any() ?? false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="AzureIPNetworks" Version="1.6.0" />
<PackageReference Include="AzureIPNetworks" Version="1.7.0" />
</ItemGroup>

</Project>