Skip to content

Commit 11e8ea1

Browse files
committed
fix(providers): retain caller overrides in metadata snapshots
Keep effective caller values for profile-declared non-secret environment destinations so supervisor metadata sees the same project ID as workloads. Apply sandbox environment, template environment, then provider default precedence, preserving explicit empty values and credential ownership. Include effective overrides in the provider environment revision to prevent stale snapshots, and document precedence and independent project aliases. Add regression coverage for missing defaults, credential classification, and revision consistency, plus Google SDK discovery cases for overrides. Validation: 73 Rust tests and all 6 focused Podman SDK E2E cases passed. Gateway Clippy, Rust/Python formatting, documentation, Markdown, and git diff --check passed. Python lint and type-check diagnostics match the committed baseline. No Go SDK changes. Signed-off-by: Philippe Martin <phmartin@redhat.com>
1 parent c948d65 commit 11e8ea1

5 files changed

Lines changed: 302 additions & 31 deletions

File tree

‎crates/openshell-server/src/grpc/policy.rs‎

Lines changed: 190 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3069,13 +3069,19 @@ async fn build_sandbox_config_snapshot_with_sources(
30693069
&policy_credential_bindings,
30703070
)?;
30713071
}
3072+
let environment_overrides = super::provider::non_secret_provider_environment_overrides(
3073+
provider_profile_catalog,
3074+
provider_records,
3075+
sandbox,
3076+
);
30723077
let provider_env_revision = compute_provider_env_revision_from_records_and_policy_bindings(
30733078
provider_profile_catalog,
30743079
provider_records,
30753080
&policy_credential_bindings,
30763081
policy
30773082
.as_ref()
30783083
.filter(|_| matches!(policy_source, PolicySource::Global)),
3084+
&environment_overrides,
30793085
)?;
30803086

30813087
let service_routes = match service_routes {
@@ -3378,6 +3384,7 @@ fn compute_provider_env_revision_from_records(
33783384
records,
33793385
&HashMap::new(),
33803386
None,
3387+
&BTreeMap::new(),
33813388
)
33823389
}
33833390

