bssh 2.0.1

Parallel SSH command execution tool for cluster management
Documentation
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
// Copyright 2025 Lablup Inc. and Jeongkyu Shin
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! File transfer filtering infrastructure.
//!
//! This module provides a policy-based system for controlling file transfers
//! in SFTP and SCP operations. It allows administrators to:
//!
//! - Allow or deny file transfers based on path patterns
//! - Log specific file operations for auditing
//! - Apply different rules per user or operation type
//!
//! # Architecture
//!
//! The filtering system is built around three main concepts:
//!
//! 1. **Operations** - Types of file operations (upload, download, delete, etc.)
//! 2. **Matchers** - Pattern matching against file paths (glob, prefix, regex)
//! 3. **Policies** - Ordered sets of rules that determine allow/deny/log actions
//!
//! # Example
//!
//! ```rust
//! use bssh::server::filter::{FilterPolicy, FilterResult, Operation};
//! use bssh::server::filter::pattern::GlobMatcher;
//! use bssh::server::filter::policy::FilterRule;
//! use std::path::Path;
//!
//! // Create a policy that blocks *.key files
//! let policy = FilterPolicy::new()
//!     .with_default(FilterResult::Allow)
//!     .add_rule(FilterRule {
//!         name: Some("block-keys".to_string()),
//!         matcher: Box::new(GlobMatcher::new("*.key").unwrap()),
//!         action: FilterResult::Deny,
//!         operations: None,
//!         users: None,
//!     });
//!
//! // Check if operation is allowed
//! let result = policy.check(Path::new("/etc/secret.key"), Operation::Download, "alice");
//! assert_eq!(result, FilterResult::Deny);
//! ```

pub mod path;
pub mod pattern;
pub mod policy;

use std::fmt;
use std::path::Path;

pub use self::path::{
    normalize_path, ComponentMatcher, ExactMatcher, ExtensionMatcher, MultiExtensionMatcher,
    PrefixMatcher, SizeMatcher,
};
pub use self::pattern::{
    AllMatcher, CombinedMatcher, CompositeMatcher, GlobMatcher, NotMatcher, RegexMatcher,
};
pub use self::policy::{FilterPolicy, FilterRule, Matcher, SharedFilterPolicy};

/// File transfer operation type.
///
/// Represents the type of file operation being performed. Used by filter rules
/// to apply different policies for different operation types.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Operation {
    /// Upload a file to the server
    Upload,
    /// Download a file from the server
    Download,
    /// Delete a file
    Delete,
    /// Rename or move a file
    Rename,
    /// Create a directory
    CreateDir,
    /// List directory contents
    ListDir,
    /// Read file attributes
    Stat,
    /// Modify file attributes
    SetStat,
    /// Create a symbolic link
    Symlink,
    /// Read a symbolic link target
    ReadLink,
}

impl Operation {
    /// Returns all available operations.
    pub fn all() -> &'static [Operation] {
        &[
            Operation::Upload,
            Operation::Download,
            Operation::Delete,
            Operation::Rename,
            Operation::CreateDir,
            Operation::ListDir,
            Operation::Stat,
            Operation::SetStat,
            Operation::Symlink,
            Operation::ReadLink,
        ]
    }
}

impl fmt::Display for Operation {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Operation::Upload => write!(f, "upload"),
            Operation::Download => write!(f, "download"),
            Operation::Delete => write!(f, "delete"),
            Operation::Rename => write!(f, "rename"),
            Operation::CreateDir => write!(f, "createdir"),
            Operation::ListDir => write!(f, "listdir"),
            Operation::Stat => write!(f, "stat"),
            Operation::SetStat => write!(f, "setstat"),
            Operation::Symlink => write!(f, "symlink"),
            Operation::ReadLink => write!(f, "readlink"),
        }
    }
}

impl std::str::FromStr for Operation {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "upload" => Ok(Operation::Upload),
            "download" => Ok(Operation::Download),
            "delete" => Ok(Operation::Delete),
            "rename" => Ok(Operation::Rename),
            "createdir" | "mkdir" => Ok(Operation::CreateDir),
            "listdir" | "readdir" => Ok(Operation::ListDir),
            "stat" => Ok(Operation::Stat),
            "setstat" => Ok(Operation::SetStat),
            "symlink" => Ok(Operation::Symlink),
            "readlink" => Ok(Operation::ReadLink),
            _ => Err(format!("unknown operation: {}", s)),
        }
    }
}

/// Result of filter check.
///
/// Determines what action should be taken for a file operation.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum FilterResult {
    /// Allow the operation to proceed
    #[default]
    Allow,
    /// Deny the operation
    Deny,
    /// Allow the operation but log it
    Log,
}

impl fmt::Display for FilterResult {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            FilterResult::Allow => write!(f, "allow"),
            FilterResult::Deny => write!(f, "deny"),
            FilterResult::Log => write!(f, "log"),
        }
    }
}

