Skip to content

Commit 3e3d5fe

Browse files
alindimaandreeaflorescu
authored andcommitted
add json compile-time feature
Signed-off-by: alindima <[email protected]>
1 parent ef0fdeb commit 3e3d5fe

File tree

8 files changed

+66
-18
lines changed

8 files changed

+66
-18
lines changed

.buildkite/custom-tests.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
{
22
"tests": [
3+
{
4+
"test_name": "build-gnu-json",
5+
"command": "RUSTFLAGS=\"-D warnings\" cargo build --release --features=json",
6+
"platform": [
7+
"x86_64",
8+
"aarch64"
9+
]
10+
},
11+
{
12+
"test_name": "build-musl-json",
13+
"command": "RUSTFLAGS=\"-D warnings\" cargo build --release --features=json --target {target_platform}-unknown-linux-musl",
14+
"platform": [
15+
"x86_64",
16+
"aarch64"
17+
]
18+
},
319
{
420
"test_name": "validate-syscall-tables",
521
"command": "tools/generate_syscall_tables.sh --test",

Cargo.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ keywords = ["seccomp", "jail", "sandbox"]
99
license = "Apache-2.0 OR BSD-3-Clause"
1010
edition = "2018"
1111

12+
[features]
13+
json = ["serde", "serde_json"]
14+
1215
[dependencies]
1316
libc = ">=0.2.39"
14-
serde = { version = ">=1.0.27", features = ["derive"] }
15-
serde_json = ">=1.0.9"
17+
serde = { version = ">=1.0.27", features = ["derive"], optional = true}
18+
serde_json = {version = ">=1.0.9", optional = true}

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,9 @@ categories to BPF programs.
259259
pub type BpfMap = HashMap<String, BpfProgram>;
260260
```
261261

262+
Note that, in order to use the JSON functionality, you need to add the `json`
263+
feature when importing the library.
264+
262265
For **Rust filters**, it’s enough to perform a `try_into()` cast, from a
263266
`SeccompFilter` to a `BpfProgram`:
264267

coverage_config_aarch64.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"coverage_score": 0,
3-
"exclude_path": "tests/integration_tests.rs",
4-
"crate_features": ""
3+
"exclude_path": "tests/integration_tests.rs,tests/json.rs",
4+
"crate_features": "json"
55
}

coverage_config_x86_64.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"coverage_score": 87.3,
3-
"exclude_path": "tests/integration_tests.rs",
4-
"crate_features": ""
2+
"coverage_score": 93.3,
3+
"exclude_path": "tests/integration_tests.rs,tests/json.rs",
4+
"crate_features": "json"
55
}

src/backend/mod.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ pub use condition::SeccompCondition;
1313
pub use filter::SeccompFilter;
1414
pub use rule::SeccompRule;
1515

16-
use core::fmt::Formatter;
16+
#[cfg(feature = "json")]
1717
use serde::Deserialize;
18+
19+
use core::fmt::Formatter;
1820
use std::convert::TryFrom;
1921
use std::fmt::Display;
2022

@@ -102,8 +104,12 @@ impl TryFrom<&str> for TargetArch {
102104
}
103105

104106
/// Comparison to perform when matching a condition.
105-
#[derive(Clone, Debug, PartialEq, Deserialize)]
106-
#[serde(rename_all = "snake_case")]
107+
#[cfg_attr(
108+
feature = "json",
109+
derive(Deserialize),
110+
serde(rename_all = "snake_case")
111+
)]
112+
#[derive(Clone, Debug, PartialEq)]
107113
pub enum SeccompCmpOp {
108114
/// Argument value is equal to the specified value.
109115
Eq,
@@ -122,8 +128,8 @@ pub enum SeccompCmpOp {
122128
}
123129

124130
/// Seccomp argument value length.
125-
#[derive(Clone, Debug, PartialEq, Deserialize)]
126-
#[serde(rename_all = "lowercase")]
131+
#[cfg_attr(feature = "json", derive(Deserialize), serde(rename_all = "lowercase"))]
132+
#[derive(Clone, Debug, PartialEq)]
127133
pub enum SeccompCmpArgLen {
128134
/// Argument value length is 4 bytes.
129135
Dword,
@@ -132,8 +138,12 @@ pub enum SeccompCmpArgLen {
132138
}
133139

134140
/// Actions that a seccomp filter can return for a syscall.
135-
#[derive(Clone, Debug, PartialEq, Deserialize)]
136-
#[serde(rename_all = "snake_case")]
141+
#[cfg_attr(
142+
feature = "json",
143+
derive(Deserialize),
144+
serde(rename_all = "snake_case")
145+
)]
146+
#[derive(Clone, Debug, PartialEq)]
137147
pub enum SeccompAction {
138148
/// Allows syscall.
139149
Allow,

src/lib.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,13 @@
108108
//! ```
109109
//!
110110
//!
111-
//! This second example defines and installs an equivalent JSON filter:
111+
//! This second example defines and installs an equivalent JSON filter (uses the `json` feature):
112112
//!
113113
//! ```
114-
//! use std::convert::TryInto;
114+
//! # #[cfg(feature = "json")]
115+
//! # {
115116
//! use seccompiler::BpfMap;
117+
//! use std::convert::TryInto;
116118
//!
117119
//! let json_input = r#"{
118120
//! "main_thread": {
@@ -164,6 +166,8 @@
164166
//! let filter = filter_map.get("main_thread").unwrap();
165167
//!
166168
//! seccompiler::apply_filter(&filter).unwrap();
169+
//!
170+
//! # }
167171
//! ```
168172
//!
169173
//! [`SeccompFilter`]: struct.SeccompFilter.html
@@ -173,14 +177,21 @@
173177
//!
174178
175179
mod backend;
180+
#[cfg(feature = "json")]
176181
mod frontend;
182+
#[cfg(feature = "json")]
177183
mod syscall_table;
178184

179-
use std::collections::HashMap;
185+
#[cfg(feature = "json")]
180186
use std::convert::TryInto;
187+
#[cfg(feature = "json")]
188+
use std::io::Read;
189+
190+
use std::collections::HashMap;
181191
use std::fmt::{Display, Formatter};
182-
use std::io::{self, Read};
192+
use std::io;
183193

194+
#[cfg(feature = "json")]
184195
use frontend::json::{Error as JsonFrontendError, JsonCompiler};
185196

186197
// Re-export the IR public types.
@@ -213,6 +224,7 @@ pub enum Error {
213224
/// System error related to calling `prctl`.
214225
Prctl(io::Error),
215226
/// Json Frontend Error.
227+
#[cfg(feature = "json")]
216228
JsonFrontend(JsonFrontendError),
217229
}
218230

@@ -230,6 +242,7 @@ impl Display for Error {
230242
Prctl(errno) => {
231243
write!(f, "Error calling `prctl`: {}", errno)
232244
}
245+
#[cfg(feature = "json")]
233246
JsonFrontend(error) => {
234247
write!(f, "Json Frontend error: {}", error)
235248
}
@@ -287,6 +300,7 @@ pub fn apply_filter(bpf_filter: BpfProgramRef) -> Result<()> {
287300
/// * `arch` - target architecture of the filter.
288301
///
289302
/// [`BpfProgram`]: type.BpfProgram.html
303+
#[cfg(feature = "json")]
290304
pub fn compile_from_json<R: Read>(reader: R, arch: TargetArch) -> Result<BpfMap> {
291305
// Run the frontend.
292306
let seccomp_filters: HashMap<String, SeccompFilter> = JsonCompiler::new(arch)

tests/json.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![cfg(feature = "json")]
2+
13
use seccompiler::{apply_filter, compile_from_json, BpfProgram};
24
use std::convert::TryInto;
35
use std::env::consts::ARCH;

0 commit comments

Comments
 (0)