Skip to content

Commit 0c01687

Browse files
committed
fix(network): adapt relay tests to current process identity
Signed-off-by: John Myers <9696606+johntmyers@users.noreply.github.com>
1 parent a3984b1 commit 0c01687

2 files changed

Lines changed: 122 additions & 35 deletions

File tree

‎crates/openshell-core/src/provider_credentials.rs‎

Lines changed: 84 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ impl InstalledCredentialVersions {
118118
}
119119
}
120120

121-
122121
/// Token-grant dynamic credentials sharing one `provider:credential`
123122
/// identity, keyed by their raw dynamic credential key and sorted.
124123
type GrantConfigs = Vec<(String, crate::proto::ProviderProfileCredential)>;
@@ -584,7 +583,6 @@ impl ProviderCredentialState {
584583
}
585584
}
586585

587-
588586
/// Whether every credential this connection injected is still at the
589587
/// version it injected. True for a connection that injected nothing;
590588
/// false for an untracked handle, which cannot tell.
@@ -4352,7 +4350,10 @@ mod tests {
43524350
#[test]
43534351
fn tracked_resolver_records_the_key_and_version_of_each_secret_sent() {
43544352
let tracked = token_state(10, &[("GITHUB_TOKEN", "secret")]).with_use_tracking();
4355-
assert!(tracked.credential_uses().unwrap().is_empty(), "nothing injected yet");
4353+
assert!(
4354+
tracked.credential_uses().unwrap().is_empty(),
4355+
"nothing injected yet"
4356+
);
43564357

43574358
assert_eq!(inject_static(&tracked, "GITHUB_TOKEN"), "secret");
43584359

@@ -4389,24 +4390,35 @@ mod tests {
43894390

43904391
#[test]
43914392
fn uses_stay_current_until_a_credential_the_connection_used_changes() {
4392-
let tracked = token_state(10, &[("GITHUB_TOKEN", "a"), ("OTHER_TOKEN", "b")])
4393-
.with_use_tracking();
4393+
let tracked =
4394+
token_state(10, &[("GITHUB_TOKEN", "a"), ("OTHER_TOKEN", "b")]).with_use_tracking();
43944395
inject_static(&tracked, "GITHUB_TOKEN");
43954396
assert!(tracked.credential_uses_current());
43964397

43974398
// Another provider's credential rotates; this connection never sent it.
4398-
reinstall_tokens(&tracked, 11, &[("GITHUB_TOKEN", "a"), ("OTHER_TOKEN", "b2")]);
4399-
assert!(tracked.credential_uses_current(), "unused credentials don't matter");
4399+
reinstall_tokens(
4400+
&tracked,
4401+
11,
4402+
&[("GITHUB_TOKEN", "a"), ("OTHER_TOKEN", "b2")],
4403+
);
4404+
assert!(
4405+
tracked.credential_uses_current(),
4406+
"unused credentials don't matter"
4407+
);
44004408

44014409
// The credential it sent rotates.
4402-
reinstall_tokens(&tracked, 12, &[("GITHUB_TOKEN", "a2"), ("OTHER_TOKEN", "b2")]);
4410+
reinstall_tokens(
4411+
&tracked,
4412+
12,
4413+
&[("GITHUB_TOKEN", "a2"), ("OTHER_TOKEN", "b2")],
4414+
);
44034415
assert!(!tracked.credential_uses_current());
44044416
}
44054417

44064418
#[test]
44074419
fn removing_a_used_credential_is_a_change() {
4408-
let tracked = token_state(10, &[("GITHUB_TOKEN", "a"), ("OTHER_TOKEN", "b")])
4409-
.with_use_tracking();
4420+
let tracked =
4421+
token_state(10, &[("GITHUB_TOKEN", "a"), ("OTHER_TOKEN", "b")]).with_use_tracking();
44104422
inject_static(&tracked, "GITHUB_TOKEN");
44114423

44124424
// Its provider is detached.
@@ -4433,7 +4445,11 @@ mod tests {
44334445
tracked.record_grant_use(malformed);
44344446

44354447
assert_eq!(
4436-
tracked.credential_uses().unwrap().grant_versions.get(malformed),
4448+
tracked
4449+
.credential_uses()
4450+
.unwrap()
4451+
.grant_versions
4452+
.get(malformed),
44374453
Some(&STALE_CREDENTIAL_VERSION)
44384454
);
44394455
assert!(
@@ -4492,11 +4508,20 @@ mod tests {
44924508
HashMap::new(),
44934509
HashMap::new(),
44944510
HashMap::from([
4495-
(API_GRANT.to_string(), grant_credential("https://idp.example.com/token")),
4496-
(OTHER_GRANT.to_string(), grant_credential("https://other-idp.example.com/token")),
4511+
(
4512+
API_GRANT.to_string(),
4513+
grant_credential("https://idp.example.com/token"),
4514+
),
4515+
(
4516+
OTHER_GRANT.to_string(),
4517+
grant_credential("https://other-idp.example.com/token"),
4518+
),
44974519
]),
44984520
);
4499-
assert!(tracked.credential_uses_current(), "only the used grant matters");
4521+
assert!(
4522+
tracked.credential_uses_current(),
4523+
"only the used grant matters"
4524+
);
45004525

45014526
// The used grant changes.
45024527
tracked.install_environment(
@@ -4568,14 +4593,26 @@ mod tests {
45684593
assert_ne!(github, other, "versions come from one counter");
45694594

45704595
reinstall_tokens(&state, 11, &[("GITHUB_TOKEN", "a"), ("OTHER_TOKEN", "b2")]);
4571-
assert_eq!(state.static_credential_version("GITHUB_TOKEN"), Some(github));
4596+
assert_eq!(
4597+
state.static_credential_version("GITHUB_TOKEN"),
4598+
Some(github)
4599+
);
45724600
let other_rotated = state.static_credential_version("OTHER_TOKEN").unwrap();
4573-
assert!(other_rotated > other, "a changed value gets a newer version");
4601+
assert!(
4602+
other_rotated > other,
4603+
"a changed value gets a newer version"
4604+
);
45744605

45754606
// Reinstalling identical content changes no version.
45764607
reinstall_tokens(&state, 12, &[("GITHUB_TOKEN", "a"), ("OTHER_TOKEN", "b2")]);
4577-
assert_eq!(state.static_credential_version("GITHUB_TOKEN"), Some(github));
4578-
assert_eq!(state.static_credential_version("OTHER_TOKEN"), Some(other_rotated));
4608+
assert_eq!(
4609+
state.static_credential_version("GITHUB_TOKEN"),
4610+
Some(github)
4611+
);
4612+
assert_eq!(
4613+
state.static_credential_version("OTHER_TOKEN"),
4614+
Some(other_rotated)
4615+
);
45794616
}
45804617

45814618
#[test]
@@ -4625,11 +4662,18 @@ mod tests {
46254662
// Revisions are sandbox-wide: another credential changing moves every
46264663
// placeholder to the new revision, but this one still sends the
46274664
// current value.
4628-
let tracked = token_state(10, &[("GITHUB_TOKEN", "same"), ("OTHER_TOKEN", "a")])
4629-
.with_use_tracking();
4665+
let tracked =
4666+
token_state(10, &[("GITHUB_TOKEN", "same"), ("OTHER_TOKEN", "a")]).with_use_tracking();
46304667
let old_placeholder = tracked.snapshot().child_env["GITHUB_TOKEN"].clone();
4631-
reinstall_tokens(&tracked, 11, &[("GITHUB_TOKEN", "same"), ("OTHER_TOKEN", "b")]);
4632-
assert_ne!(tracked.snapshot().child_env["GITHUB_TOKEN"], old_placeholder);
4668+
reinstall_tokens(
4669+
&tracked,
4670+
11,
4671+
&[("GITHUB_TOKEN", "same"), ("OTHER_TOKEN", "b")],
4672+
);
4673+
assert_ne!(
4674+
tracked.snapshot().child_env["GITHUB_TOKEN"],
4675+
old_placeholder
4676+
);
46334677

46344678
let resolver = tracked
46354679
.resolver_for_endpoint("api.github.com", 443, "/")
@@ -4726,7 +4770,10 @@ mod tests {
47264770
grant_credential("https://idp.example.com/token"),
47274771
)]),
47284772
);
4729-
assert_eq!(state.grant_credential_version("api:access_token"), Some(version));
4773+
assert_eq!(
4774+
state.grant_credential_version("api:access_token"),
4775+
Some(version)
4776+
);
47304777

47314778
state.install_environment(
47324779
12,
@@ -4737,7 +4784,10 @@ mod tests {
47374784
grant_credential("https://new-idp.example.com/token"),
47384785
)]),
47394786
);
4740-
assert_ne!(state.grant_credential_version("api:access_token"), Some(version));
4787+
assert_ne!(
4788+
state.grant_credential_version("api:access_token"),
4789+
Some(version)
4790+
);
47414791
}
47424792

47434793
// --- Installed credential versions ---
@@ -4773,7 +4823,11 @@ mod tests {
47734823
assert!(!added.same_credentials(&installed));
47744824

47754825
reinstall_tokens(&state, 12, &[("GITHUB_TOKEN", "a")]);
4776-
assert!(!state.installed_credential_versions().same_credentials(&added));
4826+
assert!(
4827+
!state
4828+
.installed_credential_versions()
4829+
.same_credentials(&added)
4830+
);
47774831
}
47784832

47794833
#[test]
@@ -4786,7 +4840,11 @@ mod tests {
47864840
assert!(!revoked.same_credentials(&installed));
47874841

47884842
state.revoke_static_provider_environment(12);
4789-
assert!(state.installed_credential_versions().same_credentials(&revoked));
4843+
assert!(
4844+
state
4845+
.installed_credential_versions()
4846+
.same_credentials(&revoked)
4847+
);
47904848
}
47914849

47924850
#[test]

‎crates/openshell-supervisor-network/src/proxy/relay.rs‎

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,7 @@ network_policies:
843843
endpoint: EndpointDecision::default(),
844844
binary: Some(std::path::PathBuf::from("/usr/bin/curl")),
845845
binary_pid: None,
846+
process: None,
846847
ancestors: vec![],
847848
cmdline_paths: vec![],
848849
};
@@ -1019,8 +1020,14 @@ network_policies:
10191020
.reload(POLICY_REGO, CURL_EXAMPLE_AND_OTHER_POLICY)
10201021
.unwrap();
10211022

1022-
assert!(!generation_guard.is_stale(), "approving another host must not close this relay");
1023-
assert_eq!(generation_guard.captured_generation(), engine.current_generation());
1023+
assert!(
1024+
!generation_guard.is_stale(),
1025+
"approving another host must not close this relay"
1026+
);
1027+
assert_eq!(
1028+
generation_guard.captured_generation(),
1029+
engine.current_generation()
1030+
);
10241031
}
10251032

