blackship 0.1.4

A FreeBSD jail manager with state machine control
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
//! Jailfile instructions
//!
//! Defines the instructions that can be used in a Jailfile.

use serde::Deserialize;
use std::collections::HashMap;

/// Build argument definition
#[derive(Debug, Clone, Deserialize)]
pub struct BuildArg {
    /// Argument name
    pub name: String,
    /// Default value (optional)
    pub default: Option<String>,
}

impl BuildArg {
    /// Create a new build arg (_unused: future feature)
    #[allow(dead_code)]
    pub fn new(name: &str) -> Self {
        Self {
            name: name.to_string(),
            default: None,
        }
    }

    /// Set default value (_unused: future feature)
    #[allow(dead_code)]
    pub fn with_default(mut self, default: &str) -> Self {
        self.default = Some(default.to_string());
        self
    }
}

/// Port exposure definition
#[derive(Debug, Clone, Deserialize)]
pub struct ExposePort {
    /// Port number
    pub port: u16,
    /// Protocol (tcp/udp)
    #[serde(default = "default_protocol")]
    pub protocol: String,
}

fn default_protocol() -> String {
    "tcp".to_string()
}

impl ExposePort {
    /// Create a TCP port exposure
    pub fn tcp(port: u16) -> Self {
        Self {
            port,
            protocol: "tcp".to_string(),
        }
    }

    /// Create a UDP port exposure (_unused: future feature)
    #[allow(dead_code)]
    pub fn udp(port: u16) -> Self {
        Self {
            port,
            protocol: "udp".to_string(),
        }
    }

    /// Parse from string like "80/tcp" or "53/udp"
    pub fn parse(s: &str) -> Option<Self> {
        let parts: Vec<&str> = s.split('/').collect();
        let port = parts.first()?.parse().ok()?;
        let protocol = parts
            .get(1)
            .map(|s| s.to_string())
            .unwrap_or_else(default_protocol);
        Some(Self { port, protocol })
    }
}

/// Copy instruction source/destination
#[derive(Debug, Clone, Deserialize)]
pub struct CopySpec {
    /// Source path (relative to build context)
    pub src: String,
    /// Destination path in jail
    pub dest: String,
    /// File mode (optional)
    pub mode: Option<u32>,
    /// Owner (optional)
    pub owner: Option<String>,
}

impl CopySpec {
    /// Create a new copy spec
    pub fn new(src: &str, dest: &str) -> Self {
        Self {
            src: src.to_string(),
            dest: dest.to_string(),
            mode: None,
            owner: None,
        }
    }

    /// Set file mode (_unused: future feature)
    #[allow(dead_code)]
    pub fn with_mode(mut self, mode: u32) -> Self {
        self.mode = Some(mode);
        self
    }

    /// Set owner (_unused: future feature)
    #[allow(dead_code)]
    pub fn with_owner(mut self, owner: &str) -> Self {
        self.owner = Some(owner.to_string());
        self
    }
}

/// A single build instruction
#[derive(Debug, Clone)]
pub enum Instruction {
    /// FROM <release> - Base release to build from
    From(String),
    /// ARG <name>[=<default>] - Build argument
    Arg(BuildArg),
    /// ENV <name>=<value> - Environment variable
    Env(String, String),
    /// RUN <command> - Execute a command
    Run(String),
    /// COPY <src> <dest> - Copy files into jail
    Copy(CopySpec),
    /// WORKDIR <path> - Set working directory
    Workdir(String),
    /// EXPOSE <port>[/<protocol>] - Expose a port
    Expose(ExposePort),
    /// CMD <command> - Default command to run
    Cmd(String),
    /// ENTRYPOINT <command> - Entry point command
    Entrypoint(String),
    /// USER <user> - Set default user
    User(String),
    /// LABEL <key>=<value> - Add metadata
    Label(String, String),
    /// VOLUME <path> - Declare a volume
    Volume(String),
    /// COMMENT - A comment line (_unused: future feature)
    #[allow(dead_code)]
    Comment(String),
}

