claude-connector: a key is never lent (validate refuses auth api-key with any lease); the meter-fixture test reads at an injected instant
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
f595227320
commit
b968ada525
4 changed files with 101 additions and 4 deletions
|
|
@ -2202,7 +2202,16 @@ fn quota_of(rig: &Rig) -> model_connector::PlanQuotaSnapshot {
|
||||||
|
|
||||||
/// The windows with their observation stamps zeroed, for comparing readings.
|
/// The windows with their observation stamps zeroed, for comparing readings.
|
||||||
fn readings(rig: &Rig) -> Vec<model_connector::PlanQuota> {
|
fn readings(rig: &Rig) -> Vec<model_connector::PlanQuota> {
|
||||||
let mut windows = quota_of(rig).windows;
|
readings_at(rig, None)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The held windows as of `now` (unix ms), or as of the wall clock when none.
|
||||||
|
fn readings_at(rig: &Rig, now: Option<u64>) -> Vec<model_connector::PlanQuota> {
|
||||||
|
let cell = rig.backend.quota();
|
||||||
|
let mut windows = match now {
|
||||||
|
Some(now) => cell.snapshot_at(now).windows,
|
||||||
|
None => cell.snapshot().windows,
|
||||||
|
};
|
||||||
for window in &mut windows {
|
for window in &mut windows {
|
||||||
window.observed_at_unix_ms = 0;
|
window.observed_at_unix_ms = 0;
|
||||||
}
|
}
|
||||||
|
|
@ -2878,9 +2887,10 @@ fn the_meter_call_reads_the_week_from_a_real_captured_event() {
|
||||||
.read_meter()
|
.read_meter()
|
||||||
.expect("the captured call completes");
|
.expect("the captured call completes");
|
||||||
assert_eq!((usage.input_tokens, usage.output_tokens), (384, 4));
|
assert_eq!((usage.input_tokens, usage.output_tokens), (384, 4));
|
||||||
// 0.06 of the week used; it resets at 1794261600 (unix s).
|
// 0.06 of the week used; it resets at 1794261600 (unix s). Read as of an instant before
|
||||||
|
// that reset, so the captured event stays a live reading whatever the wall clock says.
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
readings(&rig),
|
readings_at(&rig, Some(1_794_261_600_000 - 1)),
|
||||||
vec![window("seven_day", 60, 1_794_261_600_000)]
|
vec![window("seven_day", 60, 1_794_261_600_000)]
|
||||||
);
|
);
|
||||||
let argv = rig.seen().argv;
|
let argv = rig.seen().argv;
|
||||||
|
|
|
||||||
|
|
@ -248,6 +248,13 @@ impl ClaudeConnectorConfig {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn validate_leases(&self) -> Result<(), ConfigError> {
|
fn validate_leases(&self) -> Result<(), ConfigError> {
|
||||||
|
// A key has no plan and is never lent: no lease socket, no lease row, whatever else the
|
||||||
|
// document says. Personas call with their own connection keys and need neither.
|
||||||
|
if self.auth == ClaudeAuth::ApiKey
|
||||||
|
&& (self.lease_socket.is_some() || !self.lease_callers.is_empty())
|
||||||
|
{
|
||||||
|
return Err(ConfigError::Invalid("auth"));
|
||||||
|
}
|
||||||
let Some(socket) = &self.lease_socket else {
|
let Some(socket) = &self.lease_socket else {
|
||||||
return if self.lease_callers.is_empty() {
|
return if self.lease_callers.is_empty() {
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
||||||
|
|
@ -155,7 +155,7 @@ impl QuotaCell {
|
||||||
/// The windows as of `now`. One whose reset time has passed is reported
|
/// The windows as of `now`. One whose reset time has passed is reported
|
||||||
/// `expired` with no utilization: the reading described a window that no
|
/// `expired` with no utilization: the reading described a window that no
|
||||||
/// longer exists.
|
/// longer exists.
|
||||||
fn snapshot_at(&self, now: u64) -> PlanQuotaSnapshot {
|
pub(crate) fn snapshot_at(&self, now: u64) -> PlanQuotaSnapshot {
|
||||||
let held = self.windows.lock().unwrap_or_else(|e| e.into_inner());
|
let held = self.windows.lock().unwrap_or_else(|e| e.into_inner());
|
||||||
PlanQuotaSnapshot {
|
PlanQuotaSnapshot {
|
||||||
windows: held
|
windows: held
|
||||||
|
|
|
||||||
|
|
@ -417,3 +417,83 @@ fn config_init_takes_the_auth_flag_and_refuses_an_unknown_one() {
|
||||||
assert_eq!(auth_of(&["--auth", "api-key"]), Some(ClaudeAuth::ApiKey));
|
assert_eq!(auth_of(&["--auth", "api-key"]), Some(ClaudeAuth::ApiKey));
|
||||||
assert_eq!(auth_of(&["--auth", "key"]), None);
|
assert_eq!(auth_of(&["--auth", "key"]), None);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The document an operator could author by hand around `write_config`'s refusal: a key and a lease.
|
||||||
|
fn keyed_with_a_lease() -> ClaudeConnectorConfig {
|
||||||
|
ClaudeConnectorConfig {
|
||||||
|
auth: ClaudeAuth::ApiKey,
|
||||||
|
..valid()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn a_key_is_never_lent_so_no_lease_socket_or_row_may_accompany_it() {
|
||||||
|
assert_eq!(refused(&keyed_with_a_lease()), "auth");
|
||||||
|
let socket_only = ClaudeConnectorConfig {
|
||||||
|
lease_callers: vec![],
|
||||||
|
..keyed_with_a_lease()
|
||||||
|
};
|
||||||
|
assert_eq!(refused(&socket_only), "auth");
|
||||||
|
let rows_only = ClaudeConnectorConfig {
|
||||||
|
lease_socket: None,
|
||||||
|
..keyed_with_a_lease()
|
||||||
|
};
|
||||||
|
assert_eq!(refused(&rows_only), "auth");
|
||||||
|
let personas_only = ClaudeConnectorConfig {
|
||||||
|
lease_socket: None,
|
||||||
|
lease_callers: vec![],
|
||||||
|
..keyed_with_a_lease()
|
||||||
|
};
|
||||||
|
assert!(personas_only.validate().is_ok());
|
||||||
|
let scratch = Scratch::new();
|
||||||
|
assert!(write_config(&scratch.path("keyed.cc"), &keyed_with_a_lease()).is_err());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn config_init_refuses_a_key_with_a_lease_and_accepts_it_without() {
|
||||||
|
let lease = [
|
||||||
|
"--lease-socket",
|
||||||
|
"/run/claude-connector/lease.sock",
|
||||||
|
"--lease-user",
|
||||||
|
"mind-a-self",
|
||||||
|
"--turn",
|
||||||
|
"self",
|
||||||
|
"--lease-unit-instance",
|
||||||
|
"a",
|
||||||
|
];
|
||||||
|
let scratch = Scratch::new();
|
||||||
|
let leased = init(&scratch, &init_flags(&scratch, &[&["--auth", "api-key"], &lease[..]].concat()));
|
||||||
|
assert!(!leased.status.success(), "{leased:?}");
|
||||||
|
assert!(!scratch.path("connector.cc").exists());
|
||||||
|
let scratch = Scratch::new();
|
||||||
|
let bare = init(&scratch, &init_flags(&scratch, &["--auth", "api-key"]));
|
||||||
|
assert!(bare.status.success(), "{bare:?}");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn serve_and_check_refuse_a_key_document_that_carries_a_lease() {
|
||||||
|
let scratch = Scratch::new();
|
||||||
|
let path = scratch.path("handmade.cc");
|
||||||
|
{
|
||||||
|
// Written around write_config, which would refuse it.
|
||||||
|
let mut cache = CultCache::new();
|
||||||
|
cache.register_entry_type::<ClaudeConnectorConfig>().unwrap();
|
||||||
|
cache
|
||||||
|
.add_generic_backing_store(SingleFileMessagePackBackingStore::new(&path))
|
||||||
|
.unwrap();
|
||||||
|
cache
|
||||||
|
.put("runtime".to_string(), &keyed_with_a_lease())
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
let p = path.to_str().unwrap();
|
||||||
|
for args in [["serve", "--config", p], ["config", "check", p, ""]] {
|
||||||
|
let args: Vec<&str> = args.into_iter().filter(|a| !a.is_empty()).collect();
|
||||||
|
let out = run(&args);
|
||||||
|
assert!(!out.status.success(), "{args:?}: {out:?}");
|
||||||
|
assert!(
|
||||||
|
String::from_utf8_lossy(&out.stderr).contains("auth"),
|
||||||
|
"{args:?}: {}",
|
||||||
|
String::from_utf8_lossy(&out.stderr)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue