Skip to content

Commit 7fe53ea

Browse files
committed
scenarios: add --admin flag to run command for ClusterRole access
1 parent e158f3f commit 7fe53ea

File tree

5 files changed

+50
-4
lines changed

5 files changed

+50
-4
lines changed

resources/charts/commander/templates/rbac.yaml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,33 @@ subjects:
3333
- kind: ServiceAccount
3434
name: {{ include "commander.fullname" . }}
3535
namespace: {{ .Release.Namespace }}
36+
{{- if .Values.admin }}
37+
---
38+
apiVersion: rbac.authorization.k8s.io/v1
39+
kind: ClusterRole
40+
metadata:
41+
name: {{ include "commander.fullname" . }}
42+
namespace: {{ .Release.Namespace }}
43+
labels:
44+
app.kubernetes.io/name: {{ .Chart.Name }}
45+
rules:
46+
- apiGroups: [""]
47+
resources: ["pods", "namespaces", "configmaps"]
48+
verbs: ["get", "list", "watch"]
49+
---
50+
apiVersion: rbac.authorization.k8s.io/v1
51+
kind: ClusterRoleBinding
52+
metadata:
53+
name: {{ include "commander.fullname" . }}
54+
namespace: {{ .Release.Namespace }}
55+
labels:
56+
app.kubernetes.io/name: {{ .Chart.Name }}
57+
roleRef:
58+
kind: ClusterRole
59+
name: {{ include "commander.fullname" . }}
60+
apiGroup: rbac.authorization.k8s.io
61+
subjects:
62+
- kind: ServiceAccount
63+
name: {{ include "commander.fullname" . }}
64+
namespace: {{ .Release.Namespace }}
65+
{{- end}}

resources/charts/commander/values.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,5 @@ volumeMounts: []
6666
port:
6767

6868
args: ""
69+
70+
admin: false

resources/scenarios/commander.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,19 @@
2929
with open("/var/run/secrets/kubernetes.io/serviceaccount/namespace") as f:
3030
NAMESPACE = f.read().strip()
3131

32-
# Use the in-cluster k8s client to determine what pods we have access to
32+
# Get the in-cluster k8s client to determine what we have access to
3333
config.load_incluster_config()
3434
sclient = client.CoreV1Api()
35-
pods = sclient.list_namespaced_pod(namespace=NAMESPACE)
36-
cmaps = sclient.list_namespaced_config_map(namespace=NAMESPACE)
35+
36+
try:
37+
# An admin with cluster access can list everything.
38+
# A wargames player with namespaced access will get a FORBIDDEN error here
39+
pods = sclient.list_pod_for_all_namespaces()
40+
cmaps = sclient.list_config_map_for_all_namespaces()
41+
except Exception:
42+
# Just get whatever we have access to in this namespace only
43+
pods = sclient.list_namespaced_pod(namespace=NAMESPACE)
44+
cmaps = sclient.list_namespaced_config_map(namespace=NAMESPACE)
3745

3846
WARNET = {"tanks": [], "lightning": [], "channels": []}
3947
for pod in pods.items:

src/warnet/control.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,26 +240,29 @@ def get_active_network(namespace):
240240
"--source_dir", type=click.Path(exists=True, file_okay=False, dir_okay=True), required=False
241241
)
242242
@click.argument("additional_args", nargs=-1, type=click.UNPROCESSED)
243+
@click.option("--admin", is_flag=True, default=False, show_default=False)
243244
@click.option("--namespace", default=None, show_default=True)
244245
def run(
245246
scenario_file: str,
246247
debug: bool,
247248
source_dir,
248249
additional_args: tuple[str],
250+
admin: bool,
249251
namespace: Optional[str],
250252
):
251253
"""
252254
Run a scenario from a file.
253255
Pass `-- --help` to get individual scenario help
254256
"""
255-
return _run(scenario_file, debug, source_dir, additional_args, namespace)
257+
return _run(scenario_file, debug, source_dir, additional_args, admin, namespace)
256258

257259

258260
def _run(
259261
scenario_file: str,
260262
debug: bool,
261263
source_dir,
262264
additional_args: tuple[str],
265+
admin: bool,
263266
namespace: Optional[str],
264267
) -> str:
265268
namespace = get_default_namespace_or(namespace)
@@ -329,6 +332,8 @@ def filter(path):
329332
]
330333

331334
# Add additional arguments
335+
if admin:
336+
helm_command.extend(["--set", "admin=true"])
332337
if additional_args:
333338
helm_command.extend(["--set", f"args={' '.join(additional_args)}"])
334339

src/warnet/deploy.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,7 @@ def deploy_network(directory: Path, debug: bool = False, namespace: Optional[str
385385
debug=False,
386386
source_dir=SCENARIOS_DIR,
387387
additional_args=None,
388+
admin=False,
388389
namespace=namespace,
389390
)
390391
wait_for_pod(name, namespace=namespace)

0 commit comments

Comments
 (0)