Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add k8s auth to lookup hashi_vault - operator #220

Open
wants to merge 35 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
bfd798a
Create _auth_method_k8s.py
chris93111 Feb 13, 2022
f065345
add k8s auth
chris93111 Feb 13, 2022
4a5e245
Add role params for k8s auth
chris93111 Feb 13, 2022
e1d3581
Update auth.py
chris93111 Feb 13, 2022
1355f7d
use role_id
chris93111 Feb 13, 2022
1c344ef
use role_id
chris93111 Feb 13, 2022
86293c0
Update _authenticator.py
chris93111 Feb 13, 2022
38c5833
swith to role_id
chris93111 Feb 13, 2022
6a41d72
Update plugins/module_utils/_auth_method_k8s.py
chris93111 Feb 13, 2022
19df02f
change k8s to kubernetes
chris93111 Feb 13, 2022
24c960f
kubernetes_token
chris93111 Feb 13, 2022
ecb09ae
k8s to kubernetes + kubernetes_token
chris93111 Feb 13, 2022
6c62724
add same validate of token auth
chris93111 Feb 13, 2022
8352337
add doc on params kubernetes_token_path
chris93111 Feb 13, 2022
ee819aa
add kubernetes_token_path
chris93111 Feb 13, 2022
bb55aed
add HashiVaultValueError
chris93111 Feb 13, 2022
0e68f9f
Update hashi_vault.py
chris93111 Feb 13, 2022
963417f
fix
chris93111 Feb 14, 2022
6f5a77f
fix
chris93111 Feb 14, 2022
bc662bb
Update auth.py
chris93111 Feb 14, 2022
fcc80b3
Update plugins/lookup/hashi_vault.py
chris93111 Feb 16, 2022
2ad2618
Update plugins/doc_fragments/auth.py
chris93111 Feb 16, 2022
2ca793b
Update plugins/doc_fragments/auth.py
chris93111 Feb 16, 2022
42d7f71
change to auth.kubernetes + switch depracated
chris93111 Feb 16, 2022
0bf56f7
fix login
chris93111 Feb 16, 2022
79c21e2
use_token
chris93111 Feb 16, 2022
d9c856b
lint
chris93111 Mar 4, 2022
ee611ed
fix error with no token found
chris93111 Mar 4, 2022
f33bcc4
lint
chris93111 Mar 5, 2022
27f9283
lint
chris93111 Mar 5, 2022
4e674cb
Merge pull request #1 from ansible-collections/main
chris93111 Mar 5, 2022
897fe87
lint
chris93111 Apr 1, 2022
fd99231
Bump version_added
briantist Apr 1, 2022
d49061d
Merge branch 'main' into patch-1
chris93111 May 15, 2022
39b6f09
Merge pull request #2 from ansible-collections/main
chris93111 Aug 6, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions plugins/doc_fragments/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class ModuleDocFragment(object):
- jwt
- cert
- none
- k8s
chris93111 marked this conversation as resolved.
Show resolved Hide resolved
default: token
type: str
mount_point:
Expand Down
48 changes: 48 additions & 0 deletions plugins/module_utils/_auth_method_k8s.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2021 FERREIRA Christophe (@chris93111)
# Simplified BSD License (see licenses/simplified_bsd.txt or https://opensource.org/licenses/BSD-2-Clause)

'''Python versions supported: all controller-side versions, all remote-side versions except 2.6'''
chris93111 marked this conversation as resolved.
Show resolved Hide resolved

# FOR INTERNAL COLLECTION USE ONLY
# The interfaces in this file are meant for use within the community.hashi_vault collection
# and may not remain stable to outside uses. Changes may be made in ANY release, even a bugfix release.
# See also: https://github.com/ansible/community/issues/539#issuecomment-780839686
# Please open an issue if you have questions about this.

from __future__ import absolute_import, division, print_function
__metaclass__ = type

from ansible_collections.community.hashi_vault.plugins.module_utils._hashi_vault_common import HashiVaultAuthMethodBase


class HashiVaultAuthMethodK8S(HashiVaultAuthMethodBase):
'''HashiVault option group class for auth: k8s'''

NAME = 'k8s'
OPTIONS = ['jwt', 'role_id', 'mount_point']
chris93111 marked this conversation as resolved.
Show resolved Hide resolved

def __init__(self, option_adapter, warning_callback):
super(HashiVaultAuthMethodK8S, self).__init__(option_adapter, warning_callback)

def validate(self):
self.validate_by_required_fields('role_id')

def authenticate(self, client, use_token=True):
params = self._options.get_filled_options(*self.OPTIONS)
if not params.get('jwt'):
# Mode in cluster fetch jwt in pods
try:
f = open('/var/run/secrets/kubernetes.io/serviceaccount/token')
jwt = f.read()
params['jwt'] = jwt
except:
raise NotImplementedError("Can't read jwt in /var/run/secrets/kubernetes.io/serviceaccount/token")
params['role'] = params.pop('role_id')
chris93111 marked this conversation as resolved.
Show resolved Hide resolved

try:
response = client.auth_kubernetes(**params)
chris93111 marked this conversation as resolved.
Show resolved Hide resolved
except (NotImplementedError, AttributeError):
raise NotImplementedError("K8S authentication requires HVAC version 0.8.0 or higher.")

return response
3 changes: 3 additions & 0 deletions plugins/module_utils/_authenticator.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from ansible_collections.community.hashi_vault.plugins.module_utils._auth_method_none import HashiVaultAuthMethodNone
from ansible_collections.community.hashi_vault.plugins.module_utils._auth_method_token import HashiVaultAuthMethodToken
from ansible_collections.community.hashi_vault.plugins.module_utils._auth_method_userpass import HashiVaultAuthMethodUserpass
from ansible_collections.community.hashi_vault.plugins.module_utils._auth_method_k8s import HashiVaultAuthMethodK8S
chris93111 marked this conversation as resolved.
Show resolved Hide resolved


class HashiVaultAuthenticator():
Expand All @@ -36,6 +37,7 @@ class HashiVaultAuthenticator():
'jwt',
'cert',
'none',
'k8s',
]),
mount_point=dict(type='str'),
token=dict(type='str', no_log=True, default=None),
Expand Down Expand Up @@ -66,6 +68,7 @@ def __init__(self, option_adapter, warning_callback):
'aws_iam': HashiVaultAuthMethodAwsIam(option_adapter, warning_callback),
'cert': HashiVaultAuthMethodCert(option_adapter, warning_callback),
'jwt': HashiVaultAuthMethodJwt(option_adapter, warning_callback),
'k8s': HashiVaultAuthMethodK8S(option_adapter, warning_callback),
chris93111 marked this conversation as resolved.
Show resolved Hide resolved
'ldap': HashiVaultAuthMethodLdap(option_adapter, warning_callback),
'none': HashiVaultAuthMethodNone(option_adapter, warning_callback),
'token': HashiVaultAuthMethodToken(option_adapter, warning_callback),
Expand Down