-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Add cache for retrieved RBAC claims #25698
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -13,7 +13,7 @@ | |
| using Microsoft.AspNetCore.Http; | ||
| using Microsoft.AspNetCore.Routing; | ||
| using Microsoft.AspNetCore.TestHost; | ||
| using Microsoft.AspNetCore.Testing; | ||
| using Microsoft.Extensions.Caching.Memory; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Hosting; | ||
| using Microsoft.Net.Http.Headers; | ||
|
|
@@ -208,6 +208,28 @@ public async Task AuthHeaderAfterNtlmCompleted_ReAuthenticates(bool persist) | |
| await NtlmStage1And2Auth(server, testConnection); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task RBACClaimsRetrievedFromCacheAfterKerberosCompleted() | ||
| { | ||
| var claimsCache = new MemoryCache(new MemoryCacheOptions()); | ||
| claimsCache.Set("name", new string[] { "CN=Domain Admins,CN=Users,DC=domain,DC=net" }); | ||
| NegotiateOptions negotiateOptions = null; | ||
| using var host = await CreateHostAsync(options => | ||
| { | ||
| options.EnableLdap(ldapSettings => | ||
| { | ||
| ldapSettings.Domain = "domain.NET"; | ||
| ldapSettings.ClaimsCache = claimsCache; | ||
| ldapSettings.EnableLdapClaimResolution = false; // This disables binding to the LDAP connection on startup | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As I expected, this is really hacky but it works. |
||
| }); | ||
| negotiateOptions = options; | ||
| }); | ||
| var server = host.GetTestServer(); | ||
| var testConnection = new TestConnection(); | ||
| negotiateOptions.EnableLdap(_ => { }); // Forcefully re-enable ldap claims resolution to trigger RBAC claims retrieval from cache | ||
| await AuthenticateAndRetrieveRBACClaims(server, testConnection); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData(false)] | ||
| [InlineData(true)] | ||
|
|
@@ -304,6 +326,12 @@ public async Task OtherError_Throws() | |
| var ex = await Assert.ThrowsAsync<Exception>(() => SendAsync(server, "/404", testConnection, "Negotiate OtherError")); | ||
| Assert.Equal("A test other error occurred", ex.Message); | ||
| } | ||
| private static async Task AuthenticateAndRetrieveRBACClaims(TestServer server, TestConnection testConnection) | ||
| { | ||
| var result = await SendAsync(server, "/AuthenticateAndRetrieveRBACClaims", testConnection, "Negotiate ClientKerberosBlob"); | ||
| Assert.Equal(StatusCodes.Status200OK, result.Response.StatusCode); | ||
| Assert.Equal("Negotiate ServerKerberosBlob", result.Response.Headers[HeaderNames.WWWAuthenticate]); | ||
| } | ||
|
|
||
| // Single Stage | ||
| private static async Task KerberosAuth(TestServer server, TestConnection testConnection) | ||
|
|
@@ -408,6 +436,24 @@ private static void ConfigureEndpoints(IEndpointRouteBuilder builder) | |
| await context.Response.WriteAsync(name); | ||
| }); | ||
|
|
||
| builder.Map("/AuthenticateAndRetrieveRBACClaims", async context => | ||
| { | ||
| if (!context.User.Identity.IsAuthenticated) | ||
| { | ||
| await context.ChallengeAsync(); | ||
| return; | ||
| } | ||
|
|
||
| Assert.Equal("HTTP/1.1", context.Request.Protocol); // Not HTTP/2 | ||
| var name = context.User.Identity.Name; | ||
| Assert.False(string.IsNullOrEmpty(name), "name"); | ||
| Assert.Contains( | ||
| context.User.Claims, | ||
| claim => claim.Type == "http://schemas.microsoft.com/ws/2008/06/identity/claims/role" | ||
| && claim.Value == "CN=Domain Admins,CN=Users,DC=domain,DC=net"); | ||
| await context.Response.WriteAsync(name); | ||
| }); | ||
|
|
||
| builder.Map("/AlreadyAuthenticated", async context => | ||
| { | ||
| Assert.Equal("HTTP/1.1", context.Request.Protocol); // Not HTTP/2 | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is different from the certificate validation cache in that it's not stored in DI. It might be a bit odd to have the cache in LdapSettings but I'm not sure if there's any value in putting this cache in DI.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Main value of putting it in DI is if you want to make it easy for them to plug in their own cache implementation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While that's true, I'm not sure we'd want that kind of flexibility here at the moment. I don't see it as particularly useful in this scenario.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's fine, I was just pointing out what advantages putting it in DI has