impl Instruction {
    /// Get instruction name
    pub fn name(&self) -> &'static str {
        match self {
            Instruction::From(_) => "FROM",
            Instruction::Arg(_) => "ARG",
            Instruction::Env(_, _) => "ENV",
            Instruction::Run(_) => "RUN",
            Instruction::Copy(_) => "COPY",
            Instruction::Workdir(_) => "WORKDIR",
            Instruction::Expose(_) => "EXPOSE",
            Instruction::Cmd(_) => "CMD",
            Instruction::Entrypoint(_) => "ENTRYPOINT",
            Instruction::User(_) => "USER",
            Instruction::Label(_, _) => "LABEL",
            Instruction::Volume(_) => "VOLUME",
            Instruction::Comment(_) => "#",
        }
    }
}

/// Jailfile metadata
#[derive(Debug, Clone, Default, Deserialize)]
pub struct JailfileMetadata {
    /// Template name
    pub name: Option<String>,
    /// Template version
    pub version: Option<String>,
    /// Description
    pub description: Option<String>,
    /// Author (_unused: future feature)
    #[allow(dead_code)]
    pub author: Option<String>,
    /// Labels (_unused: future feature)
    #[serde(default)]
    #[allow(dead_code)]
    pub labels: HashMap<String, String>,
}

/// A parsed Jailfile
#[derive(Debug, Clone)]
pub struct Jailfile {
    /// Metadata
    pub metadata: JailfileMetadata,
    /// Base release
    pub from: Option<String>,
    /// Build arguments
    pub args: Vec<BuildArg>,
    /// Instructions to execute
    pub instructions: Vec<Instruction>,
    /// Start command
    pub cmd: Option<String>,
    /// Entry point
    pub entrypoint: Option<String>,
    /// Working directory
    pub workdir: Option<String>,
    /// Default user
    pub user: Option<String>,
    /// Exposed ports
    pub expose: Vec<ExposePort>,
    /// Declared volumes
    pub volumes: Vec<String>,
    /// Environment variables
    pub env: HashMap<String, String>,
}

impl Default for Jailfile {
    fn default() -> Self {
        Self::new()
    }
}

impl Jailfile {
    /// Create a new empty Jailfile
    pub fn new() -> Self {
        Self {
            metadata: JailfileMetadata::default(),
            from: None,
            args: Vec::new(),
            instructions: Vec::new(),
            cmd: None,
            entrypoint: None,
            workdir: None,
            user: None,
            expose: Vec::new(),
            volumes: Vec::new(),
            env: HashMap::new(),
        }
    }

    /// Create a Jailfile with a base release
    #[allow(dead_code)] // Public API for programmatic Jailfile creation
    pub fn from_release(release: &str) -> Self {
        let mut jf = Self::new();
        jf.from = Some(release.to_string());
        jf.instructions.push(Instruction::From(release.to_string()));
        jf
    }

    /// Add a build argument
    #[allow(dead_code)] // Public API for programmatic Jailfile creation
    pub fn arg(mut self, name: &str, default: Option<&str>) -> Self {
        let arg = BuildArg {
            name: name.to_string(),
            default: default.map(String::from),
        };
        self.args.push(arg.clone());
        self.instructions.push(Instruction::Arg(arg));
        self
    }

    /// Add an environment variable
    #[allow(dead_code)] // Public API for programmatic Jailfile creation
    pub fn env(mut self, name: &str, value: &str) -> Self {
        self.env.insert(name.to_string(), value.to_string());
        self.instructions
            .push(Instruction::Env(name.to_string(), value.to_string()));
        self
    }

    /// Add a RUN instruction
    #[allow(dead_code)] // Public API for programmatic Jailfile creation
    pub fn run(mut self, command: &str) -> Self {
        self.instructions
            .push(Instruction::Run(command.to_string()));
        self
    }

    /// Add a COPY instruction
    #[allow(dead_code)] // Public API for programmatic Jailfile creation
    pub fn copy(mut self, src: &str, dest: &str) -> Self {
        let spec = CopySpec::new(src, dest);
        self.instructions.push(Instruction::Copy(spec));
        self
    }

