nsjail-python 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. nsjail_python-0.1.0/.github/workflows/ci.yml +21 -0
  2. nsjail_python-0.1.0/.github/workflows/publish.yml +48 -0
  3. nsjail_python-0.1.0/.gitignore +9 -0
  4. nsjail_python-0.1.0/.gitmodules +3 -0
  5. nsjail_python-0.1.0/.readthedocs.yaml +16 -0
  6. nsjail_python-0.1.0/LICENSE +21 -0
  7. nsjail_python-0.1.0/PKG-INFO +141 -0
  8. nsjail_python-0.1.0/README.md +109 -0
  9. nsjail_python-0.1.0/_codegen/__init__.py +0 -0
  10. nsjail_python-0.1.0/_codegen/cli_flags.py +73 -0
  11. nsjail_python-0.1.0/_codegen/generate.py +655 -0
  12. nsjail_python-0.1.0/conftest.py +33 -0
  13. nsjail_python-0.1.0/docs/api.rst +66 -0
  14. nsjail_python-0.1.0/docs/conf.py +19 -0
  15. nsjail_python-0.1.0/docs/index.rst +17 -0
  16. nsjail_python-0.1.0/docs/plans/explore-nsjail-python-wrapper.md +111 -0
  17. nsjail_python-0.1.0/docs/quickstart.rst +100 -0
  18. nsjail_python-0.1.0/docs/superpowers/plans/2026-03-29-nsjail-python-implementation.md +3162 -0
  19. nsjail_python-0.1.0/docs/superpowers/plans/2026-03-29-v01x-enhancements.md +953 -0
  20. nsjail_python-0.1.0/docs/superpowers/specs/2026-03-29-nsjail-python-design.md +634 -0
  21. nsjail_python-0.1.0/docs/superpowers/specs/2026-03-29-v01x-enhancements-design.md +234 -0
  22. nsjail_python-0.1.0/packages/nsjail-bin/pyproject.toml +13 -0
  23. nsjail_python-0.1.0/packages/nsjail-bin/src/nsjail_bin/__init__.py +15 -0
  24. nsjail_python-0.1.0/packages/nsjail-bin-build/pyproject.toml +13 -0
  25. nsjail_python-0.1.0/packages/nsjail-bin-build/src/nsjail_bin_build/__init__.py +15 -0
  26. nsjail_python-0.1.0/packages/nsjail-bin-none/pyproject.toml +13 -0
  27. nsjail_python-0.1.0/packages/nsjail-bin-none/src/nsjail_bin_none/__init__.py +1 -0
  28. nsjail_python-0.1.0/pyproject.toml +47 -0
  29. nsjail_python-0.1.0/src/nsjail/__init__.py +21 -0
  30. nsjail_python-0.1.0/src/nsjail/_field_meta.py +177 -0
  31. nsjail_python-0.1.0/src/nsjail/_proto/__init__.py +57 -0
  32. nsjail_python-0.1.0/src/nsjail/_proto/config_pb2.py +50 -0
  33. nsjail_python-0.1.0/src/nsjail/builder.py +151 -0
  34. nsjail_python-0.1.0/src/nsjail/config.py +168 -0
  35. nsjail_python-0.1.0/src/nsjail/enums.py +27 -0
  36. nsjail_python-0.1.0/src/nsjail/exceptions.py +29 -0
  37. nsjail_python-0.1.0/src/nsjail/presets.py +74 -0
  38. nsjail_python-0.1.0/src/nsjail/runner.py +274 -0
  39. nsjail_python-0.1.0/src/nsjail/serializers/__init__.py +26 -0
  40. nsjail_python-0.1.0/src/nsjail/serializers/cli.py +69 -0
  41. nsjail_python-0.1.0/src/nsjail/serializers/protobuf.py +22 -0
  42. nsjail_python-0.1.0/src/nsjail/serializers/textproto.py +91 -0
  43. nsjail_python-0.1.0/tests/__init__.py +0 -0
  44. nsjail_python-0.1.0/tests/test_builder.py +177 -0
  45. nsjail_python-0.1.0/tests/test_cli_serializer.py +63 -0
  46. nsjail_python-0.1.0/tests/test_codegen.py +157 -0
  47. nsjail_python-0.1.0/tests/test_config.py +121 -0
  48. nsjail_python-0.1.0/tests/test_enums.py +31 -0
  49. nsjail_python-0.1.0/tests/test_exceptions.py +22 -0
  50. nsjail_python-0.1.0/tests/test_field_meta.py +55 -0
  51. nsjail_python-0.1.0/tests/test_integration.py +118 -0
  52. nsjail_python-0.1.0/tests/test_presets.py +102 -0
  53. nsjail_python-0.1.0/tests/test_protobuf_serializer.py +52 -0
  54. nsjail_python-0.1.0/tests/test_public_api.py +29 -0
  55. nsjail_python-0.1.0/tests/test_runner.py +197 -0
  56. nsjail_python-0.1.0/tests/test_serializers.py +112 -0
