1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
//! Per-person gateway keys + request identity tags (enterprise#11).
//!
//! `gateway-keys.toml` maps SHA-256 hashes of bearer keys to an identity
//! (person, optional team, optional default project), so an org gateway can
//! meter usage per person/project without the clients sharing one token:
//!
//! ```toml
//! [[keys]]
//! sha256_hex = "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"
//! person = "yves"
//! team = "platform"
//! default_project = "ai-gateway"
//! ```
//!
//! Only the hash is ever stored; the plaintext key lives with the person. The file
//! path resolves via `LEAN_CTX_GATEWAY_KEYS`, falling back to
//! `<config_dir>/gateway-keys.toml` — deployments mount it as a secret.
//!
//! A caller may override the project per request with the `x-leanctx-project`
//! header (an internal gateway header: it is deliberately not on
//! `ALLOWED_REQUEST_HEADERS`, so it never leaks upstream).
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use serde::Deserialize;
/// The identity tags attached to an authenticated gateway request. Inserted as
/// a request extension by the auth guard and stamped onto the usage record by
/// the forward path.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct GatewayTags {
pub person: Option<String>,
pub team: Option<String>,
pub project: Option<String>,
}
/// Internal trust marker set only after the proxy auth guard accepts a
/// gateway-owned credential (or explicit loopback-open mode). Provider API-key
/// fallback must not be able to manufacture managed OCLA lineage headers.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub(crate) struct TrustedGatewayRequest;
impl GatewayTags {
/// True when there is anything worth stamping.
#[must_use]
pub fn is_empty(&self) -> bool {
self.person.is_none() && self.team.is_none() && self.project.is_none()
}
}
#[derive(Debug, Deserialize)]
struct GatewayKeysFile {
#[serde(default)]
keys: Vec<GatewayKeyEntry>,
}
#[derive(Debug, Deserialize)]
struct GatewayKeyEntry {
/// Lowercase hex SHA-256 of the bearer key (never the key itself).
sha256_hex: String,
person: String,
#[serde(default)]
team: Option<String>,
#[serde(default)]
default_project: Option<String>,
}
/// Loaded, lookup-ready key set. One instance per proxy process, loaded at
/// startup (key rotation = redeploy/restart, the standard secret-mount flow).
#[derive(Debug, Default)]
pub struct GatewayKeys {
by_sha: HashMap<String, GatewayTags>,
}
impl GatewayKeys {
/// Resolve the keys file path: `LEAN_CTX_GATEWAY_KEYS` env wins, else
/// `<config_dir>/gateway-keys.toml` (next to `config.toml`).
#[must_use]
pub fn default_path() -> PathBuf {
std::env::var("LEAN_CTX_GATEWAY_KEYS").ok().map_or_else(
|| {
crate::core::paths::config_dir().map_or_else(
|_| PathBuf::from("gateway-keys.toml"),
|d| d.join("gateway-keys.toml"),
)
},
PathBuf::from,
)
}
/// Load from the default path; a missing file is an empty key set (the
/// common local case), a malformed file is a loud startup error.
pub fn load_default() -> anyhow::Result<Self> {
Self::load(&Self::default_path())
}
pub fn load(path: &Path) -> anyhow::Result<Self> {
if !path.exists() {
return Ok(Self::default());
}
let raw = std::fs::read_to_string(path)
.map_err(|e| anyhow::anyhow!("read {}: {e}", path.display()))?;
Self::parse(&raw, path)
}
/// Parses a key-file body without touching disk. `origin` is only used in
/// error messages. Callers that assemble a file body by hand validate it
/// through this BEFORE the atomic write, so an invalid assembly can never
/// replace a good file on disk (#716).
pub fn parse(raw: &str, origin: &Path) -> anyhow::Result<Self> {
let file: GatewayKeysFile =
toml::from_str(raw).map_err(|e| anyhow::anyhow!("parse {}: {e}", origin.display()))?;
let mut by_sha = HashMap::new();
for entry in file.keys {
let sha = entry.sha256_hex.trim().to_ascii_lowercase();
if sha.len() != 64 || !sha.bytes().all(|b| b.is_ascii_hexdigit()) {
anyhow::bail!(
"{}: key for '{}' has invalid sha256_hex (expected 64 hex chars)",
origin.display(),
entry.person
);
}
let person = entry.person.trim();
if person.is_empty() {
anyhow::bail!("{}: entry with empty person", origin.display());
}
by_sha.insert(
sha,
GatewayTags {
person: Some(person.to_string()),
team: entry
.team
.as_deref()
.map(str::trim)
.filter(|t| !t.is_empty())
.map(str::to_string),
project: entry
.default_project
.as_deref()
.map(str::trim)
.filter(|p| !p.is_empty())
.map(str::to_string),
},
);
}
Ok(Self { by_sha })
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.by_sha.is_empty()
}
#[must_use]
pub fn len(&self) -> usize {
self.by_sha.len()
}
/// Authenticate a bearer key: SHA-256 it and look the hash up. Returns the
/// identity tags on a match.
#[must_use]
pub fn lookup(&self, bearer_key: &str) -> Option<GatewayTags> {
self.by_sha.get(&sha256_hex(bearer_key)).cloned()
}
}
/// Re-read gateway keys from their configured file path. Callers can replace
/// their current snapshot with the result to apply a rotated secret.
pub fn reload_keys() -> anyhow::Result<GatewayKeys> {
GatewayKeys::load_default()
}
/// Lowercase hex SHA-256 (the storage form of every gateway key).
#[must_use]
pub fn sha256_hex(input: &str) -> String {
use sha2::{Digest, Sha256};
let mut h = Sha256::new();
h.update(input.as_bytes());
let digest = h.finalize();
let mut out = String::with_capacity(digest.len() * 2);
for b in digest {
use std::fmt::Write;
let _ = write!(out, "{b:02x}");
}
out
}
#[cfg(test)]
mod tests {
use super::*;
fn write_keys(dir: &Path, body: &str) -> PathBuf {
let path = dir.join("gateway-keys.toml");
std::fs::write(&path, body).unwrap();
path
}
#[test]
fn lookup_maps_key_hash_to_identity() {
let tmp = tempfile::tempdir().unwrap();
let sha = sha256_hex("gk-yves-secret");
let path = write_keys(
tmp.path(),
&format!(
r#"
[[keys]]
sha256_hex = "{sha}"
person = "yves"
team = "platform"
default_project = "ai-gateway"
[[keys]]
sha256_hex = "{}"
person = "mara"
"#,
sha256_hex("gk-mara-secret")
),
);
let keys = GatewayKeys::load(&path).unwrap();
assert_eq!(keys.len(), 2);
let yves = keys.lookup("gk-yves-secret").expect("known key");
assert_eq!(yves.person.as_deref(), Some("yves"));
assert_eq!(yves.team.as_deref(), Some("platform"));
assert_eq!(yves.project.as_deref(), Some("ai-gateway"));
let mara = keys.lookup("gk-mara-secret").expect("known key");
assert_eq!(mara.person.as_deref(), Some("mara"));
assert_eq!(mara.team, None);
assert_eq!(mara.project, None);
assert!(keys.lookup("gk-unknown").is_none());
}
#[test]
fn missing_file_is_empty_but_malformed_is_loud() {
let tmp = tempfile::tempdir().unwrap();
let missing = GatewayKeys::load(&tmp.path().join("nope.toml")).unwrap();
assert!(missing.is_empty());
let bad_hash = write_keys(
tmp.path(),
r#"
[[keys]]
sha256_hex = "not-a-hash"
person = "yves"
"#,
);
assert!(
GatewayKeys::load(&bad_hash).is_err(),
"an invalid sha256_hex must fail loudly, not silently drop the key"
);
}
#[test]
fn sha256_hex_matches_known_vector() {
// SHA-256("abc") — the FIPS 180-2 test vector.
assert_eq!(
sha256_hex("abc"),
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
);
}
#[test]
fn reload_keys_rereads_the_configured_path() {
let _env = crate::core::data_dir::test_env_lock();
let tmp = tempfile::tempdir().unwrap();
let path = write_keys(
tmp.path(),
&format!(
"[[keys]]\nsha256_hex = \"{}\"\nperson = \"mara\"\n",
sha256_hex("gk-mara-secret")
),
);
crate::test_env::set_var("LEAN_CTX_GATEWAY_KEYS", &path);
let keys = reload_keys().unwrap();
assert!(keys.lookup("gk-mara-secret").is_some());
crate::test_env::remove_var("LEAN_CTX_GATEWAY_KEYS");
}
}