    /// Set working directory
    #[allow(dead_code)] // Public API for programmatic Jailfile creation
    pub fn workdir(mut self, path: &str) -> Self {
        self.workdir = Some(path.to_string());
        self.instructions
            .push(Instruction::Workdir(path.to_string()));
        self
    }

    /// Expose a port
    #[allow(dead_code)] // Public API for programmatic Jailfile creation
    pub fn expose(mut self, port: u16, protocol: &str) -> Self {
        let exp = ExposePort {
            port,
            protocol: protocol.to_string(),
        };
        self.expose.push(exp.clone());
        self.instructions.push(Instruction::Expose(exp));
        self
    }

    /// Set the CMD
    #[allow(dead_code)] // Public API for programmatic Jailfile creation
    pub fn cmd(mut self, command: &str) -> Self {
        self.cmd = Some(command.to_string());
        self.instructions
            .push(Instruction::Cmd(command.to_string()));
        self
    }

    /// Set the entrypoint
    #[allow(dead_code)] // Public API for programmatic Jailfile creation
    pub fn entrypoint(mut self, command: &str) -> Self {
        self.entrypoint = Some(command.to_string());
        self.instructions
            .push(Instruction::Entrypoint(command.to_string()));
        self
    }

    /// Set the user
    #[allow(dead_code)] // Public API for programmatic Jailfile creation
    pub fn user(mut self, user: &str) -> Self {
        self.user = Some(user.to_string());
        self.instructions.push(Instruction::User(user.to_string()));
        self
    }

    /// Get the base release
    #[allow(dead_code)] // Public API for Jailfile inspection
    pub fn base_release(&self) -> Option<&str> {
        self.from.as_deref()
    }

    /// Get all RUN commands
    #[allow(dead_code)] // Public API for Jailfile inspection
    pub fn run_commands(&self) -> Vec<&str> {
        self.instructions
            .iter()
            .filter_map(|i| match i {
                Instruction::Run(cmd) => Some(cmd.as_str()),
                _ => None,
            })
            .collect()
    }

    /// Get all COPY specs
    #[allow(dead_code)] // Public API for Jailfile inspection
    pub fn copy_specs(&self) -> Vec<&CopySpec> {
        self.instructions
            .iter()
            .filter_map(|i| match i {
                Instruction::Copy(spec) => Some(spec),
                _ => None,
            })
            .collect()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_build_arg() {
        let arg = BuildArg::new("VERSION").with_default("1.0");
        assert_eq!(arg.name, "VERSION");
        assert_eq!(arg.default, Some("1.0".to_string()));
    }

    #[test]
    fn test_expose_port_parse() {
        let tcp = ExposePort::parse("80/tcp").unwrap();
        assert_eq!(tcp.port, 80);
        assert_eq!(tcp.protocol, "tcp");

        let udp = ExposePort::parse("53/udp").unwrap();
        assert_eq!(udp.port, 53);
        assert_eq!(udp.protocol, "udp");

        let default = ExposePort::parse("443").unwrap();
        assert_eq!(default.port, 443);
        assert_eq!(default.protocol, "tcp");
    }

    #[test]
    fn test_jailfile_builder() {
        let jf = Jailfile::from_release("14.2-RELEASE")
            .arg("VERSION", Some("1.0"))
            .env("PATH", "/usr/local/bin:/usr/bin")
            .run("pkg install -y nginx")
            .copy("nginx.conf", "/usr/local/etc/nginx/nginx.conf")
            .workdir("/usr/local")
            .expose(80, "tcp")
            .cmd("/usr/sbin/service nginx start");

        assert_eq!(jf.from, Some("14.2-RELEASE".to_string()));
        assert_eq!(jf.args.len(), 1);
        assert_eq!(jf.run_commands().len(), 1);
        assert_eq!(jf.copy_specs().len(), 1);
        assert_eq!(jf.expose.len(), 1);
    }

    #[test]
    fn test_instruction_names() {
        assert_eq!(Instruction::From("test".to_string()).name(), "FROM");
        assert_eq!(Instruction::Run("test".to_string()).name(), "RUN");
        assert_eq!(Instruction::Copy(CopySpec::new("a", "b")).name(), "COPY");
    }
}