2023-09-19 19:48:39 +00:00
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
from cli import Cli
|
2023-10-23 20:31:12 +00:00
|
|
|
from fixtures_flakes import FlakeForTest
|
2023-10-23 20:34:43 +00:00
|
|
|
|
2023-09-19 19:48:39 +00:00
|
|
|
from clan_cli.ssh import HostGroup
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from age_keys import KeyPair
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.impure
|
2023-09-21 15:17:48 +00:00
|
|
|
def test_secrets_upload(
|
2023-09-19 19:48:39 +00:00
|
|
|
monkeypatch: pytest.MonkeyPatch,
|
2023-10-23 20:31:12 +00:00
|
|
|
test_flake_with_core: FlakeForTest,
|
2023-09-19 19:48:39 +00:00
|
|
|
host_group: HostGroup,
|
|
|
|
age_keys: list["KeyPair"],
|
|
|
|
) -> None:
|
2023-10-23 20:31:12 +00:00
|
|
|
monkeypatch.chdir(test_flake_with_core.path)
|
2023-09-19 19:48:39 +00:00
|
|
|
monkeypatch.setenv("SOPS_AGE_KEY", age_keys[0].privkey)
|
|
|
|
|
|
|
|
cli = Cli()
|
2023-10-23 20:34:43 +00:00
|
|
|
cli.run(
|
|
|
|
[
|
|
|
|
"secrets",
|
|
|
|
"users",
|
|
|
|
"add",
|
2024-05-29 08:10:10 +00:00
|
|
|
"--flake",
|
|
|
|
str(test_flake_with_core.path),
|
2023-10-23 20:34:43 +00:00
|
|
|
"user1",
|
|
|
|
age_keys[0].pubkey,
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
cli.run(
|
|
|
|
[
|
|
|
|
"secrets",
|
|
|
|
"machines",
|
|
|
|
"add",
|
2024-05-29 08:10:10 +00:00
|
|
|
"--flake",
|
|
|
|
str(test_flake_with_core.path),
|
2023-10-23 20:34:43 +00:00
|
|
|
"vm1",
|
|
|
|
age_keys[1].pubkey,
|
|
|
|
]
|
|
|
|
)
|
2023-09-19 19:48:39 +00:00
|
|
|
monkeypatch.setenv("SOPS_NIX_SECRET", age_keys[0].privkey)
|
2023-11-03 11:51:17 +00:00
|
|
|
cli.run(
|
2024-05-29 08:10:10 +00:00
|
|
|
["secrets", "set", "--flake", str(test_flake_with_core.path), "vm1-age.key"]
|
2023-11-03 11:51:17 +00:00
|
|
|
)
|
2023-09-19 19:48:39 +00:00
|
|
|
|
2023-10-23 20:31:12 +00:00
|
|
|
flake = test_flake_with_core.path.joinpath("flake.nix")
|
2023-09-19 19:48:39 +00:00
|
|
|
host = host_group.hosts[0]
|
|
|
|
addr = f"{host.user}@{host.host}:{host.port}?StrictHostKeyChecking=no&UserKnownHostsFile=/dev/null&IdentityFile={host.key}"
|
2024-02-02 04:32:48 +00:00
|
|
|
new_text = flake.read_text().replace("__CLAN_TARGET_ADDRESS__", addr)
|
2023-09-19 19:48:39 +00:00
|
|
|
|
|
|
|
flake.write_text(new_text)
|
2024-05-29 08:10:10 +00:00
|
|
|
cli.run(["facts", "upload", "--flake", str(test_flake_with_core.path), "vm1"])
|
2023-09-27 15:25:17 +00:00
|
|
|
|
|
|
|
# the flake defines this path as the location where the sops key should be installed
|
2023-10-23 20:31:12 +00:00
|
|
|
sops_key = test_flake_with_core.path.joinpath("key.txt")
|
2023-09-19 19:48:39 +00:00
|
|
|
assert sops_key.exists()
|
|
|
|
assert sops_key.read_text() == age_keys[0].privkey
|