release / validate (push) Successful in 5s
test / test (push) Successful in 1m6s
release / build (, macos-arm64, macos-arm64) (push) Successful in 1m20s
release / build (amd64, linux-amd64, linux-amd64) (push) Successful in 1m7s
release / build (arm64, linux-arm64, linux-arm64) (push) Successful in 2m2s
release / release (push) Successful in 26s
New OpenBao credential engines (database/PostgreSQL, aws, approle, ssh) backed by a POST-capable BaoClient. Queries can set cache = true on dynamic database and aws roles to reuse the credential until near lease expiry (file store by default, optional keychain), revoking the old lease on refresh. Adds [cache] store and --clear-cache. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
245 lines
9.2 KiB
Python
245 lines
9.2 KiB
Python
from pathlib import Path
|
|
from typing import Any
|
|
|
|
import pytest
|
|
|
|
from envbao.client import BaoClient
|
|
from envbao.config import Config, Query, ServerConfig
|
|
from envbao.engines import ENGINES, get_engine
|
|
from envbao.errors import AuthError, ConfigError, NotFoundError, ServerError
|
|
from envbao.resolve import run_query, validate_queries
|
|
|
|
from .conftest import TOKEN, FakeBao
|
|
|
|
|
|
def client_for(fake: FakeBao, token: str = TOKEN) -> BaoClient:
|
|
return BaoClient(ServerConfig(address=fake.url, token=token))
|
|
|
|
|
|
def q(engine: str, **options: Any) -> Query:
|
|
return Query(engine=engine, options=options, name=options.get("role", engine))
|
|
|
|
|
|
@pytest.mark.parametrize("name", ["kv", "database", "aws", "approle", "ssh"])
|
|
def test_registry(name: str) -> None:
|
|
assert name in ENGINES
|
|
assert get_engine(name) is ENGINES[name]
|
|
|
|
|
|
# ---- POST on the client --------------------------------------------------------------
|
|
|
|
|
|
def test_post_sends_json_body_and_token(fake_bao: FakeBao) -> None:
|
|
fake_bao.route("things", {"a": 1})
|
|
body = client_for(fake_bao).post("things", {"x": "y"})
|
|
assert body["data"] == {"a": 1}
|
|
assert fake_bao.posts[-1] == ("/v1/things", {"x": "y"})
|
|
assert fake_bao.requests[-1][1]["x-vault-token"] == TOKEN
|
|
assert fake_bao.requests[-1][1]["content-type"] == "application/json"
|
|
|
|
|
|
def test_post_without_body_sends_empty_object(fake_bao: FakeBao) -> None:
|
|
fake_bao.route("things", {"a": 1})
|
|
client_for(fake_bao).post("things")
|
|
assert fake_bao.posts[-1][1] == {}
|
|
|
|
|
|
def test_post_does_not_follow_redirect(fake_bao: FakeBao) -> None:
|
|
with pytest.raises(ServerError, match="HTTP 307"):
|
|
client_for(fake_bao).post("redirect", {})
|
|
|
|
|
|
def test_post_auth_error_and_not_found(fake_bao: FakeBao) -> None:
|
|
with pytest.raises(AuthError):
|
|
client_for(fake_bao, token="s.wrong").post("things")
|
|
with pytest.raises(NotFoundError):
|
|
client_for(fake_bao).post("missing")
|
|
|
|
|
|
# ---- database -------------------------------------------------------------------------
|
|
|
|
|
|
def test_database_dynamic(fake_bao: FakeBao) -> None:
|
|
fake_bao.route("database/creds/ro", {"username": "v-ro-1", "password": "pw-secret"})
|
|
query = q("database", role="ro")
|
|
env = run_query(client_for(fake_bao), query, Config(ServerConfig("x", "t"), (query,)))
|
|
assert env == {"username": "v-ro-1", "password": "pw-secret"}
|
|
|
|
|
|
def test_database_prefix_and_lease_not_exported(fake_bao: FakeBao) -> None:
|
|
fake_bao.route("pg/creds/ro", {"username": "u", "password": "pw-secret"})
|
|
query = Query(
|
|
engine="database",
|
|
options={"role": "ro", "mount": "pg"},
|
|
rename={"username": "PGUSER", "password": "PGPASSWORD"},
|
|
name="pg",
|
|
)
|
|
env = run_query(client_for(fake_bao), query, Config(ServerConfig("x", "t"), (query,)))
|
|
assert env == {"PGUSER": "u", "PGPASSWORD": "pw-secret"}
|
|
|
|
|
|
def test_database_static(fake_bao: FakeBao) -> None:
|
|
fake_bao.route(
|
|
"database/static-creds/app",
|
|
{"username": "app", "password": "pw-secret", "last_vault_rotation": "now"},
|
|
)
|
|
data = get_engine("database").fetch(client_for(fake_bao), {"role": "app", "static": True})
|
|
assert data == {"username": "app", "password": "pw-secret"}
|
|
|
|
|
|
def test_database_missing_role_is_not_found(fake_bao: FakeBao) -> None:
|
|
with pytest.raises(NotFoundError):
|
|
get_engine("database").fetch(client_for(fake_bao), {"role": "nope"})
|
|
|
|
|
|
def test_database_empty_response_fails(fake_bao: FakeBao) -> None:
|
|
fake_bao.route("database/creds/r", {"other": "x"})
|
|
with pytest.raises(ServerError, match="no credentials"):
|
|
get_engine("database").fetch(client_for(fake_bao), {"role": "r"})
|
|
|
|
|
|
def test_response_without_data(fake_bao: FakeBao) -> None:
|
|
fake_bao.routes["/v1/database/creds/r"] = (200, {"data": None})
|
|
with pytest.raises(NotFoundError):
|
|
get_engine("database").fetch(client_for(fake_bao), {"role": "r"})
|
|
fake_bao.routes["/v1/database/creds/r"] = (200, {"data": "str"})
|
|
with pytest.raises(ServerError, match="shape"):
|
|
get_engine("database").fetch(client_for(fake_bao), {"role": "r"})
|
|
|
|
|
|
# ---- aws ------------------------------------------------------------------------------
|
|
|
|
|
|
def test_aws_creds(fake_bao: FakeBao) -> None:
|
|
fake_bao.route(
|
|
"aws/creds/dev", {"access_key": "AKIA1", "secret_key": "sk", "security_token": None}
|
|
)
|
|
data = get_engine("aws").fetch(client_for(fake_bao), {"role": "dev"})
|
|
assert data == {"access_key": "AKIA1", "secret_key": "sk"}
|
|
|
|
|
|
def test_aws_sts_with_ttl(fake_bao: FakeBao) -> None:
|
|
fake_bao.route(
|
|
"aws/sts/dev?ttl=15m", {"access_key": "A", "secret_key": "S", "security_token": "T"}
|
|
)
|
|
data = get_engine("aws").fetch(
|
|
client_for(fake_bao), {"role": "dev", "kind": "sts", "ttl": "15m"}
|
|
)
|
|
assert data["security_token"] == "T"
|
|
|
|
|
|
# ---- approle --------------------------------------------------------------------------
|
|
|
|
|
|
def test_approle_both(fake_bao: FakeBao) -> None:
|
|
fake_bao.route("auth/approle/role/web/role-id", {"role_id": "rid"})
|
|
fake_bao.route(
|
|
"auth/approle/role/web/secret-id", {"secret_id": "sid", "secret_id_accessor": "a"}
|
|
)
|
|
data = get_engine("approle").fetch(client_for(fake_bao), {"role": "web"})
|
|
assert data == {"role_id": "rid", "secret_id": "sid"}
|
|
assert fake_bao.posts[-1][0] == "/v1/auth/approle/role/web/secret-id"
|
|
|
|
|
|
def test_approle_only_role_id_does_not_mint_secret_id(fake_bao: FakeBao) -> None:
|
|
fake_bao.route("auth/ar/role/web/role-id", {"role_id": "rid"})
|
|
data = get_engine("approle").fetch(
|
|
client_for(fake_bao), {"role": "web", "mount": "ar", "what": "role_id"}
|
|
)
|
|
assert data == {"role_id": "rid"}
|
|
assert fake_bao.posts == []
|
|
|
|
|
|
def test_approle_only_secret_id(fake_bao: FakeBao) -> None:
|
|
fake_bao.route("auth/approle/role/web/secret-id", {"secret_id": "sid"})
|
|
data = get_engine("approle").fetch(client_for(fake_bao), {"role": "web", "what": "secret_id"})
|
|
assert data == {"secret_id": "sid"}
|
|
|
|
|
|
# ---- ssh ------------------------------------------------------------------------------
|
|
|
|
|
|
def test_ssh_otp(fake_bao: FakeBao) -> None:
|
|
fake_bao.route(
|
|
"ssh/creds/ops", {"key": "otp-value", "username": "ops", "ip": "10.0.0.5", "port": 22}
|
|
)
|
|
data = get_engine("ssh").fetch(client_for(fake_bao), {"role": "ops", "ip": "10.0.0.5"})
|
|
assert data == {"key": "otp-value", "username": "ops", "ip": "10.0.0.5"}
|
|
assert fake_bao.posts[-1][1] == {"ip": "10.0.0.5"}
|
|
|
|
|
|
def test_ssh_sign(fake_bao: FakeBao, tmp_path: Path) -> None:
|
|
pub = tmp_path / "id.pub"
|
|
pub.write_text("ssh-ed25519 AAAA test\n")
|
|
fake_bao.route("ssh/sign/ops", {"signed_key": "cert", "serial_number": "1"})
|
|
data = get_engine("ssh").fetch(
|
|
client_for(fake_bao),
|
|
{"role": "ops", "mode": "sign", "public_key_file": str(pub), "valid_principals": "ops"},
|
|
)
|
|
assert data == {"signed_key": "cert", "serial_number": "1"}
|
|
assert fake_bao.posts[-1][1] == {
|
|
"public_key": "ssh-ed25519 AAAA test",
|
|
"valid_principals": "ops",
|
|
}
|
|
|
|
|
|
def test_ssh_sign_unreadable_key(fake_bao: FakeBao, tmp_path: Path) -> None:
|
|
with pytest.raises(ConfigError, match="cannot read"):
|
|
get_engine("ssh").fetch(
|
|
client_for(fake_bao),
|
|
{"role": "ops", "mode": "sign", "public_key_file": str(tmp_path / "nope.pub")},
|
|
)
|
|
|
|
|
|
def test_ssh_sign_huge_key_rejected(fake_bao: FakeBao, tmp_path: Path) -> None:
|
|
pub = tmp_path / "big.pub"
|
|
pub.write_text("x" * (70 * 1024))
|
|
with pytest.raises(ConfigError, match="large"):
|
|
get_engine("ssh").fetch(
|
|
client_for(fake_bao),
|
|
{"role": "ops", "mode": "sign", "public_key_file": str(pub)},
|
|
)
|
|
|
|
|
|
# ---- validation -----------------------------------------------------------------------
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("engine", "options"),
|
|
[
|
|
("database", {}),
|
|
("database", {"role": ""}),
|
|
("database", {"role": "r", "bogus": 1}),
|
|
("database", {"role": "r", "static": "yes"}),
|
|
("database", {"role": "r", "mount": 5}),
|
|
("aws", {"role": "r", "kind": "other"}),
|
|
("aws", {"role": "r", "ttl": ""}),
|
|
("approle", {"role": "r", "what": "everything"}),
|
|
("ssh", {"role": "r"}),
|
|
("ssh", {"role": "r", "ip": "1.2.3.4", "public_key_file": "k"}),
|
|
("ssh", {"role": "r", "mode": "sign"}),
|
|
("ssh", {"role": "r", "mode": "sign", "public_key_file": "k", "ip": "1.2.3.4"}),
|
|
("ssh", {"role": "r", "mode": "sign", "public_key_file": "k", "valid_principals": ""}),
|
|
("ssh", {"role": "r", "mode": "bad"}),
|
|
],
|
|
)
|
|
def test_invalid_options(engine: str, options: dict[str, Any]) -> None:
|
|
with pytest.raises(ConfigError):
|
|
get_engine(engine).validate(options)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("engine", "options"),
|
|
[
|
|
("database", {"role": "r"}),
|
|
("database", {"role": "r", "mount": "pg", "static": True}),
|
|
("aws", {"role": "r", "kind": "sts", "ttl": "1h"}),
|
|
("approle", {"role": "r", "what": "role_id"}),
|
|
("ssh", {"role": "r", "ip": "1.2.3.4"}),
|
|
("ssh", {"role": "r", "mode": "sign", "public_key_file": "k", "valid_principals": "a,b"}),
|
|
],
|
|
)
|
|
def test_valid_options(engine: str, options: dict[str, Any]) -> None:
|
|
get_engine(engine).validate(options)
|
|
validate_queries(Config(ServerConfig("x", "t"), (Query(engine=engine, options=options),)))
|