Rimosso il provider OpenAI dal credential broker del proxy di rete (B)

Da f366db7f: il broker non conosce piu' OpenAI (restano GitHub e i provider configurati);
i test del loader sono adattati (stesse protezioni, verificate con GitHub e con un provider
personalizzato), non cancellati.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
andrea.difonzo
2026-11-17 12:38:28 +01:00
parent 32259c3c3f
commit 7effb5ec86
14 changed files with 228 additions and 658 deletions

View File

@@ -676,8 +676,8 @@ fn configured_provider_does_not_guess_ambiguous_alias_destinations() {
false,
),
(
"sk-proj-abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
"^sk-proj-[A-Za-z0-9]+$",
"ghp_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
"^ghp_[A-Za-z0-9]+$",
true,
),
] {
@@ -713,7 +713,7 @@ fn configured_provider_does_not_guess_ambiguous_alias_destinations() {
assert!(!broker.host_requires_mitm("first.example", /*port*/ 443));
assert!(!broker.host_requires_mitm("second.example", /*port*/ 443));
if overlaps_builtin {
assert!(!broker.host_requires_mitm("api.openai.com", /*port*/ 443));
assert!(!broker.host_requires_mitm("api.github.com", /*port*/ 443));
}
env.insert("FIRST_TOKEN".to_string(), token.to_string());
@@ -747,19 +747,15 @@ fn configured_owner_keeps_prefixed_builtin_shaped_aliases_scoped() {
),
];
for (pattern, token_length, dummy_prefix) in [
(r"\bsk-proj-[a-z]{64}\b", 64, Some("sk-proj-")),
(r"\bghp_[a-z]{64}\b", 64, Some("ghp_")),
// The fixed real branch forces an independently generated vendor-shaped dummy.
(
r"\b(?:sk-proj-a{64}|vendor_[a-z]{64})\b",
64,
Some("vendor_"),
),
(r"\b(?:ghp_a{64}|vendor_[a-z]{64})\b", 64, Some("vendor_")),
// With no distinct long alternative, registration must remain fail-open.
(r"\b(?:sk-proj-a{64}|pin)\b", 64, None),
(r"\b(?:sk-proj-[a-z]{64}|pin)\b", 64, Some("sk-proj-")),
(r"^sk-proj-[a-z]{7}$", 7, Some("sk-proj-")),
(r"\b(?:ghp_a{64}|pin)\b", 64, None),
(r"\b(?:ghp_[a-z]{64}|pin)\b", 64, Some("ghp_")),
(r"^ghp_[a-z]{7}$", 7, Some("ghp_")),
] {
let token = format!("sk-proj-{}", "a".repeat(token_length));
let token = format!("ghp_{}", "a".repeat(token_length));
for (environment_id, retain_source) in [
(None, true),
(Some("local"), true),
@@ -815,7 +811,7 @@ fn configured_owner_keeps_prefixed_builtin_shaped_aliases_scoped() {
assert!(
!broker
.host_protocols_for_environment(
"api.openai.com",
"api.github.com",
/*port*/ 443,
environment_id,
)
@@ -828,7 +824,7 @@ fn configured_owner_keeps_prefixed_builtin_shaped_aliases_scoped() {
)])
};
for target in [
"https://api.openai.com/v1/models",
"https://github.com/v1/models",
"https://api.github.com/v1/models",
"https://configured.example/v2/models",
"https://configured.example/v1/models",
@@ -862,7 +858,7 @@ fn configured_owner_keeps_prefixed_builtin_shaped_aliases_scoped() {
&original_env[key],
&env[key],
&original_env,
|source| source == "OPENAI_API_KEY"
|source| source == "GH_TOKEN"
));
let mut snapshot_alias = original_env[key].clone();
if brokered || token.len() >= super::MIN_EMBEDDED_CREDENTIAL_LENGTH {
@@ -912,7 +908,7 @@ fn configured_owner_keeps_prefixed_builtin_shaped_aliases_scoped() {
#[test]
fn known_credential_spans_preserve_overlaps_and_uncovered_adjacent_credentials() {
let short = format!("sk-proj-{}", "a".repeat(64));
let short = format!("ghp_{}", "a".repeat(64));
let long = format!("{short}bbbbbbbb");
let known = format!(":known_{}", "b".repeat(24));
let github = "ghp_spinningabcdefghijklmnopqrstuvwxyz0123456789";
@@ -937,7 +933,7 @@ fn known_credential_spans_preserve_overlaps_and_uncovered_adjacent_credentials()
"PIN_KEY".to_string(),
],
patterns: vec![
r"\bsk-proj-[ab]{64,72}\b".to_string(),
r"\bghp_[ab]{64,72}\b".to_string(),
"^:known_[a-z]{24}$".to_string(),
r"\bpin\b".to_string(),
],
@@ -1055,21 +1051,21 @@ fn known_credential_spans_preserve_overlaps_and_uncovered_adjacent_credentials()
#[test]
fn configured_provider_does_not_claim_embedded_builtin_credentials() {
let github_token = "ghp_abcdefghijklmnopqrstuvwxyz1234567890";
let openai_token = "sk-proj-abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
let oauth_token = "gho_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
let broker = broker_for(CredentialProviderConfig {
env: vec!["COMBINED_TOKEN".to_string()],
patterns: vec!["^combo_[A-Za-z0-9_-]{80,180}$".to_string()],
url_prefixes: vec!["combo.example".to_string()],
..CredentialProviderConfig::default()
});
let combined = format!("combo_{github_token}_{openai_token}");
let combined = format!("combo_{github_token}_{oauth_token}");
let mut env = HashMap::from([("AUTH_HEADER".to_string(), combined.clone())]);
broker.virtualize_child_env(&mut env);
assert_ne!(env["AUTH_HEADER"], combined);
assert!(!env["AUTH_HEADER"].contains(github_token));
assert!(!env["AUTH_HEADER"].contains(openai_token));
assert!(!env["AUTH_HEADER"].contains(oauth_token));
assert!(!broker.host_requires_mitm("combo.example", /*port*/ 443));
let mut headers = HeaderMap::new();
headers.insert(
@@ -1088,7 +1084,7 @@ fn configured_provider_does_not_claim_embedded_builtin_credentials() {
#[test]
fn configured_discovery_does_not_claim_overlapping_credential_spans() {
let suffix = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUV";
for token in [format!("sk-{suffix}"), format!("sk-proj-{suffix}{suffix}")] {
for token in [format!("ghp_{suffix}"), format!("gho_{suffix}{suffix}")] {
for patterns in [
vec!["^[A-Za-z0-9]{48}$".to_string()],
vec![
@@ -1202,10 +1198,6 @@ fn discovered_credential_aliases_rebind_when_the_destination_changes() {
for (token, host_key) in [
("stripe_live_abcdefghijklmnopqrstuvwx", "STRIPE_HOST"),
("ghp_abcdefghijklmnopqrstuvwxyz1234567890", "GH_HOST"),
(
"sk-proj-abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
"OPENAI_BASE_URL",
),
] {
let broker = broker_for(CredentialProviderConfig {
env: vec!["STRIPE_API_KEY".to_string()],
@@ -1215,13 +1207,8 @@ fn discovered_credential_aliases_rebind_when_the_destination_changes() {
});
let authorization_header = format!("Bearer {token}");
for host in ["first.example", "second.example"] {
let host_value = if host_key == "OPENAI_BASE_URL" {
format!("https://{host}/v1")
} else {
host.to_string()
};
let mut env = HashMap::from([
(host_key.to_string(), host_value),
(host_key.to_string(), host.to_string()),
("AUTH_HEADER".to_string(), authorization_header.clone()),
]);
@@ -1245,12 +1232,8 @@ fn discovered_credential_aliases_rebind_when_the_destination_changes() {
let mut unbound =
HashMap::from([("AUTH_HEADER".to_string(), authorization_header.clone())]);
broker.virtualize_child_env(&mut unbound);
if host_key == "OPENAI_BASE_URL" {
assert_ne!(unbound["AUTH_HEADER"], authorization_header);
} else {
assert_eq!(unbound["AUTH_HEADER"], authorization_header);
assert!(!broker.host_requires_mitm("api.github.com", /*port*/ 443));
}
assert_eq!(unbound["AUTH_HEADER"], authorization_header);
assert!(!broker.host_requires_mitm("api.github.com", /*port*/ 443));
}
}
@@ -1292,13 +1275,6 @@ fn credential_rotation_preserves_alias_destination_ownership() {
"ghp_",
None,
),
(
"OPENAI_API_KEY",
"OPENAI_API_KEY",
"OPENAI_BASE_URL",
"sk-proj-",
Some("api.openai.com"),
),
] {
for (environment, real_aliases, discover_parent) in [
("parent", false, false),
@@ -1321,10 +1297,9 @@ fn credential_rotation_preserves_alias_destination_ownership() {
url_prefix_from_env: Some("PROVIDER_URL".to_string()),
..CredentialProviderConfig::default()
});
let suffix_len = if key == "OPENAI_API_KEY" { 64 } else { 36 };
let first = format!("{prefix}{}", "a".repeat(suffix_len));
let second = format!("{prefix}{}", "b".repeat(suffix_len));
let auxiliary = format!("{prefix}{}", "c".repeat(suffix_len));
let first = format!("{prefix}{}", "a".repeat(36));
let second = format!("{prefix}{}", "b".repeat(36));
let auxiliary = format!("{prefix}{}", "c".repeat(36));
let endpoint = |host: &str| {
if host_key == "GH_HOST" {
host.to_string()
@@ -2839,12 +2814,6 @@ fn explicit_invalid_destinations_clear_previous_dynamic_bindings() {
"ghp_abcdefghijklmnopqrstuvwxyz0123456789",
None,
),
(
"OPENAI_API_KEY",
"OPENAI_BASE_URL",
"sk-proj-abcdefghijklmnopqrstuvwxyz0123456789",
Some("api.openai.com"),
),
(
"PROVIDER_TOKEN",
"PROVIDER_ENDPOINT",

View File

@@ -1,5 +1,4 @@
mod github;
mod openai;
use super::destination::CredentialDestination;
use rama_http::HeaderMap;
@@ -23,7 +22,6 @@ pub(super) struct CredentialProvider {
pub(super) credential_watermark: Option<&'static str>,
pub(super) minimum_credential_len: usize,
sources: &'static [CredentialSource],
pub(super) reset_on_configuration_change: bool,
dummy_value: fn(&str) -> String,
translate_request_header: TranslateRequestHeader,
request_header_value: fn(&str) -> Option<HeaderValue>,
@@ -33,7 +31,6 @@ pub(super) struct CredentialProvider {
#[derive(Clone, PartialEq, Eq)]
pub(super) enum CredentialHostBinding {
ExactHost(String),
ExactHosts(Vec<String>),
HostPattern {
exact_hosts: &'static [&'static str],
suffixes: &'static [&'static str],
@@ -46,11 +43,10 @@ pub(super) struct CredentialSource {
pub(super) binding_env_vars: &'static [&'static str],
// An explicit invalid hint can clear dynamic destinations while leaving a static fallback.
pub(super) invalidates_host_binding: fn(&HashMap<String, String>) -> bool,
pub(super) host_binding:
fn(&HashMap<String, String>, Option<&str>) -> Option<CredentialHostBinding>,
pub(super) host_binding: fn(&HashMap<String, String>) -> Option<CredentialHostBinding>,
}
const CREDENTIAL_PROVIDERS: &[&CredentialProvider] = &[&github::PROVIDER, &openai::PROVIDER];
const CREDENTIAL_PROVIDERS: &[&CredentialProvider] = &[&github::PROVIDER];
impl CredentialProvider {
pub(super) fn sources(&self) -> &[CredentialSource] {
@@ -85,7 +81,6 @@ impl CredentialHostBinding {
match self {
Self::ExactHost(host) => is_default_proxy_bypass_host(host),
Self::ExactHosts(hosts) => hosts.iter().any(|host| is_default_proxy_bypass_host(host)),
Self::HostPattern { exact_hosts, .. } => exact_hosts
.iter()
.any(|host| is_default_proxy_bypass_host(host)),
@@ -98,9 +93,6 @@ impl CredentialHostBinding {
pub(super) fn matches_host(&self, host: &str, port: u16) -> bool {
match self {
Self::ExactHost(expected_host) => host == expected_host,
Self::ExactHosts(expected_hosts) => {
expected_hosts.iter().any(|expected| host == expected)
}
Self::HostPattern {
exact_hosts,
suffixes,
@@ -119,7 +111,7 @@ impl CredentialHostBinding {
Self::ConfiguredHosts(destinations) => destinations
.iter()
.any(|destination| destination.matches_hostname(host)),
Self::ExactHost(_) | Self::ExactHosts(_) | Self::HostPattern { .. } => {
Self::ExactHost(_) | Self::HostPattern { .. } => {
self.matches_host(host, /*port*/ 443)
}
}
@@ -130,9 +122,7 @@ impl CredentialHostBinding {
Self::ConfiguredHosts(destinations) => destinations
.iter()
.any(|destination| destination.requires_mitm(host, port)),
Self::ExactHost(_) | Self::ExactHosts(_) | Self::HostPattern { .. } => {
self.matches_host(host, port)
}
Self::ExactHost(_) | Self::HostPattern { .. } => self.matches_host(host, port),
}
}
@@ -141,7 +131,7 @@ impl CredentialHostBinding {
Self::ConfiguredHosts(destinations) => destinations
.iter()
.any(|destination| destination.matches_request(host, request)),
Self::ExactHost(_) | Self::ExactHosts(_) | Self::HostPattern { .. } => {
Self::ExactHost(_) | Self::HostPattern { .. } => {
request.is_none_or(|request| request.scheme() == "https")
&& self.matches_host(
host,

View File

@@ -42,7 +42,6 @@ pub(super) static PROVIDER: CredentialProvider = CredentialProvider {
host_binding: github_enterprise_binding,
},
],
reset_on_configuration_change: false,
dummy_value,
translate_request_header,
request_header_value,
@@ -110,20 +109,14 @@ pub(super) fn translate_request_header(
}
}
fn github_cloud_binding(
_: &HashMap<String, String>,
_: Option<&str>,
) -> Option<CredentialHostBinding> {
fn github_cloud_binding(_: &HashMap<String, String>) -> Option<CredentialHostBinding> {
Some(CredentialHostBinding::HostPattern {
exact_hosts: GITHUB_CLOUD_HOSTS,
suffixes: GITHUB_CLOUD_HOST_SUFFIXES,
})
}
fn github_enterprise_binding(
env: &HashMap<String, String>,
_: Option<&str>,
) -> Option<CredentialHostBinding> {
fn github_enterprise_binding(env: &HashMap<String, String>) -> Option<CredentialHostBinding> {
github_host_hint(env)
.filter(|host| !github_cloud_host(host))
.map(CredentialHostBinding::ExactHost)

View File

@@ -1,95 +0,0 @@
use super::super::env_value;
use super::CredentialHostBinding;
use super::CredentialProvider;
use super::CredentialSource;
use super::shaped_dummy_value;
use crate::config::trusted_credential_broker_host;
use rama_http::HeaderMap;
use rama_http::HeaderValue;
use rama_http::header::AUTHORIZATION;
use std::collections::HashMap;
const OPENAI_API_KEY_ENV_VARS: &[&str] = &["OPENAI_API_KEY"];
const OPENAI_API_KEY_PREFIXES: &[&str] = &["sk-proj-", "sk-svcacct-", "sk-admin-", "sk-"];
const OPENAI_BASE_URL_ENV_VAR: &str = "OPENAI_BASE_URL";
const OPENAI_API_KEY_MIN_LEN: usize = 51;
const OPENAI_API_HOST: &str = "api.openai.com";
pub(super) static PROVIDER: CredentialProvider = CredentialProvider {
context_env_vars: &[OPENAI_BASE_URL_ENV_VAR],
credential_prefixes: OPENAI_API_KEY_PREFIXES,
ignored_credential_prefixes: &["sk-ant-", "sk-or-"],
credential_watermark: Some("T3BlbkFJ"),
minimum_credential_len: OPENAI_API_KEY_MIN_LEN,
sources: &[CredentialSource {
env_vars: OPENAI_API_KEY_ENV_VARS,
binding_env_vars: &[OPENAI_BASE_URL_ENV_VAR],
invalidates_host_binding: |env| {
env_value(env, OPENAI_BASE_URL_ENV_VAR)
.is_some_and(|value| trusted_credential_broker_host(value).is_none())
},
host_binding,
}],
reset_on_configuration_change: true,
dummy_value,
translate_request_header,
request_header_value,
insert_request_header,
};
fn dummy_value(real_value: &str) -> String {
shaped_dummy_value(
real_value,
openai_api_key_prefix(real_value),
OPENAI_API_KEY_MIN_LEN,
)
}
fn translate_request_header(
headers: &HeaderMap,
expected_value: &str,
replacement_value: &str,
) -> Option<HeaderValue> {
headers
.get(AUTHORIZATION)
.and_then(|header| header.to_str().ok())
.filter(|header| header.contains(expected_value))?;
request_header_value(replacement_value)
}
fn request_header_value(value: &str) -> Option<HeaderValue> {
HeaderValue::from_str(&format!("Bearer {value}")).ok()
}
fn insert_request_header(headers: &mut HeaderMap, value: HeaderValue) {
headers.insert(AUTHORIZATION, value);
}
fn host_binding(
env: &HashMap<String, String>,
configured_host: Option<&str>,
) -> Option<CredentialHostBinding> {
let mut hosts = vec![OPENAI_API_HOST.to_string()];
for host in configured_host
.map(str::to_string)
.into_iter()
.chain(env_value(env, OPENAI_BASE_URL_ENV_VAR).and_then(trusted_credential_broker_host))
{
if !hosts.contains(&host) {
hosts.push(host);
}
}
Some(match hosts.as_slice() {
[host] => CredentialHostBinding::ExactHost(host.clone()),
_ => CredentialHostBinding::ExactHosts(hosts),
})
}
fn openai_api_key_prefix(value: &str) -> &str {
OPENAI_API_KEY_PREFIXES
.iter()
.copied()
.find(|prefix| value.starts_with(prefix))
.unwrap_or("sk-")
}

View File

@@ -349,9 +349,7 @@ pub(super) fn active_credential_sources(
let mut sources = Vec::new();
for provider in providers::credential_providers() {
for source in provider.sources() {
if let Some(host_binding) =
(source.host_binding)(&binding_env, state.openai_api_host.as_deref())
{
if let Some(host_binding) = (source.host_binding)(&binding_env) {
sources.push(ActiveCredentialSource {
provider: BrokeredCredentialProvider::Builtin(provider),
host_binding,
@@ -448,7 +446,7 @@ pub(super) fn prioritized_credentials<'a>(
BrokeredCredentialProvider::Builtin(provider) => {
provider.sources().iter().any(|source| {
!source.binding_env_vars.is_empty()
&& (source.host_binding)(&binding_env, state.openai_api_host.as_deref())
&& (source.host_binding)(&binding_env)
.is_some_and(|binding| binding == credential.host_binding)
})
}