Skip to content

Commit 75ba1f1

Browse files
authored
Implement grant API key (#5142)
Contributes to #5096
1 parent c311a40 commit 75ba1f1

File tree

10 files changed

+494
-0
lines changed

10 files changed

+494
-0
lines changed

src/Nest/Descriptors.Security.cs

+10
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,16 @@ public partial class GetUserPrivilegesDescriptor : RequestDescriptorBase<GetUser
417417
// Request parameters
418418
}
419419

420+
///<summary>Descriptor for GrantApiKey <para>https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-grant-api-key.html</para></summary>
421+
public partial class GrantApiKeyDescriptor : RequestDescriptorBase<GrantApiKeyDescriptor, GrantApiKeyRequestParameters, IGrantApiKeyRequest>, IGrantApiKeyRequest
422+
{
423+
internal override ApiUrls ApiUrls => ApiUrlsLookups.SecurityGrantApiKey;
424+
// values part of the url path
425+
// Request parameters
426+
///<summary>If `true` (the default) then refresh the affected shards to make this operation visible to search, if `wait_for` then wait for a refresh to make this operation visible to search, if `false` then do nothing with refreshes.</summary>
427+
public GrantApiKeyDescriptor Refresh(Refresh? refresh) => Qs("refresh", refresh);
428+
}
429+
420430
///<summary>Descriptor for HasPrivileges <para>https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-has-privileges.html</para></summary>
421431
public partial class HasPrivilegesDescriptor : RequestDescriptorBase<HasPrivilegesDescriptor, HasPrivilegesRequestParameters, IHasPrivilegesRequest>, IHasPrivilegesRequest
422432
{

src/Nest/ElasticClient.Security.cs

+24
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,30 @@ internal SecurityNamespace(ElasticClient client): base(client)
517517
/// </summary>
518518
public Task<GetUserPrivilegesResponse> GetUserPrivilegesAsync(IGetUserPrivilegesRequest request, CancellationToken ct = default) => DoRequestAsync<IGetUserPrivilegesRequest, GetUserPrivilegesResponse>(request, request.RequestParameters, ct);
519519
/// <summary>
520+
/// <c>POST</c> request to the <c>security.grant_api_key</c> API, read more about this API online:
521+
/// <para></para>
522+
/// <a href = "https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-grant-api-key.html">https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-grant-api-key.html</a>
523+
/// </summary>
524+
public GrantApiKeyResponse GrantApiKey(Func<GrantApiKeyDescriptor, IGrantApiKeyRequest> selector) => GrantApiKey(selector.InvokeOrDefault(new GrantApiKeyDescriptor()));
525+
/// <summary>
526+
/// <c>POST</c> request to the <c>security.grant_api_key</c> API, read more about this API online:
527+
/// <para></para>
528+
/// <a href = "https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-grant-api-key.html">https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-grant-api-key.html</a>
529+
/// </summary>
530+
public Task<GrantApiKeyResponse> GrantApiKeyAsync(Func<GrantApiKeyDescriptor, IGrantApiKeyRequest> selector, CancellationToken ct = default) => GrantApiKeyAsync(selector.InvokeOrDefault(new GrantApiKeyDescriptor()), ct);
531+
/// <summary>
532+
/// <c>POST</c> request to the <c>security.grant_api_key</c> API, read more about this API online:
533+
/// <para></para>
534+
/// <a href = "https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-grant-api-key.html">https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-grant-api-key.html</a>
535+
/// </summary>
536+
public GrantApiKeyResponse GrantApiKey(IGrantApiKeyRequest request) => DoRequest<IGrantApiKeyRequest, GrantApiKeyResponse>(request, request.RequestParameters);
537+
/// <summary>
538+
/// <c>POST</c> request to the <c>security.grant_api_key</c> API, read more about this API online:
539+
/// <para></para>
540+
/// <a href = "https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-grant-api-key.html">https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-grant-api-key.html</a>
541+
/// </summary>
542+
public Task<GrantApiKeyResponse> GrantApiKeyAsync(IGrantApiKeyRequest request, CancellationToken ct = default) => DoRequestAsync<IGrantApiKeyRequest, GrantApiKeyResponse>(request, request.RequestParameters, ct);
543+
/// <summary>
520544
/// <c>POST</c> request to the <c>security.has_privileges</c> API, read more about this API online:
521545
/// <para></para>
522546
/// <a href = "https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-has-privileges.html">https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-has-privileges.html</a>

src/Nest/Requests.Security.cs

+23
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,29 @@ public partial class GetUserPrivilegesRequest : PlainRequestBase<GetUserPrivileg
708708
// Request parameters
709709
}
710710

