"""AWS secrets engine (dynamic IAM users or STS credentials).""" from __future__ import annotations from collections.abc import Mapping from typing import Any from envbao.client import BaoClient from envbao.engines import _common as c from envbao.errors import ConfigError _OPTIONS = {"mount", "role", "kind", "ttl"} _FIELDS = ("access_key", "secret_key", "security_token") class AwsEngine: """Reads AWS credentials for a role. Options: ``role`` (required), ``mount`` (default ``"aws"``), ``kind`` (``"creds"`` or ``"sts"``, default ``"creds"``), ``ttl`` (optional duration string such as ``"15m"``). Returns ``access_key``, ``secret_key`` and, for STS, ``security_token``. """ def validate(self, options: Mapping[str, Any]) -> None: c.check_options("aws", options, _OPTIONS) c.get_str("aws", options, "role") c.get_str("aws", options, "mount", default="aws") c.get_choice("aws", options, "kind", ("creds", "sts"), "creds") ttl = options.get("ttl") if ttl is not None and (not isinstance(ttl, str) or not ttl): raise ConfigError("aws option 'ttl' must be a non-empty string such as \"15m\"") def cacheable(self, options: Mapping[str, Any]) -> bool: return True def fetch(self, client: BaoClient, options: Mapping[str, Any]) -> dict[str, Any]: return self.fetch_leased(client, options)[0] def fetch_leased( self, client: BaoClient, options: Mapping[str, Any] ) -> tuple[dict[str, Any], c.Lease | None]: mount = c.get_str("aws", options, "mount", default="aws") role = c.get_str("aws", options, "role") kind = c.get_choice("aws", options, "kind", ("creds", "sts"), "creds") ttl = options.get("ttl") where = f"{mount}/{kind}/{role}" body = client.get(where, {"ttl": ttl} if ttl else None) return c.pick(c.response_data(body, where), _FIELDS, where), c.lease_of(body)