Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions crates/graphicle/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1189,18 +1189,16 @@ where
return Err(Error::bad("Queue configuration exceeds limits"));
}
let queue: crate::distributed::Queue = serde_json::from_slice(&bytes)?;
let password = std::env::var(&queue.credential_ref).map_err(|_| {
Error::bad("Worker queue credential environment variable is unavailable")
})?;
let password = std::env::var(&queue.credential_ref)
.ok()
.filter(|value| !value.is_empty())
.ok_or_else(|| {
Error::bad("Worker queue credential environment variable is unavailable")
})?;
let config = model::Config::load(cli.config.as_deref())?;
let (_, profile) = config.profile(model_profile.as_deref())?;
let credential = profile
.key_env
.as_ref()
.map(|name| {
std::env::var(name).map_err(|_| Error::bad("Worker model credential unavailable"))
})
.transpose()?;
let credential = model::environment_credential(profile)
.map_err(|_| Error::bad("Worker model credential unavailable"))?;
let result = tokio::select! {
result=crate::distributed::consume(&queue,&password,profile,credential.as_deref(),*once)=>result?,
_=tokio::signal::ctrl_c()=>json!({"stopped":true})
Expand Down
1 change: 1 addition & 0 deletions crates/graphicle/tests/it/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ mod query_graph_source;
mod query_variants;
mod read_only;
mod receipts;
mod redis_worker;
mod release_0_4;
mod rerank_depth;
mod retention;
Expand Down
63 changes: 63 additions & 0 deletions crates/graphicle/tests/it/redis_worker.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//! `graphicle redis-worker` refuses an empty queue password or model key before it connects.
use serde_json::json;
use std::process::{Command, Output, Stdio};
use tempfile::TempDir;

#[test]
fn redis_worker_rejects_empty_credentials_before_connecting() {
const KEY: &str = "GRAPHICLE_TEST_WORKER_KEY";
const QUEUE_PASSWORD: &str = "GRAPHICLE_TEST_WORKER_QUEUE_PASSWORD";
const QUEUE_UNAVAILABLE: &str = "Worker queue credential environment variable is unavailable";
const MODEL_UNAVAILABLE: &str = "Worker model credential unavailable";
let dir = TempDir::new().unwrap();
let models = dir.path().join("models.json");
let queue = dir.path().join("queue.json");
std::fs::write(
&models,
json!({"default_model":"m","models":{"m":{"protocol":"anthropic","base_url":"https://api.anthropic.com/v1","model":"fixture","input_limit":16000,"output_limit":2048,"key_env":KEY}}}).to_string(),
)
.unwrap();
std::fs::write(
&queue,
json!({"endpoint":"redis://127.0.0.1:1","namespace":"fixture","credential_ref":QUEUE_PASSWORD}).to_string(),
)
.unwrap();
let run = |queue_password: &str, key: &str| -> Output {
let mut command = Command::new(env!("CARGO_BIN_EXE_graphicle"));
command
.arg("--config")
.arg(&models)
.arg("redis-worker")
.arg("--queue-config")
.arg(&queue)
.arg("--once")
.env_remove("GRAPHICLE_DATA_DIR")
.env(KEY, key)
.env(QUEUE_PASSWORD, queue_password)
.stdin(Stdio::null())
.stdout(Stdio::piped())
.stderr(Stdio::piped());
crate::fork_guard::spawn(&mut command)
.unwrap()
.wait_with_output()
.unwrap()
};

for (output, expected) in [
(run("", "fixture-key"), QUEUE_UNAVAILABLE),
(run("fixture-password", ""), MODEL_UNAVAILABLE),
] {
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(!output.status.success(), "accepted empty credential");
assert!(stderr.contains(expected), "{stderr}");
}

let present = run("fixture-password", "fixture-key");
let stderr = String::from_utf8_lossy(&present.stderr);
assert!(
!present.status.success(),
"connected to the fixture Redis port"
);
assert!(!stderr.contains(QUEUE_UNAVAILABLE), "{stderr}");
assert!(!stderr.contains(MODEL_UNAVAILABLE), "{stderr}");
}
Loading