Skip to content
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

Support clear api key cache #5147

Merged
merged 3 commits into from
Dec 7, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions src/ApiGenerator/Domain/Specification/UrlPart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public string HighLevelTypeName

case "forecast_id":
case "action_id":
case "ids" when Type == "list":
return "Ids";

case "index":
Expand Down
21 changes: 21 additions & 0 deletions src/Nest/Descriptors.Security.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,27 @@ public ChangePasswordDescriptor(): base()
public ChangePasswordDescriptor Refresh(Refresh? refresh) => Qs("refresh", refresh);
}

///<summary>Descriptor for ClearApiKeyCache <para>https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-clear-api-key-cache.html</para></summary>
public partial class ClearApiKeyCacheDescriptor : RequestDescriptorBase<ClearApiKeyCacheDescriptor, ClearApiKeyCacheRequestParameters, IClearApiKeyCacheRequest>, IClearApiKeyCacheRequest
{
internal override ApiUrls ApiUrls => ApiUrlsLookups.SecurityClearApiKeyCache;
///<summary>/_security/api_key/{ids}/_clear_cache</summary>
///<param name = "ids">this parameter is required</param>
public ClearApiKeyCacheDescriptor(Ids ids): base(r => r.Required("ids", ids))
{
}

///<summary>Used for serialization purposes, making sure we have a parameterless constructor</summary>
[SerializationConstructor]
protected ClearApiKeyCacheDescriptor(): base()
{
}

// values part of the url path
Ids IClearApiKeyCacheRequest.Ids => Self.RouteValues.Get<Ids>("ids");
// Request parameters
}

///<summary>Descriptor for ClearCachedPrivileges <para>https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-clear-privilege-cache.html</para></summary>
public partial class ClearCachedPrivilegesDescriptor : RequestDescriptorBase<ClearCachedPrivilegesDescriptor, ClearCachedPrivilegesRequestParameters, IClearCachedPrivilegesRequest>, IClearCachedPrivilegesRequest
{
Expand Down
24 changes: 24 additions & 0 deletions src/Nest/ElasticClient.Security.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,30 @@ internal SecurityNamespace(ElasticClient client): base(client)
/// </summary>
public Task<ChangePasswordResponse> ChangePasswordAsync(IChangePasswordRequest request, CancellationToken ct = default) => DoRequestAsync<IChangePasswordRequest, ChangePasswordResponse>(request, request.RequestParameters, ct);
/// <summary>
/// <c>POST</c> request to the <c>security.clear_api_key_cache</c> API, read more about this API online:
/// <para></para>
/// <a href = "https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-clear-api-key-cache.html">https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-clear-api-key-cache.html</a>
/// </summary>
public ClearApiKeyCacheResponse ClearApiKeyCache(Ids ids, Func<ClearApiKeyCacheDescriptor, IClearApiKeyCacheRequest> selector = null) => ClearApiKeyCache(selector.InvokeOrDefault(new ClearApiKeyCacheDescriptor(ids: ids)));
/// <summary>
/// <c>POST</c> request to the <c>security.clear_api_key_cache</c> API, read more about this API online:
/// <para></para>
/// <a href = "https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-clear-api-key-cache.html">https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-clear-api-key-cache.html</a>
/// </summary>
public Task<ClearApiKeyCacheResponse> ClearApiKeyCacheAsync(Ids ids, Func<ClearApiKeyCacheDescriptor, IClearApiKeyCacheRequest> selector = null, CancellationToken ct = default) => ClearApiKeyCacheAsync(selector.InvokeOrDefault(new ClearApiKeyCacheDescriptor(ids: ids)), ct);
/// <summary>
/// <c>POST</c> request to the <c>security.clear_api_key_cache</c> API, read more about this API online:
/// <para></para>
/// <a href = "https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-clear-api-key-cache.html">https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-clear-api-key-cache.html</a>
/// </summary>
public ClearApiKeyCacheResponse ClearApiKeyCache(IClearApiKeyCacheRequest request) => DoRequest<IClearApiKeyCacheRequest, ClearApiKeyCacheResponse>(request, request.RequestParameters);
/// <summary>
/// <c>POST</c> request to the <c>security.clear_api_key_cache</c> API, read more about this API online:
/// <para></para>
/// <a href = "https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-clear-api-key-cache.html">https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-clear-api-key-cache.html</a>
/// </summary>
public Task<ClearApiKeyCacheResponse> ClearApiKeyCacheAsync(IClearApiKeyCacheRequest request, CancellationToken ct = default) => DoRequestAsync<IClearApiKeyCacheRequest, ClearApiKeyCacheResponse>(request, request.RequestParameters, ct);
/// <summary>
/// <c>POST</c> request to the <c>security.clear_cached_privileges</c> API, read more about this API online:
/// <para></para>
/// <a href = "https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-clear-privilege-cache.html">https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-clear-privilege-cache.html</a>
Expand Down
33 changes: 33 additions & 0 deletions src/Nest/Requests.Security.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,39 @@ public Refresh? Refresh
}
}