/// Trait for file transfer filters.
///
/// Implement this trait to create custom file transfer filtering logic.
/// The default implementation provides basic path and operation filtering.
pub trait TransferFilter: Send + Sync {
    /// Check if an operation is allowed on a given path.
    ///
    /// # Arguments
    ///
    /// * `path` - The file path being operated on
    /// * `operation` - The type of operation
    /// * `user` - The username performing the operation
    ///
    /// # Returns
    ///
    /// A `FilterResult` indicating whether to allow, deny, or log the operation.
    fn check(&self, path: &Path, operation: Operation, user: &str) -> FilterResult;

    /// Check if an operation involving source and destination paths is allowed.
    ///
    /// Used for rename, copy, and symlink operations that involve two paths.
    /// The default implementation checks both paths and returns the most restrictive result.
    ///
    /// # Arguments
    ///
    /// * `src` - The source file path
    /// * `dest` - The destination file path
    /// * `operation` - The type of operation
    /// * `user` - The username performing the operation
    ///
    /// # Returns
    ///
    /// A `FilterResult` indicating whether to allow, deny, or log the operation.
    fn check_with_dest(
        &self,
        src: &Path,
        dest: &Path,
        operation: Operation,
        user: &str,
    ) -> FilterResult {
        let src_result = self.check(src, operation, user);
        let dest_result = self.check(dest, operation, user);

        // Return most restrictive result
        match (src_result, dest_result) {
            (FilterResult::Deny, _) | (_, FilterResult::Deny) => FilterResult::Deny,
            (FilterResult::Log, _) | (_, FilterResult::Log) => FilterResult::Log,
            _ => FilterResult::Allow,
        }
    }

    /// Returns true if filtering is enabled.
    fn is_enabled(&self) -> bool {
        true
    }
}

/// A no-op filter that allows all operations.
///
/// Used when filtering is disabled or not configured.
#[derive(Debug, Clone, Default)]
pub struct NoOpFilter;

impl TransferFilter for NoOpFilter {
    fn check(&self, _path: &Path, _operation: Operation, _user: &str) -> FilterResult {
        FilterResult::Allow
    }

    fn is_enabled(&self) -> bool {
        false
    }
}

/// Trait for size-aware file transfer filters.
///
/// This extends the basic `TransferFilter` trait to include file size
/// in the filtering decision. Use this when you need to filter based
/// on file size (e.g., block uploads larger than 100MB).
///
/// # Example
///
/// ```rust
/// use bssh::server::filter::{FilterResult, Operation, SizeAwareFilter, TransferFilter};
/// use bssh::server::filter::path::SizeMatcher;
/// use std::path::Path;
///
/// struct MaxUploadSizeFilter {
///     max_bytes: u64,
/// }
///
/// impl TransferFilter for MaxUploadSizeFilter {
///     fn check(&self, _path: &Path, _operation: Operation, _user: &str) -> FilterResult {
///         // Without size info, we allow by default
///         FilterResult::Allow
///     }
/// }
///
/// impl SizeAwareFilter for MaxUploadSizeFilter {
///     fn check_with_size(
///         &self,
///         _path: &Path,
///         size: u64,
///         operation: Operation,
///         _user: &str,
///     ) -> FilterResult {
///         if operation == Operation::Upload && size > self.max_bytes {
///             FilterResult::Deny
///         } else {
///             FilterResult::Allow
///         }
///     }
/// }
/// ```
pub trait SizeAwareFilter: TransferFilter {
    /// Check if an operation is allowed, taking file size into account.
    ///
    /// # Arguments
    ///
    /// * `path` - The file path being operated on
    /// * `size` - The file size in bytes
    /// * `operation` - The type of operation
    /// * `user` - The username performing the operation
    ///
    /// # Returns
    ///
    /// A `FilterResult` indicating whether to allow, deny, or log the operation.
    fn check_with_size(
        &self,
        path: &Path,
        size: u64,
        operation: Operation,
        user: &str,
    ) -> FilterResult;

