Feat: Add api_token field to client configuration#514
Merged
declark1 merged 2 commits intofoundation-model-stack:mainfrom Nov 17, 2025
Merged
Feat: Add api_token field to client configuration#514declark1 merged 2 commits intofoundation-model-stack:mainfrom
declark1 merged 2 commits intofoundation-model-stack:mainfrom
Conversation
5f0213b to
daca788
Compare
declark1
reviewed
Nov 10, 2025
Contributor
There was a problem hiding this comment.
Assuming token values do not change during runtime, I would suggest reading the env var once at startup so that invalid values (env var does not exist) for api_key are caught when the config is deserialized.
A helper function can be used with serde's deserialize_with attribute for this. Here is one that can be added to utils.rs if you all want to do this.
// utils.rs
/// Serde helper to deserialize value from environment variable.
pub fn from_env<'de, D>(deserializer: D) -> Result<Option<String>, D::Error>
where
D: Deserializer<'de>,
{
let env_name: Option<String> = Option::deserialize(deserializer)?;
if let Some(env_name) = env_name {
let value = std::env::var(&env_name)
.map_err(|_| serde::de::Error::custom(format!("env var `{env_name}` not found")))?;
Ok(Some(value))
} else {
Ok(None)
}
}
#[cfg(test)]
mod tests {
use serde::Deserialize;
use serde_json::json;
use super::from_env;
#[derive(Debug, Deserialize)]
pub struct Config {
#[serde(default, deserialize_with = "from_env")]
pub api_token: Option<String>,
}
#[test]
fn test_from_env() -> Result<(), Box<dyn std::error::Error>> {
// Test no value
let config: Config = serde_json::from_value(json!({}))?;
assert_eq!(config.api_token, None);
// Test invalid value
let config: Result<Config, serde_json::error::Error> = serde_json::from_value(json!({
"api_token": "DOES_NOT_EXIST"
}));
assert!(config.is_err_and(|err| err.to_string() == "env var `DOES_NOT_EXIST` not found"));
// Test valid value
unsafe {
std::env::set_var("CLIENT_API_TOKEN", "token");
}
let config: Config = serde_json::from_value(json!({
"api_token": "CLIENT_API_TOKEN"
}))?;
assert_eq!(config.api_token, Some("token".into()));
Ok(())
}
}To use with ServiceConfig, just import crate::utils::from_env and add the attribute, e.g.
pub struct ServiceConfig {
// ...
#[serde(default, deserialize_with = "from_env")]
pub api_token: Option<String>,
}HttpClient::inject_api_token() would then just use self.api_token instead of reading via std::env::var().
…om env vars Signed-off-by: Rob Geada <rob@geada.net>
daca788 to
6712b22
Compare
Signed-off-by: Rob Geada <rob@geada.net>
6712b22 to
f975ce9
Compare
Contributor
Author
|
@declark1 thanks for the review- I've incorporated your changes and re-verified that it all works as expected |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Introduces
api_tokenas a field to the client config struct:If set, the orchestrator will look for an environment variable matching the value, e.g.,
$MODEL_TOKEN. If it exists, it is injected as a bearer token in the HTTP request. In the example above,Authorization: Bearer $MODEL_TOKENwill be sent as a header in all requests to that client.This enables:
kube-rbac-proxy)Verification
Manual testing confirmed that this works as expected