| 1 |
# Copyright (c) 2023 Ansible Project |
| 2 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
|
| 4 |
import importlib |
| 5 |
import importlib.metadata |
| 6 |
import typing as t |
| 7 |
import traceback |
| 8 |
|
| 9 |
|
| 10 |
from ansible.plugins.action import ActionBase |
| 11 |
from ansible.utils.display import Display |
| 12 |
|
| 13 |
display = Display() |
| 14 |
|
| 15 |
try: |
| 16 |
import dns.resolver |
| 17 |
|
| 18 |
HAS_DNSRESOLVER = True |
| 19 |
except Exception: |
| 20 |
HAS_DNSRESOLVER = False |
| 21 |
|
| 22 |
|
| 23 |
try: |
| 24 |
import krb5 |
| 25 |
|
| 26 |
HAS_KRB5 = True |
| 27 |
except Exception: |
| 28 |
HAS_KRB5 = False |
| 29 |
|
| 30 |
|
| 31 |
class ActionModule(ActionBase): |
| 32 |
def run( |
| 33 |
self, |
| 34 |
tmp: t.Optional[str] = None, |
| 35 |
task_vars: t.Optional[t.Dict[str, t.Any]] = None, |
| 36 |
) -> t.Dict[str, t.Any]: |
| 37 |
self._supports_check_mode = True |
| 38 |
self._supports_async = True |
| 39 |
|
| 40 |
result = super().run(tmp=tmp, task_vars=task_vars) |
| 41 |
del tmp |
| 42 |
|
| 43 |
kerb_info = self._get_kerberos_info() |
| 44 |
|
| 45 |
dns_info: t.Dict[str, t.Any] = {} |
| 46 |
default_realm = kerb_info.get("default_realm", None) |
| 47 |
if default_realm: |
| 48 |
dns_info = self._get_server_lookup_info(default_realm) |
| 49 |
|
| 50 |
result.update( |
| 51 |
{ |
| 52 |
"dns": dns_info, |
| 53 |
"kerberos": kerb_info, |
| 54 |
"packages": { |
| 55 |
"dnspython": self._import_lib( |
| 56 |
"dns.resolver", package_name="dnspython" |
| 57 |
), |
| 58 |
"dpapi_ng": self._import_lib("dpapi_ng", package_name="dpapi-ng"), |
| 59 |
"krb5": self._import_lib("krb5"), |
| 60 |
"pyspnego": self._import_lib("spnego", package_name="pyspnego"), |
| 61 |
"sansldap": self._import_lib("sansldap"), |
| 62 |
}, |
| 63 |
} |
| 64 |
) |
| 65 |
|
| 66 |
return result |
| 67 |
|
| 68 |
def _get_kerberos_info(self) -> t.Dict[str, t.Any]: |
| 69 |
if not HAS_KRB5: |
| 70 |
return {} |
| 71 |
|
| 72 |
res: t.Dict[str, t.Any] = { |
| 73 |
"exception": None, |
| 74 |
"default_realm": None, |
| 75 |
"default_cc": {}, |
| 76 |
} |
| 77 |
|
| 78 |
try: |
| 79 |
ctx = krb5.init_context() |
| 80 |
except Exception: |
| 81 |
res["exception"] = traceback.format_exc() |
| 82 |
return res |
| 83 |
|
| 84 |
try: |
| 85 |
res["default_realm"] = krb5.get_default_realm(ctx).decode("utf-8") |
| 86 |
except Exception: |
| 87 |
res["exception"] = traceback.format_exc() |
| 88 |
|
| 89 |
res["default_cc"] = self._get_kerberos_cc_info(ctx) |
| 90 |
|
| 91 |
return res |
| 92 |
|
| 93 |
def _get_kerberos_cc_info( |
| 94 |
self, |
| 95 |
ctx: "krb5.Context", |
| 96 |
) -> t.Dict[str, t.Any]: |
| 97 |
creds: t.List[t.Dict[str, t.Any]] = [] |
| 98 |
res: t.Dict[str, t.Any] = { |
| 99 |
"exception": None, |
| 100 |
"name": None, |
| 101 |
"principal": None, |
| 102 |
"creds": creds, |
| 103 |
} |
| 104 |
|
| 105 |
try: |
| 106 |
default_cc = krb5.cc_default(ctx) |
| 107 |
except Exception: |
| 108 |
res["exception"] = traceback.format_exc() |
| 109 |
return res |
| 110 |
|
| 111 |
try: |
| 112 |
res["name"] = str(default_cc) |
| 113 |
res["principal"] = str(default_cc.principal) |
| 114 |
for cred in default_cc: |
| 115 |
# cred attrs added in krb5 0.5.0 |
| 116 |
creds.append( |
| 117 |
{ |
| 118 |
"client": str(getattr(cred, "client", "krb5 too old")), |
| 119 |
"server": str(getattr(cred, "server", "krb5 too old")), |
| 120 |
} |
| 121 |
) |
| 122 |
except Exception: |
| 123 |
res["exception"] = traceback.format_exc() |
| 124 |
|
| 125 |
return res |
| 126 |
|
| 127 |
def _get_server_lookup_info( |
| 128 |
self, |
| 129 |
default_realm: str, |
| 130 |
) -> t.Dict[str, t.Any]: |
| 131 |
if not HAS_DNSRESOLVER: |
| 132 |
return {} |
| 133 |
|
| 134 |
records: t.List[t.Dict[str, t.Any]] = [] |
| 135 |
res: t.Dict[str, t.Any] = { |
| 136 |
"exception": None, |
| 137 |
"default_server": None, |
| 138 |
"default_port": None, |
| 139 |
"records": records, |
| 140 |
} |
| 141 |
|
| 142 |
try: |
| 143 |
srv_record = f"_ldap._tcp.dc._msdcs.{default_realm}" |
| 144 |
|
| 145 |
for rec in dns.resolver.resolve(srv_record, "SRV"): |
| 146 |
records.append( |
| 147 |
{ |
| 148 |
"target": str(rec.target), |
| 149 |
"port": rec.port, |
| 150 |
"weight": rec.weight, |
| 151 |
"priority": rec.priority, |
| 152 |
} |
| 153 |
) |
| 154 |
|
| 155 |
highest_record = next( |
| 156 |
iter(sorted(records, key=lambda k: (k["priority"], -k["weight"]))), None |
| 157 |
) |
| 158 |
if highest_record: |
| 159 |
res["default_server"] = highest_record["target"].rstrip(".") |
| 160 |
res["default_port"] = highest_record["port"] |
| 161 |
|
| 162 |
except Exception: |
| 163 |
res["exception"] = traceback.format_exc() |
| 164 |
|
| 165 |
return res |
| 166 |
|
| 167 |
def _import_lib( |
| 168 |
self, |
| 169 |
name: str, |
| 170 |
package_name: t.Optional[str] = None, |
| 171 |
) -> str: |
| 172 |
try: |
| 173 |
importlib.import_module(name) |
| 174 |
return importlib.metadata.version(package_name or name) |
| 175 |
except Exception: |
| 176 |
return traceback.format_exc() |