-
Notifications
You must be signed in to change notification settings - Fork 10.8k
execpolicycheck command in codex cli #7012
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
e402f80
execpolicycheck command in codex cli
zhao-oai 56cea41
delete extraneous test
zhao-oai d96310c
remove unused import
zhao-oai 11a7a06
remove bad.codexpolicy
zhao-oai bfd8cee
being explicit with letter used by short
zhao-oai 75a5373
moving printing inside run()
zhao-oai 761cd1a
debug subcommand refactor
zhao-oai 1b1fd2a
update README
zhao-oai ea45f4a
codex execpolicy check
zhao-oai 19034ff
update readme
zhao-oai 74a44a8
fmt readme
zhao-oai aa69153
integration test for execpolicy
zhao-oai cb6c1c8
undo unecessary diff
zhao-oai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| use std::fs; | ||
|
|
||
| use assert_cmd::Command; | ||
| use pretty_assertions::assert_eq; | ||
| use serde_json::json; | ||
| use tempfile::TempDir; | ||
|
|
||
| #[test] | ||
| fn execpolicy_check_matches_expected_json() -> Result<(), Box<dyn std::error::Error>> { | ||
| let codex_home = TempDir::new()?; | ||
| let policy_path = codex_home.path().join("policy.codexpolicy"); | ||
| fs::write( | ||
| &policy_path, | ||
| r#" | ||
| prefix_rule( | ||
| pattern = ["git", "push"], | ||
| decision = "forbidden", | ||
| ) | ||
| "#, | ||
| )?; | ||
|
|
||
| let output = Command::cargo_bin("codex")? | ||
| .env("CODEX_HOME", codex_home.path()) | ||
| .args([ | ||
| "execpolicy", | ||
| "check", | ||
| "--policy", | ||
| policy_path | ||
| .to_str() | ||
| .expect("policy path should be valid UTF-8"), | ||
| "git", | ||
| "push", | ||
| "origin", | ||
| "main", | ||
| ]) | ||
| .output()?; | ||
|
|
||
| assert!(output.status.success()); | ||
| let result: serde_json::Value = serde_json::from_slice(&output.stdout)?; | ||
| assert_eq!( | ||
| result, | ||
| json!({ | ||
| "match": { | ||
| "decision": "forbidden", | ||
| "matchedRules": [ | ||
| { | ||
| "prefixRuleMatch": { | ||
| "matchedPrefix": ["git", "push"], | ||
| "decision": "forbidden" | ||
| } | ||
| } | ||
| ] | ||
| } | ||
| }) | ||
| ); | ||
|
|
||
| Ok(()) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| use std::fs; | ||
| use std::path::PathBuf; | ||
|
|
||
| use anyhow::Context; | ||
| use anyhow::Result; | ||
| use clap::Parser; | ||
|
|
||
| use crate::Evaluation; | ||
| use crate::Policy; | ||
| use crate::PolicyParser; | ||
|
|
||
| /// Arguments for evaluating a command against one or more execpolicy files. | ||
| #[derive(Debug, Parser, Clone)] | ||
| pub struct ExecPolicyCheckCommand { | ||
| /// Paths to execpolicy files to evaluate (repeatable). | ||
| #[arg(short = 'p', long = "policy", value_name = "PATH", required = true)] | ||
| pub policies: Vec<PathBuf>, | ||
|
|
||
| /// Pretty-print the JSON output. | ||
| #[arg(long)] | ||
| pub pretty: bool, | ||
|
|
||
| /// Command tokens to check against the policy. | ||
| #[arg( | ||
| value_name = "COMMAND", | ||
| required = true, | ||
| trailing_var_arg = true, | ||
| allow_hyphen_values = true | ||
| )] | ||
| pub command: Vec<String>, | ||
| } | ||
|
|
||
| impl ExecPolicyCheckCommand { | ||
| /// Load the policies for this command, evaluate the command, and render JSON output. | ||
| pub fn run(&self) -> Result<()> { | ||
| let policy = load_policies(&self.policies)?; | ||
| let evaluation = policy.check(&self.command); | ||
|
|
||
| let json = format_evaluation_json(&evaluation, self.pretty)?; | ||
| println!("{json}"); | ||
|
|
||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| pub fn format_evaluation_json(evaluation: &Evaluation, pretty: bool) -> Result<String> { | ||
| if pretty { | ||
| serde_json::to_string_pretty(evaluation).map_err(Into::into) | ||
| } else { | ||
| serde_json::to_string(evaluation).map_err(Into::into) | ||
| } | ||
| } | ||
|
|
||
| pub fn load_policies(policy_paths: &[PathBuf]) -> Result<Policy> { | ||
| let mut parser = PolicyParser::new(); | ||
|
|
||
| for policy_path in policy_paths { | ||
| let policy_file_contents = fs::read_to_string(policy_path) | ||
| .with_context(|| format!("failed to read policy at {}", policy_path.display()))?; | ||
| let policy_identifier = policy_path.to_string_lossy().to_string(); | ||
| parser | ||
| .parse(&policy_identifier, &policy_file_contents) | ||
| .with_context(|| format!("failed to parse policy at {}", policy_path.display()))?; | ||
| } | ||
|
|
||
| Ok(parser.build()) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,66 +1,22 @@ | ||
| use std::fs; | ||
| use std::path::PathBuf; | ||
|
|
||
| use anyhow::Context; | ||
| use anyhow::Result; | ||
| use clap::Parser; | ||
| use codex_execpolicy::PolicyParser; | ||
| use codex_execpolicy::ExecPolicyCheckCommand; | ||
|
|
||
| /// CLI for evaluating exec policies | ||
| #[derive(Parser)] | ||
| #[command(name = "codex-execpolicy")] | ||
| enum Cli { | ||
| /// Evaluate a command against a policy. | ||
| Check { | ||
| #[arg(short, long = "policy", value_name = "PATH", required = true)] | ||
| policies: Vec<PathBuf>, | ||
|
|
||
| /// Pretty-print the JSON output. | ||
| #[arg(long)] | ||
| pretty: bool, | ||
|
|
||
| /// Command tokens to check. | ||
| #[arg( | ||
| value_name = "COMMAND", | ||
| required = true, | ||
| trailing_var_arg = true, | ||
| allow_hyphen_values = true | ||
| )] | ||
| command: Vec<String>, | ||
| }, | ||
| Check(ExecPolicyCheckCommand), | ||
| } | ||
|
|
||
| fn main() -> Result<()> { | ||
| let cli = Cli::parse(); | ||
| match cli { | ||
| Cli::Check { | ||
| policies, | ||
| command, | ||
| pretty, | ||
| } => cmd_check(policies, command, pretty), | ||
| Cli::Check(cmd) => cmd_check(cmd), | ||
| } | ||
| } | ||
|
|
||
| fn cmd_check(policy_paths: Vec<PathBuf>, args: Vec<String>, pretty: bool) -> Result<()> { | ||
| let policy = load_policies(&policy_paths)?; | ||
|
|
||
| let eval = policy.check(&args); | ||
| let json = if pretty { | ||
| serde_json::to_string_pretty(&eval)? | ||
| } else { | ||
| serde_json::to_string(&eval)? | ||
| }; | ||
| println!("{json}"); | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn load_policies(policy_paths: &[PathBuf]) -> Result<codex_execpolicy::Policy> { | ||
| let mut parser = PolicyParser::new(); | ||
| for policy_path in policy_paths { | ||
| let policy_file_contents = fs::read_to_string(policy_path) | ||
| .with_context(|| format!("failed to read policy at {}", policy_path.display()))?; | ||
| let policy_identifier = policy_path.to_string_lossy().to_string(); | ||
| parser.parse(&policy_identifier, &policy_file_contents)?; | ||
| } | ||
| Ok(parser.build()) | ||
| fn cmd_check(cmd: ExecPolicyCheckCommand) -> Result<()> { | ||
| cmd.run() | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bolinfest added integration test here!