Skip to content

Commit d5ff5f0

Browse files
committed
Add support for _shield related API's (#2053)
* authenticate * clear cached realms * roles * clear cached roles * delete role(s) * put role * get role(s) * user * delete user(s) * put user * get user(s) This also adds support for any request to be able to specify the `es-shield-runas-user` http header though the `RunAs` property on `RequestConfiguration` The OIS version of requests now directly exposes `RequestConfiguration`
1 parent cf3cb78 commit d5ff5f0

File tree

89 files changed

+3821
-287
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

89 files changed

+3821
-287
lines changed

Diff for: src/CodeGeneration/CodeGeneration.LowLevelClient/Domain/ApiUrlPart.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,12 @@ public string ClrTypeName
2929
case "repository":
3030
case "snapshot":
3131
case "lang":
32-
case "name": return this.Type == "string" ? "Name" : "Names";
32+
case "username":
33+
case "usernames":
34+
case "realm":
35+
case "realms":
36+
case "name":
37+
return this.Type == "string" ? "Name" : "Names";
3338
case "task_id": return "TaskId";
3439
default: return this.Type + "_";
3540
}

Diff for: src/CodeGeneration/CodeGeneration.LowLevelClient/Program.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ static void Main(string[] args)
2828
if (redownloadCoreSpecification)
2929
RestSpecDownloader.Download(downloadBranch);
3030

31-
ApiGenerator.Generate("Core", "DeleteByQuery", "Graph", "License");
31+
ApiGenerator.Generate("Core", "DeleteByQuery", "Graph", "License", "Shield");
3232
//ApiGenerator.Generate("Core", "Graph", "License");
3333
//ApiGenerator.Generate(); //generates everything under ApiSpecification
3434
}

