2023-08-09 08:17:11 +00:00
|
|
|
from pathlib import Path
|
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
|
|
|
|
import pytest
|
2023-08-24 15:29:31 +00:00
|
|
|
from cli import Cli
|
2023-10-23 20:34:43 +00:00
|
|
|
from fixtures_flakes import FlakeForTest
|
|
|
|
|
2023-08-09 08:17:11 +00:00
|
|
|
if TYPE_CHECKING:
|
2023-08-09 13:51:10 +00:00
|
|
|
from age_keys import KeyPair
|
2023-08-09 08:17:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_import_sops(
|
|
|
|
test_root: Path,
|
2023-10-23 20:31:12 +00:00
|
|
|
test_flake: FlakeForTest,
|
2023-08-09 08:17:11 +00:00
|
|
|
capsys: pytest.CaptureFixture,
|
2023-08-26 09:44:38 +00:00
|
|
|
monkeypatch: pytest.MonkeyPatch,
|
2023-08-09 13:51:10 +00:00
|
|
|
age_keys: list["KeyPair"],
|
2023-08-09 08:17:11 +00:00
|
|
|
) -> None:
|
2023-08-24 15:29:31 +00:00
|
|
|
cli = Cli()
|
2023-08-09 08:17:11 +00:00
|
|
|
|
2023-08-26 09:44:38 +00:00
|
|
|
monkeypatch.setenv("SOPS_AGE_KEY", age_keys[1].privkey)
|
2023-10-23 20:34:43 +00:00
|
|
|
cli.run(
|
2023-11-03 11:51:17 +00:00
|
|
|
[
|
|
|
|
"secrets",
|
|
|
|
"machines",
|
|
|
|
"add",
|
2024-05-29 08:10:10 +00:00
|
|
|
"--flake",
|
|
|
|
str(test_flake.path),
|
2023-11-03 11:51:17 +00:00
|
|
|
"machine1",
|
|
|
|
age_keys[0].pubkey,
|
|
|
|
]
|
|
|
|
)
|
|
|
|
cli.run(
|
|
|
|
[
|
|
|
|
"secrets",
|
|
|
|
"users",
|
|
|
|
"add",
|
2024-05-29 08:10:10 +00:00
|
|
|
"--flake",
|
|
|
|
str(test_flake.path),
|
2023-11-03 11:51:17 +00:00
|
|
|
"user1",
|
|
|
|
age_keys[1].pubkey,
|
|
|
|
]
|
|
|
|
)
|
|
|
|
cli.run(
|
|
|
|
[
|
|
|
|
"secrets",
|
|
|
|
"users",
|
|
|
|
"add",
|
2024-05-29 08:10:10 +00:00
|
|
|
"--flake",
|
|
|
|
str(test_flake.path),
|
2023-11-03 11:51:17 +00:00
|
|
|
"user2",
|
|
|
|
age_keys[2].pubkey,
|
|
|
|
]
|
|
|
|
)
|
|
|
|
cli.run(
|
|
|
|
[
|
|
|
|
"secrets",
|
|
|
|
"groups",
|
|
|
|
"add-user",
|
2024-05-29 08:10:10 +00:00
|
|
|
"--flake",
|
|
|
|
str(test_flake.path),
|
2023-11-03 11:51:17 +00:00
|
|
|
"group1",
|
|
|
|
"user1",
|
|
|
|
]
|
|
|
|
)
|
|
|
|
cli.run(
|
|
|
|
[
|
|
|
|
"secrets",
|
|
|
|
"groups",
|
|
|
|
"add-user",
|
2024-05-29 08:10:10 +00:00
|
|
|
"--flake",
|
|
|
|
str(test_flake.path),
|
2023-11-03 11:51:17 +00:00
|
|
|
"group1",
|
|
|
|
"user2",
|
|
|
|
]
|
2023-10-23 20:34:43 +00:00
|
|
|
)
|
2023-08-09 08:17:11 +00:00
|
|
|
|
2023-08-26 09:44:38 +00:00
|
|
|
# To edit:
|
|
|
|
# SOPS_AGE_KEY=AGE-SECRET-KEY-1U5ENXZQAY62NC78Y2WC0SEGRRMAEEKH79EYY5TH4GPFWJKEAY0USZ6X7YQ sops --age age14tva0txcrl0zes05x7gkx56qd6wd9q3nwecjac74xxzz4l47r44sv3fz62 ./data/secrets.yaml
|
2023-10-23 20:31:12 +00:00
|
|
|
cmd = [
|
2023-10-23 20:34:43 +00:00
|
|
|
"secrets",
|
|
|
|
"import-sops",
|
2024-05-29 08:10:10 +00:00
|
|
|
"--flake",
|
|
|
|
str(test_flake.path),
|
2023-10-23 20:34:43 +00:00
|
|
|
"--group",
|
|
|
|
"group1",
|
|
|
|
"--machine",
|
|
|
|
"machine1",
|
|
|
|
str(test_root.joinpath("data", "secrets.yaml")),
|
|
|
|
]
|
2023-10-24 14:44:54 +00:00
|
|
|
|
2023-10-23 20:34:43 +00:00
|
|
|
cli.run(cmd)
|
2023-08-26 09:44:38 +00:00
|
|
|
capsys.readouterr()
|
2024-05-29 08:10:10 +00:00
|
|
|
cli.run(["secrets", "users", "list", "--flake", str(test_flake.path)])
|
2023-08-26 09:44:38 +00:00
|
|
|
users = sorted(capsys.readouterr().out.rstrip().split())
|
|
|
|
assert users == ["user1", "user2"]
|
2023-08-09 08:17:11 +00:00
|
|
|
|
2023-08-26 09:44:38 +00:00
|
|
|
capsys.readouterr()
|
2024-05-29 08:10:10 +00:00
|
|
|
cli.run(["secrets", "get", "--flake", str(test_flake.path), "secret-key"])
|
2023-08-26 09:44:38 +00:00
|
|
|
assert capsys.readouterr().out == "secret-value"
|