[InterfaceDataContract]
public partial interface IClearApiKeyCacheRequest : IRequest<ClearApiKeyCacheRequestParameters>
{
[IgnoreDataMember]
Ids Ids
{
get;
}
}

///<summary>Request for ClearApiKeyCache <para>https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-clear-api-key-cache.html</para></summary>
public partial class ClearApiKeyCacheRequest : PlainRequestBase<ClearApiKeyCacheRequestParameters>, IClearApiKeyCacheRequest
{
protected IClearApiKeyCacheRequest Self => this;
internal override ApiUrls ApiUrls => ApiUrlsLookups.SecurityClearApiKeyCache;
///<summary>/_security/api_key/{ids}/_clear_cache</summary>
///<param name = "ids">this parameter is required</param>
public ClearApiKeyCacheRequest(Ids ids): base(r => r.Required("ids", ids))
{
}

///<summary>Used for serialization purposes, making sure we have a parameterless constructor</summary>
[SerializationConstructor]
protected ClearApiKeyCacheRequest(): base()
{
}

// values part of the url path
[IgnoreDataMember]
Ids IClearApiKeyCacheRequest.Ids => Self.RouteValues.Get<Ids>("ids");
// Request parameters
}

[InterfaceDataContract]
public partial interface IClearCachedPrivilegesRequest : IRequest<ClearCachedPrivilegesRequestParameters>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// 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

