Skip to content

Commit e96b6ea

Browse files
authored
Add support to retrieve all API keys if user has privilege. (#4272)
Add support to retrieve all API keys if user has privilege. Allow steps to be excluded based on version range.
1 parent eaea711 commit e96b6ea

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

src/Tests/Tests/Framework/EndpointTests/CoordinatedIntegrationTestBase.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public abstract class CoordinatedIntegrationTestBase<TCluster>
2525
protected async Task Assert<TResponse>(string name, Action<TResponse> assert)
2626
where TResponse : class, IResponse
2727
{
28+
if (_coordinatedUsage.Skips(name)) return;
29+
2830
var lazyResponses = await ExecuteOnceInOrderUntil(name);
2931
if (lazyResponses == null) throw new Exception($"{name} is defined but it yields no LazyResponses object");
3032

src/Tests/Tests/Framework/EndpointTests/TestState/CoordinatedUsage.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
using System.Collections.ObjectModel;
44
using System.Threading.Tasks;
55
using Nest;
6+
using Tests.Configuration;
7+
using Tests.Core.Extensions;
68
using Tests.Core.ManagedElasticsearch.Clusters;
79
using Tests.Framework.Extensions;
810

@@ -39,13 +41,27 @@ public CoordinatedUsage(INestTestCluster cluster, EndpointUsage usage, string pr
3941
private readonly Dictionary<ClientMethod, string> _values;
4042
public IReadOnlyDictionary<ClientMethod, string> MethodIsolatedValues => _values;
4143

44+
private readonly Dictionary<string, string> _callsNotInRange = new Dictionary<string, string>();
45+
46+
public bool Skips(string name) => _callsNotInRange.ContainsKey(name);
47+
4248
protected override string GetKeyForItem(LazyResponses item) => item.Name;
4349

4450
public void Add(string name, Func<CoordinatedUsage, Func<string, LazyResponses>> create)
4551
{
4652
var responses = create(this)(name);
4753
Add(responses);
4854
}
55+
public void Add(string name, string versionRange, Func<CoordinatedUsage, Func<string, LazyResponses>> create)
56+
{
57+
if (!TestConfiguration.Instance.InRange(versionRange))
58+
{
59+
_callsNotInRange.Add(name, versionRange);
60+
return;
61+
}
62+
var responses = create(this)(name);
63+
Add(responses);
64+
}
4965

5066
protected static string RandomString() => Guid.NewGuid().ToString("N").Substring(0, 8);
5167

src/Tests/Tests/XPack/Security/ApiKey/SecurityApiKeyTests.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class SecurityApiKeyTests : CoordinatedIntegrationTestBase<XPackCluster>
1717
private const string CreateApiKeyWithRolesStep = nameof(CreateApiKeyWithRolesStep);
1818
private const string CreateApiKeyWithNoRolesStep = nameof(CreateApiKeyWithNoRolesStep);
1919
private const string GetApiKeyStep = nameof(GetApiKeyStep);
20+
private const string GetAllApiKeysStep = nameof(GetAllApiKeysStep);
2021
private const string InvalidateApiKeyStep = nameof(InvalidateApiKeyStep);
2122

2223
public SecurityApiKeyTests(XPackCluster cluster, EndpointUsage usage) : base(new CoordinatedUsage(cluster, usage)
@@ -120,6 +121,26 @@ public SecurityApiKeyTests(XPackCluster cluster, EndpointUsage usage) : base(new
120121
(v, c, r) => c.Security.CreateApiKeyAsync(r)
121122
)
122123
},
124+
{
125+
// This was fixed in 7.5.0
126+
GetAllApiKeysStep, ">=7.5.0", u =>
127+
u.Calls<GetApiKeyDescriptor, GetApiKeyRequest, IGetApiKeyRequest, GetApiKeyResponse>(
128+
v => new GetApiKeyRequest
129+
{
130+
RequestConfiguration = new RequestConfiguration
131+
{
132+
BasicAuthenticationCredentials = new BasicAuthenticationCredentials($"user-{v}", "password")
133+
}
134+
},
135+
(v, d) => d
136+
.RequestConfiguration(r => r.BasicAuthentication($"user-{v}", "password"))
137+
,
138+
(v, c, f) => c.Security.GetApiKey(f),
139+
(v, c, f) => c.Security.GetApiKeyAsync(f),
140+
(v, c, r) => c.Security.GetApiKey(r),
141+
(v, c, r) => c.Security.GetApiKeyAsync(r)
142+
)
143+
},
123144
{
124145
GetApiKeyStep, u =>
125146
u.Calls<GetApiKeyDescriptor, GetApiKeyRequest, IGetApiKeyRequest, GetApiKeyResponse>(
@@ -173,6 +194,11 @@ [I] public async Task SecurityCreateApiKeyResponse() => await Assert<CreateApiKe
173194
r.ApiKey.Should().NotBeNullOrEmpty();
174195
});
175196

197+
[I] public async Task SecurityGetAllApiKeysResponse() => await Assert<GetApiKeyResponse>(GetAllApiKeysStep, r =>
198+
{
199+
r.IsValid.Should().BeTrue();
200+
});
201+
176202
[I] public async Task SecurityGetApiKeyResponse() => await Assert<GetApiKeyResponse>(GetApiKeyStep, r =>
177203
{
178204
r.IsValid.Should().BeTrue();

0 commit comments

Comments
 (0)