-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix GetRoleMappings when multiple distinguished names returned (#5271)
* Fix deserialisation of multiple dns * Support serialisation
- Loading branch information
1 parent
b2a0705
commit c1316d3
Showing
6 changed files
with
156 additions
and
0 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
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
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,66 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Elastic.Elasticsearch.Xunit.XunitPlumbing; | ||
using Elasticsearch.Net; | ||
using FluentAssertions; | ||
using Nest; | ||
|
||
namespace Tests.Reproduce | ||
{ | ||
public class GitHubIssue5270 | ||
{ | ||
private static readonly byte[] ResponseBytes = Encoding.UTF8.GetBytes(@"{ | ||
""test admin role mapping"" : { | ||
""enabled"" : true, | ||
""roles"" : [ | ||
""apm_user"" | ||
], | ||
""rules"" : { | ||
""any"" : [ | ||
{ | ||
""field"" : { | ||
""dn"" : [ | ||
""CN=Bloggs Joe abcdef01,OU=Users,OU=_Central,OU=S1000,OU=SG001,DC=ad001,DC=example,DC=net"", | ||
""cn=bloggs joe abcdef02,ou=usersfunctional,ou=_central,ou=accadm,OU=SG001,DC=ad001,DC=example,DC=net"" | ||
] | ||
} | ||
} | ||
] | ||
}, | ||
""metadata"" : { } | ||
} | ||
}"); | ||
|
||
[U] | ||
public async Task GetRoleMappings() | ||
{ | ||
var pool = new SingleNodeConnectionPool(new Uri($"http://localhost:9200")); | ||
var settings = new ConnectionSettings(pool, new InMemoryConnection(ResponseBytes)); | ||
var client = new ElasticClient(settings); | ||
|
||
// ReSharper disable once MethodHasAsyncOverload | ||
var roleResponse = client.Security.GetRoleMapping(); | ||
roleResponse.RoleMappings.Count.Should().Be(1); | ||
Assert(roleResponse); | ||
|
||
roleResponse = await client.Security.GetRoleMappingAsync(); | ||
roleResponse.RoleMappings.Count.Should().Be(1); | ||
Assert(roleResponse); | ||
} | ||
|
||
private static void Assert(GetRoleMappingResponse roleResponse) => | ||
roleResponse.RoleMappings["test admin role mapping"] | ||
.Rules.Should() | ||
.BeAssignableTo<AnyRoleMappingRule>() | ||
.Subject.Any.First() | ||
.Should() | ||
.BeAssignableTo<FieldRoleMappingRule>() | ||
.Subject.Field.Should() | ||
.BeAssignableTo<DistinguishedNameRule>() | ||
.Subject["dn"].Should().BeAssignableTo<IEnumerable<string>>() | ||
.Subject.Count().Should().Be(2); | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
tests/Tests/XPack/Security/RoleMapping/DistinguishedNamesRoleMappingsTests.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,68 @@ | ||
// Licensed to Elasticsearch B.V under one or more agreements. | ||
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. | ||
// See the LICENSE file in the project root for more information | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using Elastic.Elasticsearch.Xunit.XunitPlumbing; | ||
using Elasticsearch.Net; | ||
using Nest; | ||
using Tests.Core.ManagedElasticsearch.Clusters; | ||
using Tests.Framework.EndpointTests; | ||
using Tests.Framework.EndpointTests.TestState; | ||
|
||
namespace Tests.XPack.Security.RoleMapping | ||
{ | ||
[SkipVersion("<5.5.0", "Does not exist in earlier versions")] | ||
public class DistinguishedNamesRoleMappingsTests | ||
: ApiTestBase<XPackCluster, PutRoleMappingResponse, IPutRoleMappingRequest, PutRoleMappingDescriptor, PutRoleMappingRequest> | ||
{ | ||
public DistinguishedNamesRoleMappingsTests(XPackCluster cluster, EndpointUsage usage) : base(cluster, usage) { } | ||
|
||
protected override HttpMethod HttpMethod => HttpMethod.PUT; | ||
protected override string UrlPath => "/_security/role_mapping/name"; | ||
protected override bool SupportsDeserialization => false; | ||
|
||
protected override object ExpectJson => new | ||
{ | ||
enabled = true, | ||
roles = new[] { "user_role" }, | ||
rules = new | ||
{ | ||
any = new object[] | ||
{ | ||
new | ||
{ | ||
field = new { | ||
dn = new [] { | ||
"a", | ||
"b" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
|
||
protected override PutRoleMappingRequest Initializer { get; } = new("name") | ||
{ | ||
Enabled = true, | ||
Roles = new List<string> { "user_role" }, | ||
Rules = new AnyRoleMappingRule(new DistinguishedNameRule(new List<string> | ||
{ | ||
"a", "b" | ||
})) | ||
}; | ||
|
||
protected override PutRoleMappingDescriptor NewDescriptor() => new("name"); | ||
|
||
protected override Func<PutRoleMappingDescriptor, IPutRoleMappingRequest> Fluent => | ||
d => d.Enabled().Roles("user_role").Rules(r => r.Any(a => a.DistinguishedName(new List<string> { "a", "b" }))); | ||
|
||
protected override LazyResponses ClientUsage() => Calls( | ||
(client, f) => client.Security.PutRoleMapping("name", f), | ||
(client, f) => client.Security.PutRoleMappingAsync("name", f), | ||
(client, r) => client.Security.PutRoleMapping(r), | ||
(client, r) => client.Security.PutRoleMappingAsync(r)); | ||
} | ||
} |