Add configurable credential providers to the network proxy (#44056)

## What changed

- Add `credential_providers` configuration for environment variables, credential patterns, static or environment-derived URL prefixes, and bearer, token, Basic, or custom header authentication.
- Generate matching dummy credentials and restrict replacement to authorized schemes, hosts, ports, and path prefixes. Scope credentials and destination history to each environment.
- Preserve real credentials for destinations that bypass the proxy with `allow_local_binding`, while providing snapshot redaction and alias matching.
- Validate provider definitions and preserve unchanged providers across configuration reloads.

## Testing

Add coverage for dummy generation, authentication translation, URL restrictions, environment isolation, configuration reloads, and credential restoration for local proxy bypass.

GitOrigin-RevId: 5d60ecc684cf4e9aa7637af33af12179aa0c672a
This commit is contained in:
Winston Howes authored and copyberry committed 2026-09-09 04:59:01 +00:00
1 parent 96c2b4377f
commit 1bfd383890
25 files changed
+3540 -299

No files matched your search

@@ -29,11 +29,16 @@ pub(super) static PROVIDER: CredentialProvider = CredentialProvider {
CredentialSource {
env_vars: GITHUB_CLOUD_TOKEN_ENV_VARS,
binding_env_vars: &[],
invalidates_host_binding: |_| false,
host_binding: github_cloud_binding,
},
CredentialSource {
env_vars: GITHUB_ENTERPRISE_TOKEN_ENV_VARS,
binding_env_vars: &[GH_HOST_ENV_VAR],
invalidates_host_binding: |env| {
env_value(env, GH_HOST_ENV_VAR).is_some()
&& github_host_hint(env).is_none_or(|host| github_cloud_host(&host))
},
host_binding: github_enterprise_binding,
},
],
@@ -60,7 +65,7 @@ fn insert_request_header(headers: &mut HeaderMap, value: HeaderValue) {
headers.insert(AUTHORIZATION, value);
}
fn translate_request_header(
pub(super) fn translate_request_header(
headers: &HeaderMap,
expected_value: &str,
replacement_value: &str,
@@ -72,16 +77,27 @@ fn translate_request_header(
let decoded = base64::engine::general_purpose::STANDARD
.decode(value)
.ok()?;
let separator = decoded.iter().position(|byte| *byte == b':')?;
let mut translated = Vec::with_capacity(decoded.len() + replacement_value.len());
if &decoded[..separator] == expected_value.as_bytes() {
translated.extend_from_slice(replacement_value.as_bytes());
translated.extend_from_slice(&decoded[separator..]);
} else if &decoded[separator + 1..] == expected_value.as_bytes() {
translated.extend_from_slice(&decoded[..=separator]);
if decoded == expected_value.as_bytes() {
translated.extend_from_slice(replacement_value.as_bytes());
} else {
return None;
let separator = decoded.iter().position(|byte| *byte == b':')?;
let username = &decoded[..separator];
let password = &decoded[separator + 1..];
if username != expected_value.as_bytes() && password != expected_value.as_bytes() {
return None;
}
translated.extend_from_slice(if username == expected_value.as_bytes() {
replacement_value.as_bytes()
} else {
username
});
translated.push(b':');
translated.extend_from_slice(if password == expected_value.as_bytes() {
replacement_value.as_bytes()
} else {
password
});
}
let encoded = base64::engine::general_purpose::STANDARD.encode(translated);
HeaderValue::from_str(&format!("{scheme} {encoded}")).ok()
@@ -129,7 +145,27 @@ fn github_token_prefix(value: &str) -> &str {
}
fn github_host_hint(env: &HashMap<String, String>) -> Option<String> {
env_value(env, GH_HOST_ENV_VAR)
.map(normalize_host)
.filter(|host| !host.is_empty())
let value = env_value(env, GH_HOST_ENV_VAR)?.trim();
if value.is_empty() || value.contains(['/', '\\', '@', '?', '#', '*']) {
return None;
}
let authority = if value.parse::<std::net::IpAddr>().is_ok()
|| crate::policy::unscoped_ip_literal(value).is_some()
{
format!("[{value}]")
} else {
value.to_string()
};
let parsed = authority.parse::<rama_http::uri::Authority>().ok()?;
let suffix = authority.strip_prefix(parsed.host())?;
if !suffix.is_empty() {
suffix.strip_prefix(':')?.parse::<u16>().ok()?;
}
let host = normalize_host(parsed.host());
let ip_literal = host.parse::<std::net::IpAddr>().is_ok()
|| crate::policy::unscoped_ip_literal(&host).is_some();
if !ip_literal && (parsed.host().starts_with('[') || url::Host::parse(&host).is_err()) {
return None;
}
Some(host)
}