711+
[InterfaceDataContract]
712+
public partial interface IGrantApiKeyRequest : IRequest<GrantApiKeyRequestParameters>
713+
{
714+
}
715+
716+
///<summary>Request for GrantApiKey <para>https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-grant-api-key.html</para></summary>
717+
public partial class GrantApiKeyRequest : PlainRequestBase<GrantApiKeyRequestParameters>, IGrantApiKeyRequest
718+
{
719+
protected IGrantApiKeyRequest Self => this;
720+
internal override ApiUrls ApiUrls => ApiUrlsLookups.SecurityGrantApiKey;
721+
// values part of the url path
722+
// Request parameters
723+
///<summary>
724+
/// If `true` (the default) then refresh the affected shards to make this operation visible to search, if `wait_for` then wait for a refresh
725+
/// to make this operation visible to search, if `false` then do nothing with refreshes.
726+
///</summary>
727+
public Refresh? Refresh
728+
{
729+
get => Q<Refresh? >("refresh");
730+
set => Q("refresh", value);
731+
}
732+
}
733+
711734
[InterfaceDataContract]
712735
public partial interface IHasPrivilegesRequest : IRequest<HasPrivilegesRequestParameters>
713736
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information
4+
5+
using System;
6+
using System.Runtime.Serialization;
7+
using Elasticsearch.Net.Utf8Json;
8+
9+
namespace Nest
10+
{
11+
[InterfaceDataContract]
12+
[ReadAs(typeof(ApiKey))]
13+
public interface IApiKey
14+
{
15+
/// <summary>
16+
/// Optional expiration for the API key being generated.
17+
/// If an expiration is not provided then the API keys do not expire.
18+
/// </summary>
19+
[DataMember(Name = "expiration")]
20+
Time Expiration { get; set; }
21+
22+
/// <summary>
23+
/// A name for this API key.
24+
/// </summary>
25+
[DataMember(Name = "name")]
26+
string Name { get; set; }
27+
28+
/// <summary>
29+
/// Optional role descriptors for this API key, if not provided then permissions of authenticated user are applied.
30+
/// </summary>
31+
[DataMember(Name = "role_descriptors")]
32+
IApiKeyRoles Roles { get; set; }
33+
}
34+
35+
public class ApiKey : IApiKey
36+
{
37+
/// <inheritdoc cref="IApiKey.Expiration" />
38+
public Time Expiration { get; set; }
39+
40+
/// <inheritdoc cref="IApiKey.Name" />
41+
public string Name { get; set; }
42+
43+
/// <inheritdoc cref="IApiKey.Roles" />
44+
public IApiKeyRoles Roles { get; set; }
45+
}
46+
47+
public class ApiKeyDescriptor : DescriptorBase<ApiKeyDescriptor, IApiKey>, IApiKey
48+
{
49+
/// <inheritdoc cref="IApiKey.Expiration" />
50+
Time IApiKey.Expiration { get; set; }
51+
52+
/// <inheritdoc cref="IApiKey.Name" />
53+
string IApiKey.Name { get; set; }
54+
55+
/// <inheritdoc cref="IApiKey.Roles" />
56+
IApiKeyRoles IApiKey.Roles { get; set; }
57+
58+
/// <inheritdoc cref="IApiKey.Expiration" />
59+
public ApiKeyDescriptor Expiration(Time expiration) => Assign(expiration, (a, v) => a.Expiration = v);
60+
61+
/// <inheritdoc cref="IApiKey.Name" />
62+
public ApiKeyDescriptor Name(string name) => Assign(name, (a, v) => a.Name = v);
63+
64+
/// <inheritdoc cref="IApiKey.Roles" />
65+
public ApiKeyDescriptor Roles(Func<ApiKeyRolesDescriptor, IPromise<IApiKeyRoles>> selector) =>
66+
Assign(selector, (a, v) => a.Roles = v.InvokeOrDefault(new ApiKeyRolesDescriptor()).Value);
67+
}
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information
4+
5+
using System;
6+
using System.Runtime.Serialization;
7+
8+
namespace Nest
9+
{
10+
[MapsApi("security.grant_api_key.json")]
11+
[ReadAs(typeof(GrantApiKeyRequest))]
12+
public partial interface IGrantApiKeyRequest
13+
{
14+
/// <summary>
15+
/// The user’s access token. If you specify the access_token grant type,
16+
/// this parameter is required. It is not valid with other grant types.
17+
/// </summary>
18+
[DataMember(Name = "access_token")]
19+
string AccessToken { get; set; }
20+
21+
/// <summary>
22+
/// The type of grant. Supported grant types are: access_token,password.
23+
/// </summary>
24+
[DataMember(Name = "grant_type")]
25+
GrantType? GrantType { get; set; }
26+
27+
/// <summary>
28+
/// The user’s password. If you specify the password grant type,
29+
/// this parameter is required. It is not valid with other grant types.
30+
/// </summary>
31+
[DataMember(Name = "password")]
32+
string Password { get; set; }
33+
34+
/// <summary>
35+
/// The user name that identifies the user. If you specify the password grant type,
36+
/// this parameter is required. It is not valid with other grant types.
37+
/// </summary>
38+
[DataMember(Name = "username")]
39+
string Username { get; set; }
40+
41+
/// <summary>
42+
/// Defines the API key.
43+
/// </summary>
44+
[DataMember(Name = "api_key")]
45+
IApiKey ApiKey { get; set; }
46+
}
47+
48+
public partial class GrantApiKeyRequest
49+
{
50+
/// <inheritdoc cref="IGrantApiKeyRequest.AccessToken" />
51+
public string AccessToken { get; set; }
52+
53+
/// <inheritdoc cref="IGrantApiKeyRequest.GrantType" />
54+
public GrantType? GrantType { get; set; }
55+
56+
/// <inheritdoc cref="IGrantApiKeyRequest.Password" />
57+
public string Password { get; set; }
58+
59+
/// <inheritdoc cref="IGrantApiKeyRequest.Username" />
60+
public string Username { get; set; }
61+
62+
/// <inheritdoc cref="IGrantApiKeyRequest.ApiKey" />
63+
public IApiKey ApiKey { get; set; }
64+
}
65+
66+
public partial class GrantApiKeyDescriptor
67+
{
68+
/// <inheritdoc cref="IGrantApiKeyRequest.AccessToken" />
69+
string IGrantApiKeyRequest.AccessToken { get; set; }
70+
71+
/// <inheritdoc cref="IGrantApiKeyRequest.GrantType" />
72+
GrantType? IGrantApiKeyRequest.GrantType { get; set; } = Nest.GrantType.AccessToken;
73+
74+
/// <inheritdoc cref="IGrantApiKeyRequest.Password" />
75+
string IGrantApiKeyRequest.Password { get; set; }
76+
77+
/// <inheritdoc cref="IGrantApiKeyRequest.Username" />
78+
string IGrantApiKeyRequest.Username { get; set; }
79+
80+
/// <inheritdoc cref="IGrantApiKeyRequest.ApiKey" />
81+
IApiKey IGrantApiKeyRequest.ApiKey { get; set; }
82+
83+
/// <inheritdoc cref="IGrantApiKeyRequest.AccessToken" />
84+
public GrantApiKeyDescriptor AccessToken(string accessToken) => Assign(accessToken, (a, v) => a.AccessToken = v);
85+
86+
/// <inheritdoc cref="IGrantApiKeyRequest.GrantType" />
87+
public GrantApiKeyDescriptor GrantType(GrantType? type) => Assign(type, (a, v) => a.GrantType = v);
88+
89+
/// <inheritdoc cref="IGrantApiKeyRequest.Password" />
90+
public GrantApiKeyDescriptor Password(string password) => Assign(password, (a, v) => a.Password = v);
91+
92+
/// <inheritdoc cref="IGrantApiKeyRequest.Username" />
93+
public GrantApiKeyDescriptor Username(string username) => Assign(username, (a, v) => a.Username = v);
94+
95+
/// <inheritdoc cref="IGrantApiKeyRequest.ApiKey" />
96+
public GrantApiKeyDescriptor ApiKey(Func<ApiKeyDescriptor, IApiKey> selector) =>
97+
Assign(selector, (a, v) => a.ApiKey = v?.Invoke(new ApiKeyDescriptor()));
98+
}
99+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information
4+
5+
using System;
6+
using System.Runtime.Serialization;
7+
using Elasticsearch.Net.Utf8Json;
8+
9+
namespace Nest
10+
{
11+
public class GrantApiKeyResponse : ResponseBase
12+
{
13+
/// <summary>
14+
/// Id for the API key
15+
/// </summary>
16+
[DataMember(Name = "id")]
17+
public string Id { get; internal set; }
18+
19+
/// <summary>
20+
/// Name of the API key
21+
/// </summary>
22+
[DataMember(Name = "name")]
23+
public string Name { get; internal set; }
24+
25+
/// <summary>
26+
/// Optional expiration time for the API key in milliseconds
27+
/// </summary>
28+
[DataMember(Name = "expiration")]
29+
[JsonFormatter(typeof(NullableDateTimeOffsetEpochMillisecondsFormatter))]
30+
public DateTimeOffset? Expiration { get; internal set; }
31+
32+
/// <summary>
33+
/// Generated API key
34+
/// </summary>
35+
[DataMember(Name = "api_key")]
36+
public string ApiKey { get; internal set; }
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information
4+
5+
using System.Runtime.Serialization;
6+
using Elasticsearch.Net;
7+
8+
namespace Nest
9+
{
10+
[StringEnum]
11+
public enum GrantType
12+
{
13+
[EnumMember(Value = "password")] Password,
14+
[EnumMember(Value = "access_token")] AccessToken
15+
}
16+
}

src/Nest/_Generated/ApiUrlsLookup.generated.cs

+1
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ internal static class ApiUrlsLookups
259259
internal static ApiUrls SecurityGetUserAccessToken = new ApiUrls(new[]{"_security/oauth2/token"});
260260
internal static ApiUrls SecurityGetUser = new ApiUrls(new[]{"_security/user/{username}", "_security/user"});
261261
internal static ApiUrls SecurityGetUserPrivileges = new ApiUrls(new[]{"_security/user/_privileges"});
262+
internal static ApiUrls SecurityGrantApiKey = new ApiUrls(new[]{"_security/api_key/grant"});
262263
internal static ApiUrls SecurityHasPrivileges = new ApiUrls(new[]{"_security/user/_has_privileges", "_security/user/{user}/_has_privileges"});
263264
internal static ApiUrls SecurityInvalidateApiKey = new ApiUrls(new[]{"_security/api_key"});
264265
internal static ApiUrls SecurityInvalidateUserAccessToken = new ApiUrls(new[]{"_security/oauth2/token"});

0 commit comments

Comments
 (0)