|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import os |
| 4 | +from pathlib import Path |
| 5 | +from typing import Callable, Optional |
| 6 | + |
| 7 | +from test_base import TestBase |
| 8 | + |
| 9 | +from warnet.k8s import get_kubeconfig_value |
| 10 | +from warnet.process import run_command |
| 11 | + |
| 12 | + |
| 13 | +class NamespaceAdminTest(TestBase): |
| 14 | + def __init__(self): |
| 15 | + super().__init__() |
| 16 | + self.namespace_dir = ( |
| 17 | + Path(os.path.dirname(__file__)) |
| 18 | + / "data" |
| 19 | + / "admin" |
| 20 | + / "namespaces" |
| 21 | + / "two_namespaces_two_users" |
| 22 | + ) |
| 23 | + self.network_dir = ( |
| 24 | + Path(os.path.dirname(__file__)) / "data" / "admin" / "networks" / "6_node_bitcoin" |
| 25 | + ) |
| 26 | + |
| 27 | + def run_test(self): |
| 28 | + try: |
| 29 | + self.setup_namespaces() |
| 30 | + self.setup_service_accounts() |
| 31 | + self.deploy_network_in_team_namespaces() |
| 32 | + self.authenticate_and_become_bob() |
| 33 | + self.become_minikube_once_again() |
| 34 | + finally: |
| 35 | + self.cleanup() |
| 36 | + |
| 37 | + def become_minikube_once_again(self): |
| 38 | + minikube = "minikube" |
| 39 | + cmd = f"kubectl config use-context {minikube}" |
| 40 | + self.log.info(run_command(cmd)) |
| 41 | + self.wait_for_predicate(self.this_is_the_current_context(minikube)) |
| 42 | + |
| 43 | + def this_is_the_current_context(self, context: str) -> Callable[[], bool]: |
| 44 | + cmd = "kubectl config current-context" |
| 45 | + current_context = run_command(cmd).strip() |
| 46 | + self.log.info(f"Current context: {current_context} {context == current_context}") |
| 47 | + return lambda: current_context == context |
| 48 | + |
| 49 | + def setup_namespaces(self): |
| 50 | + self.log.info("Setting up the namespaces") |
| 51 | + self.log.info(self.warnet(f"deploy {self.namespace_dir}")) |
| 52 | + self.wait_for_predicate(self.two_namespaces_are_validated) |
| 53 | + self.log.info("Namespace setup complete") |
| 54 | + |
| 55 | + def setup_service_accounts(self): |
| 56 | + self.log.info("Creating service accounts...") |
| 57 | + self.log.info(self.warnet("admin create-kubeconfigs")) |
| 58 | + self.wait_for_predicate(self.service_accounts_are_validated) |
| 59 | + |
| 60 | + def deploy_network_in_team_namespaces(self): |
| 61 | + self.log.info("Deploy networks to team namespaces") |
| 62 | + self.log.info(self.warnet(f"deploy {self.network_dir} --to-all-users")) |
| 63 | + self.wait_for_all_tanks_status() |
| 64 | + self.log.info("Waiting for all edges") |
| 65 | + self.wait_for_all_edges() |
| 66 | + |
| 67 | + def authenticate_and_become_bob(self): |
| 68 | + self.log.info("Authenticating and becoming bob...") |
| 69 | + assert get_kubeconfig_value("{.current-context}") == "minikube" |
| 70 | + self.log.info(self.warnet("auth kubeconfigs/bob-wargames-red-team-kubeconfig")) |
| 71 | + assert get_kubeconfig_value("{.current-context}") == "bob-wargames-red-team" |
| 72 | + |
| 73 | + def get_service_accounts(self) -> Optional[dict[str, str]]: |
| 74 | + self.log.info("Setting up service accounts") |
| 75 | + resp = self.warnet("admin service-accounts list") |
| 76 | + if resp == "Could not find any matching service accounts.": |
| 77 | + return None |
| 78 | + service_accounts: dict[str, [str]] = {} |
| 79 | + current_namespace = "" |
| 80 | + for line in resp.splitlines(): |
| 81 | + if line.startswith("Service"): |
| 82 | + current_namespace = line.split(": ")[1] |
| 83 | + service_accounts[current_namespace] = [] |
| 84 | + if line.startswith("- "): |
| 85 | + sa = line.lstrip("- ") |
| 86 | + service_accounts[current_namespace].append(sa) |
| 87 | + self.log.info(f"Service accounts: {service_accounts}") |
| 88 | + return service_accounts |
| 89 | + |
| 90 | + def service_accounts_are_validated(self) -> bool: |
| 91 | + self.log.info("Checking service accounts") |
| 92 | + maybe_service_accounts = self.get_service_accounts() |
| 93 | + expected = { |
| 94 | + "wargames-blue-team": ["carol", "default", "mallory"], |
| 95 | + "wargames-red-team": ["alice", "bob", "default"], |
| 96 | + } |
| 97 | + return maybe_service_accounts == expected |
| 98 | + |
| 99 | + def get_namespaces(self) -> Optional[list[str]]: |
| 100 | + self.log.info("Querying the namespaces...") |
| 101 | + resp = self.warnet("admin namespaces list") |
| 102 | + if resp == "No warnet namespaces found.": |
| 103 | + return None |
| 104 | + namespaces = [] |
| 105 | + for line in resp.splitlines(): |
| 106 | + if line.startswith("- "): |
| 107 | + namespaces.append(line.lstrip("- ")) |
| 108 | + self.log.info(f"Namespaces: {namespaces}") |
| 109 | + return namespaces |
| 110 | + |
| 111 | + def two_namespaces_are_validated(self) -> bool: |
| 112 | + maybe_namespaces = self.get_namespaces() |
| 113 | + if maybe_namespaces is None: |
| 114 | + return False |
| 115 | + if len(maybe_namespaces) != 2: |
| 116 | + return False |
| 117 | + if "wargames-blue-team" not in maybe_namespaces: |
| 118 | + return False |
| 119 | + return "wargames-red-team" in maybe_namespaces |
| 120 | + |
| 121 | + |
| 122 | +if __name__ == "__main__": |
| 123 | + test = NamespaceAdminTest() |
| 124 | + test.run_test() |
0 commit comments