Skip to content

Commit 3b4dee2

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 004e05a commit 3b4dee2

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)>;
@@ -586,7 +585,6 @@ impl ProviderCredentialState {
586585
}
587586
}
588587

589-
590588
/// Whether every credential this connection injected is still at the
591589
/// version it injected. True for a connection that injected nothing;
592590
/// false for an untracked handle, which cannot tell.
@@ -4268,7 +4266,10 @@ mod tests {
42684266
#[test]
42694267
fn tracked_resolver_records_the_key_and_version_of_each_secret_sent() {
42704268
let tracked = token_state(10, &[("GITHUB_TOKEN", "secret")]).with_use_tracking();
4271-
assert!(tracked.credential_uses().unwrap().is_empty(), "nothing injected yet");
4269+
assert!(
4270+
tracked.credential_uses().unwrap().is_empty(),
4271+
"nothing injected yet"
4272+
);
42724273

42734274
assert_eq!(inject_static(&tracked, "GITHUB_TOKEN"), "secret");
42744275

@@ -4305,24 +4306,35 @@ mod tests {
43054306

43064307
#[test]
43074308
fn uses_stay_current_until_a_credential_the_connection_used_changes() {
4308-
let tracked = token_state(10, &[("GITHUB_TOKEN", "a"), ("OTHER_TOKEN", "b")])
4309-
.with_use_tracking();
4309+
let tracked =
4310+
token_state(10, &[("GITHUB_TOKEN", "a"), ("OTHER_TOKEN", "b")]).with_use_tracking();
43104311
inject_static(&tracked, "GITHUB_TOKEN");
43114312
assert!(tracked.credential_uses_current());
43124313

43134314
// Another provider's credential rotates; this connection never sent it.
4314-
reinstall_tokens(&tracked, 11, &[("GITHUB_TOKEN", "a"), ("OTHER_TOKEN", "b2")]);
4315-
assert!(tracked.credential_uses_current(), "unused credentials don't matter");
4315+
reinstall_tokens(
4316+
&tracked,
4317+
11,
4318+
&[("GITHUB_TOKEN", "a"), ("OTHER_TOKEN", "b2")],
4319+
);
4320+
assert!(
4321+
tracked.credential_uses_current(),
4322+
"unused credentials don't matter"
4323+
);
43164324

43174325
// The credential it sent rotates.
4318-
reinstall_tokens(&tracked, 12, &[("GITHUB_TOKEN", "a2"), ("OTHER_TOKEN", "b2")]);
4326+
reinstall_tokens(
4327+
&tracked,
4328+
12,
4329+
&[("GITHUB_TOKEN", "a2"), ("OTHER_TOKEN", "b2")],
4330+
);
43194331
assert!(!tracked.credential_uses_current());
43204332
}
43214333

43224334
#[test]
43234335
fn removing_a_used_credential_is_a_change() {
4324-
let tracked = token_state(10, &[("GITHUB_TOKEN", "a"), ("OTHER_TOKEN", "b")])
4325-
.with_use_tracking();
4336+
let tracked =
4337+
token_state(10, &[("GITHUB_TOKEN", "a"), ("OTHER_TOKEN", "b")]).with_use_tracking();
43264338
inject_static(&tracked, "GITHUB_TOKEN");
43274339

43284340
// Its provider is detached.
@@ -4349,7 +4361,11 @@ mod tests {
43494361
tracked.record_grant_use(malformed);
43504362

43514363
assert_eq!(
4352-
tracked.credential_uses().unwrap().grant_versions.get(malformed),
4364+
tracked
4365+
.credential_uses()
4366+
.unwrap()
4367+
.grant_versions
4368+
.get(malformed),
43534369
Some(&STALE_CREDENTIAL_VERSION)
43544370
);
43554371
assert!(
@@ -4408,11 +4424,20 @@ mod tests {
44084424
HashMap::new(),
44094425
HashMap::new(),
44104426
HashMap::from([
4411-
(API_GRANT.to_string(), grant_credential("https://idp.example.com/token")),
4412-
(OTHER_GRANT.to_string(), grant_credential("https://other-idp.example.com/token")),
4427+
(
4428+
API_GRANT.to_string(),
4429+
grant_credential("https://idp.example.com/token"),
4430+
),
4431+
(
4432+
OTHER_GRANT.to_string(),
4433+
grant_credential("https://other-idp.example.com/token"),
4434+
),
44134435
]),
44144436
);
4415-
assert!(tracked.credential_uses_current(), "only the used grant matters");
4437+
assert!(
4438+
tracked.credential_uses_current(),
4439+
"only the used grant matters"
4440+
);
44164441

44174442
// The used grant changes.
44184443
tracked.install_environment(
@@ -4484,14 +4509,26 @@ mod tests {
44844509
assert_ne!(github, other, "versions come from one counter");
44854510

44864511
reinstall_tokens(&state, 11, &[("GITHUB_TOKEN", "a"), ("OTHER_TOKEN", "b2")]);
4487-
assert_eq!(state.static_credential_version("GITHUB_TOKEN"), Some(github));
4512+
assert_eq!(
4513+
state.static_credential_version("GITHUB_TOKEN"),
4514+
Some(github)
4515+
);
44884516
let other_rotated = state.static_credential_version("OTHER_TOKEN").unwrap();
4489-
assert!(other_rotated > other, "a changed value gets a newer version");
4517+
assert!(
4518+
other_rotated > other,
4519+
"a changed value gets a newer version"
4520+
);
44904521

44914522
// Reinstalling identical content changes no version.
44924523
reinstall_tokens(&state, 12, &[("GITHUB_TOKEN", "a"), ("OTHER_TOKEN", "b2")]);
4493-
assert_eq!(state.static_credential_version("GITHUB_TOKEN"), Some(github));
4494-
assert_eq!(state.static_credential_version("OTHER_TOKEN"), Some(other_rotated));
4524+
assert_eq!(
4525+
state.static_credential_version("GITHUB_TOKEN"),
4526+
Some(github)
4527+
);
4528+
assert_eq!(
4529+
state.static_credential_version("OTHER_TOKEN"),
4530+
Some(other_rotated)
4531+
);
44954532
}
44964533

44974534
#[test]
@@ -4541,11 +4578,18 @@ mod tests {
45414578
// Revisions are sandbox-wide: another credential changing moves every
45424579
// placeholder to the new revision, but this one still sends the
45434580
// current value.
4544-
let tracked = token_state(10, &[("GITHUB_TOKEN", "same"), ("OTHER_TOKEN", "a")])
4545-
.with_use_tracking();
4581+
let tracked =
4582+
token_state(10, &[("GITHUB_TOKEN", "same"), ("OTHER_TOKEN", "a")]).with_use_tracking();
45464583
let old_placeholder = tracked.snapshot().child_env["GITHUB_TOKEN"].clone();
4547-
reinstall_tokens(&tracked, 11, &[("GITHUB_TOKEN", "same"), ("OTHER_TOKEN", "b")]);
4548-
assert_ne!(tracked.snapshot().child_env["GITHUB_TOKEN"], old_placeholder);
4584+
reinstall_tokens(
4585+
&tracked,
4586+
11,
4587+
&[("GITHUB_TOKEN", "same"), ("OTHER_TOKEN", "b")],
4588+
);
4589+
assert_ne!(
4590+
tracked.snapshot().child_env["GITHUB_TOKEN"],
4591+
old_placeholder
4592+
);
45494593

45504594
let resolver = tracked
45514595
.resolver_for_endpoint("api.github.com", 443, "/")
@@ -4642,7 +4686,10 @@ mod tests {
46424686
grant_credential("https://idp.example.com/token"),
46434687
)]),
46444688
);
4645-
assert_eq!(state.grant_credential_version("api:access_token"), Some(version));
4689+
assert_eq!(
4690+
state.grant_credential_version("api:access_token"),
4691+
Some(version)
4692+
);
46464693

46474694
state.install_environment(
46484695
12,
@@ -4653,7 +4700,10 @@ mod tests {
46534700
grant_credential("https://new-idp.example.com/token"),
46544701
)]),
46554702
);
4656-
assert_ne!(state.grant_credential_version("api:access_token"), Some(version));
4703+
assert_ne!(
4704+
state.grant_credential_version("api:access_token"),
4705+
Some(version)
4706+
);
46574707
}
46584708

46594709
// --- Installed credential versions ---
@@ -4689,7 +4739,11 @@ mod tests {
46894739
assert!(!added.same_credentials(&installed));
46904740

46914741
reinstall_tokens(&state, 12, &[("GITHUB_TOKEN", "a")]);
4692-
assert!(!state.installed_credential_versions().same_credentials(&added));
4742+
assert!(
4743+
!state
4744+
.installed_credential_versions()
4745+
.same_credentials(&added)
4746+
);
46934747
}
46944748

46954749
#[test]
@@ -4702,7 +4756,11 @@ mod tests {
47024756
assert!(!revoked.same_credentials(&installed));
47034757

47044758
state.revoke_static_provider_environment(12);
4705-
assert!(state.installed_credential_versions().same_credentials(&revoked));
4759+
assert!(
4760+
state
4761+
.installed_credential_versions()
4762+
.same_credentials(&revoked)
4763+
);
47064764
}
47074765

47084766
#[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)