secrets
napt.secrets
Secrets a recipe may send with a discovery request.
A recipe references an environment variable as ${NAME} inside a
discovery.headers value (api_json) or the discovery.token
(api_github). The variable must be declared under secrets: in
defaults/org.yaml together with the hosts it may be sent to, the
request must go to one of those hosts, and the URL must use https:
secrets:
API_TOKEN:
hosts: ["api.vendor.com"]
Variables the org file does not declare are unreachable from recipes, so a
recipe (including one imported from elsewhere) cannot pick up the runner's
other secrets such as AZURE_CLIENT_SECRET. The config loader honors the
secrets section from defaults/org.yaml alone; entries a vendor file,
parent recipe, or recipe adds never reach the merged configuration.
Violations are reported by napt validate and refused by napt
discover before any request is made. A request that carries a secret
follows redirects only to the bound hosts, because requests would
otherwise resend custom headers to whatever host a redirect names. The
host is read with the parser requests connects with, and a URL whose
host part two parsers could read differently is refused outright.
secret_names
Lists the environment variables a value references, in order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str
|
A header value or token from a recipe. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
Each referenced name once, in order of first appearance. |
Source code in napt/secrets.py
declared_hosts
Reads the hosts each declared secret may be sent to.
Entries that are not shaped as NAME: {hosts: [...]} are skipped
here; validation reports them.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
dict[str, Any]
|
Effective configuration, whose |
required |
Returns:
| Type | Description |
|---|---|
dict[str, set[str]]
|
Lower-cased hostnames keyed by variable name. |
Source code in napt/secrets.py
check_secret_use
Checks that a value's secrets may be sent to a URL.
This is the static half of the rule, shared by napt validate and
the discovery strategies: it needs no environment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
dict[str, Any]
|
Effective configuration carrying the |
required |
value
|
str
|
The header value or token as written in the recipe. |
required |
url
|
str
|
The URL the value would be sent to. |
required |
field_path
|
str
|
Recipe field for error messages, such as
|
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
Error messages; empty when the value carries no secret or every |
list[str]
|
secret is allowed to go to the URL. |
Source code in napt/secrets.py
expand_secrets
Replaces each ${NAME} in a value with the environment variable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
dict[str, Any]
|
Effective configuration carrying the |
required |
value
|
str
|
The header value or token as written in the recipe. |
required |
url
|
str
|
The URL the value will be sent to. |
required |
field_path
|
str
|
Recipe field for error messages. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The value with every reference expanded; a value without |
str
|
references is returned unchanged. |
Raises:
| Type | Description |
|---|---|
ConfigError
|
If a referenced variable is undeclared, bound to other hosts, would travel over plain http or to an ambiguous host, or is not set. |
Source code in napt/secrets.py
bound_hosts
Computes the hosts a request carrying these values may visit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
dict[str, Any]
|
Effective configuration carrying the |
required |
values
|
Iterable[str]
|
The header values or token as written in the recipe. |
required |
Returns:
| Type | Description |
|---|---|
set[str] | None
|
The hosts every referenced secret is bound to, or None when the |
set[str] | None
|
values reference no secret and redirects need no guard. An |
set[str] | None
|
undeclared reference, which |
set[str] | None
|
check_secret_use rejects before |
set[str] | None
|
any request, binds to no host at all. |
Source code in napt/secrets.py
guarded_get
guarded_get(
session: Session,
url: str,
headers: dict[str, str],
*,
hosts: set[str] | None,
timeout: int,
stream: bool = False
) -> requests.Response
Sends a GET whose headers may carry secrets.
Without bound hosts the request follows redirects as requests
normally does. With them, each redirect is followed by hand and must
stay on a bound host over https, so the headers never reach anywhere
defaults/org.yaml did not allow.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
Session
|
The session to send with. |
required |
url
|
str
|
The request URL. |
required |
headers
|
dict[str, str]
|
Request headers, secrets already expanded. |
required |
hosts
|
set[str] | None
|
The bound hosts from bound_hosts, or None. |
required |
timeout
|
int
|
Per-request timeout in seconds. |
required |
stream
|
bool
|
Whether to leave the final response's body unread, for a caller that reads it in bounded pieces. |
False
|
Returns:
| Type | Description |
|---|---|
Response
|
The final response. |
Raises:
| Type | Description |
|---|---|
NetworkError
|
If a redirect leaves the bound hosts, drops to plain http, names an ambiguous host, or does not end within a few hops. |
RequestException
|
On transport failure, for the caller to wrap with its own context. |