-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding auth scenario; needs cleaning
- Loading branch information
1 parent
5210b62
commit ee06bc4
Showing
15 changed files
with
304 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
Services/DataX.ServiceHost/DataX.ServiceHost.AspNetCore/Authorization/DataXAuthConstants.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using DataX.Utilities.Web; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace DataX.ServiceHost.AspNetCore.Authorization | ||
{ | ||
public static class DataXAuthConstants | ||
{ | ||
public const string PolicyPrefix = "DataXAuth_"; | ||
|
||
public static string WriterPolicyName { get; } = PolicyPrefix + RolesCheck.WriterRoleName; | ||
|
||
public static string ReaderPolicyName { get; } = PolicyPrefix + RolesCheck.ReaderRoleName; | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...ices/DataX.ServiceHost/DataX.ServiceHost.AspNetCore/Authorization/DataXAuthRequirement.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using DataX.ServiceHost.Settings; | ||
using DataX.Utilities.Web; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Authorization.Infrastructure; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace DataX.ServiceHost.AspNetCore.Authorization | ||
{ | ||
/// <summary> | ||
/// A for assertion requirements. We're extending this so that we can easily identify the DataX requirement instance | ||
/// when adding in policy requirements. This lets us prevent duplication of requirements and handlers. | ||
/// This is made internal as using it outside of this context may cause configuration issues if used improperly. | ||
/// </summary> | ||
internal abstract class DataXAuthRequirement : IAuthorizationHandler, IAuthorizationRequirement | ||
{ | ||
public DataXSettings Settings { get; set; } | ||
|
||
public DataXAuthRequirement() { } | ||
|
||
public DataXAuthRequirement(DataXSettings settings) | ||
{ | ||
Settings = settings; | ||
} | ||
|
||
public Task HandleAsync(AuthorizationHandlerContext context) | ||
{ | ||
if(IsAuthorized(context, Settings)) | ||
{ | ||
context.Succeed(this); | ||
} | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
protected abstract bool IsAuthorized(AuthorizationHandlerContext context, DataXSettings settings); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...s/DataX.ServiceHost/DataX.ServiceHost.AspNetCore/Authorization/DataXAuthorizeAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using DataX.Utilities.Web; | ||
using Microsoft.AspNetCore.Authorization; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace DataX.ServiceHost.AspNetCore.Authorization | ||
{ | ||
public abstract class DataXAuthorizeAttribute : AuthorizeAttribute | ||
{ | ||
public DataXAuthorizeAttribute() | ||
{ | ||
Policy = DataXAuthConstants.PolicyPrefix; | ||
} | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
Services/DataX.ServiceHost/DataX.ServiceHost.AspNetCore/Authorization/DataXPolicyBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
namespace DataX.ServiceHost.AspNetCore.Authorization | ||
{ | ||
using DataX.ServiceHost.Settings; | ||
using Microsoft.AspNetCore.Authorization; | ||
using System; | ||
using System.Linq; | ||
|
||
// This class is meant to simplify the syntax for adding requirements for policies | ||
internal class DataXPolicyBuilder | ||
{ | ||
private readonly AuthorizationOptions _options; | ||
private readonly DataXSettings _settings; | ||
private readonly Action<AuthorizationPolicyBuilder> _configurePolicy; | ||
|
||
public DataXPolicyBuilder( | ||
AuthorizationOptions options, | ||
DataXSettings settings, | ||
Action<AuthorizationPolicyBuilder> configurePolicy) | ||
{ | ||
_options = options; | ||
_settings = settings; | ||
_configurePolicy = configurePolicy; | ||
} | ||
|
||
public DataXPolicyBuilder AddPolicy<TRequirement>(string name) | ||
where TRequirement : DataXAuthRequirement, new() | ||
{ | ||
_options.AddPolicy(name, DataXPolicy<TRequirement>); | ||
|
||
return this; | ||
} | ||
|
||
private void DataXPolicy<TDataXRequirement>(AuthorizationPolicyBuilder policy) | ||
where TDataXRequirement : DataXAuthRequirement, new() | ||
{ | ||
AddDataXRequirements<TDataXRequirement>(policy); | ||
} | ||
|
||
/// <summary> | ||
/// Adds the basic DataX auth policy to the builder | ||
/// </summary> | ||
private AuthorizationPolicyBuilder AddDataXRequirements<TDataXRequirement>(AuthorizationPolicyBuilder policy) | ||
where TDataXRequirement : DataXAuthRequirement, new() | ||
{ | ||
// We don't want to add the same requirement in again. | ||
// If it does exist and the settings changed, then we want to make sure the new settings are used | ||
RemoveDataXRequirements(policy); | ||
|
||
var requirement = new TDataXRequirement() | ||
{ | ||
Settings = _settings | ||
}; | ||
|
||
policy.RequireAuthenticatedUser(); | ||
policy.AddRequirements(requirement); | ||
_configurePolicy?.Invoke(policy); | ||
|
||
return policy; | ||
} | ||
|
||
/// <summary> | ||
/// Removes the DataXRequirements set in the policy builder if they exist. | ||
/// </summary> | ||
private static AuthorizationPolicyBuilder RemoveDataXRequirements(AuthorizationPolicyBuilder policy) | ||
{ | ||
var requirements = policy.Requirements.Where(req => req is DataXAuthRequirement); | ||
|
||
foreach (var req in requirements) | ||
{ | ||
policy.Requirements.Remove(req); | ||
} | ||
|
||
return policy; | ||
} | ||
} | ||
} |
22 changes: 0 additions & 22 deletions
22
...ceHost/DataX.ServiceHost.AspNetCore/Authorization/DataXRoleRequirementAttribute - Copy.cs
This file was deleted.
Oops, something went wrong.
22 changes: 0 additions & 22 deletions
22
...X.ServiceHost/DataX.ServiceHost.AspNetCore/Authorization/DataXRoleRequirementAttribute.cs
This file was deleted.
Oops, something went wrong.
44 changes: 44 additions & 0 deletions
44
...ost/DataX.ServiceHost.AspNetCore/Authorization/Extensions/DataXAuthorizationExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using DataX.ServiceHost.Settings; | ||
using DataX.Utilities.Web; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Linq; | ||
using DataX.ServiceHost.AspNetCore.Authorization.Requirements; | ||
|
||
namespace DataX.ServiceHost.AspNetCore.Authorization.Extensions | ||
{ | ||
public static class DataXAuthorizationExtensions | ||
{ | ||
public static IServiceCollection AddDataXAuthorization(this IServiceCollection services) | ||
{ | ||
return services.AddDataXAuthorization(null); | ||
} | ||
|
||
public static IServiceCollection AddDataXAuthorization(this IServiceCollection services, Action<AuthorizationPolicyBuilder> configurePolicy) | ||
{ | ||
var settings = services.BuildServiceProvider().GetService<DataXSettings>(); | ||
|
||
// EnableOneBox scenario as it requires the least configuration and we can't assume cloud connection settings | ||
if (settings == null) | ||
{ | ||
settings = new DataXSettings() | ||
{ | ||
EnableOneBox = true, | ||
LocalRoot = "", | ||
MetricsHttpEndpoint = "http://localhost:2020/", | ||
SparkHome = "", | ||
}; | ||
} | ||
|
||
return services.AddAuthorization(options => | ||
{ | ||
new DataXPolicyBuilder(options, settings, configurePolicy) | ||
.AddPolicy<DataXWriterRequirement>(DataXAuthConstants.WriterPolicyName) | ||
.AddPolicy<DataXReaderRequirement>(DataXAuthConstants.ReaderPolicyName); | ||
}); | ||
} | ||
} | ||
} |
17 changes: 0 additions & 17 deletions
17
...ces/DataX.ServiceHost/DataX.ServiceHost.AspNetCore/Authorization/LocalCheckRequirement.cs
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.