-
Notifications
You must be signed in to change notification settings - Fork 72
[GH-352] kubernetes auth for lookup #353
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
Open
pfeifferj
wants to merge
23
commits into
ansible-collections:main
Choose a base branch
from
pfeifferj:patch-1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 22 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
ed5be2c
Create _auth_method_k8s.py, add k8s auth, add role params for k8s auth
chris93111 795fa08
Update plugins/lookup/hashi_vault.py
chris93111 a035ef2
change to auth.kubernetes + switch depracated
chris93111 16fb27f
Bump version_added
briantist 420cf1e
remove old conflict artifact
briantist 88f3094
[chore] use relative imports
briantist 8d76146
update auth method name
briantist bcbeb9d
nit: change to preferred style
briantist b4653aa
add required deprecate_callback to init
briantist dfb9ba5
draft setup k8s auth int/unit tests
pfeifferj c80f36d
rebase
pfeifferj dad910a
Merge branch 'ansible-collections:main' into patch-1
pfeifferj 40b9981
Merge branch 'ansible-collections:main' into patch-1
pfeifferj 73132b8
Fix Kubernetes auth implementation issues
pfeifferj 50cb515
Add secret_field option for Ansible 2.17+ compatibility
pfeifferj 8f017cf
Add missing canary variable for K8s integration tests
pfeifferj 794e8ad
Merge branch 'main' of https://github.com/pfeifferj/community.hashi_v…
pfeifferj 24e4947
Merge branch 'main' of https://github.com/pfeifferj/community.hashi_v…
pfeifferj b838dcb
Merge branch 'main' of https://github.com/pfeifferj/community.hashi_v…
pfeifferj 24c4324
Fix Kubernetes auth integration test for Vault 1.21.0
pfeifferj af038c8
Fix Kubernetes auth integration test for Vault 1.21.0
pfeifferj df115f2
Merge branch 'patch-1' of https://github.com/pfeifferj/community.hash…
pfeifferj 88b4cc7
chore: correct version_added
pfeifferj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| # -*- 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 ..module_utils._hashi_vault_common import HashiVaultAuthMethodBase, HashiVaultValueError | ||
| import os | ||
|
|
||
|
|
||
| class HashiVaultAuthMethodKubernetes(HashiVaultAuthMethodBase): | ||
| '''HashiVault option group class for auth: kubernetes''' | ||
|
|
||
| NAME = 'kubernetes' | ||
| OPTIONS = ['kubernetes_token', 'kubernetes_token_path', 'role_id', 'mount_point'] | ||
|
|
||
| def __init__(self, option_adapter, warning_callback, deprecate_callback): | ||
| super(HashiVaultAuthMethodKubernetes, self).__init__(option_adapter, warning_callback, deprecate_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." | ||
| ) | ||
|
|
||
| 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'), | ||
| "use_token": use_token, | ||
| } | ||
|
|
||
| if 'mount_point' in origin_params: | ||
| params['mount_point'] = origin_params['mount_point'] | ||
|
|
||
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| vault/auth/kubernetes | ||
| context/target | ||
| needs/target/setup_vault_configure | ||
| needs/target/setup_vault_test_plugins |
10 changes: 10 additions & 0 deletions
10
tests/integration/targets/auth_kubernetes/defaults/main.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| --- | ||
| ansible_hashi_vault_url: "{{ vault_test_server_http }}" | ||
| ansible_hashi_vault_auth_method: kubernetes | ||
|
|
||
| auth_paths: | ||
| - kubernetes | ||
|
|
||
| vault_kubernetes_canary: | ||
| path: cubbyhole/configure_kubernetes | ||
| value: complete # value does not matter |
1 change: 1 addition & 0 deletions
1
tests/integration/targets/auth_kubernetes/files/service_account_token.jwt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOlsidGVzdCJdLCJleHAiOjMyNDk5MDUxMzU5LCJpYXQiOjE2MDQ4MzUxMDAsImlzcyI6Imh0dHBzOi8va3ViZXJuZXRlcy5kZWZhdWx0LnN2Yy5jbHVzdGVyLmxvY2FsIiwia3ViZXJuZXRlcy5pbyI6eyJuYW1lc3BhY2UiOiJkZWZhdWx0Iiwic2VydmljZWFjY291bnQiOnsibmFtZSI6InZhdWx0LWF1dGgiLCJ1aWQiOiJkNzdjMWZlYi1jOTE5LTQxMDYtYWE4YS01MGY2N2MzYTE2MWUifX0sIm5iZiI6MTYwNDgzNTEwMCwic3ViIjoic3lzdGVtOnNlcnZpY2VhY2NvdW50OmRlZmF1bHQ6dmF1bHQtYXV0aCJ9.qfOfj4FeVBlBoWbaME6UnDCqKXE2FXKyil3ia1lDMaqcc87fYYE4yVByhqCSJy2pToO2MW3mYBKwfgfKxKaZj64hMh3yu1RNRASvWAVi4JM4nrBtdXei24e3WgRIjYOJnaHuypYtiBfHC-ArNJN_AhoGI00SP7Mx_ohcxtHbBcIE15Dlbh9eHOVtQSSMazaOAyQQLUybNmw9PDD0wwLnKgiky35dG03tZWd2pUttDWKlTzuIDPHyUyY9CYe-C3rVidJ6Vpw9atIO6NcffD5dvXujY1boUVO52xhcNCyL7UX1YqjcaqgUvEtUN1ridAb_rispVNzbe2w1cp41CqGHcQ |
1 change: 1 addition & 0 deletions
1
tests/integration/targets/auth_kubernetes/files/service_account_token_invalid.jwt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiIxMjM0IiwidXNlcl9jbGFpbSI6InVzZXJfY2xhaW0iLCJuYmYiOjE2MDQ4MzUxMDAsImV4cCI6MzI0OTkwNTEzNTl9.etc2WSH7kR3fHFlVt4wlBYFKNn7Z4DQcRVXUK4gGF-Q |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| --- | ||
| dependencies: | ||
| - setup_vault_test_plugins | ||
| - setup_vault_configure |
40 changes: 40 additions & 0 deletions
40
tests/integration/targets/auth_kubernetes/tasks/kubernetes_setup.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| - name: "Setup block" | ||
| vars: | ||
| is_default_path: "{{ this_path == default_path }}" | ||
| jwt_public_key: | | ||
| -----BEGIN PUBLIC KEY----- | ||
| MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvUJ8sf9bUQtAyESZ73mw | ||
| LCHjA8cj2ly3GTy5fxsyniUvkl6x+fVOXYqxgDYc9m14iaUXjE2eUIdQOnmRNIAz | ||
| xIhxKvXMhJVRSnjCcdQZ9FUPyvQ1bcNo6rISOEEroUdQ6ymX6/DKhW/tXPrYPFyK | ||
| 502UF+YhuP+t/OdKT4qbIxHvkDyGorJDlfKDx9wIr88xY4+KFLKSkaCsO6fZvmhw | ||
| L2pVl5UfJ/mYXZdeJ8b8pJLSnWmKzXwamGNnvJK3Z4DwicL6Z9PS8ryZWMAUswFR | ||
| menCQdtebBuaKi401N9SlPl/qb3nFW3YVO1NrqGvF9nprdtBNN3++0a1fuMUmPhb | ||
| 1QIDAQAB | ||
| -----END PUBLIC KEY----- | ||
| block: | ||
| - name: "Enable the Kubernetes auth method" | ||
| vault_ci_enable_auth: | ||
| method_type: kubernetes | ||
| path: "{{ omit if is_default_path else this_path }}" | ||
| config: | ||
| default_lease_ttl: 60m | ||
|
|
||
| - name: "Configure the Kubernetes auth method" | ||
| vault_ci_write: | ||
| path: "auth/{{ this_path }}/config" | ||
| data: | ||
| kubernetes_host: "http://mmock:8900" | ||
| pem_keys: | | ||
| {{ jwt_public_key }} | ||
| disable_local_ca_jwt: true | ||
| disable_iss_validation: true | ||
|
|
||
| - name: "Create a named role" | ||
| vault_ci_write: | ||
| path: "auth/{{ this_path }}/role/test-role" | ||
| data: | ||
| bound_service_account_names: "*" | ||
| bound_service_account_namespaces: "*" | ||
| policies: "{{ 'test-policy' if is_default_path else 'alt-policy' }}" | ||
| ttl: 60m | ||
| audience: "test" |
38 changes: 38 additions & 0 deletions
38
tests/integration/targets/auth_kubernetes/tasks/kubernetes_test_controller.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| - name: "Test block" | ||
| vars: | ||
| kubernetes_token: '{{ lookup("file", "service_account_token.jwt") }}' | ||
| kubernetes_token_invalid: '{{ lookup("file", "service_account_token_invalid.jwt") }}' | ||
| is_default_path: "{{ this_path == default_path }}" | ||
| kwargs_common: | ||
| role_id: test-role | ||
| kwargs_mount: "{{ {} if is_default_path else {'mount_point': this_path} }}" | ||
| kwargs_kubernetes_token: | ||
| kubernetes_token: "{{ kubernetes_token }}" | ||
| kwargs: "{{ kwargs_common | combine(kwargs_mount) | combine(kwargs_kubernetes_token) }}" | ||
| block: | ||
| # the purpose of this test is to catch when the plugin accepts mount_point but does not pass it into hvac | ||
| # we set the policy of the default mount to deny access to this secret and so we expect failure when the mount | ||
| # is default, and success when the mount is alternate | ||
| - name: Check auth mount differing result | ||
| set_fact: | ||
| response: "{{ lookup('vault_test_auth', **kwargs) }}" | ||
|
|
||
| - assert: | ||
| fail_msg: "A token from mount path '{{ this_path }}' had the wrong policy: {{ response.login.auth.policies }}" | ||
| that: | ||
| - ('test-policy' in response.login.auth.policies) | bool == is_default_path | ||
| - ('test-policy' not in response.login.auth.policies) | bool != is_default_path | ||
| - ('alt-policy' in response.login.auth.policies) | bool != is_default_path | ||
| - ('alt-policy' not in response.login.auth.policies) | bool == is_default_path | ||
|
|
||
| - name: Failure expected when erroneous credentials are used | ||
| vars: | ||
| kwargs_invalid: "{{ kwargs_common | combine(kwargs_mount) | combine({'kubernetes_token': kubernetes_token_invalid}) }}" | ||
| set_fact: | ||
| response: "{{ lookup('vault_test_auth', want_exception=true, **kwargs_invalid) }}" | ||
|
|
||
| - assert: | ||
| fail_msg: "An invalid JWT somehow did not cause a failure." | ||
| that: | ||
| - response is failed | ||
| - response.msg is search('permission denied|no known key successfully validated the token signature') |
39 changes: 39 additions & 0 deletions
39
tests/integration/targets/auth_kubernetes/tasks/kubernetes_test_target.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| - name: "Test block" | ||
| vars: | ||
| is_default_path: "{{ this_path == default_path }}" | ||
| kubernetes_token: '{{ lookup("file", "service_account_token.jwt") }}' | ||
| kubernetes_token_invalid: '{{ lookup("file", "service_account_token_invalid.jwt") }}' | ||
| module_defaults: | ||
| vault_test_auth: | ||
| url: '{{ ansible_hashi_vault_url }}' | ||
| auth_method: '{{ ansible_hashi_vault_auth_method }}' | ||
| role_id: test-role | ||
| mount_point: '{{ omit if is_default_path else this_path }}' | ||
| kubernetes_token: '{{ kubernetes_token }}' | ||
| block: | ||
| # the purpose of this test is to catch when the plugin accepts mount_point but does not pass it into hvac | ||
| # we set the policy of the default mount to deny access to this secret and so we expect failure when the mount | ||
| # is default, and success when the mount is alternate | ||
| - name: Check auth mount differing result | ||
| register: response | ||
| vault_test_auth: | ||
|
|
||
| - assert: | ||
| fail_msg: "A token from mount path '{{ this_path }}' had the wrong policy: {{ response.login.auth.policies }}" | ||
| that: | ||
| - ('test-policy' in response.login.auth.policies) | bool == is_default_path | ||
| - ('test-policy' not in response.login.auth.policies) | bool != is_default_path | ||
| - ('alt-policy' in response.login.auth.policies) | bool != is_default_path | ||
| - ('alt-policy' not in response.login.auth.policies) | bool == is_default_path | ||
|
|
||
| - name: Failure expected when erroneous credentials are used | ||
| register: response | ||
| vault_test_auth: | ||
| kubernetes_token: '{{ kubernetes_token_invalid }}' | ||
| want_exception: yes | ||
|
|
||
| - assert: | ||
| fail_msg: "An invalid Kubernetes Service Account Token somehow did not cause a failure." | ||
| that: | ||
| - response.inner is failed | ||
| - response.msg is search('permission denied|no known key successfully validated the token signature') |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.