10261033
/// Approving an endpoint for another host leaves an inspected (L7)
@@ -1194,7 +1201,11 @@ network_policies:
11941201
!guard.is_stale(),
11951202
"a connection that never carried credentials cannot hold a stale one"
11961203
);
1197-
let uses = request.provider_credentials.as_ref().unwrap().credential_uses();
1204+
let uses = request
1205+
.provider_credentials
1206+
.as_ref()
1207+
.unwrap()
1208+
.credential_uses();
11981209
assert!(uses.expect("http_context tracks credential use").is_empty());
11991210
}
12001211

@@ -1211,7 +1222,10 @@ network_policies:
12111222
inject_static_credential(&request, "API_TOKEN");
12121223
change_provider_credentials(&engine, &credentials, 2, &[("API_TOKEN", "a2")]);
12131224

1214-
assert!(guard.is_stale(), "the upstream session may hold the old credential");
1225+
assert!(
1226+
guard.is_stale(),
1227+
"the upstream session may hold the old credential"
1228+
);
12151229
}
12161230

12171231
/// Rotating another provider's credential, or attaching a new
@@ -1232,12 +1246,19 @@ network_policies:
12321246
2,
12331247
&[("API_TOKEN", "a"), ("OTHER_TOKEN", "b2")],
12341248
);
1235-
assert!(!guard.is_stale(), "OTHER_TOKEN was never sent on this connection");
1249+
assert!(
1250+
!guard.is_stale(),
1251+
"OTHER_TOKEN was never sent on this connection"
1252+
);
12361253
change_provider_credentials(
12371254
&engine,
12381255
&credentials,
12391256
3,
1240-
&[("API_TOKEN", "a"), ("OTHER_TOKEN", "b2"), ("NEW_TOKEN", "c")],
1257+
&[
1258+
("API_TOKEN", "a"),
1259+
("OTHER_TOKEN", "b2"),
1260+
("NEW_TOKEN", "c"),
1261+
],
12411262
);
12421263
assert!(!guard.is_stale());
12431264
}
@@ -1823,7 +1844,9 @@ network_policies:
18231844
.reload(POLICY_REGO, CURL_EXAMPLE_REST_NARROWED_POLICY)
18241845
.unwrap();
18251846
assert!(evaluator.is_stale());
1826-
engine.reload(POLICY_REGO, CURL_EXAMPLE_REST_POLICY).unwrap();
1847+
engine
1848+
.reload(POLICY_REGO, CURL_EXAMPLE_REST_POLICY)
1849+
.unwrap();
18271850

18281851
assert!(evaluator.is_stale());
18291852
}
@@ -2174,7 +2197,10 @@ network_policies:
21742197

21752198
let outcome = mcp_status_across_reload(None, Some(upcoming)).await;
21762199

2177-
assert!(outcome.second_forwarded, "nothing changed for the running tunnel");
2200+
assert!(
2201+
outcome.second_forwarded,
2202+
"nothing changed for the running tunnel"
2203+
);
21782204
assert_eq!(outcome.second_reported, None);
21792205
}
21802206

@@ -2387,7 +2413,10 @@ network_policies:
23872413
2,
23882414
&[("API_TOKEN", "a"), ("OTHER_TOKEN", "b2")],
23892415
);
2390-
assert_ne!(credentials.snapshot().child_env["API_TOKEN"], old_placeholder);
2416+
assert_ne!(
2417+
credentials.snapshot().child_env["API_TOKEN"],
2418+
old_placeholder
2419+
);
23912420
let decision = authorized_decision(&engine);
23922421
let request = connection_context(&decision, &credentials);
23932422
let guard = http_relay_guard(&engine, &decision, &request);

0 commit comments

Comments
 (0)