1
1
use std:: collections:: HashMap ;
2
2
3
- use aws_credential_types:: provider:: SharedCredentialsProvider ;
4
- use aws_types:: region:: Region ;
5
3
use bytes:: { Buf , Bytes } ;
6
4
use http:: { Response , StatusCode , Uri } ;
7
5
use hyper:: { body, Body } ;
@@ -15,11 +13,12 @@ use super::{
15
13
InvalidHostSnafu , Request ,
16
14
} ;
17
15
use crate :: {
18
- http:: { Auth , HttpClient , MaybeAuth } ,
16
+ http:: { HttpClient , MaybeAuth } ,
19
17
sinks:: {
20
18
elasticsearch:: {
21
- ElasticsearchAuth , ElasticsearchCommonMode , ElasticsearchConfig , ParseError ,
19
+ ElasticsearchAuthConfig , ElasticsearchCommonMode , ElasticsearchConfig , ParseError ,
22
20
} ,
21
+ util:: auth:: Auth ,
23
22
util:: { http:: RequestConfig , TowerRequestConfig , UriSerde } ,
24
23
HealthcheckError ,
25
24
} ,
@@ -31,12 +30,10 @@ use crate::{
31
30
pub struct ElasticsearchCommon {
32
31
pub base_url : String ,
33
32
pub bulk_uri : Uri ,
34
- pub http_auth : Option < Auth > ,
35
- pub aws_auth : Option < SharedCredentialsProvider > ,
33
+ pub auth : Option < Auth > ,
36
34
pub mode : ElasticsearchCommonMode ,
37
35
pub request_builder : ElasticsearchRequestBuilder ,
38
36
pub tls_settings : TlsSettings ,
39
- pub region : Option < Region > ,
40
37
pub request : RequestConfig ,
41
38
pub query_params : HashMap < String , String > ,
42
39
pub metric_to_log : MetricToLog ,
@@ -61,31 +58,35 @@ impl ElasticsearchCommon {
61
58
. into ( ) ) ;
62
59
}
63
60
64
- let authorization = match & config. auth {
65
- Some ( ElasticsearchAuth :: Basic { user, password } ) => Some ( Auth :: Basic {
66
- user : user. clone ( ) ,
67
- password : password. clone ( ) ,
68
- } ) ,
69
- _ => None ,
70
- } ;
71
61
let uri = endpoint. parse :: < UriSerde > ( ) ?;
72
- let http_auth = authorization. choose_one ( & uri. auth ) ?;
73
- let base_url = uri. uri . to_string ( ) . trim_end_matches ( '/' ) . to_owned ( ) ;
74
-
75
- let aws_auth = match & config. auth {
76
- Some ( ElasticsearchAuth :: Basic { .. } ) | None => None ,
77
- Some ( ElasticsearchAuth :: Aws ( aws) ) => {
62
+ let auth = match & config. auth {
63
+ Some ( ElasticsearchAuthConfig :: Basic { user, password } ) => {
64
+ let auth = Some ( crate :: http:: Auth :: Basic {
65
+ user : user. clone ( ) ,
66
+ password : password. clone ( ) ,
67
+ } ) ;
68
+ // basic auth must be some for now
69
+ let auth = auth. choose_one ( & uri. auth ) ?. unwrap ( ) ;
70
+ Some ( Auth :: Basic ( auth) )
71
+ }
72
+ #[ cfg( feature = "aws-core" ) ]
73
+ Some ( ElasticsearchAuthConfig :: Aws ( aws) ) => {
78
74
let region = config
79
75
. aws
80
76
. as_ref ( )
81
77
. map ( |config| config. region ( ) )
82
78
. ok_or ( ParseError :: RegionRequired ) ?
83
79
. ok_or ( ParseError :: RegionRequired ) ?;
84
-
85
- Some ( aws. credentials_provider ( region) . await ?)
80
+ Some ( Auth :: Aws {
81
+ credentials_provider : aws. credentials_provider ( region. clone ( ) ) . await ?,
82
+ region,
83
+ } )
86
84
}
85
+ None => None ,
87
86
} ;
88
87
88
+ let base_url = uri. uri . to_string ( ) . trim_end_matches ( '/' ) . to_owned ( ) ;
89
+
89
90
let mode = config. common_mode ( ) ?;
90
91
91
92
let tower_request = config
@@ -126,8 +127,6 @@ impl ElasticsearchCommon {
126
127
metric_config. metric_tag_values ,
127
128
) ;
128
129
129
- let region = config. aws . as_ref ( ) . and_then ( |config| config. region ( ) ) ;
130
-
131
130
let version = if let Some ( version) = * version {
132
131
version
133
132
} else {
@@ -136,16 +135,7 @@ impl ElasticsearchCommon {
136
135
ElasticsearchApiVersion :: V7 => 7 ,
137
136
ElasticsearchApiVersion :: V8 => 8 ,
138
137
ElasticsearchApiVersion :: Auto => {
139
- match get_version (
140
- & base_url,
141
- & http_auth,
142
- & aws_auth,
143
- & region,
144
- & request,
145
- & tls_settings,
146
- proxy_config,
147
- )
148
- . await
138
+ match get_version ( & base_url, & auth, & request, & tls_settings, proxy_config) . await
149
139
{
150
140
Ok ( version) => {
151
141
debug ! ( message = "Auto-detected Elasticsearch API version." , %version) ;
@@ -195,15 +185,13 @@ impl ElasticsearchCommon {
195
185
} ;
196
186
197
187
Ok ( Self {
198
- http_auth ,
188
+ auth ,
199
189
base_url,
200
190
bulk_uri,
201
- aws_auth,
202
191
mode,
203
192
request_builder,
204
193
query_params,
205
194
request,
206
- region,
207
195
tls_settings,
208
196
metric_to_log,
209
197
} )
@@ -248,9 +236,7 @@ impl ElasticsearchCommon {
248
236
pub async fn healthcheck ( self , client : HttpClient ) -> crate :: Result < ( ) > {
249
237
match get (
250
238
& self . base_url ,
251
- & self . http_auth ,
252
- & self . aws_auth ,
253
- & self . region ,
239
+ & self . auth ,
254
240
& self . request ,
255
241
client,
256
242
"/_cluster/health" ,
@@ -264,19 +250,18 @@ impl ElasticsearchCommon {
264
250
}
265
251
}
266
252
253
+ #[ cfg( feature = "aws-core" ) ]
267
254
pub async fn sign_request (
268
255
request : & mut http:: Request < Bytes > ,
269
- credentials_provider : & SharedCredentialsProvider ,
270
- region : & Option < Region > ,
256
+ credentials_provider : & aws_credential_types :: provider :: SharedCredentialsProvider ,
257
+ region : & Option < aws_types :: region :: Region > ,
271
258
) -> crate :: Result < ( ) > {
272
259
crate :: aws:: sign_request ( "es" , request, credentials_provider, region) . await
273
260
}
274
261
275
262
async fn get_version (
276
263
base_url : & str ,
277
- http_auth : & Option < Auth > ,
278
- aws_auth : & Option < SharedCredentialsProvider > ,
279
- region : & Option < Region > ,
264
+ auth : & Option < Auth > ,
280
265
request : & RequestConfig ,
281
266
tls_settings : & TlsSettings ,
282
267
proxy_config : & ProxyConfig ,
@@ -291,7 +276,7 @@ async fn get_version(
291
276
}
292
277
293
278
let client = HttpClient :: new ( tls_settings. clone ( ) , proxy_config) ?;
294
- let response = get ( base_url, http_auth , aws_auth , region , request, client, "/" )
279
+ let response = get ( base_url, auth , request, client, "/" )
295
280
. await
296
281
. map_err ( |error| format ! ( "Failed to get Elasticsearch API version: {}" , error) ) ?;
297
282
@@ -314,28 +299,34 @@ async fn get_version(
314
299
315
300
async fn get (
316
301
base_url : & str ,
317
- http_auth : & Option < Auth > ,
318
- aws_auth : & Option < SharedCredentialsProvider > ,
319
- region : & Option < Region > ,
302
+ auth : & Option < Auth > ,
320
303
request : & RequestConfig ,
321
304
client : HttpClient ,
322
305
path : & str ,
323
306
) -> crate :: Result < Response < Body > > {
324
307
let mut builder = Request :: get ( format ! ( "{}{}" , base_url, path) ) ;
325
308
326
- if let Some ( authorization) = & http_auth {
327
- builder = authorization. apply_builder ( builder) ;
328
- }
329
-
330
309
for ( header, value) in & request. headers {
331
310
builder = builder. header ( & header[ ..] , & value[ ..] ) ;
332
311
}
333
-
334
312
let mut request = builder. body ( Bytes :: new ( ) ) ?;
335
313
336
- if let Some ( credentials_provider) = aws_auth {
337
- sign_request ( & mut request, credentials_provider, region) . await ?;
314
+ if let Some ( auth) = auth {
315
+ match auth {
316
+ Auth :: Basic ( http_auth) => {
317
+ http_auth. apply ( & mut request) ;
318
+ }
319
+ #[ cfg( feature = "aws-core" ) ]
320
+ Auth :: Aws {
321
+ credentials_provider : provider,
322
+ region,
323
+ } => {
324
+ let region = region. clone ( ) ;
325
+ sign_request ( & mut request, provider, & Some ( region) ) . await ?;
326
+ }
327
+ }
338
328
}
329
+
339
330
client
340
331
. send ( request. map ( hyper:: Body :: from) )
341
332
. await
0 commit comments