| 1 |
from __future__ import absolute_import, division, print_function |
| 2 |
|
| 3 |
__metaclass__ = type |
| 4 |
|
| 5 |
import warnings |
| 6 |
|
| 7 |
|
| 8 |
def list_dict_str(value): |
| 9 |
if isinstance(value, (list, dict, str)): |
| 10 |
return value |
| 11 |
raise TypeError |
| 12 |
|
| 13 |
|
| 14 |
def extract_sensitive_values_from_kubeconfig(kubeconfig_data): |
| 15 |
""" |
| 16 |
Extract only sensitive string values from kubeconfig data for no_log_values. |
| 17 |
|
| 18 |
:arg kubeconfig_data: Dictionary containing kubeconfig data |
| 19 |
:returns: Set of sensitive string values to be added to no_log_values |
| 20 |
""" |
| 21 |
values = set() |
| 22 |
sensitive_fields = { |
| 23 |
"token", |
| 24 |
"password", |
| 25 |
"secret", |
| 26 |
"client-key-data", |
| 27 |
"client-certificate-data", |
| 28 |
"certificate-authority-data", |
| 29 |
"api_key", |
| 30 |
"access-token", |
| 31 |
"refresh-token", |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
if isinstance(kubeconfig_data, dict): |
| 36 |
api_version = kubeconfig_data.get("apiVersion", "v1") |
| 37 |
if api_version != "v1": |
| 38 |
warnings.warn( |
| 39 |
f"Kubeconfig API version '{api_version}' is not 'v1'. " |
| 40 |
f"Sensitive field redaction is only guaranteed for API version 'v1'. " |
| 41 |
f"Some sensitive data may not be properly redacted from the logs.", |
| 42 |
UserWarning, |
| 43 |
) |
| 44 |
|
| 45 |
def _extract_recursive(data, current_path=""): |
| 46 |
if isinstance(data, dict): |
| 47 |
for key, value in data.items(): |
| 48 |
path = f"{current_path}.{key}" if current_path else key |
| 49 |
if key in sensitive_fields: |
| 50 |
if isinstance(value, str): |
| 51 |
values.add(value) |
| 52 |
else: |
| 53 |
_extract_recursive(value, path) |
| 54 |
elif isinstance(data, list): |
| 55 |
for i, item in enumerate(data): |
| 56 |
_extract_recursive(item, f"{current_path}[{i}]") |
| 57 |
|
| 58 |
_extract_recursive(kubeconfig_data) |
| 59 |
return values |
| 60 |
|
| 61 |
|
| 62 |
AUTH_PROXY_HEADERS_SPEC = dict( |
| 63 |
proxy_basic_auth=dict(type="str", no_log=True), |
| 64 |
basic_auth=dict(type="str", no_log=True), |
| 65 |
user_agent=dict(type="str"), |
| 66 |
) |
| 67 |
|
| 68 |
AUTH_ARG_SPEC = { |
| 69 |
"kubeconfig": {"type": "raw"}, |
| 70 |
"context": {}, |
| 71 |
"host": {}, |
| 72 |
"api_key": {"no_log": True}, |
| 73 |
"username": {}, |
| 74 |
"password": {"no_log": True}, |
| 75 |
"validate_certs": {"type": "bool", "aliases": ["verify_ssl"]}, |
| 76 |
"ca_cert": {"type": "path", "aliases": ["ssl_ca_cert"]}, |
| 77 |
"client_cert": {"type": "path", "aliases": ["cert_file"]}, |
| 78 |
"client_key": {"type": "path", "aliases": ["key_file"]}, |
| 79 |
"proxy": {"type": "str"}, |
| 80 |
"no_proxy": {"type": "str"}, |
| 81 |
"proxy_headers": {"type": "dict", "options": AUTH_PROXY_HEADERS_SPEC}, |
| 82 |
"persist_config": {"type": "bool"}, |
| 83 |
"impersonate_user": {}, |
| 84 |
"impersonate_groups": {"type": "list", "elements": "str"}, |
| 85 |
} |
| 86 |
|
| 87 |
WAIT_ARG_SPEC = dict( |
| 88 |
wait=dict(type="bool", default=False), |
| 89 |
wait_sleep=dict(type="int", default=5), |
| 90 |
wait_timeout=dict(type="int", default=120), |
| 91 |
wait_condition=dict( |
| 92 |
type="dict", |
| 93 |
default=None, |
| 94 |
options=dict( |
| 95 |
type=dict(), |
| 96 |
status=dict(default=True, choices=[True, False, "Unknown"]), |
| 97 |
reason=dict(), |
| 98 |
), |
| 99 |
), |
| 100 |
) |
| 101 |
|
| 102 |
|
| 103 |
AUTH_ARG_MAP = { |
| 104 |
"kubeconfig": "kubeconfig", |
| 105 |
"context": "context", |
| 106 |
"host": "host", |
| 107 |
"api_key": "api_key", |
| 108 |
"username": "username", |
| 109 |
"password": "password", |
| 110 |
"verify_ssl": "validate_certs", |
| 111 |
"ssl_ca_cert": "ca_cert", |
| 112 |
"cert_file": "client_cert", |
| 113 |
"key_file": "client_key", |
| 114 |
"proxy": "proxy", |
| 115 |
"no_proxy": "no_proxy", |
| 116 |
"proxy_headers": "proxy_headers", |
| 117 |
"persist_config": "persist_config", |
| 118 |
} |
| 119 |
|
| 120 |
NAME_ARG_SPEC = { |
| 121 |
"kind": {}, |
| 122 |
"name": {}, |
| 123 |
"namespace": {}, |
| 124 |
"api_version": {"default": "v1", "aliases": ["api", "version"]}, |
| 125 |
} |
| 126 |
|
| 127 |
COMMON_ARG_SPEC = { |
| 128 |
"state": {"default": "present", "choices": ["present", "absent"]}, |
| 129 |
"force": {"type": "bool", "default": False}, |
| 130 |
} |
| 131 |
|
| 132 |
RESOURCE_ARG_SPEC = { |
| 133 |
"resource_definition": {"type": list_dict_str, "aliases": ["definition", "inline"]}, |
| 134 |
"src": {"type": "path"}, |
| 135 |
} |
| 136 |
|
| 137 |
ARG_ATTRIBUTES_BLACKLIST = ("property_path",) |
| 138 |
|
| 139 |
DELETE_OPTS_ARG_SPEC = { |
| 140 |
"propagationPolicy": {"choices": ["Foreground", "Background", "Orphan"]}, |
| 141 |
"gracePeriodSeconds": {"type": "int"}, |
| 142 |
"preconditions": { |
| 143 |
"type": "dict", |
| 144 |
"options": {"resourceVersion": {"type": "str"}, "uid": {"type": "str"}}, |
| 145 |
}, |
| 146 |
} |