namespace Nest
{
[MapsApi("security.clear_api_key_cache")]
[ReadAs(typeof(ClearApiKeyCacheRequest))]
public partial interface IClearApiKeyCacheRequest { }

public partial class ClearApiKeyCacheRequest { }

public partial class ClearApiKeyCacheDescriptor { }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// 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.Collections.Generic;
using System.Runtime.Serialization;
using Elasticsearch.Net;
using Elasticsearch.Net.Utf8Json;

namespace Nest
{
public class ClearApiKeyCacheResponse : NodesResponseBase
{
/// <summary>
/// The cluster name.
/// </summary>
[DataMember(Name = "cluster_name")]
public string ClusterName { get; internal set; }

/// <summary>
/// A dictionary of <see cref="CompactNodeInfo"/> container details of the nodes that cleared the cache.
/// </summary>
[DataMember(Name = "nodes")]
[JsonFormatter(typeof(VerbatimInterfaceReadOnlyDictionaryKeysFormatter<string, CompactNodeInfo>))]
public IReadOnlyDictionary<string, CompactNodeInfo> Nodes { get; internal set; } = EmptyReadOnly<string, CompactNodeInfo>.Dictionary;
}
}
1 change: 1 addition & 0 deletions src/Nest/_Generated/ApiUrlsLookup.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ internal static class ApiUrlsLookups
internal static ApiUrls NoNamespaceSearchTemplate = new ApiUrls(new[]{"_search/template", "{index}/_search/template"});
internal static ApiUrls SecurityAuthenticate = new ApiUrls(new[]{"_security/_authenticate"});
internal static ApiUrls SecurityChangePassword = new ApiUrls(new[]{"_security/user/{username}/_password", "_security/user/_password"});
internal static ApiUrls SecurityClearApiKeyCache = new ApiUrls(new[]{"_security/api_key/{ids}/_clear_cache"});
internal static ApiUrls SecurityClearCachedPrivileges = new ApiUrls(new[]{"_security/privilege/{application}/_clear_cache"});
internal static ApiUrls SecurityClearCachedRealms = new ApiUrls(new[]{"_security/realm/{realms}/_clear_cache"});
internal static ApiUrls SecurityClearCachedRoles = new ApiUrls(new[]{"_security/role/{name}/_clear_cache"});
Expand Down
44 changes: 44 additions & 0 deletions tests/Tests/XPack/Security/ApiKey/SecurityApiKeyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information

using System;
using System.Linq;
using System.Threading.Tasks;
using Elastic.Elasticsearch.Xunit.XunitPlumbing;
using Elasticsearch.Net;
Expand All @@ -23,6 +24,8 @@ public class SecurityApiKeyTests : CoordinatedIntegrationTestBase<XPackCluster>
private const string GetApiKeyStep = nameof(GetApiKeyStep);
private const string GetAllApiKeysStep = nameof(GetAllApiKeysStep);
private const string InvalidateApiKeyStep = nameof(InvalidateApiKeyStep);
private const string GetAnotherApiKeyStep = nameof(GetAnotherApiKeyStep);
private const string ClearApiKeyCacheStep = nameof(ClearApiKeyCacheStep);

public SecurityApiKeyTests(XPackCluster cluster, EndpointUsage usage) : base(new CoordinatedUsage(cluster, usage)
{
Expand Down Expand Up @@ -186,6 +189,39 @@ public SecurityApiKeyTests(XPackCluster cluster, EndpointUsage usage) : base(new
(v, c, r) => c.Security.InvalidateApiKey(r),
(v, c, r) => c.Security.InvalidateApiKeyAsync(r)
)
},
{
GetAnotherApiKeyStep, u =>
u.Calls<GetApiKeyDescriptor, GetApiKeyRequest, IGetApiKeyRequest, GetApiKeyResponse>(
v => new GetApiKeyRequest
{
Name = v,
RequestConfiguration = new RequestConfiguration
{
BasicAuthenticationCredentials = new BasicAuthenticationCredentials($"user-{v}", "password")
}
},
(v, d) => d
.Name(v)
.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),
(r, values) => values.ExtendedValue("apiKey", r.ApiKeys.FirstOrDefault()?.Id ?? string.Empty)
)
},
{
ClearApiKeyCacheStep, u =>
u.Calls<ClearApiKeyCacheDescriptor, ClearApiKeyCacheRequest, IClearApiKeyCacheRequest, ClearApiKeyCacheResponse>(
v => new ClearApiKeyCacheRequest(u.Usage.CallUniqueValues.ExtendedValue<string>("apiKey") ?? string.Empty),
(v, d) => d,
(v, c, f) => c.Security.ClearApiKeyCache(u.Usage.CallUniqueValues.ExtendedValue<string>("apiKey") ?? string.Empty, f),
(v, c, f) => c.Security.ClearApiKeyCacheAsync(u.Usage.CallUniqueValues.ExtendedValue<string>("apiKey") ?? string.Empty, f),
(v, c, r) => c.Security.ClearApiKeyCache(r),
(v, c, r) => c.Security.ClearApiKeyCacheAsync(r)
)
}
}) { }

Expand Down Expand Up @@ -227,5 +263,13 @@ [I] public async Task SecurityInvalidateApiKeyResponse() => await Assert<Invalid
r.PreviouslyInvalidatedApiKeys.Should().BeEmpty();
r.InvalidatedApiKeys.Should().HaveCount(2);
});

[I] public async Task SecurityClearApiKeyCacheResponse() => await Assert<ClearApiKeyCacheResponse>(ClearApiKeyCacheStep, r =>
stevejgordon marked this conversation as resolved.
Show resolved Hide resolved
{
r.IsValid.Should().BeTrue();
r.NodeStatistics.Successful.Should().BeGreaterOrEqualTo(1);
r.ClusterName.Should().NotBeNullOrEmpty();
r.Nodes.Count.Should().BeGreaterOrEqualTo(1);
});
}
}
9 changes: 9 additions & 0 deletions tests/Tests/XPack/Security/ApiKey/SecurityApiKeyUrlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,13 @@ [U] public override async Task Urls() => await UrlTester.PUT("/_security/api_key
.FluentAsync(c => c.Security.CreateApiKeyAsync(p => p))
.RequestAsync(c => c.Security.CreateApiKeyAsync(new CreateApiKeyRequest()));
}

public class SecurityClearApiKeyCacheUrlTests : UrlTestsBase
{
[U] public override async Task Urls() => await UrlTester.POST("/_security/api_key/id1%2Cid2/_clear_cache")
.Fluent(c => c.Security.ClearApiKeyCache("id1,id2", p => p))
.Request(c => c.Security.ClearApiKeyCache(new ClearApiKeyCacheRequest("id1,id2")))
.FluentAsync(c => c.Security.ClearApiKeyCacheAsync("id1,id2", p => p))
.RequestAsync(c => c.Security.ClearApiKeyCacheAsync(new ClearApiKeyCacheRequest("id1,id2")));
}
}