@@ -0,0 +1,21 @@
1
+ name: CI
2
+
3
+ on:
4
+ push:
5
+ branches: [main]
6
+ pull_request:
7
+ branches: [main]
8
+
9
+ jobs:
10
+ test:
11
+ runs-on: ubuntu-latest
12
+ strategy:
13
+ matrix:
14
+ python-version: ["3.12", "3.13"]
15
+ steps:
16
+ - uses: actions/checkout@v4
17
+ - uses: actions/setup-python@v5
18
+ with:
19
+ python-version: ${{ matrix.python-version }}
20
+ - run: pip install -e ".[dev]"
21
+ - run: pytest tests/ -v
@@ -0,0 +1,48 @@
1
+ name: Publish to PyPI
2
+
3
+ on:
4
+ release:
5
+ types: [published]
6
+
7
+ permissions:
8
+ id-token: write
9
+
10
+ jobs:
11
+ test:
12
+ runs-on: ubuntu-latest
13
+ strategy:
14
+ matrix:
15
+ python-version: ["3.12", "3.13"]
16
+ steps:
17
+ - uses: actions/checkout@v4
18
+ - uses: actions/setup-python@v5
19
+ with:
20
+ python-version: ${{ matrix.python-version }}
21
+ - run: pip install -e ".[dev]"
22
+ - run: pytest tests/ -v
23
+
24
+ build:
25
+ needs: test
26
+ runs-on: ubuntu-latest
27
+ steps:
28
+ - uses: actions/checkout@v4
29
+ - uses: actions/setup-python@v5
30
+ with:
31
+ python-version: "3.12"
32
+ - run: pip install build
33
+ - run: python -m build
34
+ - uses: actions/upload-artifact@v4
35
+ with:
36
+ name: dist
37
+ path: dist/
38
+
39
+ publish:
40
+ needs: build
41
+ runs-on: ubuntu-latest
42
+ environment: pypi
43
+ steps:
44
+ - uses: actions/download-artifact@v4
45
+ with:
46
+ name: dist
47
+ path: dist/
48
+ - uses: pypa/gh-action-pypi-publish@release/v1
@@ -0,0 +1,9 @@
1
+ __pycache__/
2
+ *.pyc
3
+ *.egg-info/
4
+ dist/
5
+ build/
6
+ .venv/
7
+ *.so
8
+ _vendor/*/
9
+ !_vendor/nsjail
@@ -0,0 +1,3 @@
1
+ [submodule "_vendor/nsjail"]
2
+ path = _vendor/nsjail
3
+ url = https://github.com/google/nsjail.git
@@ -0,0 +1,16 @@
1
+ version: 2
2
+
3
+ build:
4
+ os: ubuntu-22.04
5
+ tools:
6
+ python: "3.12"
7
+
8
+ sphinx:
9
+ configuration: docs/conf.py
10
+
11
+ python:
12
+ install:
13
+ - method: pip
14
+ path: .
15
+ extra_requirements:
16
+ - dev
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 nsjail-python contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,141 @@
1
+ Metadata-Version: 2.4
2
+ Name: nsjail-python
3
+ Version: 0.1.0
4
+ Summary: Python wrapper for Google's nsjail sandboxing tool
5
+ Project-URL: Homepage, https://github.com/teaguesterling/nsjail-python
6
+ Project-URL: Documentation, https://nsjail-python.readthedocs.io
7
+ Project-URL: Repository, https://github.com/teaguesterling/nsjail-python
8
+ Project-URL: Issues, https://github.com/teaguesterling/nsjail-python/issues
9
+ License-Expression: MIT
10
+ License-File: LICENSE
11
+ Keywords: container,isolation,nsjail,sandbox,security
12
+ Classifier: Development Status :: 3 - Alpha
13
+ Classifier: Intended Audience :: Developers
14
+ Classifier: Intended Audience :: System Administrators
15
+ Classifier: License :: OSI Approved :: MIT License
16
+ Classifier: Operating System :: POSIX :: Linux
17
+ Classifier: Programming Language :: Python :: 3
18
+ Classifier: Programming Language :: Python :: 3.12
19
+ Classifier: Programming Language :: Python :: 3.13
20
+ Classifier: Topic :: Security
21
+ Classifier: Topic :: System :: Operating System Kernels :: Linux
22
+ Requires-Python: >=3.12
23
+ Provides-Extra: dev
24
+ Requires-Dist: furo; extra == 'dev'
25
+ Requires-Dist: grpcio-tools; extra == 'dev'
26
+ Requires-Dist: protobuf>=4.0; extra == 'dev'
27
+ Requires-Dist: pytest; extra == 'dev'
28
+ Requires-Dist: sphinx; extra == 'dev'
29
+ Provides-Extra: proto
30
+ Requires-Dist: protobuf>=4.0; extra == 'proto'
31
+ Description-Content-Type: text/markdown
32
+
33
+ # nsjail-python
34
+
35
+ Python wrapper for Google's nsjail sandboxing tool
36
+
37
+ [![PyPI](https://img.shields.io/pypi/v/nsjail-python)](https://pypi.org/project/nsjail-python/)
38
+ [![Python Version](https://img.shields.io/pypi/pyversions/nsjail-python)](https://pypi.org/project/nsjail-python/)
39
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
40
+
41
+ ## Installation
42
+
43
+ ```bash
44
+ pip install nsjail-python
45
+ ```
46
+
47
+ Optional extras:
48
+
49
+ ```bash
50
+ pip install nsjail-python[proto] # Enable protobuf validation
51
+ pip install nsjail-python[system] # Use system-provided nsjail
52
+ pip install nsjail-python[build] # Build nsjail from source
53
+ ```
54
+
55
+ ## Quick Start
56
+
57
+ ### Low-level: NsJailConfig dataclass
58
+
59
+ ```python
60
+ from nsjail import NsJailConfig, MountPt, Exe
61
+
62
+ cfg = NsJailConfig(
63
+ hostname="sandbox",
64
+ time_limit=30,
65
+ mount=[MountPt(src="/", dst="/", is_bind=True, rw=False)],
66
+ exec_bin=Exe(path="/bin/sh", arg=["-c", "echo hello"]),
67
+ )
68
+ ```
69
+
70
+ ### Mid-level: sandbox() preset
71
+
72
+ ```python
73
+ from nsjail import sandbox
74
+
75
+ cfg = sandbox(
76
+ command=["python", "script.py"],
77
+ memory_mb=512,
78
+ timeout_sec=60,
79
+ writable_dirs=["/workspace", "/tmp"],
80
+ )
81
+ ```
82
+
83
+ ### High-level: Jail() fluent builder
84
+
85
+ ```python
86
+ from nsjail import Jail
87
+
88
+ cfg = (
89
+ Jail()
90
+ .sh("pytest tests/ -v")
91
+ .memory(512, "MB")
92
+ .timeout(60)
93
+ .readonly_root()
94
+ .writable("/workspace")
95
+ .writable("/tmp", tmpfs=True, size="64M")
96
+ .no_network()
97
+ .build()
98
+ )
99
+ ```
100
+
101
+ ## Serialization
102
+
103
+ ```python
104
+ from nsjail.serializers import to_textproto, to_cli_args, to_file
105
+
106
+ # Protobuf text format (for --config flag)
107
+ print(to_textproto(cfg))
108
+
109
+ # CLI arguments
110
+ args = to_cli_args(cfg, on_unsupported="skip")
111
+
112
+ # Write to file
113
+ to_file(cfg, "sandbox.cfg")
114
+ ```
115
+
116
+ ## Running nsjail
117
+
118
+ ```python
119
+ from nsjail import Runner, Jail
120
+
121
+ runner = Runner(
122
+ base_config=Jail()
123
+ .command("python", "-m", "pytest")
124
+ .memory(512, "MB")
125
+ .timeout(300)
126
+ .readonly_root()
127
+ .writable("/workspace")
128
+ .build(),
129
+ )
130
+
131
+ result = runner.run(extra_args=["tests/unit/", "-x"])
132
+ print(result.returncode, result.stdout)
133
+ ```
134
+
135
+ ## Documentation
136
+
137
+ Full documentation is available at [nsjail-python.readthedocs.io](https://nsjail-python.readthedocs.io).
138
+
139
+ ## License
140
+
141
+ MIT
@@ -0,0 +1,109 @@
1
+ # nsjail-python
2
+
3
+ Python wrapper for Google's nsjail sandboxing tool
4
+
5
+ [![PyPI](https://img.shields.io/pypi/v/nsjail-python)](https://pypi.org/project/nsjail-python/)
6
+ [![Python Version](https://img.shields.io/pypi/pyversions/nsjail-python)](https://pypi.org/project/nsjail-python/)
7
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
8
+
9
+ ## Installation
10
+
11
+ ```bash
12
+ pip install nsjail-python
13
+ ```
14
+
15
+ Optional extras:
16
+
17
+ ```bash
18
+ pip install nsjail-python[proto] # Enable protobuf validation
19
+ pip install nsjail-python[system] # Use system-provided nsjail
20
+ pip install nsjail-python[build] # Build nsjail from source
21
+ ```
22
+
23
+ ## Quick Start
24
+
25
+ ### Low-level: NsJailConfig dataclass
26
+
27
+ ```python
28
+ from nsjail import NsJailConfig, MountPt, Exe
29
+
30
+ cfg = NsJailConfig(
31
+ hostname="sandbox",
32
+ time_limit=30,
33
+ mount=[MountPt(src="/", dst="/", is_bind=True, rw=False)],
34
+ exec_bin=Exe(path="/bin/sh", arg=["-c", "echo hello"]),
35
+ )
36
+ ```
37
+
38
+ ### Mid-level: sandbox() preset
39
+
40
+ ```python
41
+ from nsjail import sandbox
42
+
43
+ cfg = sandbox(
44
+ command=["python", "script.py"],
45
+ memory_mb=512,
46
+ timeout_sec=60,
47
+ writable_dirs=["/workspace", "/tmp"],
48
+ )
49
+ ```
50
+
51
+ ### High-level: Jail() fluent builder
52
+
53
+ ```python
54
+ from nsjail import Jail
55
+
56
+ cfg = (
57
+ Jail()
58
+ .sh("pytest tests/ -v")
59
+ .memory(512, "MB")
60
+ .timeout(60)
61
+ .readonly_root()
62
+ .writable("/workspace")
63
+ .writable("/tmp", tmpfs=True, size="64M")
64
+ .no_network()
65
+ .build()
66
+ )
67
+ ```
68
+
69
+ ## Serialization
70
+
71
+ ```python
72
+ from nsjail.serializers import to_textproto, to_cli_args, to_file
73
+
74
+ # Protobuf text format (for --config flag)
75
+ print(to_textproto(cfg))
76
+
77
+ # CLI arguments
78
+ args = to_cli_args(cfg, on_unsupported="skip")
79
+
80
+ # Write to file
81
+ to_file(cfg, "sandbox.cfg")
82
+ ```
83
+
84
+ ## Running nsjail
85
+
86
+ ```python
87
+ from nsjail import Runner, Jail
88
+
89
+ runner = Runner(
90
+ base_config=Jail()
91
+ .command("python", "-m", "pytest")
92
+ .memory(512, "MB")
93
+ .timeout(300)
94
+ .readonly_root()
95
+ .writable("/workspace")
96
+ .build(),
97
+ )
98
+
99
+ result = runner.run(extra_args=["tests/unit/", "-x"])
100
+ print(result.returncode, result.stdout)
101
+ ```
102
+
103
+ ## Documentation
104
+
105
+ Full documentation is available at [nsjail-python.readthedocs.io](https://nsjail-python.readthedocs.io).
106
+
107
+ ## License
108
+
109
+ MIT
File without changes
@@ -0,0 +1,73 @@
1
+ """Hand-maintained mapping of proto field names to CLI flags.
2
+
3
+ The code generator merges this table into _field_meta.py.
4
+ Fields not listed here are assumed to have no CLI equivalent.
5
+ """
6
+
7
+ # (message_name, field_name) -> (cli_flag, cli_supported)
8
+ CLI_FLAGS: dict[tuple[str, str], tuple[str, bool]] = {
9
+ ("NsJailConfig", "name"): ("--name", True),
10
+ ("NsJailConfig", "hostname"): ("--hostname", True),
11
+ ("NsJailConfig", "cwd"): ("--cwd", True),
12
+ ("NsJailConfig", "port"): ("--port", True),
13
+ ("NsJailConfig", "bindhost"): ("--bindhost", True),
14
+ ("NsJailConfig", "max_conns"): ("--max_conns", True),
15
+ ("NsJailConfig", "max_conns_per_ip"): ("--max_conns_per_ip", True),
16
+ ("NsJailConfig", "time_limit"): ("--time_limit", True),
17
+ ("NsJailConfig", "daemon"): ("--daemon", True),
18
+ ("NsJailConfig", "max_cpus"): ("--max_cpus", True),
19
+ ("NsJailConfig", "nice_level"): ("--nice_level", True),
20
+ ("NsJailConfig", "keep_env"): ("--keep_env", True),
21
+ ("NsJailConfig", "envar"): ("--env", True),
22
+ ("NsJailConfig", "silent"): ("--silent", True),
23
+ ("NsJailConfig", "skip_setsid"): ("--skip_setsid", True),
24
+ ("NsJailConfig", "stderr_to_null"): ("--stderr_to_null", True),
25
+ ("NsJailConfig", "pass_fd"): ("--pass_fd", True),
26
+ ("NsJailConfig", "disable_no_new_privs"): ("--disable_no_new_privs", True),
27
+ ("NsJailConfig", "forward_signals"): ("--forward_signals", True),
28
+ ("NsJailConfig", "disable_tsc"): ("--disable_tsc", True),
29
+ ("NsJailConfig", "oom_score_adj"): ("--oom_score_adj", True),
30
+ ("NsJailConfig", "log_fd"): ("--log_fd", True),
31
+ ("NsJailConfig", "log_file"): ("--log", True),
32
+ ("NsJailConfig", "keep_caps"): ("--keep_caps", True),
33
+ ("NsJailConfig", "cap"): ("--cap", True),
34
+ ("NsJailConfig", "rlimit_as"): ("--rlimit_as", True),
35
+ ("NsJailConfig", "rlimit_core"): ("--rlimit_core", True),
36
+ ("NsJailConfig", "rlimit_cpu"): ("--rlimit_cpu", True),
37
+ ("NsJailConfig", "rlimit_fsize"): ("--rlimit_fsize", True),
38
+ ("NsJailConfig", "rlimit_nofile"): ("--rlimit_nofile", True),
39
+ ("NsJailConfig", "rlimit_nproc"): ("--rlimit_nproc", True),
40
+ ("NsJailConfig", "rlimit_stack"): ("--rlimit_stack", True),
41
+ ("NsJailConfig", "disable_rl"): ("--disable_rl", True),
42
+ ("NsJailConfig", "clone_newnet"): (None, True),
43
+ ("NsJailConfig", "clone_newuser"): (None, True),
44
+ ("NsJailConfig", "clone_newns"): (None, True),
45
+ ("NsJailConfig", "clone_newpid"): (None, True),
46
+ ("NsJailConfig", "clone_newipc"): (None, True),
47
+ ("NsJailConfig", "clone_newuts"): (None, True),
48
+ ("NsJailConfig", "clone_newcgroup"): (None, True),
49
+ ("NsJailConfig", "clone_newtime"): (None, True),
50
+ ("NsJailConfig", "uidmap"): ("--uid_mapping", True),
51
+ ("NsJailConfig", "gidmap"): ("--gid_mapping", True),
52
+ ("NsJailConfig", "mount_proc"): ("--mount_proc", True),
53
+ ("NsJailConfig", "no_pivotroot"): ("--no_pivotroot", True),
54
+ ("NsJailConfig", "seccomp_policy_file"): ("--seccomp_policy", True),
55
+ ("NsJailConfig", "seccomp_string"): ("--seccomp_string", True),
56
+ ("NsJailConfig", "seccomp_log"): ("--seccomp_log", True),
57
+ ("NsJailConfig", "cgroup_mem_max"): ("--cgroup_mem_max", True),
58
+ ("NsJailConfig", "cgroup_mem_memsw_max"): ("--cgroup_mem_memsw_max", True),
59
+ ("NsJailConfig", "cgroup_mem_swap_max"): ("--cgroup_mem_swap_max", True),
60
+ ("NsJailConfig", "cgroup_pids_max"): ("--cgroup_pids_max", True),
61
+ ("NsJailConfig", "cgroup_net_cls_classid"): ("--cgroup_net_cls_classid", True),
62
+ ("NsJailConfig", "cgroup_cpu_ms_per_sec"): ("--cgroup_cpu_ms_per_sec", True),
63
+ ("NsJailConfig", "use_cgroupv2"): ("--use_cgroupv2", True),
64
+ ("NsJailConfig", "detect_cgroupv2"): ("--detect_cgroupv2", True),
65
+ ("NsJailConfig", "iface_no_lo"): ("--iface_no_lo", True),
66
+ ("NsJailConfig", "iface_own"): ("--iface_own", True),
67
+ ("NsJailConfig", "macvlan_iface"): ("--macvlan_iface", True),
68
+ ("NsJailConfig", "macvlan_vs_ip"): ("--macvlan_vs_ip", True),
69
+ ("NsJailConfig", "macvlan_vs_nm"): ("--macvlan_vs_nm", True),
70
+ ("NsJailConfig", "macvlan_vs_gw"): ("--macvlan_vs_gw", True),
71
+ ("NsJailConfig", "macvlan_vs_ma"): ("--macvlan_vs_ma", True),
72
+ ("NsJailConfig", "macvlan_vs_mo"): ("--macvlan_vs_mo", True),
73
+ }