Skip to content

Add support to retrieve all API keys if user has privilege. #4272

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 2 commits into from
Dec 18, 2019
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 @@ -25,6 +25,8 @@ public abstract class CoordinatedIntegrationTestBase<TCluster>
protected async Task Assert<TResponse>(string name, Action<TResponse> assert)
where TResponse : class, IResponse
{
if (_coordinatedUsage.Skips(name)) return;

var lazyResponses = await ExecuteOnceInOrderUntil(name);
if (lazyResponses == null) throw new Exception($"{name} is defined but it yields no LazyResponses object");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using Nest;
using Tests.Configuration;
using Tests.Core.Extensions;
using Tests.Core.ManagedElasticsearch.Clusters;
using Tests.Framework.Extensions;

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

private readonly Dictionary<string, string> _callsNotInRange = new Dictionary<string, string>();

public bool Skips(string name) => _callsNotInRange.ContainsKey(name);

protected override string GetKeyForItem(LazyResponses item) => item.Name;

public void Add(string name, Func<CoordinatedUsage, Func<string, LazyResponses>> create)
{
var responses = create(this)(name);
Add(responses);
}
public void Add(string name, string versionRange, Func<CoordinatedUsage, Func<string, LazyResponses>> create)
{
if (!TestConfiguration.Instance.InRange(versionRange))
{
_callsNotInRange.Add(name, versionRange);
return;
}
var responses = create(this)(name);
Add(responses);
}

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

Expand Down
26 changes: 26 additions & 0 deletions src/Tests/Tests/XPack/Security/ApiKey/SecurityApiKeyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class SecurityApiKeyTests : CoordinatedIntegrationTestBase<XPackCluster>
private const string CreateApiKeyWithRolesStep = nameof(CreateApiKeyWithRolesStep);
private const string CreateApiKeyWithNoRolesStep = nameof(CreateApiKeyWithNoRolesStep);
private const string GetApiKeyStep = nameof(GetApiKeyStep);
private const string GetAllApiKeysStep = nameof(GetAllApiKeysStep);
private const string InvalidateApiKeyStep = nameof(InvalidateApiKeyStep);

public SecurityApiKeyTests(XPackCluster cluster, EndpointUsage usage) : base(new CoordinatedUsage(cluster, usage)
Expand Down Expand Up @@ -120,6 +121,26 @@ public SecurityApiKeyTests(XPackCluster cluster, EndpointUsage usage) : base(new
(v, c, r) => c.Security.CreateApiKeyAsync(r)
)
},
{
// This was fixed in 7.5.0
GetAllApiKeysStep, ">=7.5.0", u =>
u.Calls<GetApiKeyDescriptor, GetApiKeyRequest, IGetApiKeyRequest, GetApiKeyResponse>(
v => new GetApiKeyRequest
{
RequestConfiguration = new RequestConfiguration
{
BasicAuthenticationCredentials = new BasicAuthenticationCredentials($"user-{v}", "password")
}
},
(v, d) => d
.RequestConfiguration(r => r.BasicAuthentication($"user-{v}", "password"))
,
(v, c, f) => c.Security.GetApiKey(f),
(v, c, f) => c.Security.GetApiKeyAsync(f),
(v, c, r) => c.Security.GetApiKey(r),
(v, c, r) => c.Security.GetApiKeyAsync(r)
)
},
{
GetApiKeyStep, u =>
u.Calls<GetApiKeyDescriptor, GetApiKeyRequest, IGetApiKeyRequest, GetApiKeyResponse>(
Expand Down Expand Up @@ -173,6 +194,11 @@ [I] public async Task SecurityCreateApiKeyResponse() => await Assert<CreateApiKe
r.ApiKey.Should().NotBeNullOrEmpty();
});

[I] public async Task SecurityGetAllApiKeysResponse() => await Assert<GetApiKeyResponse>(GetAllApiKeysStep, r =>
{
r.IsValid.Should().BeTrue();
});

[I] public async Task SecurityGetApiKeyResponse() => await Assert<GetApiKeyResponse>(GetApiKeyStep, r =>
{
r.IsValid.Should().BeTrue();
Expand Down