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 28 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
23 changes: 23 additions & 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
- kubernetes
chris93111 marked this conversation as resolved.
Show resolved Hide resolved
default: token
type: str
mount_point:
Expand Down Expand Up @@ -72,6 +73,15 @@ class ModuleDocFragment(object):
jwt:
description: The JSON Web Token (JWT) to use for JWT authentication to Vault.
type: str
kubernetes_token:
description: The Kubernetes Token (JWT) to use for Kubernetes authentication to Vault.
type: str
version_added: 2.4.0
briantist marked this conversation as resolved.
Show resolved Hide resolved
kubernetes_token_path:
description: If no kubernetes_token is specified, will try to read the token from this path.
default: '/var/run/secrets/kubernetes.io/serviceaccount/token'
type: str
version_added: 2.4.0
briantist marked this conversation as resolved.
Show resolved Hide resolved
aws_profile:
description: The AWS profile
type: str
Expand Down Expand Up @@ -245,4 +255,17 @@ class ModuleDocFragment(object):
ini:
- section: hashi_vault_collection
key: cert_auth_private_key
kubernetes_token:
env:
- name: ANSIBLE_HASHI_VAULT_KUBERNETES_TOKEN
vars:
- name: ansible_hashi_vault_kubernetes_token
kubernetes_token_path:
env:
- name: ANSIBLE_HASHI_VAULT_KUBERNETES_TOKEN_PATH
ini:
- section: hashi_vault_collection
key: kubernetes_token_path
vars:
- name: ansible_hashi_vault_kubernetes_token_path
'''
57 changes: 57 additions & 0 deletions plugins/module_utils/_auth_method_k8s.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# -*- 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: >=3.6'''

# 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, HashiVaultValueError
import os


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

NAME = 'kubernetes'
OPTIONS = ['kubernetes_token', 'kubernetes_token_path', 'role_id', 'mount_point']

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

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

if self._options.get_option('kubernetes_token') is None and self._options.get_option('kubernetes_token_path') is not None:
token_filename = self._options.get_option('kubernetes_token_path')
if os.path.exists(token_filename):
if not os.path.isfile(token_filename):
raise HashiVaultValueError("The Kubernetes token file '%s' was found but is not a file." % token_filename)
with open(token_filename) as token_file:
self._options.set_option('kubernetes_token', token_file.read().strip())

if self._options.get_option('kubernetes_token') is None:
raise HashiVaultValueError(self._options.get_option_default('kubernetes_token_path') +
" No Kubernetes Token specified or discovered.")
chris93111 marked this conversation as resolved.
Show resolved Hide resolved

def authenticate(self, client, use_token=True):
origin_params = self._options.get_filled_options(*self.OPTIONS)
params = {"role": origin_params.get('role_id'),
"jwt": origin_params.get('kubernetes_token'),
"mount_point": origin_params.get('mount_point'),
"use_token": use_token}

try:
response = client.auth.kubernetes.login(**params)
except (NotImplementedError, AttributeError):
self.warn("Kubernetes authentication requires HVAC version 1.0.0 or higher. Deprecated method 'auth_kubernetes' will be used.")
response = client.auth_kubernetes(**params)

return response
5 changes: 5 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 HashiVaultAuthMethodKubernetes


class HashiVaultAuthenticator():
Expand All @@ -36,6 +37,7 @@ class HashiVaultAuthenticator():
'jwt',
'cert',
'none',
'kubernetes',
chris93111 marked this conversation as resolved.
Show resolved Hide resolved
]),
mount_point=dict(type='str'),
token=dict(type='str', no_log=True, default=None),
Expand All @@ -47,6 +49,8 @@ class HashiVaultAuthenticator():
role_id=dict(type='str'),
secret_id=dict(type='str', no_log=True),
jwt=dict(type='str', no_log=True),
kubernetes_token=dict(type='str', no_log=True),
kubernetes_token_path=dict(type='str', default='/var/run/secrets/kubernetes.io/serviceaccount/token', no_log=False),
aws_profile=dict(type='str', aliases=['boto_profile']),
aws_access_key=dict(type='str', aliases=['aws_access_key_id'], no_log=False),
aws_secret_key=dict(type='str', aliases=['aws_secret_access_key'], no_log=True),
Expand All @@ -66,6 +70,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),
'kubernetes': HashiVaultAuthMethodKubernetes(option_adapter, warning_callback),
'ldap': HashiVaultAuthMethodLdap(option_adapter, warning_callback),
'none': HashiVaultAuthMethodNone(option_adapter, warning_callback),
'token': HashiVaultAuthMethodToken(option_adapter, warning_callback),
Expand Down