Skip to content

Commit 056a860

Browse files
Add Security APIs for API Key Management (#6347) (#6348)
Co-authored-by: Steve Gordon <sgordon@hotmail.co.uk>
1 parent 982ea24 commit 056a860

23 files changed

+2127
-70
lines changed

src/Elastic.Clients.Elasticsearch/Common/Infer/Id/Ids.cs

Lines changed: 64 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -2,72 +2,70 @@
22
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
33
// See the LICENSE file in the project root for more information.
44

5+
using System;
6+
using System.Collections.Generic;
7+
using System.Diagnostics;
8+
using System.Linq;
9+
using Elastic.Transport;
10+
511
namespace Elastic.Clients.Elasticsearch
612
{
7-
//public partial class Ids
8-
//{
9-
// // This is temporary
10-
// public Ids(IEnumerable<Id> ids) => _idList.AddRange(ids);
11-
12-
// public string GetString(ITransportConfiguration settings) => throw new NotImplementedException();
13-
//}
14-
15-
//[DebuggerDisplay("{DebugDisplay,nq}")]
16-
//public partial class Ids : IUrlParameter, IEquatable<Ids>
17-
//{
18-
// private readonly List<string> _ids;
19-
20-
// public Ids(IEnumerable<string> value) => _ids = value?.ToList();
21-
22-
// public Ids(string value)
23-
// {
24-
// if (!value.IsNullOrEmptyCommaSeparatedList(out var arr))
25-
// _ids = arr.ToList();
26-
// }
27-
28-
// private string DebugDisplay => ((IUrlParameter)this).GetString(null);
29-
30-
// public bool Equals(Ids other)
31-
// {
32-
// if (other == null)
33-
// return false;
34-
// if (_ids == null && other._ids == null)
35-
// return true;
36-
// if (_ids == null || other._ids == null)
37-
// return false;
38-
39-
// return _ids.Count == other._ids.Count &&
40-
// _ids.OrderBy(id => id).SequenceEqual(other._ids.OrderBy(id => id));
41-
// }
42-
43-
// string IUrlParameter.GetString(ITransportConfiguration settings) =>
44-
// string.Join(",", _ids ?? Enumerable.Empty<string>());
45-
46-
// public override string ToString() => DebugDisplay;
47-
48-
// public static implicit operator Ids(string value) =>
49-
// value.IsNullOrEmptyCommaSeparatedList(out var arr) ? null : new Ids(arr);
50-
51-
// public static implicit operator Ids(string[] value) =>
52-
// value.IsEmpty() ? null : new Ids(value);
53-
54-
// public override bool Equals(object obj) => obj is Ids other && Equals(other);
55-
56-
// public override int GetHashCode()
57-
// {
58-
// if (_ids == null)
59-
// return 0;
60-
// unchecked
61-
// {
62-
// var hc = 0;
63-
// foreach (var id in _ids.OrderBy(id => id))
64-
// hc = hc * 17 + id.GetHashCode();
65-
// return hc;
66-
// }
67-
// }
68-
69-
// public static bool operator ==(Ids left, Ids right) => Equals(left, right);
70-
71-
// public static bool operator !=(Ids left, Ids right) => !Equals(left, right);
72-
//}
13+
[DebuggerDisplay("{DebugDisplay,nq}")]
14+
public partial class Ids : IUrlParameter, IEquatable<Ids>
15+
{
16+
private readonly List<string> _ids;
17+
18+
public Ids(IEnumerable<string> value) => _ids = value?.ToList();
19+
20+
public Ids(string value)
21+
{
22+
if (!value.IsNullOrEmptyCommaSeparatedList(out var arr))
23+
_ids = arr.ToList();
24+
}
25+
26+
private string DebugDisplay => ((IUrlParameter)this).GetString(null);
27+
28+
public bool Equals(Ids other)
29+
{
30+
if (other == null)
31+
return false;
32+
if (_ids == null && other._ids == null)
33+
return true;
34+
if (_ids == null || other._ids == null)
35+
return false;
36+
37+
return _ids.Count == other._ids.Count &&
38+
_ids.OrderBy(id => id).SequenceEqual(other._ids.OrderBy(id => id));
39+
}
40+
41+
string IUrlParameter.GetString(ITransportConfiguration settings) =>
42+
string.Join(",", _ids ?? Enumerable.Empty<string>());
43+
44+
public override string ToString() => DebugDisplay;
45+
46+
public static implicit operator Ids(string value) =>
47+
value.IsNullOrEmptyCommaSeparatedList(out var arr) ? null : new Ids(arr);
48+
49+
public static implicit operator Ids(string[] value) =>
50+
value.IsEmpty() ? null : new Ids(value);
51+
52+
public override bool Equals(object obj) => obj is Ids other && Equals(other);
53+
54+
public override int GetHashCode()
55+
{
56+
if (_ids == null)
57+
return 0;
58+
unchecked
59+
{
60+
var hc = 0;
61+
foreach (var id in _ids.OrderBy(id => id))
62+
hc = hc * 17 + id.GetHashCode();
63+
return hc;
64+
}
65+
}
66+
67+
public static bool operator ==(Ids left, Ids right) => Equals(left, right);
68+
69+
public static bool operator !=(Ids left, Ids right) => !Equals(left, right);
70+
}
7371
}

src/Elastic.Clients.Elasticsearch/_Generated/Api/ApiUrlsLookup.g.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,12 @@ internal static class ApiUrlsLookups
242242
internal static ApiUrls SearchableSnapshotsStats = new ApiUrls(new[] { "/_searchable_snapshots/stats", "/{index}/_searchable_snapshots/stats" });
243243
internal static ApiUrls NoNamespaceSearch = new ApiUrls(new[] { "/_search", "/{index}/_search" });
244244
internal static ApiUrls NoNamespaceSearchShards = new ApiUrls(new[] { "/_search_shards", "/{index}/_search_shards" });
245+
internal static ApiUrls SecurityClearApiKeyCache = new ApiUrls(new[] { "/_security/api_key/{ids}/_clear_cache" });
246+
internal static ApiUrls SecurityCreateApiKey = new ApiUrls(new[] { "/_security/api_key" });
247+
internal static ApiUrls SecurityGetApiKey = new ApiUrls(new[] { "/_security/api_key" });
248+
internal static ApiUrls SecurityGrantApiKey = new ApiUrls(new[] { "/_security/api_key/grant" });
249+
internal static ApiUrls SecurityInvalidateApiKey = new ApiUrls(new[] { "/_security/api_key" });
250+
internal static ApiUrls SecurityQueryApiKeys = new ApiUrls(new[] { "/_security/_query/api_key" });
245251
internal static ApiUrls SnapshotLifecycleManagementDeleteLifecycle = new ApiUrls(new[] { "/_slm/policy/{policy_id}" });
246252
internal static ApiUrls SnapshotLifecycleManagementExecuteLifecycle = new ApiUrls(new[] { "/_slm/policy/{policy_id}/_execute" });
247253
internal static ApiUrls SnapshotLifecycleManagementExecuteRetention = new ApiUrls(new[] { "/_slm/_execute_retention" });
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
// ███╗ ██╗ ██████╗ ████████╗██╗ ██████╗███████╗
6+
// ████╗ ██║██╔═══██╗╚══██╔══╝██║██╔════╝██╔════╝
7+
// ██╔██╗ ██║██║ ██║ ██║ ██║██║ █████╗
8+
// ██║╚██╗██║██║ ██║ ██║ ██║██║ ██╔══╝
9+
// ██║ ╚████║╚██████╔╝ ██║ ██║╚██████╗███████╗
10+
// ╚═╝ ╚═══╝ ╚═════╝ ╚═╝ ╚═╝ ╚═════╝╚══════╝
11+
// ------------------------------------------------
12+
//
13+
// This file is automatically generated.
14+
// Please do not edit these files manually.
15+
//
16+
// ------------------------------------------------
17+
18+
using Elastic.Transport;
19+
using System;
20+
using System.Collections.Generic;
21+
using System.Linq.Expressions;
22+
using System.Text.Json;
23+
using System.Text.Json.Serialization;
24+
25+
#nullable restore
26+
namespace Elastic.Clients.Elasticsearch.Security
27+
{
28+
public class SecurityClearApiKeyCacheRequestParameters : RequestParameters<SecurityClearApiKeyCacheRequestParameters>
29+
{
30+
}
31+
32+
public partial class SecurityClearApiKeyCacheRequest : PlainRequestBase<SecurityClearApiKeyCacheRequestParameters>
33+
{
34+
public SecurityClearApiKeyCacheRequest(Elastic.Clients.Elasticsearch.Ids ids) : base(r => r.Required("ids", ids))
35+
{
36+
}
37+
38+
internal override ApiUrls ApiUrls => ApiUrlsLookups.SecurityClearApiKeyCache;
39+
protected override HttpMethod HttpMethod => HttpMethod.POST;
40+
protected override bool SupportsBody => false;
41+
}
42+
43+
public sealed partial class SecurityClearApiKeyCacheRequestDescriptor : RequestDescriptorBase<SecurityClearApiKeyCacheRequestDescriptor, SecurityClearApiKeyCacheRequestParameters>
44+
{
45+
internal SecurityClearApiKeyCacheRequestDescriptor(Action<SecurityClearApiKeyCacheRequestDescriptor> configure) => configure.Invoke(this);
46+
public SecurityClearApiKeyCacheRequestDescriptor(Elastic.Clients.Elasticsearch.Ids ids) : base(r => r.Required("ids", ids))
47+
{
48+
}
49+
50+
internal SecurityClearApiKeyCacheRequestDescriptor()
51+
{
52+
}
53+
54+
internal override ApiUrls ApiUrls => ApiUrlsLookups.SecurityClearApiKeyCache;
55+
protected override HttpMethod HttpMethod => HttpMethod.POST;
56+
protected override bool SupportsBody => false;
57+
public SecurityClearApiKeyCacheRequestDescriptor Ids(Elastic.Clients.Elasticsearch.Ids ids)
58+
{
59+
RouteValues.Required("ids", ids);
60+
return Self;
61+
}
62+
63+
protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions options, IElasticsearchClientSettings settings)
64+
{
65+
}
66+
}
67+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
// ███╗ ██╗ ██████╗ ████████╗██╗ ██████╗███████╗
6+
// ████╗ ██║██╔═══██╗╚══██╔══╝██║██╔════╝██╔════╝
7+
// ██╔██╗ ██║██║ ██║ ██║ ██║██║ █████╗
8+
// ██║╚██╗██║██║ ██║ ██║ ██║██║ ██╔══╝
9+
// ██║ ╚████║╚██████╔╝ ██║ ██║╚██████╗███████╗
10+
// ╚═╝ ╚═══╝ ╚═════╝ ╚═╝ ╚═╝ ╚═════╝╚══════╝
11+
// ------------------------------------------------
12+
//
13+
// This file is automatically generated.
14+
// Please do not edit these files manually.
15+
//
16+
// ------------------------------------------------
17+
18+
using Elastic.Transport.Products.Elasticsearch;
19+
using System.Collections.Generic;
20+
using System.Text.Json.Serialization;
21+
22+
#nullable restore
23+
namespace Elastic.Clients.Elasticsearch.Security
24+
{
25+
public partial class SecurityClearApiKeyCacheResponse : ElasticsearchResponseBase
26+
{
27+
[JsonInclude]
28+
[JsonPropertyName("cluster_name")]
29+
public string ClusterName { get; init; }
30+
31+
[JsonInclude]
32+
[JsonPropertyName("nodes")]
33+
public Dictionary<string, Elastic.Clients.Elasticsearch.Security.ClusterNode> Nodes { get; init; }
34+
35+
[JsonInclude]
36+
[JsonPropertyName("_nodes")]
37+
public Elastic.Clients.Elasticsearch.NodeStatistics NodeStats { get; init; }
38+
}
39+
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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+
// ███╗ ██╗ ██████╗ ████████╗██╗ ██████╗███████╗
6+
// ████╗ ██║██╔═══██╗╚══██╔══╝██║██╔════╝██╔════╝
7+
// ██╔██╗ ██║██║ ██║ ██║ ██║██║ █████╗
8+
// ██║╚██╗██║██║ ██║ ██║ ██║██║ ██╔══╝
9+
// ██║ ╚████║╚██████╔╝ ██║ ██║╚██████╗███████╗
10+
// ╚═╝ ╚═══╝ ╚═════╝ ╚═╝ ╚═╝ ╚═════╝╚══════╝
11+
// ------------------------------------------------
12+
//
13+
// This file is automatically generated.
14+
// Please do not edit these files manually.
15+
//
16+
// ------------------------------------------------
17+
18+
using Elastic.Transport;
19+
using System;
20+
using System.Collections.Generic;
21+
using System.Linq.Expressions;
22+
using System.Text.Json;
23+
using System.Text.Json.Serialization;
24+
25+
#nullable restore
26+
namespace Elastic.Clients.Elasticsearch.Security
27+
{
28+
public class SecurityCreateApiKeyRequestParameters : RequestParameters<SecurityCreateApiKeyRequestParameters>
29+
{
30+
[JsonIgnore]
31+
public Elastic.Clients.Elasticsearch.Refresh? Refresh { get => Q<Elastic.Clients.Elasticsearch.Refresh?>("refresh"); set => Q("refresh", value); }
32+
}
33+
34+
public partial class SecurityCreateApiKeyRequest : PlainRequestBase<SecurityCreateApiKeyRequestParameters>
35+
{
36+
internal override ApiUrls ApiUrls => ApiUrlsLookups.SecurityCreateApiKey;
37+
protected override HttpMethod HttpMethod => HttpMethod.PUT;
38+
protected override bool SupportsBody => true;
39+
[JsonIgnore]
40+
public Elastic.Clients.Elasticsearch.Refresh? Refresh { get => Q<Elastic.Clients.Elasticsearch.Refresh?>("refresh"); set => Q("refresh", value); }
41+
42+
[JsonInclude]
43+
[JsonPropertyName("expiration")]
44+
public Elastic.Clients.Elasticsearch.Time? Expiration { get; set; }
45+
46+
[JsonInclude]
47+
[JsonPropertyName("name")]
48+
public Elastic.Clients.Elasticsearch.Name? Name { get; set; }
49+
50+
[JsonInclude]
51+
[JsonPropertyName("role_descriptors")]
52+
public Dictionary<string, Elastic.Clients.Elasticsearch.Security.RoleDescriptor>? RoleDescriptors { get; set; }
53+
54+
[JsonInclude]
55+
[JsonPropertyName("metadata")]
56+
public Dictionary<string, object>? Metadata { get; set; }
57+
}
58+
59+
public sealed partial class SecurityCreateApiKeyRequestDescriptor : RequestDescriptorBase<SecurityCreateApiKeyRequestDescriptor, SecurityCreateApiKeyRequestParameters>
60+
{
61+
internal SecurityCreateApiKeyRequestDescriptor(Action<SecurityCreateApiKeyRequestDescriptor> configure) => configure.Invoke(this);
62+
public SecurityCreateApiKeyRequestDescriptor()
63+
{
64+
}
65+
66+
internal override ApiUrls ApiUrls => ApiUrlsLookups.SecurityCreateApiKey;
67+
protected override HttpMethod HttpMethod => HttpMethod.PUT;
68+
protected override bool SupportsBody => true;
69+
public SecurityCreateApiKeyRequestDescriptor Refresh(Elastic.Clients.Elasticsearch.Refresh? refresh) => Qs("refresh", refresh);
70+
private Elastic.Clients.Elasticsearch.Time? ExpirationValue { get; set; }
71+
72+
private Dictionary<string, object>? MetadataValue { get; set; }
73+
74+
private Elastic.Clients.Elasticsearch.Name? NameValue { get; set; }
75+
76+
private Dictionary<string, Elastic.Clients.Elasticsearch.Security.RoleDescriptor>? RoleDescriptorsValue { get; set; }
77+
78+
public SecurityCreateApiKeyRequestDescriptor Expiration(Elastic.Clients.Elasticsearch.Time? expiration)
79+
{
80+
ExpirationValue = expiration;
81+
return Self;
82+
}
83+
84+
public SecurityCreateApiKeyRequestDescriptor Metadata(Func<FluentDictionary<string, object>, FluentDictionary<string, object>> selector)
85+
{
86+
MetadataValue = selector?.Invoke(new FluentDictionary<string, object>());
87+
return Self;
88+
}
89+
90+
public SecurityCreateApiKeyRequestDescriptor Name(Elastic.Clients.Elasticsearch.Name? name)
91+
{
92+
NameValue = name;
93+
return Self;
94+
}
95+
96+
public SecurityCreateApiKeyRequestDescriptor RoleDescriptors(Func<FluentDictionary<string, Elastic.Clients.Elasticsearch.Security.RoleDescriptor>, FluentDictionary<string, Elastic.Clients.Elasticsearch.Security.RoleDescriptor>> selector)
97+
{
98+
RoleDescriptorsValue = selector?.Invoke(new FluentDictionary<string, Elastic.Clients.Elasticsearch.Security.RoleDescriptor>());
99+
return Self;
100+
}
101+
102+
protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions options, IElasticsearchClientSettings settings)
103+
{
104+
writer.WriteStartObject();
105+
if (ExpirationValue is not null)
106+
{
107+
writer.WritePropertyName("expiration");
108+
JsonSerializer.Serialize(writer, ExpirationValue, options);
109+
}
110+
111+
if (MetadataValue is not null)
112+
{
113+
writer.WritePropertyName("metadata");
114+
JsonSerializer.Serialize(writer, MetadataValue, options);
115+
}
116+
117+
if (NameValue is not null)
118+
{
119+
writer.WritePropertyName("name");
120+
JsonSerializer.Serialize(writer, NameValue, options);
121+
}
122+
123+
if (RoleDescriptorsValue is not null)
124+
{
125+
writer.WritePropertyName("role_descriptors");
126+
JsonSerializer.Serialize(writer, RoleDescriptorsValue, options);
127+
}
128+
129+
writer.WriteEndObject();
130+
}
131+
}
132+
}

0 commit comments

Comments
 (0)