    /// Check a two-path operation with size information.
    ///
    /// Used for rename/copy operations where both source and destination
    /// are considered.
    fn check_with_size_dest(
        &self,
        src: &Path,
        src_size: u64,
        dest: &Path,
        operation: Operation,
        user: &str,
    ) -> FilterResult {
        let src_result = self.check_with_size(src, src_size, operation, user);
        let dest_result = self.check(dest, operation, user);

        match (src_result, dest_result) {
            (FilterResult::Deny, _) | (_, FilterResult::Deny) => FilterResult::Deny,
            (FilterResult::Log, _) | (_, FilterResult::Log) => FilterResult::Log,
            _ => FilterResult::Allow,
        }
    }
}

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

    #[test]
    fn test_operation_display() {
        assert_eq!(Operation::Upload.to_string(), "upload");
        assert_eq!(Operation::Download.to_string(), "download");
        assert_eq!(Operation::Delete.to_string(), "delete");
        assert_eq!(Operation::Rename.to_string(), "rename");
        assert_eq!(Operation::CreateDir.to_string(), "createdir");
        assert_eq!(Operation::ListDir.to_string(), "listdir");
    }

    #[test]
    fn test_operation_parse() {
        assert_eq!("upload".parse::<Operation>().unwrap(), Operation::Upload);
        assert_eq!(
            "DOWNLOAD".parse::<Operation>().unwrap(),
            Operation::Download
        );
        assert_eq!("mkdir".parse::<Operation>().unwrap(), Operation::CreateDir);
        assert_eq!("readdir".parse::<Operation>().unwrap(), Operation::ListDir);
        assert!("invalid".parse::<Operation>().is_err());
    }

    #[test]
    fn test_filter_result_default() {
        assert_eq!(FilterResult::default(), FilterResult::Allow);
    }

    #[test]
    fn test_filter_result_display() {
        assert_eq!(FilterResult::Allow.to_string(), "allow");
        assert_eq!(FilterResult::Deny.to_string(), "deny");
        assert_eq!(FilterResult::Log.to_string(), "log");
    }

    #[test]
    fn test_noop_filter() {
        let filter = NoOpFilter;
        assert!(!filter.is_enabled());
        assert_eq!(
            filter.check(Path::new("/any/path"), Operation::Upload, "user"),
            FilterResult::Allow
        );
    }

    #[test]
    fn test_check_with_dest_deny_takes_precedence() {
        struct DenyDownload;
        impl TransferFilter for DenyDownload {
            fn check(&self, path: &Path, _operation: Operation, _user: &str) -> FilterResult {
                if path.to_string_lossy().contains("secret") {
                    FilterResult::Deny
                } else {
                    FilterResult::Allow
                }
            }
        }

        let filter = DenyDownload;

        // Both paths allowed
        assert_eq!(
            filter.check_with_dest(
                Path::new("/safe/src"),
                Path::new("/safe/dest"),
                Operation::Rename,
                "user"
            ),
            FilterResult::Allow
        );

        // Source path denied
        assert_eq!(
            filter.check_with_dest(
                Path::new("/secret/src"),
                Path::new("/safe/dest"),
                Operation::Rename,
                "user"
            ),
            FilterResult::Deny
        );

        // Destination path denied
        assert_eq!(
            filter.check_with_dest(
                Path::new("/safe/src"),
                Path::new("/secret/dest"),
                Operation::Rename,
                "user"
            ),
            FilterResult::Deny
        );
    }

    #[test]
    fn test_check_with_dest_log_priority() {
        struct LogSensitive;
        impl TransferFilter for LogSensitive {
            fn check(&self, path: &Path, _operation: Operation, _user: &str) -> FilterResult {
                if path.to_string_lossy().contains("sensitive") {
                    FilterResult::Log
                } else {
                    FilterResult::Allow
                }
            }
        }

        let filter = LogSensitive;

        // Source is sensitive, should log
        assert_eq!(
            filter.check_with_dest(
                Path::new("/sensitive/src"),
                Path::new("/safe/dest"),
                Operation::Rename,
                "user"
            ),
            FilterResult::Log
        );

        // Destination is sensitive, should log
        assert_eq!(
            filter.check_with_dest(
                Path::new("/safe/src"),
                Path::new("/sensitive/dest"),
                Operation::Rename,
                "user"
            ),
            FilterResult::Log
        );
    }

    #[test]
    fn test_operation_all() {
        let all_ops = Operation::all();

        // Should contain all 10 operations
        assert_eq!(all_ops.len(), 10);

        // Verify all operations are included
        assert!(all_ops.contains(&Operation::Upload));
        assert!(all_ops.contains(&Operation::Download));
        assert!(all_ops.contains(&Operation::Delete));
        assert!(all_ops.contains(&Operation::Rename));
        assert!(all_ops.contains(&Operation::CreateDir));
        assert!(all_ops.contains(&Operation::ListDir));
        assert!(all_ops.contains(&Operation::Stat));
        assert!(all_ops.contains(&Operation::SetStat));
        assert!(all_ops.contains(&Operation::Symlink));
        assert!(all_ops.contains(&Operation::ReadLink));
    }

    #[test]
    fn test_operation_display_all() {
        // Test all operations have a string representation
        assert_eq!(Operation::Stat.to_string(), "stat");
        assert_eq!(Operation::SetStat.to_string(), "setstat");
        assert_eq!(Operation::Symlink.to_string(), "symlink");
        assert_eq!(Operation::ReadLink.to_string(), "readlink");
    }

    #[test]
    fn test_operation_parse_all_variants() {
        // Test parsing all valid variants
        assert_eq!("stat".parse::<Operation>().unwrap(), Operation::Stat);
        assert_eq!("setstat".parse::<Operation>().unwrap(), Operation::SetStat);
        assert_eq!("symlink".parse::<Operation>().unwrap(), Operation::Symlink);
        assert_eq!(
            "readlink".parse::<Operation>().unwrap(),
            Operation::ReadLink
        );

        // Test case insensitivity
        assert_eq!("STAT".parse::<Operation>().unwrap(), Operation::Stat);
        assert_eq!("SetStat".parse::<Operation>().unwrap(), Operation::SetStat);
    }

    #[test]
    fn test_noop_filter_default() {
        let filter = NoOpFilter;
        assert!(!filter.is_enabled());
    }

    #[test]
    fn test_noop_filter_clone() {
        let filter = NoOpFilter;
        let cloned = filter.clone();
        assert!(!cloned.is_enabled());
    }
}