11"""Manage the plugin for the tutorcodejail."""
2+
23from __future__ import annotations
34
45import os
56from glob import glob
67from pathlib import Path
78
9+ import typing as t
10+
811import importlib_resources
912from tutor import hooks
13+ from tutor .types import Config
1014
1115from .__about__ import __version__
1216
1721 "SECRET_KEY" : "{{ 24|random_string }}" ,
1822 },
1923 "defaults" : {
20- "APPARMOR_DOCKER_IMAGE" : "docker.io/ednxops/codejail_apparmor_loader:apparmor-3 " ,
24+ "APPARMOR_DOCKER_IMAGE" : "docker.io/ednxops/codejail_apparmor_loader:apparmor-4 " ,
2125 "DOCKER_IMAGE" : f"docker.io/ednxops/codejailservice:{ __version__ } " ,
26+ "DOCKER_IMAGE_V2" : "{{ CODEJAIL_DOCKER_IMAGE }}-v2" ,
2227 "ENABLE_K8S_DAEMONSET" : False ,
2328 "ENFORCE_APPARMOR" : True ,
2429 "EXTRA_PIP_REQUIREMENTS" : [],
2530 "HOST" : "codejailservice" ,
2631 "SANDBOX_PYTHON_VERSION" : "3.11.14" ,
2732 "SERVICE_REPOSITORY" : "https://github.com/edunext/codejailservice.git" ,
33+ "SERVICE_V2_REPOSITORY" : "https://github.com/openedx/codejail-service.git" ,
34+ "SERVICE_V2_VERSION" : "{{ OPENEDX_COMMON_VERSION }}" ,
2835 "SERVICE_VERSION" : "{{ OPENEDX_COMMON_VERSION }}" ,
2936 "SKIP_INIT" : False ,
37+ "USE_SERVICE_V2" : False ,
3038 "VERSION" : __version__ ,
3139 },
3240 "overrides" : {},
3543
3644def get_apparmor_abi ():
3745 """
38- Return the default abi 3.0 rule if available in the system.
46+ Return the latest default abi rule if available in the system.
3947
4048 AppArmor uses the Policy feature ABI to establish which rules it can
4149 enforce based on the kernel capabilities. AppArmor profiles can include an
4250 ABI rule to indicate the ABI they were developed under. If no rule is used
4351 AppArmor will fallback to whichever rule is pinned in the
4452 `/etc/apparmor/parser.conf` file.
4553
46- We try to use the 3.0 abi whenever it's available at `/etc/apparmor.d/abi/`
54+ We try to use at least the 3.0 abi whenever it's available at `/etc/apparmor.d/abi/`
4755 to guarantee that network rules are correctly enforced on newer versions of
48- the kernel. If the ABI is not present we don't set the abi rule and instead
49- rely on the default fallback.
56+ the kernel. If neither the 3.0 ABI nor the 4.0 ABI are present we don't set
57+ the abi rule and instead rely on the default fallback.
5058
5159 See: https://github.com/netblue30/firejail/issues/3659#issuecomment-711074899
5260 """
61+ if Path (f"{ ABI_PATH } /4.0" ).exists ():
62+ return "abi <abi/4.0>,"
63+
5364 if Path (f"{ ABI_PATH } /3.0" ).exists ():
5465 return "abi <abi/3.0>,"
66+
5567 return ""
5668
5769
@@ -62,45 +74,56 @@ def get_apparmor_abi():
6274)
6375
6476
65- hooks .Filters .IMAGES_BUILD .add_item ((
66- "codejail" ,
67- ("plugins" , "codejail" , "build" , "codejail" ),
68- "{{ CODEJAIL_DOCKER_IMAGE }}" ,
69- (),
70- ))
71-
72-
73- hooks .Filters .IMAGES_BUILD .add_item ((
74- "codejail_apparmor" ,
75- ("plugins" , "codejail" , "build" , "codejail_apparmor" ),
76- "{{CODEJAIL_APPARMOR_DOCKER_IMAGE}}" ,
77- (),
78- ))
79-
80-
81- hooks .Filters .IMAGES_PULL .add_item ((
82- "codejail" ,
83- "{{ CODEJAIL_DOCKER_IMAGE }}" ,
84- ))
85-
86-
87- hooks .Filters .IMAGES_PULL .add_item ((
88- "codejail_apparmor" ,
89- "{{CODEJAIL_APPARMOR_DOCKER_IMAGE}}" ,
90- ))
91-
92-
93- hooks .Filters .IMAGES_PUSH .add_item ((
94- "codejail" ,
95- "{{ CODEJAIL_DOCKER_IMAGE }}" ,
96- ))
97-
98-
99- hooks .Filters .IMAGES_PUSH .add_item ((
100- "codejail_apparmor" ,
101- "{{CODEJAIL_APPARMOR_DOCKER_IMAGE}}" ,
102- ))
103-
77+ @hooks .Filters .IMAGES_BUILD .add ()
78+ def _build_codejail_images (
79+ images : list [tuple [str , t .Union [str , tuple [str , ...]], str , tuple [str , ...]]],
80+ config : Config ,
81+ ):
82+ # TODO: Remove after the Verawood update
83+ if config .get ("CODEJAIL_USE_SERVICE_V2" ):
84+ codejail_img = (
85+ "codejail" ,
86+ "plugins/codejail/build/codejail-service" ,
87+ "{{ CODEJAIL_DOCKER_IMAGE_V2 }}" ,
88+ (),
89+ )
90+ else :
91+ codejail_img = (
92+ "codejail" ,
93+ "plugins/codejail/build/codejail" ,
94+ "{{ CODEJAIL_DOCKER_IMAGE }}" ,
95+ (),
96+ )
97+ apparmor_img = (
98+ "codejail_apparmor" ,
99+ ("plugins" , "codejail" , "build" , "codejail_apparmor" ),
100+ "{{CODEJAIL_APPARMOR_DOCKER_IMAGE}}" ,
101+ (),
102+ )
103+
104+ return images + [codejail_img , apparmor_img ]
105+
106+ @hooks .Filters .IMAGES_PUSH .add ()
107+ def _push_codejail_images (
108+ images : list [tuple [str , t .Union [str , tuple [str , ...]], str , tuple [str , ...]]],
109+ config : Config ,
110+ ):
111+ # TODO: Remove after the Verawood update
112+ if config .get ("CODEJAIL_USE_SERVICE_V2" ):
113+ codejail_img = (
114+ "codejail" ,
115+ "{{ CODEJAIL_DOCKER_IMAGE_V2 }}" ,
116+ )
117+ else :
118+ codejail_img = (
119+ "codejail" ,
120+ "{{ CODEJAIL_DOCKER_IMAGE }}" ,
121+ )
122+ apparmor_img = (
123+ "codejail_apparmor" ,
124+ "{{CODEJAIL_APPARMOR_DOCKER_IMAGE}}" ,
125+ )
126+ return images + [codejail_img , apparmor_img ]
104127
105128# Boilerplate code
106129# Add the "templates" folder as a template root
@@ -121,15 +144,9 @@ def get_apparmor_abi():
121144 hooks .Filters .ENV_PATCHES .add_item ((os .path .basename (path ), patch_file .read ()))
122145# Add configuration entries
123146hooks .Filters .CONFIG_DEFAULTS .add_items (
124- [
125- (f"CODEJAIL_{ key } " , value )
126- for key , value in config .get ("defaults" , {}).items ()
127- ]
147+ [(f"CODEJAIL_{ key } " , value ) for key , value in config .get ("defaults" , {}).items ()]
128148)
129149hooks .Filters .CONFIG_UNIQUE .add_items (
130- [
131- (f"CODEJAIL_{ key } " , value )
132- for key , value in config .get ("unique" , {}).items ()
133- ]
150+ [(f"CODEJAIL_{ key } " , value ) for key , value in config .get ("unique" , {}).items ()]
134151)
135152hooks .Filters .CONFIG_OVERRIDES .add_items (list (config .get ("overrides" , {}).items ()))
0 commit comments