A Python library for code analysis and sandboxing
Project description
GuardX
A Python package for code analysis and sandbox.
This library can be used to create pipelines that filter code generated by GenAI code models, and for guarding the execution of generated code.
Create a python virtual env
This is recommended.
python -m venv .venv
source .venv/bin/activate
Quick start
To install the library, choose one of the following methods:
git+https (using a github personal access token):
pip install guardx@git+https://${GITHUB_TOKEN}@https://github.com/ibm/guardx.git@${GUARDX_VERSION}
git+ssh:
pip install guardx@git+ssh://git@github.com/ibm/guardx.git@${GUARDX_VERSION}
git clone:
git clone git@github.com:ibm/guardx.git
make -C guardx init
make -C guardx install
Initialization
The library container images must be built before importing and using the library.
guardx init
Note: Depending on your system, you may need to run as sudo .venv/bin/guardx init.
Podman: GuardX uses the docker python package to communicate with containers. Hence, if you are using Podman, you will need to set the env variable DOCKER_HOST to point to the unix socket used by Podman. See docs/container.rst.
podman machine inspect --format '{{.ConnectionInfo.PodmanSocket.Path}}'
export DOCKER_HOST=unix://<your_podman_socket_location>
Test using provided example
python example.py --file example_gen_code.py
Library Usage
Here is an example of how to use this library in your code.
from guardx import Guardx
from guardx.analysis import AnalysisType
python_code = """<your code here>"""
g = Guardx(config_path="./resources/config.yaml")
# To analyze code
result = g.analyze(python_code, {AnalysisType.DETECT_SECRET, AnalysisType.UNSAFE_CODE})
print(result)
# To execute code in sandbox with a default security policy
result = g.execute(python_code).get_docker_result()
print(result)
Setting up the development environment
Install pre-requisites
git clone git@github.com:ibm/guardx.git
cd guardx
make init
Note: This installs Poetry. Make sure to configure your PATH to access poetry.
Install dependencies
To install the dev dependencies (editable mode):
make install/dev
Note: To add additional dependencies, use poetry add "package". For help, poetry add -h.
Build the library container images
make containers/docker
OR
make containers/podman
Note: Fresh build takes 5-10 minutes. Make sure to update the GuardX config file in resources/config.yaml to match built image name and tag.
Testing
Test modules are created under the tests directory.
To run all tests, use the following command:
make test
Note: To enable logging, set log_cli = true in tests/pytest.ini.
Code Linting
Before checking in any code for the project, please lint the code. This can be done using:
make lint
Precommit Hooks
We are currently using detect-secrets in our precommit hooks. Refer to this repo for configuration instructions
Docs config & build
cd docs
make html
Seccomp policy category
Set the seccomp policy category in resources/config.yaml.
Categories description below:
- memory: only allow rt_sigaction, exit_group, munmap, read stdin, write stdout, write stderr
- nonet: disallow network related syscalls
- crit_syscalls: disallow syscalls associated with known CVEs or used as launchpad to carry out attacks.
- log: logs all syscalls to auditd.log
- unconfined: no seccomp
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file guardx-0.2.0.tar.gz.
File metadata
- Download URL: guardx-0.2.0.tar.gz
- Upload date:
- Size: 25.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ef84ad9eaa643c977d29e423b4f48e341b74a73cecd9b15ed515d4f42e118aa6
|
|
| MD5 |
1008fac3d5c7b04e8087aee6e34f5f6b
|
|
| BLAKE2b-256 |
28e08b84b21dce6ef2de9c221b2b9eb52242456219ba8037ec954e1792e6699a
|
Provenance
The following attestation bundles were made for guardx-0.2.0.tar.gz:
Publisher:
publish-to-test-pypi.yml on IBM/guardx
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
guardx-0.2.0.tar.gz -
Subject digest:
ef84ad9eaa643c977d29e423b4f48e341b74a73cecd9b15ed515d4f42e118aa6 - Sigstore transparency entry: 529213478
- Sigstore integration time:
-
Permalink:
IBM/guardx@03f4fca6b979c4856f569cc521f0b5bd144a36a2 -
Branch / Tag:
refs/tags/0.2.0 - Owner: https://github.com/IBM
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-to-test-pypi.yml@03f4fca6b979c4856f569cc521f0b5bd144a36a2 -
Trigger Event:
push
-
Statement type:
File details
Details for the file guardx-0.2.0-py3-none-any.whl.
File metadata
- Download URL: guardx-0.2.0-py3-none-any.whl
- Upload date:
- Size: 31.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
728255b01a1db48b25139e1b14d2177f3c82ad54dba13a233cd37ce3191a6fcc
|
|
| MD5 |
4141c982798ac0248185abeaeeebcf91
|
|
| BLAKE2b-256 |
6c407e0459b97b4e8341fd7cf17500762fcd4e28594b49a41a8eea1b390e478d
|
Provenance
The following attestation bundles were made for guardx-0.2.0-py3-none-any.whl:
Publisher:
publish-to-test-pypi.yml on IBM/guardx
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
guardx-0.2.0-py3-none-any.whl -
Subject digest:
728255b01a1db48b25139e1b14d2177f3c82ad54dba13a233cd37ce3191a6fcc - Sigstore transparency entry: 529213484
- Sigstore integration time:
-
Permalink:
IBM/guardx@03f4fca6b979c4856f569cc521f0b5bd144a36a2 -
Branch / Tag:
refs/tags/0.2.0 - Owner: https://github.com/IBM
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-to-test-pypi.yml@03f4fca6b979c4856f569cc521f0b5bd144a36a2 -
Trigger Event:
push
-
Statement type: