Files
napalm-vmware/napalm_vmware/web_login.py
T
Christian Manivong 3275f06615
driver / lint (pull_request) Successful in 13s
driver / standard (pull_request) Successful in 14s
driver / lowest (pull_request) Successful in 51s
driver / types (pull_request) Successful in 1m24s
driver / test (3.13) (pull_request) Successful in 1m12s
driver / test (3.12) (pull_request) Successful in 1m26s
driver / build (pull_request) Successful in 32s
driver / security (pull_request) Successful in 1m10s
CI / driver (pull_request) Successful in 2m15s
driver / publish (pull_request) Skipped
refactor: split web_login_sources so it stays within the complexity ratchet (NetOrk/netork#662)
2026-11-29 23:30:19 +01:00

146 lines
6.3 KiB
Python

"""How people log in to vCenter's web interface (NetOrk/netork#662).
napalm-device-types' ``WebLoginAuthDict``: the sources a login is checked against,
each with what it asks for. Read only. vCenter only: the ESXi host client's
logins are not read, so the ESXi driver has no ``get_web_login_auth``.
**What a source is.** The vSphere Client hands every login to vCenter Single
Sign-On, so the sources are SSO's:
- **each SSO domain** the vSphere API names (``UserDirectory.domainList``): the
system domain (``vsphere.local`` unless renamed at install), ``localos`` (the
appliance's own accounts) and each Active Directory or LDAP identity source.
``{"name": "vsphere.local", "kind": "sso", "factors": "unknown"}``; ``localos``
has the kind ``localos``. The API names a domain, not its type, so an AD and an
OpenLDAP source are both ``sso``;
- **each identity provider of identity federation** (``GET
/api/vcenter/identity/providers``, vCenter 7.0 U2 or later): ADFS, Entra ID,
Okta, PingFederate. ``{"name": "Microsoft ADFS", "kind": "oauth2", "factors":
"external"}``, the kind its ``config_tag``. The default provider comes first. An
SSO domain that a provider serves (its ``domain_names``) is the provider's and is
not listed again. Federation does not switch the local domain off:
``administrator@vsphere.local`` still logs in, so the SSO domains stay sources.
**What a source asks for.**
- A federated provider is ``external``: the provider decides whether it asks for
a second factor, and vCenter cannot say.
- An SSO domain is ``unknown``. Whether SSO asks for a smart card (with password
login switched off) or an RSA SecurID code is in SSO's authentication policy
(``sso-config.sh``), which neither the vSphere API nor the vCenter REST API
exposes. Saying ``password`` would be a guess, and so would ``password_otp``.
So no reading of a vCenter proves a second factor: its web interface is judged
*not assessed*, with the sources named, until an API shows the policy.
**The privileges, and why federation is best effort.** ``domainList`` is read
with ``System.View``, which every role has; if even that is refused, the driver
raises ``PermissionError``. ``GET /api/vcenter/identity/providers`` needs
``VcIdentityProviders.Read`` **and** ``VcIdentityProviders.Manage`` (vSphere
Automation API reference). *Manage* lets an account change the identity provider
configuration, a lot to grant for a reading whose verdict is *not assessed*
either way, so the driver does not need it: when the vCenter refuses the list
(401 or 403), the reading is the SSO domains plus :data:`FEDERATION_NOT_READ`, a
source that stands for the providers that could not be read (``unknown``, never
a pass). With both privileges the providers are named instead. Any other error
of that read is raised as it is.
"""
from __future__ import annotations
from collections.abc import Iterable, Mapping
from typing import Any
from napalm_device_types import LoginSourceDict, WebLoginAuthDict
from napalm_vmware import _session
#: The vCenter REST endpoint listing identity federation's providers.
PROVIDERS_PATH = "/api/vcenter/identity/providers"
#: The source that stands for identity federation when the vCenter refused to list
#: its providers: there may be some, and what they ask for cannot be read.
FEDERATION_NOT_READ: LoginSourceDict = {
"name": "identity federation (not read)",
"kind": "federation",
"factors": "unknown",
}
#: The SSO identity source of the appliance's own operating system accounts.
LOCAL_OS = "localos"
def _provider_source(provider: Mapping[str, Any]) -> LoginSourceDict:
name = str(provider.get("name") or provider.get("provider") or "")
kind = str(provider.get("config_tag") or "provider").lower()
return {"name": name, "kind": kind, "factors": "external"}
def _domain_source(domain: str) -> LoginSourceDict:
kind = LOCAL_OS if domain.lower() == LOCAL_OS else "sso"
return {"name": domain, "kind": kind, "factors": "unknown"}
def web_login_sources(
*, domains: Iterable[str], providers: Iterable[Mapping[str, Any]] | None
) -> list[LoginSourceDict]:
"""The providers (the default first), then the SSO domains in the API's order.
*domains* is ``UserDirectory.domainList``, *providers* the answer of
:data:`PROVIDERS_PATH`, or None when it was refused: then
:data:`FEDERATION_NOT_READ` takes the providers' place.
"""
if providers is None:
return [FEDERATION_NOT_READ, *_domain_sources(domains, served=set())]
federated = sorted(providers, key=lambda p: not p.get("is_default"))
sources = [_provider_source(p) for p in federated]
return sources + _domain_sources(domains, served=_served_domains(federated))
def _served_domains(providers: Iterable[Mapping[str, Any]]) -> set[str]:
"""The SSO domains the providers serve (``domain_names``), lowercased."""
return {str(d).lower() for p in providers for d in p.get("domain_names") or ()}
def _domain_sources(domains: Iterable[str], *, served: set[str]) -> list[LoginSourceDict]:
"""A source per named SSO domain that no provider serves, in the API's order."""
return [_domain_source(d) for d in domains if d and d.lower() not in served]
class VcenterWebLoginMixin:
"""``get_web_login_auth`` for the vCenter driver."""
hostname: str
username: str
password: str
timeout: int
_port: int
_verify_ssl: bool
_inventory: Any
def get_web_login_auth(self) -> WebLoginAuthDict:
"""The sources a vSphere Client login can go to, each with what it asks for.
Example::
{"sources": [{"name": "vsphere.local", "kind": "sso", "factors": "unknown"}]}
:raises PermissionError: when the API login may not read the SSO domains.
A refused list of identity providers is not an error (see the module
docstring).
"""
domains = self._inventory.sso_domains()
try:
providers = _session.rest_get(
self.hostname,
self._port,
self.username,
self.password,
PROVIDERS_PATH,
verify_ssl=self._verify_ssl,
timeout=self.timeout,
)
except PermissionError:
providers = None
else:
providers = providers or []
return {"sources": web_login_sources(domains=domains, providers=providers)}