|
| 1 | +// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +mod common; |
| 5 | + |
| 6 | +#[test] |
| 7 | +fn policy_lint_runs_offline_and_reports_file_errors() { |
| 8 | + let dir = tempfile::tempdir().unwrap(); |
| 9 | + let file = dir.path().join("policy.yaml"); |
| 10 | + let path = file.to_str().unwrap(); |
| 11 | + let cases = [ |
| 12 | + ( |
| 13 | + "version: 1\n", |
| 14 | + true, |
| 15 | + vec!["Policy is valid (offline)", "Gateway checks"], |
| 16 | + ), |
| 17 | + ("process: {}\n", false, vec!["version"]), |
| 18 | + ("version: 1\nunknown: true\n", false, vec!["unknown"]), |
| 19 | + ( |
| 20 | + "version: 1\nprocess:\n run_as_user: '0'\n run_as_group: root\nfilesystem_policy:\n read_write: [relative, /]\n", |
| 21 | + false, |
| 22 | + vec![ |
| 23 | + "run_as_user", |
| 24 | + "run_as_group", |
| 25 | + "path must be absolute", |
| 26 | + "overly broad", |
| 27 | + ], |
| 28 | + ), |
| 29 | + ( |
| 30 | + "version: 1\nnetwork_policies:\n _provider_private:\n endpoints: []\n", |
| 31 | + false, |
| 32 | + vec!["reserved '_provider_'", "--base"], |
| 33 | + ), |
| 34 | + ( |
| 35 | + "version: 1\nnetwork_policies:\n bound:\n endpoints:\n - host: example.com\n port: 443\n credential_binding: {provider: ' '}\n", |
| 36 | + false, |
| 37 | + vec!["credential_binding.provider"], |
| 38 | + ), |
| 39 | + ( |
| 40 | + "version: 1\nprocess:\n run_as_user: '0'\nnetwork_middlewares:\n redact:\n middleware: openshell/regex\n order: 1\n endpoints: {include: [example.com]}\n config: {mode: allow}\n", |
| 41 | + false, |
| 42 | + vec!["mode: redact", "run_as_user"], |
| 43 | + ), |
| 44 | + ( |
| 45 | + "version: 1\nnetwork_policies:\n conflict:\n endpoints:\n - {host: example.com, port: 443}\n - {host: example.com, port: 443, tls: skip}\n", |
| 46 | + false, |
| 47 | + vec!["overlap", "conflicting"], |
| 48 | + ), |
| 49 | + ]; |
| 50 | + for (yaml, valid, messages) in cases { |
| 51 | + std::fs::write(&file, yaml).unwrap(); |
| 52 | + for args in [ |
| 53 | + vec!["policy", "lint", path], |
| 54 | + vec![ |
| 55 | + "--gateway-endpoint", |
| 56 | + "http://127.0.0.1:1", |
| 57 | + "policy", |
| 58 | + "lint", |
| 59 | + path, |
| 60 | + ], |
| 61 | + ] { |
| 62 | + let output = common::run_isolated(&args); |
| 63 | + assert_eq!(output.code == 0, valid, "{yaml}\n{}", output.combined); |
| 64 | + assert!( |
| 65 | + !output.combined.contains("failed to connect"), |
| 66 | + "{}", |
| 67 | + output.combined |
| 68 | + ); |
| 69 | + for message in &messages { |
| 70 | + assert!( |
| 71 | + output.combined.contains(message), |
| 72 | + "expected {message}: {}", |
| 73 | + output.combined |
| 74 | + ); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + std::fs::write( |
| 79 | + &file, |
| 80 | + format!( |
| 81 | + "version: 1\nnetwork_policies:\n large:\n name: {}\n", |
| 82 | + "x".repeat(openshell_policy::MAX_AUTHORED_POLICY_SIZE) |
| 83 | + ), |
| 84 | + ) |
| 85 | + .unwrap(); |
| 86 | + let oversized = common::run_isolated(&["policy", "lint", path]); |
| 87 | + assert_ne!(oversized.code, 0); |
| 88 | + assert!( |
| 89 | + oversized |
| 90 | + .combined |
| 91 | + .contains("policy serialized size exceeds maximum"), |
| 92 | + "{}", |
| 93 | + oversized.combined |
| 94 | + ); |
| 95 | + let missing = dir.path().join("missing.yaml"); |
| 96 | + assert_ne!( |
| 97 | + common::run_isolated(&["policy", "lint", missing.to_str().unwrap()]).code, |
| 98 | + 0 |
| 99 | + ); |
| 100 | +} |
0 commit comments