Skip to content

Commit fd266b1

Browse files
committed
Shield on CoreFX broken
- BasicAuthentication only listened to the authentication set on the request, not connectionsettings. - HttpConnection-CoreFX did not take RunAs into account - Maximum runtime for integration tests is now 30minutes, we are well under that luckily though :) Conflicts: paket.lock
1 parent ad18194 commit fd266b1

File tree

2 files changed

+50
-44
lines changed

2 files changed

+50
-44
lines changed

build/scripts/Paths.fsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,16 +288,16 @@ module Tooling =
288288
match (runtime, hasClr, hasCoreClr) with
289289
| (Core, _, Some c) ->
290290
let proc = c.Process exe
291-
execProcess proc arguments
291+
execProcessWithTimeout proc arguments (TimeSpan.FromMinutes 30.)
292292
| (Desktop, Some d, _) ->
293293
let proc = d.Process exe
294-
execProcess proc arguments
294+
execProcessWithTimeout proc arguments (TimeSpan.FromMinutes 30.)
295295
| (Both, Some d, Some c) ->
296296
let proc = d.Process exe
297297
let result = execProcess proc arguments
298298
if result <> 0 then failwith (sprintf "Failed to run dnx tooling for %s args: %A" proc arguments)
299299
let proc = c.Process exe
300-
execProcess proc arguments
300+
execProcessWithTimeout proc arguments (TimeSpan.FromMinutes 30.)
301301
| _ -> failwith "Tried to run dnx tooling in unknown state"
302302
|> ignore
303303

src/Elasticsearch.Net/Connection/HttpConnection-CoreFx.cs

Lines changed: 47 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -30,46 +30,46 @@ public class HttpConnection : IConnection
3030
private readonly object _lock = new object();
3131
private readonly ConcurrentDictionary<int, HttpClient> _clients = new ConcurrentDictionary<int, HttpClient>();
3232

33-
private string DefaultContentType => "application/json";
33+
private string DefaultContentType => "application/json";
3434

3535
public HttpConnection() { }
36-
36+
3737
private HttpClient GetClient(RequestData requestData)
3838
{
3939
var hashCode = requestData.GetHashCode();
4040
HttpClient client;
4141
if (this._clients.TryGetValue(hashCode, out client)) return client;
42-
lock(_lock)
42+
lock (_lock)
4343
{
4444
if (this._clients.TryGetValue(hashCode, out client)) return client;
4545

46-
var handler = new HttpClientHandler
47-
{
48-
AutomaticDecompression = requestData.HttpCompression ? GZip | Deflate : None
49-
};
50-
51-
if (!requestData.ProxyAddress.IsNullOrEmpty())
52-
{
53-
var uri = new Uri(requestData.ProxyAddress);
54-
var proxy = new WebProxy(uri);
55-
var credentials = new NetworkCredential(requestData.ProxyUsername, requestData.ProxyPassword);
56-
proxy.Credentials = credentials;
57-
handler.Proxy = proxy;
58-
}
59-
60-
if (requestData.DisableAutomaticProxyDetection)
61-
handler.Proxy = null;
62-
63-
client = new HttpClient(handler, false)
64-
{
65-
Timeout = requestData.RequestTimeout
66-
};
67-
68-
client.DefaultRequestHeaders.ExpectContinue = false;
69-
70-
//TODO add headers
71-
//client.DefaultRequestHeaders =
72-
this._clients.TryAdd(hashCode, client);
46+
var handler = new HttpClientHandler
47+
{
48+
AutomaticDecompression = requestData.HttpCompression ? GZip | Deflate : None
49+
};
50+
51+
if (!requestData.ProxyAddress.IsNullOrEmpty())
52+
{
53+
var uri = new Uri(requestData.ProxyAddress);
54+
var proxy = new WebProxy(uri);
55+
var credentials = new NetworkCredential(requestData.ProxyUsername, requestData.ProxyPassword);
56+
proxy.Credentials = credentials;
57+
handler.Proxy = proxy;
58+
}
59+
60+
if (requestData.DisableAutomaticProxyDetection)
61+
handler.Proxy = null;
62+
63+
client = new HttpClient(handler, false)
64+
{
65+
Timeout = requestData.RequestTimeout
66+
};
67+
68+
client.DefaultRequestHeaders.ExpectContinue = false;
69+
70+
//TODO add headers
71+
//client.DefaultRequestHeaders =
72+
this._clients.TryAdd(hashCode, client);
7373
return client;
7474
}
7575

@@ -122,13 +122,27 @@ private static HttpRequestMessage CreateHttpRequestMessage(RequestData requestDa
122122
var method = ConvertHttpMethod(requestData.Method);
123123
var requestMessage = new HttpRequestMessage(method, requestData.Uri);
124124

125-
foreach(string key in requestData.Headers)
125+
foreach (string key in requestData.Headers)
126126
{
127127
requestMessage.Headers.TryAddWithoutValidation(key, requestData.Headers.GetValues(key));
128128
}
129129

130130
requestMessage.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(requestData.ContentType));
131131

132+
if (!requestData.RunAs.IsNullOrEmpty())
133+
requestMessage.Headers.Add("es-shield-runas-user", requestData.RunAs);
134+
135+
string userInfo = null;
136+
if (!requestData.Uri.UserInfo.IsNullOrEmpty())
137+
userInfo = Uri.UnescapeDataString(requestData.Uri.UserInfo);
138+
else if (requestData.BasicAuthorizationCredentials != null)
139+
userInfo = requestData.BasicAuthorizationCredentials.ToString();
140+
if (!userInfo.IsNullOrEmpty())
141+
{
142+
var credentials = Convert.ToBase64String(Encoding.UTF8.GetBytes(userInfo));
143+
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Basic", credentials);
144+
}
145+
132146
var data = requestData.PostData;
133147

134148
if (data != null)
@@ -153,18 +167,10 @@ private static HttpRequestMessage CreateHttpRequestMessage(RequestData requestDa
153167
else
154168
{
155169
// Set content in order to set a Content-Type header.
156-
// Content gets diposed so can't be shared instance
170+
// Content gets diposed so can't be shared instance
157171
requestMessage.Content = new ByteArrayContent(new byte[0]);
158172
}
159-
160173
requestMessage.Content.Headers.ContentType = new MediaTypeHeaderValue(requestData.ContentType);
161-
162-
if (!string.IsNullOrWhiteSpace(requestData.Uri.UserInfo))
163-
{
164-
var credentials = Convert.ToBase64String(Encoding.UTF8.GetBytes(requestMessage.RequestUri.UserInfo));
165-
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Basic", credentials);
166-
}
167-
168174
return requestMessage;
169175
}
170176

@@ -186,7 +192,7 @@ private static System.Net.Http.HttpMethod ConvertHttpMethod(HttpMethod httpMetho
186192

187193
protected virtual void DisposeManagedResources()
188194
{
189-
foreach(var c in _clients)
195+
foreach (var c in _clients)
190196
c.Value.Dispose();
191197
}
192198
}

0 commit comments

Comments
 (0)