Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Dec 1, 2023
1 parent dd575cd commit ead00e9
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 31 deletions.
13 changes: 9 additions & 4 deletions gix/src/config/cache/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,16 +384,21 @@ fn apply_environment_overrides(
let key = &gitoxide::Http::VERBOSE;
(env(key), key.name)
},
{
let key = &gitoxide::Http::SSL_NO_VERIFY;
(env(key), key.name)
},
{
let key = &gitoxide::Http::PROXY_AUTH_METHOD;
(env(key), key.name)
},
],
),
(
"gitoxide",
Some(Cow::Borrowed("http".into())),
git_prefix,
&[{
let key = &gitoxide::Http::SSL_NO_VERIFY;
(env(key), key.name)
}],
),
(
"gitoxide",
Some(Cow::Borrowed("credentials".into())),
Expand Down
3 changes: 1 addition & 2 deletions gix/src/config/tree/sections/gitoxide.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,7 @@ mod subsections {
/// git server uses a self-signed certificate and the user accepts the associated security risks.
pub const SSL_NO_VERIFY: keys::Boolean = keys::Boolean::new_boolean("sslNoVerify", &Gitoxide::HTTP)
.with_environment_override("GIT_SSL_NO_VERIFY")
.with_deviation("Only supported when using curl as https backend")
.with_note("Used to disable SSL verification. When this is enabled it takes prority over http.sslVerify.");
.with_note("used to disable SSL verification. When this is enabled it takes priority over http.sslVerify");
/// The `gitoxide.http.proxyAuthMethod` key.
pub const PROXY_AUTH_METHOD: http::ProxyAuthMethod =
http::ProxyAuthMethod::new_proxy_auth_method("proxyAuthMethod", &Gitoxide::HTTP)
Expand Down
3 changes: 2 additions & 1 deletion gix/src/config/tree/sections/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ impl Http {
);
/// The `http.sslVerify` key.
pub const SSL_VERIFY: keys::Boolean = keys::Boolean::new_boolean("sslVerify", &config::Tree::HTTP)
.with_deviation("Only supported when using curl as https backend");
.with_note("also see the `gitoxide.http.sslNoVerify` key");
/// The `http.proxy` key.
pub const PROXY: keys::String =
keys::String::new_string("proxy", &config::Tree::HTTP).with_deviation("fails on strings with illformed UTF-8");
Expand Down Expand Up @@ -61,6 +61,7 @@ impl Section for Http {
fn keys(&self) -> &[&dyn Key] {
&[
&Self::SSL_VERSION,
&Self::SSL_VERIFY,
&Self::PROXY,
&Self::PROXY_AUTH_METHOD,
&Self::VERSION,
Expand Down
27 changes: 11 additions & 16 deletions gix/src/repository/config/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,31 +406,26 @@ impl crate::Repository {
}

{
let key = "http.sslVerify";
let ssl_verify = config
let key = "gitoxide.http.sslNoVerify";
let ssl_no_verify = config
.boolean_filter_by_key(key, &mut trusted_only)
.map(|value| config::tree::Http::SSL_VERIFY.enrich_error(value))
.map(|value| config::tree::gitoxide::Http::SSL_NO_VERIFY.enrich_error(value))
.transpose()
.with_leniency(lenient)
.map_err(config::transport::http::Error::from)?
.unwrap_or(true);

let ssl_no_verify = config
.boolean_filter(
"gitoxide",
Some("http".into()),
gitoxide::Http::SSL_NO_VERIFY.name,
&mut trusted_only,
)
.and_then(Result::ok)
.unwrap_or_default();

// ssl_no_verify take prority here because it is based on environment variable
// and we try to match git behavior.
if ssl_no_verify {
opts.ssl_verify = false;
} else {
opts.ssl_verify = ssl_verify;
let key = "http.sslVerify";
opts.ssl_verify = config
.boolean_filter_by_key(key, &mut trusted_only)
.map(|value| config::tree::Http::SSL_VERIFY.enrich_error(value))
.transpose()
.with_leniency(lenient)
.map_err(config::transport::http::Error::from)?
.unwrap_or(true);
}
}

Expand Down
Git LFS file not shown
10 changes: 8 additions & 2 deletions gix/tests/fixtures/make_config_repos.sh
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,13 @@ mkdir not-a-repo-with-files;
touch this that
)

git init no-ssl-verify
(cd no-ssl-verify
git init ssl-verify-disabled
(cd ssl-verify-disabled
git config http.sslVerify false
)

git init ssl-no-verify-enabled
(cd ssl-no-verify-enabled
git config http.sslVerify true
git config gitoxide.http.sslNoVerify true
)
17 changes: 13 additions & 4 deletions gix/tests/repository/config/transport_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ mod http {
);

assert!(ssl_verify, "SSL verification is enabled by default if not configured");

assert_eq!(http_version, Some(HttpVersion::V1_1));
}

Expand Down Expand Up @@ -320,11 +319,21 @@ mod http {
}

#[test]
fn no_ssl_verify() {
let repo = repo("no-ssl-verify");
fn ssl_verify_disabled() {
let repo = repo("ssl-verify-disabled");

let opts = http_options(&repo, None, "https://example.com/does/not/matter");

assert!(!opts.ssl_verify);
}

#[test]
fn ssl_no_verify_takes_precedence() {
let repo = repo("ssl-no-verify-enabled");

let opts = http_options(&repo, None, "https://example.com/does/not/matter");
assert!(
!opts.ssl_verify,
"even with `http.sslVerify` enabled, `gitoxide.http.sslNoVerify` takes precedence`"
);
}
}

0 comments on commit ead00e9

Please sign in to comment.