Skip to content

Commit

Permalink
ApiKey: Add ApiKey authentication.
Browse files Browse the repository at this point in the history
  • Loading branch information
andrefsp committed Sep 18, 2024
1 parent ea8b863 commit 4d28021
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
6 changes: 6 additions & 0 deletions plugins/doc_fragments/login_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class ModuleDocFragment(object):
choices:
- ''
- http_auth
- api_key
default: ''
auth_scheme:
description:
Expand Down Expand Up @@ -57,6 +58,11 @@ class ModuleDocFragment(object):
required: no
type: int
default: 9200
api_key:
description:
- The ApiKey to authenticate with the server.
required: no
type: str
timeout:
description:
- Response timeout in seconds.
Expand Down
33 changes: 21 additions & 12 deletions plugins/module_utils/elastic_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ def elastic_common_argument_spec():
Returns a dict containing common options shared across the elastic modules
"""
options = dict(
auth_method=dict(type='str', choices=['', 'http_auth'], default=''),
auth_method=dict(type='str', choices=['', 'http_auth', 'api_key'], default=''),
auth_scheme=dict(type='str', choices=['http', 'https'], default='http'),
cafile=dict(type='str', default=None),
api_key=dict(type='str', default=None, no_log=True),
connection_options=dict(type='list', elements='dict', default=[]),
login_user=dict(type='str', required=False),
login_password=dict(type='str', required=False, no_log=True),
Expand All @@ -53,17 +54,25 @@ def build_auth(self, module):
Build the auth list for elastic according to the passed in parameters
'''
auth = {}
if module.params['auth_method'] != '':
if module.params['auth_method'] == 'http_auth':
auth["http_auth"] = (module.params['login_user'],
module.params['login_password'])

if module.params['cafile'] is not None:
from ssl import create_default_context
context = create_default_context(module.params['cafile'])
auth["ssl_context"] = context
else:
module.fail_json("Invalid or unsupported auth_method provided")
if not module.params['auth_method']:
return auth

if module.params['auth_method'] == 'http_auth':
# username/password authentication.
auth["http_auth"] = (module.params['login_user'],
module.params['login_password'])
elif module.params['auth_method'] == 'api_key':
# api key authentication.
auth["api_key"] = module.params['api_key']
else:
module.fail_json("Invalid or unsupported auth_method provided")

# CA file has been provided. Add it to auth dict
if module.params['cafile'] is not None:
from ssl import create_default_context
context = create_default_context(module.params['cafile'])
auth["ssl_context"] = context

return auth

def connect(self):
Expand Down

0 comments on commit 4d28021

Please sign in to comment.