Diff for: src/CodeGeneration/CodeGeneration.LowLevelClient/RestSpecification/XPack/Shield/shield.clear_cached_realms.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
"paths": [ "/_shield/realm/{realms}/_clear_cache" ],
88
"parts": {
99
"realms": {
10-
"type" : "string",
10+
"type" : "list",
1111
"description" : "Comma-separated list of realms to clear",
1212
"required" : true
1313
}
1414
},
1515
"params": {
1616
"usernames": {
17-
"type" : "string",
17+
"type" : "list",
1818
"description" : "Comma-separated list of usernames to clear from the cache",
1919
"required" : false
2020
}

Diff for: src/CodeGeneration/CodeGeneration.LowLevelClient/RestSpecification/XPack/Shield/shield.clear_cached_roles.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{
22
"shield.clear_cached_roles": {
33
"documentation": "Clears the internal caches for specified roles",
4-
"methods": [ "PUT", "POST" ],
4+
"methods": [ "POST" ],
55
"url": {
66
"path": "/_shield/role/{name}/_clear_cache",
77
"paths": [ "/_shield/role/{name}/_clear_cache" ],
88
"parts": {
99
"name": {
10-
"type" : "string",
10+
"type" : "list",
1111
"description" : "Role name",
1212
"required" : true
1313
}

Diff for: src/CodeGeneration/CodeGeneration.LowLevelClient/Views/_Requests.Generated.cshtml

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ namespace Nest
4343
<text> } </text>
4444
}
4545
<text>///<summary>Request parameters for @method.FullName <pre>@method.Documentation</pre></summary>
46-
public partial class @Raw(method.RequestType + method.RequestTypeGeneric) @Raw(string.Format(" : RequestBase<{0}>, {1}", method.QueryStringParamName, method.InterfaceType))
46+
public partial class @Raw(method.RequestType + method.RequestTypeGeneric) @Raw(string.Format(" : PlainRequestBase<{0}>, {1}", method.QueryStringParamName, method.InterfaceType))
4747
{
4848
protected @(Raw(method.InterfaceType)) Self => this;
4949
@foreach (ApiUrlPart part in method.AllParts.Where(p=>!ApiUrl.BlackListRouteValues.Contains(p.Name)))

Diff for: src/Elasticsearch.Net/Configuration/ConnectionConfiguration.cs

+8-8
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
namespace Elasticsearch.Net
1414
{
1515
/// <summary>
16-
/// ConnectionConfiguration allows you to control how ElasticLowLevelClient behaves and where/how it connects
16+
/// ConnectionConfiguration allows you to control how ElasticLowLevelClient behaves and where/how it connects
1717
/// to elasticsearch
1818
/// </summary>
1919
public class ConnectionConfiguration : ConnectionConfiguration<ConnectionConfiguration>
@@ -23,7 +23,7 @@ public class ConnectionConfiguration : ConnectionConfiguration<ConnectionConfigu
2323
public static readonly TimeSpan DefaultPingTimeoutOnSSL = TimeSpan.FromSeconds(5);
2424

2525
/// <summary>
26-
/// ConnectionConfiguration allows you to control how ElasticLowLevelClient behaves and where/how it connects
26+
/// ConnectionConfiguration allows you to control how ElasticLowLevelClient behaves and where/how it connects
2727
/// to elasticsearch
2828
/// </summary>
2929
/// <param name="uri">The root of the elasticsearch node we want to connect to. Defaults to http://localhost:9200</param>
@@ -33,12 +33,12 @@ public ConnectionConfiguration(Uri uri = null)
3333
{ }
3434

3535
/// <summary>
36-
/// ConnectionConfiguration allows you to control how ElasticLowLevelClient behaves and where/how it connects
36+
/// ConnectionConfiguration allows you to control how ElasticLowLevelClient behaves and where/how it connects
3737
/// to elasticsearch
3838
/// </summary>
3939
/// <param name="connectionPool">A connection pool implementation that'll tell the client what nodes are available</param>
4040
public ConnectionConfiguration(IConnectionPool connectionPool)
41-
// ReSharper disable once IntroduceOptionalParameters.Global
41+
// ReSharper disable once IntroduceOptionalParameters.Global
4242
: this(connectionPool, null, null)
4343
{ }
4444

@@ -208,7 +208,7 @@ public T EnableTcpKeepAlive(TimeSpan keepAliveTime, TimeSpan keepAliveInterval)
208208

209209
/// <summary>
210210
/// Instead of following a c/go like error checking on response.IsValid always throw an exception
211-
/// on the client when a call resulted in an exception on either the client or the Elasticsearch server.
211+
/// on the client when a call resulted in an exception on either the client or the Elasticsearch server.
212212
/// <para>Reasons for such exceptions could be search parser errors, index missing exceptions, etc...</para>
213213
/// </summary>
214214
public T ThrowExceptions(bool alwaysThrow = true) => Assign(a => a._throwExceptions = alwaysThrow);
@@ -252,7 +252,7 @@ public T EnableTcpKeepAlive(TimeSpan keepAliveTime, TimeSpan keepAliveInterval)
252252
public T DeadTimeout(TimeSpan timeout) => Assign(a => a._deadTimeout = timeout);
253253

254254
/// <summary>
255-
/// Sets the maximum time a node can be marked dead.
255+
/// Sets the maximum time a node can be marked dead.
256256
/// Different implementations of IConnectionPool may choose a different default.
257257
/// </summary>
258258
/// <param name="timeout">The timeout in milliseconds</param>
@@ -279,7 +279,7 @@ public T Proxy(Uri proxyAdress, string username, string password)
279279
}
280280

281281
/// <summary>
282-
/// Forces all requests to have ?pretty=true, causing elasticsearch to return formatted json.
282+
/// Forces all requests to have ?pretty=true, causing elasticsearch to return formatted json.
283283
/// Also forces the client to send out formatted json. Defaults to false
284284
/// </summary>
285285
public T PrettyJson(bool b = true) => Assign(a =>
@@ -310,7 +310,7 @@ public T BasicAuthentication(string userName, string password)
310310
{
311311
this._basicAuthCredentials = new BasicAuthenticationCredentials
312312
{
313-
UserName = userName,
313+
Username = userName,
314314
Password = password
315315
};
316316
return (T)this;

Diff for: src/Elasticsearch.Net/Configuration/IConnectionConfigurationValues.cs

+13-13
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ public interface IConnectionConfigurationValues : IDisposable
1111

1212
/// <summary> The connection pool to use when talking with elasticsearch </summary>
1313
IConnectionPool ConnectionPool { get; }
14-
14+
1515
/// <summary> The connection implementation to use when talking with elasticsearch </summary>
1616
IConnection Connection { get; }
17-
17+
1818
/// <summary>The serializer to use to serialize requests and deserialize responses</summary>
1919
IElasticsearchSerializer Serializer { get; }
2020

@@ -32,7 +32,7 @@ public interface IConnectionConfigurationValues : IDisposable
3232
/// The time to put dead nodes out of rotation (this will be multiplied by the number of times they've been dead)
3333
/// </summary>
3434
TimeSpan? DeadTimeout { get; }
35-
35+
3636
/// <summary>
3737
/// The maximum ammount of time a node is allowed to marked dead
3838
/// </summary>
@@ -63,16 +63,16 @@ public interface IConnectionConfigurationValues : IDisposable
6363
/// <para>http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/modules-http.html</para>
6464
/// </summary>
6565
bool EnableHttpCompression { get; }
66-
66+
6767
/// <summary>
6868
/// When set will force all connections through this proxy
6969
/// </summary>
7070
string ProxyAddress { get; }
7171
string ProxyUsername { get; }
7272
string ProxyPassword { get; }
73-
73+
7474
/// <summary>
75-
/// Forces all requests to have ?pretty=true, causing elasticsearch to return formatted json.
75+
/// Forces all requests to have ?pretty=true, causing elasticsearch to return formatted json.
7676
/// Also forces the client to send out formatted json. Defaults to false
7777
/// </summary>
7878
bool PrettyJson { get; }
@@ -97,10 +97,10 @@ public interface IConnectionConfigurationValues : IDisposable
9797

9898
/// <summary>
9999
/// Instead of following a c/go like error checking on response.IsValid always throw an exception
100-
/// on the client when a call resulted in an exception on either the client or the Elasticsearch server.
100+
/// on the client when a call resulted in an exception on either the client or the Elasticsearch server.
101101
/// <para>Reasons for such exceptions could be search parser errors, index missing exceptions, etc...</para>
102102
/// </summary>
103-
bool ThrowExceptions { get; }
103+
bool ThrowExceptions { get; }
104104

105105
/// <summary>
106106
/// Sniff the cluster state immediatly on startup
@@ -137,18 +137,18 @@ public interface IConnectionConfigurationValues : IDisposable
137137
/// Basic access authorization credentials to specify with all requests.
138138
/// </summary>
139139
BasicAuthenticationCredentials BasicAuthenticationCredentials { get; }
140-
140+
141141
/// <summary>
142142
/// KeepAliveTime - specifies the timeout, in milliseconds, with no
143-
/// activity until the first keep-alive packet is sent.
143+
/// activity until the first keep-alive packet is sent.
144144
/// </summary>
145145
TimeSpan? KeepAliveTime { get; }
146146

147147
/// <summary>
148148
/// KeepAliveInterval - specifies the interval, in milliseconds, between
149-
/// when successive keep-alive packets are sent if no acknowledgement is
150-
/// received.
149+
/// when successive keep-alive packets are sent if no acknowledgement is
150+
/// received.
151151
/// </summary>
152152
TimeSpan? KeepAliveInterval { get; }
153153
}
154-
}
154+
}

Diff for: src/Elasticsearch.Net/Configuration/RequestConfiguration.cs

+47-19
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Elasticsearch.Net
66
{
7-
public interface IRequestConfiguration
7+
public interface IRequestConfiguration
88
{
99
/// <summary>
1010
/// The timeout for this specific request, takes precedence over the global timeout settings
@@ -20,7 +20,7 @@ public interface IRequestConfiguration
2020
/// Force a difference content type header on the request
2121
/// </summary>
2222
string ContentType { get; set; }
23-
23+
2424
/// <summary>
2525
/// This will override whatever is set on the connection configuration or whatever default the connectionpool has.
2626
/// </summary>
@@ -32,13 +32,13 @@ public interface IRequestConfiguration
3232
Uri ForceNode { get; set; }
3333

3434
/// <summary>
35-
/// Forces no sniffing to occur on the request no matter what configuration is in place
35+
/// Forces no sniffing to occur on the request no matter what configuration is in place
3636
/// globally
3737
/// </summary>
3838
bool? DisableSniff { get; set; }
3939

4040
/// <summary>
41-
/// Under no circumstance do a ping before the actual call. If a node was previously dead a small ping with
41+
/// Under no circumstance do a ping before the actual call. If a node was previously dead a small ping with
4242
/// low connect timeout will be tried first in normal circumstances
4343
/// </summary>
4444
bool? DisablePing { get; set; }
@@ -63,48 +63,76 @@ public interface IRequestConfiguration
6363
/// The cancellation token to use to internally to cancel async operations
6464
/// </summary>
6565
CancellationToken CancellationToken { get; set; }
66+
67+
/// <summary>
68+
/// Submit the request on behalf in the context of a different shield user
69+
/// <pre/>https://www.elastic.co/guide/en/shield/current/submitting-requests-for-other-users.html
70+
/// </summary>
71+
string RunAs { get; set; }
6672
}
6773

6874
public class RequestConfiguration : IRequestConfiguration
6975
{
7076
public TimeSpan? RequestTimeout { get; set; }
7177
public TimeSpan? PingTimeout { get; set; }
72-
public string ContentType { get; set; }
78+
public string ContentType { get; set; }
7379
public int? MaxRetries { get; set; }
7480
public Uri ForceNode { get; set; }
7581
public bool? DisableSniff { get; set; }
7682
public bool? DisablePing { get; set; }
7783
public IEnumerable<int> AllowedStatusCodes { get; set; }
7884
public BasicAuthenticationCredentials BasicAuthenticationCredentials { get; set; }
7985
public bool EnableHttpPipelining { get; set; } = true;
80-
public CancellationToken CancellationToken { get; set; }
86+
public CancellationToken CancellationToken { get; set; }
87+
/// <summary>
88+
/// Submit the request on behalf in the context of a different user
89+
/// https://www.elastic.co/guide/en/shield/current/submitting-requests-for-other-users.html
90+
/// </summary>
91+
public string RunAs { get; set; }
8192
}
8293

8394
public class RequestConfigurationDescriptor : IRequestConfiguration
8495
{
85-
private IRequestConfiguration Self => this;
8696

97+
private IRequestConfiguration Self => this;
8798
TimeSpan? IRequestConfiguration.RequestTimeout { get; set; }
88-
8999
TimeSpan? IRequestConfiguration.PingTimeout { get; set; }
90-
91100
string IRequestConfiguration.ContentType { get; set; }
92-
93101
int? IRequestConfiguration.MaxRetries { get; set; }
94-
95102
Uri IRequestConfiguration.ForceNode { get; set; }
96-
97103
bool? IRequestConfiguration.DisableSniff { get; set; }
98-
99104
bool? IRequestConfiguration.DisablePing { get; set; }
100-
101105
IEnumerable<int> IRequestConfiguration.AllowedStatusCodes { get; set; }
102-
103106
BasicAuthenticationCredentials IRequestConfiguration.BasicAuthenticationCredentials { get; set; }
104-
105107
bool IRequestConfiguration.EnableHttpPipelining { get; set; } = true;
106-
107108
CancellationToken IRequestConfiguration.CancellationToken { get; set; }
109+
string IRequestConfiguration.RunAs { get; set; }
110+
111+
public RequestConfigurationDescriptor(IRequestConfiguration config)
112+
{
113+
Self.RequestTimeout = config?.RequestTimeout;
114+
Self.PingTimeout = config?.PingTimeout;
115+
Self.ContentType = config?.ContentType;
116+
Self.MaxRetries = config?.MaxRetries;
117+
Self.ForceNode = config?.ForceNode;
118+
Self.DisableSniff = config?.DisableSniff;
119+
Self.DisablePing = config?.DisablePing;
120+
Self.AllowedStatusCodes = config?.AllowedStatusCodes;
121+
Self.BasicAuthenticationCredentials = config?.BasicAuthenticationCredentials;
122+
Self.EnableHttpPipelining = config?.EnableHttpPipelining ?? true;
123+
Self.CancellationToken = config?.CancellationToken ?? default(CancellationToken);
124+
Self.RunAs = config?.RunAs;
125+
}
126+
127+
/// <summary>
128+
/// Submit the request on behalf in the context of a different shield user
129+
/// <pre/>https://www.elastic.co/guide/en/shield/current/submitting-requests-for-other-users.html
130+
/// </summary>
131+
public RequestConfigurationDescriptor RunAs(string username)
132+
{
133+
Self.RunAs = username;
134+
return this;
135+
}
108136

109137
public RequestConfigurationDescriptor RequestTimeout(TimeSpan requestTimeout)
110138
{
@@ -168,7 +196,7 @@ public RequestConfigurationDescriptor BasicAuthentication(string userName, strin
168196
{
169197
if (Self.BasicAuthenticationCredentials == null)
170198
Self.BasicAuthenticationCredentials = new BasicAuthenticationCredentials();
171-
Self.BasicAuthenticationCredentials.UserName = userName;
199+
Self.BasicAuthenticationCredentials.Username = userName;
172200
Self.BasicAuthenticationCredentials.Password = password;
173201
return this;
174202
}
@@ -179,4 +207,4 @@ public RequestConfigurationDescriptor EnableHttpPipelining(bool enable = true)
179207
return this;
180208
}
181209
}
182-
}
210+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
3+
namespace Elasticsearch.Net
4+
{
5+
public class BasicAuthenticationCredentials
6+
{
7+
public string Username { get; set; }
8+
[Obsolete("Scheduled to be removed use Username instead, note the lowercase n")]
9+
public string UserName { get { return Username; } set { Username = value; } }
10+
public string Password { get; set; }
11+
12+
public override string ToString()
13+
{
14+
return this.Username + ":" + this.Password;
15+
}
16+
}
17+
}

Diff for: src/Elasticsearch.Net/Configuration/Security/BasicAuthorizationCredentials.cs

-13
This file was deleted.

Diff for: src/Elasticsearch.Net/Connection/HttpConnection.cs

+2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ protected virtual HttpWebRequest CreateWebRequest(RequestData requestData)
4949
request.Headers.Add("Accept-Encoding", "gzip,deflate");
5050
request.Headers.Add("Content-Encoding", "gzip");
5151
}
52+
if (!requestData.RunAs.IsNullOrEmpty())
53+
request.Headers.Add("es-shield-runas-user", requestData.RunAs);
5254

5355
if (requestData.Headers != null && requestData.Headers.HasKeys())
5456
request.Headers.Add(requestData.Headers);

0 commit comments

Comments
 (0)