-
Notifications
You must be signed in to change notification settings - Fork 14k
Conditional codex_home dotenv #29959
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
Open
canvrno-oai
wants to merge
8
commits into
main
Choose a base branch
from
canvrno/conditional_dotenv_base
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+771
−7
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
11e2f5e
Implement conditional dotenv feature
canvrno-oai 6308074
Merge branch 'main' into canvrno/conditional_dotenv_base
canvrno-oai b886ec9
Merge branch 'main' into canvrno/conditional_dotenv_base
canvrno-oai d85a4d0
Fix conditional dotenv env matching on Windows
canvrno-oai f20f87e
Merge branch 'main' into canvrno/conditional_dotenv_base
canvrno-oai 3ee0094
Reject NUL bytes in conditional dotenv values
canvrno-oai 4ae71ff
Bound conditional dotenv DNS resolution
canvrno-oai 621237d
Run conditional dotenv probes in helper process
canvrno-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
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
281 changes: 281 additions & 0 deletions
281
codex-rs/arg0/src/conditional_dotenv/conditional_dotenv_tests.rs
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,281 @@ | ||
| use super::*; | ||
| use pretty_assertions::assert_eq; | ||
| use std::cell::Cell; | ||
| use std::cell::RefCell; | ||
| use std::collections::BTreeMap; | ||
| use std::fs; | ||
| use std::time::Duration; | ||
|
|
||
| #[derive(Debug, Default, PartialEq, Eq)] | ||
| struct TestEnvironment(BTreeMap<String, String>); | ||
|
|
||
| impl StartupEnvironment for TestEnvironment { | ||
| fn set(&mut self, key: &str, value: &str) { | ||
| self.0.insert(key.to_string(), value.to_string()); | ||
| } | ||
|
|
||
| fn remove(&mut self, key: &str) { | ||
| self.0.remove(key); | ||
| } | ||
| } | ||
|
|
||
| fn write_overlay(codex_home: &Path, name: &str, contents: &str) -> std::io::Result<()> { | ||
| fs::write(codex_home.join(name), contents) | ||
| } | ||
|
|
||
| fn test_environment<const N: usize>(values: [(&str, &str); N]) -> TestEnvironment { | ||
| TestEnvironment(BTreeMap::from( | ||
| values.map(|(key, value)| (key.to_string(), value.to_string())), | ||
| )) | ||
| } | ||
|
|
||
| #[test] | ||
| fn proxy_overlays_set_or_unset_environment_for_network() -> anyhow::Result<()> { | ||
| let codex_home = tempfile::tempdir()?; | ||
| write_overlay( | ||
| codex_home.path(), | ||
| ".env.10-proxy-on", | ||
| r#"# codex-env-if: {"type":"tcp_connect","from":"HTTPS_PROXY","timeout_ms":500} | ||
| HTTPS_PROXY=http://user:password@proxy.example.com:8080 | ||
| HTTP_PROXY=http://proxy.example.com:8080 | ||
| ALL_PROXY=http://proxy.example.com:8080 | ||
| NO_PROXY=localhost,127.0.0.1,.example.com | ||
| "#, | ||
| )?; | ||
| write_overlay( | ||
| codex_home.path(), | ||
| ".env.20-proxy-off", | ||
| r#"# codex-env-if: {"not":{"type":"tcp_connect","host":"proxy.example.com","port":8080,"timeout_ms":500}} | ||
| # codex-env-unset: ["HTTPS_PROXY","HTTP_PROXY","ALL_PROXY","NO_PROXY"] | ||
| "#, | ||
| )?; | ||
|
|
||
| let on_network_calls = Cell::new(0); | ||
| let on_network_connector = |host: &str, port: u16, timeout: Duration| { | ||
| on_network_calls.set(on_network_calls.get() + 1); | ||
| assert_eq!(host, "proxy.example.com"); | ||
| assert_eq!(port, 8080); | ||
| assert_eq!(timeout, Duration::from_millis(500)); | ||
| true | ||
| }; | ||
| let mut on_network_environment = test_environment([ | ||
| ("HTTPS_PROXY", "http://stale.example.com:8080"), | ||
| ("UNRELATED", "preserved"), | ||
| ]); | ||
|
|
||
| let warnings = load_conditional_dotenv_overlays( | ||
| codex_home.path(), | ||
| &mut on_network_environment, | ||
| &on_network_connector, | ||
| ); | ||
|
|
||
| assert_eq!(on_network_calls.get(), 2); | ||
| assert_eq!(warnings, Vec::<String>::new()); | ||
| assert_eq!( | ||
| on_network_environment, | ||
| test_environment([ | ||
| ("ALL_PROXY", "http://proxy.example.com:8080"), | ||
| ("HTTPS_PROXY", "http://user:password@proxy.example.com:8080"), | ||
| ("HTTP_PROXY", "http://proxy.example.com:8080"), | ||
| ("NO_PROXY", "localhost,127.0.0.1,.example.com"), | ||
| ("UNRELATED", "preserved"), | ||
| ]) | ||
| ); | ||
|
|
||
| let off_network_calls = Cell::new(0); | ||
| let off_network_connector = |host: &str, port: u16, timeout: Duration| { | ||
| off_network_calls.set(off_network_calls.get() + 1); | ||
| assert_eq!(host, "proxy.example.com"); | ||
| assert_eq!(port, 8080); | ||
| assert_eq!(timeout, Duration::from_millis(500)); | ||
| false | ||
| }; | ||
| let mut off_network_environment = test_environment([ | ||
| ("ALL_PROXY", "http://inherited.example.com:8080"), | ||
| ("HTTPS_PROXY", "http://inherited.example.com:8080"), | ||
| ("HTTP_PROXY", "http://inherited.example.com:8080"), | ||
| ("NO_PROXY", "localhost"), | ||
| ("UNRELATED", "preserved"), | ||
| ]); | ||
|
|
||
| let warnings = load_conditional_dotenv_overlays( | ||
| codex_home.path(), | ||
| &mut off_network_environment, | ||
| &off_network_connector, | ||
| ); | ||
|
|
||
| assert_eq!(off_network_calls.get(), 2); | ||
| assert_eq!(warnings, Vec::<String>::new()); | ||
| assert_eq!( | ||
| off_network_environment, | ||
| test_environment([("UNRELATED", "preserved")]) | ||
| ); | ||
| Ok(()) | ||
| } | ||
|
|
||
| #[test] | ||
| fn overlay_filters_reserved_names_for_set_and_unset() -> anyhow::Result<()> { | ||
| let codex_home = tempfile::tempdir()?; | ||
| write_overlay( | ||
| codex_home.path(), | ||
| ".env.proxy", | ||
| r#"# codex-env-if: {"type":"tcp_connect","host":"proxy.example.com","port":8080} | ||
| # codex-env-unset: ["ALL_PROXY","codex_internal"] | ||
| HTTP_PROXY=http://proxy.example.com:8080 | ||
| codex_internal=changed | ||
| "#, | ||
| )?; | ||
| let mut environment = test_environment([ | ||
| ("ALL_PROXY", "http://old.example.com:8080"), | ||
| ("codex_internal", "preserved"), | ||
| ]); | ||
|
|
||
| let warnings = | ||
| load_conditional_dotenv_overlays(codex_home.path(), &mut environment, &|_, _, _| true); | ||
|
|
||
| assert_eq!(warnings, Vec::<String>::new()); | ||
| assert_eq!( | ||
| environment, | ||
| test_environment([ | ||
| ("HTTP_PROXY", "http://proxy.example.com:8080"), | ||
| ("codex_internal", "preserved"), | ||
| ]) | ||
| ); | ||
| Ok(()) | ||
| } | ||
|
|
||
| #[test] | ||
| fn invalid_timeout_fails_closed_without_blocking_valid_overlays() -> anyhow::Result<()> { | ||
| let codex_home = tempfile::tempdir()?; | ||
| write_overlay(codex_home.path(), ".env.example", "SHOULD_NOT_LOAD=1\n")?; | ||
| write_overlay( | ||
| codex_home.path(), | ||
| ".env.10-timeout", | ||
| r#"# codex-env-if: {"type":"tcp_connect","host":"proxy.example.com","port":8080,"timeout_ms":5001} | ||
| SHOULD_NOT_LOAD=2 | ||
| "#, | ||
| )?; | ||
| write_overlay( | ||
| codex_home.path(), | ||
| ".env.20-maximum-timeout", | ||
| r#"# codex-env-if: {"type":"tcp_connect","host":"proxy.example.com","port":8080,"timeout_ms":5000} | ||
| MAXIMUM_TIMEOUT=loaded | ||
| "#, | ||
| )?; | ||
| write_overlay( | ||
| codex_home.path(), | ||
| ".env.30-default-timeout", | ||
| r#"# codex-env-if: {"type":"tcp_connect","host":"proxy.example.com","port":8080} | ||
| DEFAULT_TIMEOUT=loaded | ||
| "#, | ||
| )?; | ||
| let observed_timeouts = RefCell::new(Vec::new()); | ||
| let connector = |_: &str, _: u16, timeout: Duration| { | ||
| observed_timeouts.borrow_mut().push(timeout); | ||
| true | ||
| }; | ||
| let mut environment = TestEnvironment::default(); | ||
|
|
||
| let warnings = | ||
| load_conditional_dotenv_overlays(codex_home.path(), &mut environment, &connector); | ||
|
|
||
| assert_eq!(warnings.len(), 1); | ||
| assert!(warnings[0].contains(".env.10-timeout")); | ||
| assert_eq!( | ||
| observed_timeouts.into_inner(), | ||
| vec![Duration::from_millis(5000), Duration::from_millis(500)] | ||
| ); | ||
| assert_eq!( | ||
| environment, | ||
| test_environment([("DEFAULT_TIMEOUT", "loaded"), ("MAXIMUM_TIMEOUT", "loaded"),]) | ||
| ); | ||
| Ok(()) | ||
| } | ||
|
|
||
| #[test] | ||
| fn invalid_from_sources_fail_closed_without_connecting() -> anyhow::Result<()> { | ||
| let codex_home = tempfile::tempdir()?; | ||
| write_overlay( | ||
| codex_home.path(), | ||
| ".env.10-missing-source", | ||
| r#"# codex-env-if: {"type":"tcp_connect","from":"HTTPS_PROXY"} | ||
| SHOULD_NOT_LOAD=1 | ||
| "#, | ||
| )?; | ||
| write_overlay( | ||
| codex_home.path(), | ||
| ".env.20-invalid-source", | ||
| r#"# codex-env-if: {"type":"tcp_connect","from":"HTTPS_PROXY"} | ||
| HTTPS_PROXY="not a url" | ||
| "#, | ||
| )?; | ||
| let connector_called = Cell::new(false); | ||
| let connector = |_: &str, _: u16, _: Duration| { | ||
| connector_called.set(true); | ||
| true | ||
| }; | ||
| let mut environment = TestEnvironment::default(); | ||
|
|
||
| let warnings = | ||
| load_conditional_dotenv_overlays(codex_home.path(), &mut environment, &connector); | ||
|
|
||
| assert!(!connector_called.get()); | ||
| assert_eq!(warnings.len(), 2); | ||
| assert_eq!(environment, TestEnvironment::default()); | ||
| Ok(()) | ||
| } | ||
|
|
||
| #[test] | ||
| fn directives_inside_multiline_values_are_not_scanned() -> anyhow::Result<()> { | ||
| let codex_home = tempfile::tempdir()?; | ||
| write_overlay( | ||
| codex_home.path(), | ||
| ".env.multiline", | ||
| r#"# codex-env-if: {"type":"tcp_connect","host":"proxy.example.com","port":8080} | ||
| # codex-env-unset: ["HTTP_PROXY"] | ||
| MESSAGE='before | ||
| # codex-env-unset: ["ALL_PROXY"] | ||
| # codex-env-if: {"not":{"type":"tcp_connect","host":"other.example.com","port":8080}} | ||
| after' | ||
| "#, | ||
| )?; | ||
| let mut environment = test_environment([ | ||
| ("ALL_PROXY", "http://proxy.example.com:8080"), | ||
| ("HTTP_PROXY", "http://proxy.example.com:8080"), | ||
| ]); | ||
|
|
||
| let warnings = | ||
| load_conditional_dotenv_overlays(codex_home.path(), &mut environment, &|_, _, _| true); | ||
|
|
||
| assert_eq!(warnings, Vec::<String>::new()); | ||
| assert_eq!( | ||
| environment, | ||
| test_environment([ | ||
| ("ALL_PROXY", "http://proxy.example.com:8080"), | ||
| ( | ||
| "MESSAGE", | ||
| "before\n# codex-env-unset: [\"ALL_PROXY\"]\n# codex-env-if: {\"not\":{\"type\":\"tcp_connect\",\"host\":\"other.example.com\",\"port\":8080}}\nafter", | ||
| ), | ||
| ]) | ||
| ); | ||
| Ok(()) | ||
| } | ||
|
|
||
| #[test] | ||
| fn endpoint_parser_supports_urls_bare_authorities_and_known_default_ports() { | ||
| assert_eq!( | ||
| [ | ||
| "http://user:password@proxy.example.com:8080", | ||
| "proxy.example.com:8080", | ||
| "https://proxy.example.com", | ||
| "http://[::1]:8080", | ||
| ] | ||
| .map(parse_endpoint), | ||
| [ | ||
| Some(("proxy.example.com".to_string(), 8080)), | ||
| Some(("proxy.example.com".to_string(), 8080)), | ||
| Some(("proxy.example.com".to_string(), 443)), | ||
| Some(("::1".to_string(), 8080)), | ||
| ] | ||
| ); | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.