@@ -3386,6 +3393,7 @@ fn compute_provider_env_revision_from_records_and_policy_bindings(
33863393
records: &[super::provider::ProviderEnvironmentRecord],
33873394
policy_bindings: &HashMap<String, Vec<StaticCredentialEndpointBinding>>,
33883395
global_policy: Option<&ProtoSandboxPolicy>,
3396+
environment_overrides: &BTreeMap<String, String>,
33893397
) -> Result<u64, Status> {
33903398
let mut hasher = Sha256::new();
33913399
hasher.update(b"openshell-provider-env-revision-v4");
@@ -3429,6 +3437,18 @@ fn compute_provider_env_revision_from_records_and_policy_bindings(
34293437
hasher.update(deterministic_policy_hash(policy).as_bytes());
34303438
}
34313439

3440+
if !environment_overrides.is_empty() {
3441+
// Hash the same effective values that the environment snapshot installs.
3442+
// Length prefixes preserve boundaries, including explicitly empty values.
3443+
hasher.update(b"caller-non-secret-environment");
3444+
for (key, value) in environment_overrides {
3445+
hasher.update((key.len() as u64).to_le_bytes());
3446+
hasher.update(key.as_bytes());
3447+
hasher.update((value.len() as u64).to_le_bytes());
3448+
hasher.update(value.as_bytes());
3449+
}
3450+
}
3451+
34323452
let digest = hasher.finalize();
34333453
Ok(u64::from_le_bytes(digest[..8].try_into().map_err(
34343454
|_| Status::internal("provider env revision digest too short"),
@@ -3895,6 +3915,7 @@ pub async fn build_provider_environment_snapshot(
38953915
/// is resolved.
38963916
struct ProviderEnvironmentTarget<'a> {
38973917
spec: &'a openshell_core::proto::SandboxSpec,
3918+
environment_overrides: BTreeMap<String, String>,
38983919
effective_policy: ProtoSandboxPolicy,
38993920
policy_source: PolicySource,
39003921
policy_credential_bindings: HashMap<String, Vec<StaticCredentialEndpointBinding>>,
@@ -3926,14 +3947,21 @@ fn provider_environment_target(
39263947
&effective_policy,
39273948
&policy_credential_bindings,
39283949
)?;
3950+
let environment_overrides = super::provider::non_secret_provider_environment_overrides(
3951+
provider_profile_catalog,
3952+
provider_records,
3953+
sandbox,
3954+
);
39293955
let provider_env_revision = compute_provider_env_revision_from_records_and_policy_bindings(
39303956
provider_profile_catalog,
39313957
provider_records,
39323958
&policy_credential_bindings,
39333959
matches!(policy_source, PolicySource::Global).then_some(&effective_policy),
3960+
&environment_overrides,
39343961
)?;
39353962
Ok(ProviderEnvironmentTarget {
39363963
spec,
3964+
environment_overrides,
39373965
effective_policy,
39383966
policy_source,
39393967
policy_credential_bindings,
@@ -3952,6 +3980,7 @@ pub async fn build_provider_environment_snapshot_from_inputs(
39523980
let sandbox_id = inputs.sandbox.object_id().to_string();
39533981
let ProviderEnvironmentTarget {
39543982
spec,
3983+
environment_overrides,
39553984
effective_policy,
39563985
policy_source,
39573986
policy_credential_bindings,
@@ -3989,20 +4018,12 @@ pub async fn build_provider_environment_snapshot_from_inputs(
39894018
credential.token_grant_owners.clone_from(&owners);
39904019
}
39914020
}
3992-
// Sandbox template and spec values are caller-owned. Drop only non-secret
3993-
// profile defaults for those keys before sending the snapshot to either
3994-
// the canonical process or later execs. Credential keys keep their
3995-
// existing binding and placeholder precedence.
3996-
let caller_has_key = |key: &str| {
3997-
spec.environment.contains_key(key)
3998-
|| spec
3999-
.template
4000-
.as_ref()
4001-
.is_some_and(|template| template.environment.contains_key(key))
4002-
};
4003-
provider_environment.environment.retain(|key, _| {
4004-
provider_environment.static_credential_keys.contains(key) || !caller_has_key(key)
4005-
});
4021+
// Retain effective non-secret values for both process environments and
4022+
// supervisor consumers such as metadata. Credential keys are excluded
4023+
// from overrides and keep their binding and placeholder precedence.
4024+
provider_environment
4025+
.environment
4026+
.extend(environment_overrides);
40064027

40074028
let mut readiness_reason = provider_environment.readiness_reason;
40084029

@@ -14871,6 +14892,10 @@ mod tests {
1487114892
.environment
1487214893
.config
1487314894
.insert("CUSTOM_PROJECT".to_string(), "project".to_string());
14895+
profile
14896+
.environment
14897+
.config
14898+
.insert("CUSTOM_MISSING".to_string(), "missing".to_string());
1487414899
profile
1487514900
.environment
1487614901
.fixed
@@ -14903,17 +14928,39 @@ mod tests {
1490314928
.insert("CUSTOM_PROJECT".to_string(), "caller-project".to_string());
1490414929
spec.environment
1490514930
.insert("GITHUB_TOKEN".to_string(), "caller-token".to_string());
14906-
spec.template
14907-
.get_or_insert_with(Default::default)
14908-
.environment
14909-
.insert("CUSTOM_MODE".to_string(), String::new());
14931+
spec.environment
14932+
.insert("CUSTOM_MISSING".to_string(), " caller value ".to_string());
14933+
spec.environment
14934+
.insert("UNRELATED".to_string(), "caller-only".to_string());
14935+
let template = spec.template.get_or_insert_with(Default::default);
14936+
template.environment = HashMap::from([
14937+
("CUSTOM_PROJECT".to_string(), "template-project".to_string()),
14938+
("CUSTOM_MODE".to_string(), String::new()),
14939+
]);
1491014940
state.store.put_message(&sandbox).await.unwrap();
1491114941

1491214942
let snapshot = load_sandbox_provider_environment(&state, &sandbox, true, true)
1491314943
.await
1491414944
.unwrap();
14915-
assert!(!snapshot.environment.contains_key("CUSTOM_PROJECT"));
14916-
assert!(!snapshot.environment.contains_key("CUSTOM_MODE"));
14945+
assert_eq!(
14946+
snapshot
14947+
.environment
14948+
.get("CUSTOM_PROJECT")
14949+
.map(String::as_str),
14950+
Some("caller-project")
14951+
);
14952+
assert_eq!(
14953+
snapshot.environment.get("CUSTOM_MODE").map(String::as_str),
14954+
Some("")
14955+
);
14956+
assert_eq!(
14957+
snapshot
14958+
.environment
14959+
.get("CUSTOM_MISSING")
14960+
.map(String::as_str),
14961+
Some(" caller value ")
14962+
);
14963+
assert!(!snapshot.environment.contains_key("UNRELATED"));
1491714964
assert_eq!(
1491814965
snapshot.environment.get("CUSTOM_UNSET").map(String::as_str),
1491914966
Some("default")
@@ -14927,7 +14974,129 @@ mod tests {
1492714974
.static_credential_bindings
1492814975
.contains_key("GITHUB_TOKEN")
1492914976
);
14930-
assert_eq!(snapshot.non_secret_environment_keys, ["CUSTOM_UNSET"]);
14977+
assert_eq!(
14978+
snapshot.non_secret_environment_keys,
14979+
[
14980+
"CUSTOM_MISSING",
14981+
"CUSTOM_MODE",
14982+
"CUSTOM_PROJECT",
14983+
"CUSTOM_UNSET"
14984+
]
14985+
);
14986+
}
14987+
14988+
#[tokio::test]
14989+
async fn caller_environment_revision_tracks_effective_non_secret_values() {
14990+
let state = test_server_state().await;
14991+
let mut provider = test_provider("work-gcp", "google-cloud");
14992+
provider.credentials.clear();
14993+
state.store.put_message(&provider).await.unwrap();
14994+
let sandbox = test_sandbox(
14995+
"sb-caller-revision",
14996+
"caller-revision",
14997+
ProtoSandboxPolicy::default(),
14998+
vec!["work-gcp".to_string()],
14999+
);
15000+
let mut inputs = load_sandbox_config_inputs(&state, sandbox).await.unwrap();
15001+
let mut revisions = Vec::new();
15002+
for (spec_value, template_value, unrelated, expected) in [
15003+
(None, None, "one", None),
15004+
(
15005+
None,
15006+
Some("template-project"),
15007+
"one",
15008+
Some("template-project"),
15009+
),
15010+
(
15011+
Some("caller-project"),
15012+
Some("template-project"),
15013+
"one",
15014+
Some("caller-project"),
15015+
),
15016+
(
15017+
Some("caller-project"),
15018+
Some("shadowed-project"),
15019+
"two",
15020+
Some("caller-project"),
15021+
),
15022+
(Some(""), Some("template-project"), "one", Some("")),
15023+
] {
15024+
let spec = inputs.sandbox.spec.as_mut().unwrap();
15025+
spec.environment = HashMap::from([("UNRELATED".to_string(), unrelated.to_string())]);
15026+
if let Some(value) = spec_value {
15027+
spec.environment
15028+
.insert("GCP_PROJECT_ID".to_string(), value.to_string());
15029+
}
15030+
spec.template
15031+
.get_or_insert_with(Default::default)
15032+
.environment = template_value
15033+
.map(|value| HashMap::from([("GCP_PROJECT_ID".to_string(), value.to_string())]))
15034+
.unwrap_or_default();
15035+
let config = build_sandbox_config_snapshot_from_inputs(&state, &inputs)
15036+
.await
15037+
.unwrap();
15038+
let snapshot =
15039+
build_provider_environment_snapshot_from_inputs(&state, &inputs, true, true)
15040+
.await
15041+
.unwrap();
15042+
assert_eq!(config.provider_env_revision, snapshot.provider_env_revision);
15043+
let value = snapshot
15044+
.values
15045+
.iter()
15046+
.find(|value| value.name == "GCP_PROJECT_ID");
15047+
assert_eq!(value.map(|value| value.value.as_str()), expected);
15048+
if let Some(value) = value {
15049+
assert_eq!(
15050+
value.classification,
15051+
ProviderEnvironmentValueClassification::NonSecret as i32
15052+
);
15053+
}
15054+
revisions.push(snapshot.provider_env_revision);
15055+
}
15056+
assert_ne!(revisions[0], revisions[1]);
15057+
assert_ne!(revisions[1], revisions[2]);
15058+
assert_eq!(revisions[2], revisions[3]);
15059+
assert_ne!(revisions[2], revisions[4]);
15060+
assert_ne!(revisions[0], revisions[4]);
15061+
}
15062+
15063+
#[tokio::test]
15064+
async fn caller_environment_does_not_reclassify_expired_credentials_as_non_secret() {
15065+
let state = test_server_state().await;
15066+
let mut provider = test_provider("work-gcp", "google-cloud");
15067+
provider.credentials.clear();
15068+
state.store.put_message(&provider).await.unwrap();
15069+
let mut legacy_provider = test_provider("legacy-project", "legacy-custom");
15070+
legacy_provider.credentials =
15071+
HashMap::from([("GCP_PROJECT_ID".to_string(), "expired-secret".to_string())]);
15072+
legacy_provider.credential_expiration_times.insert(
15073+
"GCP_PROJECT_ID".to_string(),
15074+
openshell_core::time::timestamp_from_millis(1).unwrap(),
15075+
);
15076+
state.store.put_message(&legacy_provider).await.unwrap();
15077+
let mut sandbox = test_sandbox(
15078+
"sb-caller-expired",
15079+
"caller-expired",
15080+
ProtoSandboxPolicy::default(),
15081+
vec!["work-gcp".to_string(), "legacy-project".to_string()],
15082+
);
15083+
sandbox
15084+
.spec
15085+
.as_mut()
15086+
.unwrap()
15087+
.environment
15088+
.insert("GCP_PROJECT_ID".to_string(), "caller-project".to_string());
15089+
15090+
let snapshot = load_sandbox_provider_environment(&state, &sandbox, true, true)
15091+
.await
15092+
.unwrap();
15093+
assert!(!snapshot.environment.contains_key("GCP_PROJECT_ID"));
15094+
assert!(
15095+
!snapshot
15096+
.non_secret_environment_keys
15097+
.iter()
15098+
.any(|key| key == "GCP_PROJECT_ID")
15099+
);
1493115100
}
1493215101

1493315102
#[tokio::test]

‎crates/openshell-server/src/grpc/provider.rs‎

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ use openshell_policy::ProviderPolicyLayer;
4040
use openshell_providers::LEGACY_VERTEX_PRIVATE_KEY_ENV;
4141
use prost::Message;
4242
use sha2::{Digest, Sha256};
43-
use std::collections::{HashMap, HashSet};
43+
use std::collections::{BTreeMap, HashMap, HashSet};
4444
use tonic::Status;
4545
use tracing::warn;
4646

@@ -2255,6 +2255,57 @@ fn validate_provider_environment_records_unique_at(
22552255
Ok(())
22562256
}
22572257

2258+
/// Effective caller values for declared non-secret destinations only. Reserve
2259+
/// credential keys even when their values are missing, expired, or withheld.
2260+
pub(super) fn non_secret_provider_environment_overrides(
2261+
catalog: &EffectiveProviderProfileCatalog,
2262+
records: &[ProviderEnvironmentRecord],
2263+
sandbox: &Sandbox,
2264+
) -> BTreeMap<String, String> {
2265+
let Some(spec) = sandbox.spec.as_ref() else {
2266+
return BTreeMap::new();
2267+
};
2268+
let mut destinations = HashSet::new();
2269+
let mut credential_keys = reserved_bootstrap_credential_keys();
2270+
for record in records {
2271+
let provider = &record.provider;
2272+
credential_keys.extend(provider.credentials.keys().cloned());
2273+
credential_keys.extend(provider.credential_handles.keys().cloned());
2274+
for state in &record.refresh_states {
2275+
credential_keys.insert(state.credential_key.clone());
2276+
credential_keys.extend(state.additional_output_keys.values().cloned());
2277+
}
2278+
if let Some(profile) = get_provider_type_profile_for_scope(
2279+
catalog,
2280+
&provider.r#type,
2281+
&provider.profile_workspace,
2282+
) {
2283+
destinations.extend(profile.environment.config.keys().cloned());
2284+
destinations.extend(profile.environment.fixed.keys().cloned());
2285+
credential_keys.extend(
2286+
profile
2287+
.credentials
2288+
.iter()
2289+
.flat_map(|credential| credential.accepted_stored_keys())
2290+
.map(str::to_string),
2291+
);
2292+
credential_keys.extend(broker_only_provider_credential_keys(&profile.to_proto()));
2293+
}
2294+
}
2295+
destinations
2296+
.into_iter()
2297+
.filter(|key| is_valid_env_key(key) && !credential_keys.contains(key))
2298+
.filter_map(|key| {
2299+
let value = spec.environment.get(&key).or_else(|| {
2300+
spec.template
2301+
.as_ref()
2302+
.and_then(|template| template.environment.get(&key))
2303+
})?;
2304+
Some((key, value.clone()))
2305+
})
2306+
.collect()
2307+
}
2308+
22582309
fn provider_profile_environment_values(
22592310
catalog: &EffectiveProviderProfileCatalog,
22602311
provider: &Provider,

‎docs/how-it-works/providers/google.mdx‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,13 @@ as placeholders for proxy-time resolution.
175175
| `CLOUD_ML_REGION` | from `region` config | GCP region |
176176
| `GCP_LOCATION` | from `region` config | Alternative region var |
177177

178+
For each non-secret variable, a sandbox create-time value overrides its template
179+
value, which overrides the provider default. The metadata project-ID endpoint
180+
uses the effective `GCP_PROJECT_ID`, including an explicitly empty value. A caller
181+
can supply it even when the provider has no `project_id` config. OpenShell resolves
182+
aliases independently; set both `GCP_PROJECT_ID` and `GOOGLE_CLOUD_PROJECT` when
183+
overriding the project so SDKs that read different variables agree.
184+
178185
## Using with GCP APIs
179186

180187
The metadata emulator serves tokens with the `cloud-platform` OAuth2 scope,

0 commit comments

Comments
 (0)