@@ -297,22 +297,27 @@ def getdefault(name):
297
297
return lambda : os .environ .get (envvar , config .get (name ))
298
298
299
299
300
- def get_elasticsearch_client (cloud_id = None , elasticsearch_url = None , es_user = None , es_password = None , ctx = None , ** kwargs ):
300
+ def get_elasticsearch_client (cloud_id : str = None , elasticsearch_url : str = None , es_user : str = None ,
301
+ es_password : str = None , ctx : click .Context = None , api_key : str = None , ** kwargs ):
301
302
"""Get an authenticated elasticsearch client."""
302
303
from elasticsearch import AuthenticationException , Elasticsearch
303
304
304
305
if not (cloud_id or elasticsearch_url ):
305
306
client_error ("Missing required --cloud-id or --elasticsearch-url" )
306
307
307
308
# don't prompt for these until there's a cloud id or elasticsearch URL
308
- es_user = es_user or click .prompt ("es_user" )
309
- es_password = es_password or click .prompt ("es_password" , hide_input = True )
309
+ basic_auth : (str , str ) | None = None
310
+ if not api_key :
311
+ es_user = es_user or click .prompt ("es_user" )
312
+ es_password = es_password or click .prompt ("es_password" , hide_input = True )
313
+ basic_auth = (es_user , es_password )
314
+
310
315
hosts = [elasticsearch_url ] if elasticsearch_url else None
311
316
timeout = kwargs .pop ('timeout' , 60 )
312
317
kwargs ['verify_certs' ] = not kwargs .pop ('ignore_ssl_errors' , False )
313
318
314
319
try :
315
- client = Elasticsearch (hosts = hosts , cloud_id = cloud_id , http_auth = ( es_user , es_password ), timeout = timeout ,
320
+ client = Elasticsearch (hosts = hosts , cloud_id = cloud_id , http_auth = basic_auth , timeout = timeout , api_key = api_key ,
316
321
** kwargs )
317
322
# force login to test auth
318
323
client .info ()
@@ -322,16 +327,17 @@ def get_elasticsearch_client(cloud_id=None, elasticsearch_url=None, es_user=None
322
327
client_error (error_msg , e , ctx = ctx , err = True )
323
328
324
329
325
- def get_kibana_client (cloud_id , kibana_url , kibana_user , kibana_password , kibana_cookie , space , ignore_ssl_errors ,
326
- provider_type , provider_name , ** kwargs ):
330
+ def get_kibana_client (cloud_id : str , kibana_url : str , kibana_user : str , kibana_password : str , kibana_cookie : str ,
331
+ space : str , ignore_ssl_errors : bool , provider_type : str , provider_name : str , api_key : str ,
332
+ ** kwargs ):
327
333
"""Get an authenticated Kibana client."""
328
334
from requests import HTTPError
329
335
from kibana import Kibana
330
336
331
337
if not (cloud_id or kibana_url ):
332
338
client_error ("Missing required --cloud-id or --kibana-url" )
333
339
334
- if not kibana_cookie :
340
+ if not ( kibana_cookie or api_key ) :
335
341
# don't prompt for these until there's a cloud id or Kibana URL
336
342
kibana_user = kibana_user or click .prompt ("kibana_user" )
337
343
kibana_password = kibana_password or click .prompt ("kibana_password" , hide_input = True )
@@ -342,6 +348,9 @@ def get_kibana_client(cloud_id, kibana_url, kibana_user, kibana_password, kibana
342
348
if kibana_cookie :
343
349
kibana .add_cookie (kibana_cookie )
344
350
return kibana
351
+ elif api_key :
352
+ kibana .add_api_key (api_key )
353
+ return kibana
345
354
346
355
try :
347
356
kibana .login (kibana_user , kibana_password , provider_type = provider_type , provider_name = provider_name )
@@ -359,6 +368,7 @@ def get_kibana_client(cloud_id, kibana_url, kibana_user, kibana_password, kibana
359
368
'kibana' : {
360
369
'cloud_id' : click .Option (['--cloud-id' ], default = getdefault ('cloud_id' ),
361
370
help = "ID of the cloud instance." ),
371
+ 'api_key' : click .Option (['--api-key' ], default = getdefault ('api_key' )),
362
372
'kibana_cookie' : click .Option (['--kibana-cookie' , '-kc' ], default = getdefault ('kibana_cookie' ),
363
373
help = 'Cookie from an authed session' ),
364
374
'kibana_password' : click .Option (['--kibana-password' , '-kp' ], default = getdefault ('kibana_password' )),
@@ -373,6 +383,7 @@ def get_kibana_client(cloud_id, kibana_url, kibana_user, kibana_password, kibana
373
383
},
374
384
'elasticsearch' : {
375
385
'cloud_id' : click .Option (['--cloud-id' ], default = getdefault ("cloud_id" )),
386
+ 'api_key' : click .Option (['--api-key' ], default = getdefault ('api_key' )),
376
387
'elasticsearch_url' : click .Option (['--elasticsearch-url' ], default = getdefault ("elasticsearch_url" )),
377
388
'es_user' : click .Option (['--es-user' , '-eu' ], default = getdefault ("es_user" )),
378
389
'es_password' : click .Option (['--es-password' , '-ep' ], default = getdefault ("es_password" )),
0 commit comments