OXYGENHUB
◆ api-unknown-fields oxygenhub / crates / hub / src / keyless.rs
18.5 KiB · Rust · last changed Certificates name the grant owner's newest identity (refs andymccall/oxygen#82) 9 days ago source blame history raw
1
//! Certificates for short-lived keys (oxygenhub#241, docs/keyless.md,
2
//! Oxygen spec §14.7): the hub vouches, for ten minutes, for a fresh key
3
//! whose holder proves who it is with a credential the hub issued. A Flow
4
//! job asks with its job token; anything else with one of the hub's OIDC
5
//! tokens for the keyless audience. The key signs, then is thrown away:
6
//! nothing long-lived sits on a runner.
7
//!
8
//! An agent asks with a grant token instead (step 5): its owner signed an
9
//! Oxygen grant letting `agent:NAME@ACCOUNT` act for them (spec §14.7),
10
//! and every certificate issued under it carries that grant, so the
11
//! agent's fresh keys sign in the owner's name with nothing kept on disk
12
//! but the token. Revoking the grant stops the certificates.
13
14
use crate::api::ApiError;
15
use crate::{db, Shared};
16
use axum::extract::Path;
17
use axum::extract::State;
18
use axum::http::{HeaderMap, StatusCode};
19
use axum::Json;
20
use oxy_core::cert::{certify_challenge, Certificate, SignedCertificate, SignedGrant};
21
use oxy_core::identity::{verify_challenge, Identity, Scope, Signature};
22
use serde::{Deserialize, Serialize};
23
24
/// How long a certificate lasts.
25
pub const LIFETIME_MS: i64 = 10 * 60 * 1000;
26
/// How far back a certificate starts, for clocks a little behind.
27
const SKEW_MS: i64 = 30 * 1000;
28
/// What an agent holds in place of a key: `oxyagent_` and 32 random bytes.
29
pub const GRANT_TOKEN_PREFIX: &str = "oxyagent_";
30
31
/// The OIDC audience a token must have to be exchanged here.
32
pub fn audience(base_url: &str) -> String {
33
    format!("{}/keyless", base_url.trim_end_matches('/'))
34
}
35
36
#[derive(Deserialize)]
37
pub struct CertifyRequest {
38
    /// The fresh key's identity object, base64.
39
    pub identity: String,
40
    /// The key's signature over `certify_challenge(credential)`, base64 CBOR.
41
    pub proof: String,
42
    /// `changes,refs` and so on.
43
    pub scope: String,
44
}
45
46
#[derive(Serialize)]
47
pub struct CertifyResponse {
48
    /// The signed certificate, base64 (`[certificate, signature]`).
49
    pub certificate: String,
50
    pub subject: String,
51
    pub expires_ms: i64,
52
    /// For an agent's certificate, the identity that signed its grant
53
    /// (base64), so a repository that never saw the owner's current key can
54
    /// check the grant, and pass it on with its push (oxygen#134).
55
    #[serde(skip_serializing_if = "Option::is_none")]
56
    pub grantor_identity: Option<String>,
57
    /// For an agent's certificate, the grant's owner's newest linked
58
    /// identity and its predecessors back to the grant's key (base64,
59
    /// newest first, the grant's key's own left out): a home with
60
    /// `author.for` authors as the first (oxygen#82). Empty when the
61
    /// grant's key is the newest.
62
    #[serde(skip_serializing_if = "Vec::is_empty")]
63
    pub author_chain: Vec<String>,
64
}
65
66
/// The newest identity linked to `account_id` whose `previous` chain
67
/// reaches `grantor`, with the chain down to (not including) it, newest
68
/// first (oxygen#82). Empty when none is newer.
69
pub async fn author_chain(app: &Shared, account_id: i64, grantor: oxy_core::hash::KeyId) -> Vec<Identity> {
70
    let linked = db::accounts::identities_of(&app.db, account_id).await.unwrap_or_default();
71
    let ids: std::collections::HashMap<oxy_core::hash::KeyId, Identity> =
72
        linked.iter().filter_map(|l| Identity::decode(&l.object).ok()).map(|i| (i.hash(), i)).collect();
73
    let mut best: Vec<Identity> = Vec::new();
74
    for head in ids.values() {
75
        let mut chain = vec![head.clone()];
76
        let mut cur = head;
77
        let reached = loop {
78
            match cur.previous {
79
                Some(p) if p == grantor => break true,
80
                Some(p) => match ids.get(&p) {
81
                    Some(prev) if chain.len() < 64 => {
82
                        chain.push(prev.clone());
83
                        cur = prev;
84
                    }
85
                    _ => break false,
86
                },
87
                None => break false,
88
            }
89
        };
90
        if reached && best.first().is_none_or(|b| head.created > b.created) {
91
            best = chain;
92
        }
93
    }
94
    best
95
}
96
97
/// Who a credential says its holder is.
98
struct Proven {
99
    subject: String,
100
    claims: Vec<(String, String)>,
101
    /// The credential's own end, which a certificate never outlives.
102
    until_ms: i64,
103
    target: String,
104
    /// The most the certificate may give.
105
    allowed: Scope,
106
    /// The owner's grant the certificate carries, for an agent.
107
    grant: Option<SignedGrant>,
108
}
109
110
fn bad(status: StatusCode, why: impl Into<String>) -> ApiError {
111
    ApiError(status, why.into())
112
}
113
114
/// A running job's token: its repository, bookmark, run and job.
115
async fn job_proof(app: &Shared, token: &str) -> Result<Proven, ApiError> {
116
    let invalid = || bad(StatusCode::UNAUTHORIZED, "this job token is not valid (the job must be running)");
117
    let hash = crate::auth::token_hash(token);
118
    let job = db::flow::job_by_token(&app.db, &hash, crate::flow::JOB_TOKEN_MAX_AGE_MS)
119
        .await
120
        .map_err(|e| bad(StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?
121
        .ok_or_else(invalid)?;
122
    let run = db::flow::run_by_id(&app.db, job.run_id)
123
        .await
124
        .map_err(|e| bad(StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?
125
        .ok_or_else(invalid)?;
126
    let repo = db::repos::get_by_id(&app.db, run.repo_id)
127
        .await
128
        .map_err(|e| bad(StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?
129
        .ok_or_else(invalid)?;
130
    let mut claims = vec![
131
        ("repository".to_string(), format!("{}/{}", repo.owner, repo.name)),
132
        ("bookmark".to_string(), run.bookmark.clone()),
133
        ("run".to_string(), run.number.to_string()),
134
        ("job".to_string(), job.name.clone()),
135
        ("runner".to_string(), job.runner.clone()),
136
    ];
137
    if !job.environment.is_empty() && !job.approved_by.is_empty() {
138
        claims.push(("environment".to_string(), job.environment.clone()));
139
        claims.push(("approved_by".to_string(), job.approved_by.clone()));
140
    }
141
    Ok(Proven {
142
        subject: format!("repo:{}/{}:bookmark:{}", repo.owner, repo.name, run.bookmark),
143
        claims,
144
        until_ms: job.started_ms.unwrap_or(0) + crate::flow::JOB_TOKEN_MAX_AGE_MS,
145
        target: format!("{}/{}", repo.owner, repo.name),
146
        allowed: Scope::CHANGES | Scope::REFS | Scope::SEAL,
147
        grant: None,
148
    })
149
}
150
151
/// One of this hub's OIDC tokens for the keyless audience.
152
async fn oidc_proof(app: &Shared, token: &str, now: i64) -> Result<Proven, ApiError> {
153
    let issuer = app.oidc().await.map_err(|e| bad(StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
154
    let aud = audience(app.config.base_url());
155
    let claims = issuer
156
        .verify(token, &aud, now)
157
        .ok_or_else(|| bad(StatusCode::UNAUTHORIZED, format!("the token is not one of this hub's, for audience {aud}, still valid")))?;
158
    let subject = claims["sub"].as_str().unwrap_or("").to_string();
159
    if subject.is_empty() {
160
        return Err(bad(StatusCode::UNAUTHORIZED, "the token names no subject"));
161
    }
162
    let keep = ["repository", "bookmark", "run_number", "job", "runner", "environment", "approved_by"];
163
    let mut out = Vec::new();
164
    for k in keep {
165
        match &claims[k] {
166
            serde_json::Value::String(s) => out.push((k.to_string(), s.clone())),
167
            serde_json::Value::Number(n) => out.push((k.to_string(), n.to_string())),
168
            _ => {}
169
        }
170
    }
171
    Ok(Proven {
172
        target: claims["repository"].as_str().unwrap_or(&subject).to_string(),
173
        subject,
174
        claims: out,
175
        until_ms: claims["exp"].as_i64().unwrap_or(0) * 1000,
176
        allowed: Scope::CHANGES | Scope::REFS | Scope::SEAL,
177
        grant: None,
178
    })
179
}
180
181
/// An agent's grant token: the owner's live grant.
182
async fn grant_proof(app: &Shared, token: &str, now: i64) -> Result<Proven, ApiError> {
183
    let invalid = || bad(StatusCode::UNAUTHORIZED, "this agent grant is not valid (revoked, ended or unknown)");
184
    let g = db::accounts::agent_grant_by_token(&app.db, &crate::auth::token_hash(token))
185
        .await
186
        .map_err(|e| bad(StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?
187
        .filter(|g| g.live(now))
188
        .ok_or_else(invalid)?;
189
    let signed = SignedGrant::decode(&g.signed).map_err(|e| bad(StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
190
    db::accounts::touch_agent_grant(&app.db, g.id).await.ok();
191
    Ok(Proven {
192
        subject: g.subject.clone(),
193
        claims: vec![("agent".to_string(), g.agent.clone()), ("grant".to_string(), g.id.to_string())],
194
        until_ms: g.expires_ms,
195
        target: g.subject,
196
        allowed: signed.grant.scope,
197
        grant: Some(signed),
198
    })
199
}
200
201
/// `POST /api/v1/keyless/certify`: a ten-minute certificate for a fresh
202
/// key, for whoever the bearer credential proves.
203
pub async fn certify(
204
    State(app): State<Shared>,
205
    headers: HeaderMap,
206
    Json(req): Json<CertifyRequest>,
207
) -> Result<Json<CertifyResponse>, ApiError> {
208
    let token = oxy_server::bearer_token(&headers)
209
        .ok_or_else(|| bad(StatusCode::UNAUTHORIZED, "ask with a job token or one of this hub's OIDC tokens as the bearer"))?
210
        .to_string();
211
    let now = db::now_ms();
212
    let proven = if token.starts_with(crate::flow::JOB_TOKEN_PREFIX) {
213
        job_proof(&app, &token).await?
214
    } else if token.starts_with(GRANT_TOKEN_PREFIX) {
215
        grant_proof(&app, &token, now).await?
216
    } else {
217
        oidc_proof(&app, &token, now).await?
218
    };
219
    let decode = |what: &str, b64: &str| {
220
        oxy_core::dsse::base64_decode(b64.trim()).ok_or_else(|| bad(StatusCode::BAD_REQUEST, format!("{what} is not base64")))
221
    };
222
    let identity =
223
        Identity::decode(&decode("identity", &req.identity)?).map_err(|e| bad(StatusCode::BAD_REQUEST, format!("identity: {e}")))?;
224
    let proof = oxy_core::cbor::decode(&decode("proof", &req.proof)?)
225
        .map_err(|e| e.to_string())
226
        .and_then(|v| Signature::from_value(&v).map_err(|e| e.to_string()))
227
        .map_err(|e| bad(StatusCode::BAD_REQUEST, format!("proof: {e}")))?;
228
    verify_challenge(&identity, &proof, &certify_challenge(&token))
229
        .map_err(|e| bad(StatusCode::UNAUTHORIZED, format!("the proof does not show this key's holder asked: {e}")))?;
230
    // What was asked, within what the credential allows (an agent's grant
231
    // may leave out `refs`, say): a key asks for all it might sign.
232
    let asked = Scope::parse(&req.scope).map_err(|e| bad(StatusCode::BAD_REQUEST, e))?;
233
    let scope =
234
        asked.names().into_iter().filter_map(|n| Scope::parse(n).ok()).filter(|s| proven.allowed.has(*s)).fold(Scope::NONE, |a, s| a | s);
235
    if scope.is_empty() {
236
        return Err(bad(StatusCode::FORBIDDEN, format!("{} may be certified for {} only", proven.subject, proven.allowed)));
237
    }
238
    let expires = (now + LIFETIME_MS).min(proven.until_ms);
239
    if expires <= now {
240
        return Err(bad(StatusCode::UNAUTHORIZED, "the credential is about to expire: ask with a fresh one"));
241
    }
242
    let (key, secret) =
243
        crate::keys::load_secret(&app.config.keys_dir()).map_err(|e| bad(StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
244
    let cert = Certificate {
245
        key: identity,
246
        issuer: key,
247
        subject: proven.subject.clone(),
248
        claims: proven.claims,
249
        scope,
250
        not_before: (now - SKEW_MS) as u64,
251
        expires: expires as u64,
252
        grant: proven.grant,
253
    };
254
    // The grant's signer, as linked to its account (the grant was checked
255
    // against it when it was made).
256
    let grantor = match &cert.grant {
257
        Some(g) => db::accounts::identity_by_key(&app.db, &g.grant.grantor.to_hex()).await.ok().flatten(),
258
        None => None,
259
    };
260
    let grantor_account = grantor.as_ref().map(|l| l.account_id);
261
    let grantor_identity = grantor.map(|l| oxy_core::dsse::base64(&l.object));
262
    let author_chain = match (&cert.grant, &grantor_account) {
263
        (Some(g), Some(account)) => {
264
            author_chain(&app, *account, g.grant.grantor).await.iter().map(|i| oxy_core::dsse::base64(&i.encode())).collect()
265
        }
266
        _ => Vec::new(),
267
    };
268
    let signed = SignedCertificate::sign(cert, &app.hub_key.identity, &secret);
269
    let detail = format!("{} for {} until {}", signed.cert.key_id().short(12), proven.subject, expires);
270
    db::accounts::audit(&app.db, None, "keyless.certified", &proven.target, &detail).await.ok();
271
    // Every certificate is logged (#238), as Fulcio's are in Rekor.
272
    crate::tlog::log(
273
        &app,
274
        vec![crate::tlog::Pending {
275
            kind: "certificate",
276
            repo: [0; 16],
277
            repo_row: None,
278
            object: signed.cert.hash(),
279
            signers: vec![app.hub_key.key],
280
        }],
281
    )
282
    .await;
283
    Ok(Json(CertifyResponse {
284
        certificate: oxy_core::dsse::base64(&signed.encode()),
285
        subject: proven.subject,
286
        expires_ms: expires,
287
        grantor_identity,
288
        author_chain,
289
    }))
290
}
291
292
#[derive(Deserialize)]
293
pub struct GrantRequest {
294
    /// The signed Oxygen grant (`[grant, signature]`), base64.
295
    pub grant: String,
296
}
297
298
#[derive(Serialize)]
299
pub struct GrantView {
300
    pub id: i64,
301
    pub agent: String,
302
    pub subject: String,
303
    pub grantor: String,
304
    pub scope: String,
305
    pub created_ms: i64,
306
    pub expires_ms: i64,
307
    pub revoked_ms: Option<i64>,
308
    pub last_used_ms: Option<i64>,
309
    /// Shown once, when the grant is made.
310
    #[serde(skip_serializing_if = "Option::is_none")]
311
    pub token: Option<String>,
312
}
313
314
impl From<db::accounts::AgentGrant> for GrantView {
315
    fn from(g: db::accounts::AgentGrant) -> Self {
316
        GrantView {
317
            id: g.id,
318
            agent: g.agent,
319
            subject: g.subject,
320
            grantor: g.grantor,
321
            scope: g.scope,
322
            created_ms: g.created_ms,
323
            expires_ms: g.expires_ms,
324
            revoked_ms: g.revoked_ms,
325
            last_used_ms: g.last_used_ms,
326
            token: None,
327
        }
328
    }
329
}
330
331
/// The account behind a personal token. A grant needs no token scope:
332
/// what makes it is the owner's signature, by a key linked to the account.
333
async fn owner(app: &Shared, headers: &HeaderMap) -> Result<db::accounts::Account, ApiError> {
334
    let c = crate::api::caller(app, headers).await?;
335
    // Grants are their owner's to make and end, not an agent's.
336
    if c.agent {
337
        return Err(bad(StatusCode::FORBIDDEN, "an agent's token cannot manage agent grants: use your own"));
338
    }
339
    c.account.ok_or_else(|| bad(StatusCode::UNAUTHORIZED, "a personal access token is required"))
340
}
341
342
/// An agent's name: what follows `agent:` and precedes `@`.
343
fn agent_name_ok(name: &str) -> bool {
344
    !name.is_empty() && name.len() <= 64 && name.chars().all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_' || c == '.')
345
}
346
347
/// `POST /api/v1/agents/grants`: keep an owner's signed grant to one of
348
/// their agents and give the agent its token.
349
pub async fn grants_create(
350
    State(app): State<Shared>,
351
    headers: HeaderMap,
352
    Json(req): Json<GrantRequest>,
353
) -> Result<(StatusCode, Json<GrantView>), ApiError> {
354
    let account = owner(&app, &headers).await?;
355
    let bytes = oxy_core::dsse::base64_decode(req.grant.trim()).ok_or_else(|| bad(StatusCode::BAD_REQUEST, "the grant is not base64"))?;
356
    let signed = SignedGrant::decode(&bytes).map_err(|e| bad(StatusCode::BAD_REQUEST, format!("grant: {e}")))?;
357
    let g = &signed.grant;
358
    let suffix = format!("@{}", account.username);
359
    let agent = g
360
        .subject
361
        .strip_prefix("agent:")
362
        .and_then(|r| r.strip_suffix(&suffix))
363
        .filter(|n| agent_name_ok(n))
364
        .ok_or_else(|| bad(StatusCode::BAD_REQUEST, format!("a grant here is for agent:NAME{suffix}, not {}", g.subject)))?
365
        .to_string();
366
    if g.issuer != app.hub_key.key {
367
        return Err(bad(StatusCode::BAD_REQUEST, "the grant names another issuer than this hub's key (/.well-known/oxyhub-key)"));
368
    }
369
    let grantor = g.grantor.to_hex();
370
    let linked = db::accounts::identity_by_key(&app.db, &grantor)
371
        .await
372
        .map_err(|e| bad(StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?
373
        .filter(|i| i.account_id == account.id)
374
        .ok_or_else(|| bad(StatusCode::FORBIDDEN, format!("key {} is not linked to your account: link it first", g.grantor.short(12))))?;
375
    let identity = Identity::decode(&linked.object).map_err(|e| bad(StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
376
    signed.verify(&identity).map_err(|e| bad(StatusCode::BAD_REQUEST, format!("the grant does not verify: {e}")))?;
377
    let now = db::now_ms();
378
    if (g.expires as i64) <= now {
379
        return Err(bad(StatusCode::BAD_REQUEST, "the grant has already ended"));
380
    }
381
    let token = format!("{GRANT_TOKEN_PREFIX}{}", crate::auth::random_hex(32));
382
    let hash = crate::auth::token_hash(&token);
383
    let scope = g.scope.to_string();
384
    let id = db::accounts::create_agent_grant(
385
        &app.db,
386
        db::accounts::NewAgentGrant {
387
            account_id: account.id,
388
            agent: &agent,
389
            subject: &g.subject,
390
            grantor: &grantor,
391
            scope: &scope,
392
            signed: &bytes,
393
            token_hash: &hash,
394
            expires_ms: g.expires as i64,
395
        },
396
    )
397
    .await
398
    .map_err(|e| bad(StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
399
    db::accounts::audit(&app.db, Some(&account), "agent.granted", &g.subject, &format!("grant {id}, {scope}, until {}", g.expires))
400
        .await
401
        .ok();
402
    Ok((
403
        StatusCode::CREATED,
404
        Json(GrantView {
405
            id,
406
            agent,
407
            subject: g.subject.clone(),
408
            grantor,
409
            scope,
410
            created_ms: now,
411
            expires_ms: g.expires as i64,
412
            revoked_ms: None,
413
            last_used_ms: None,
414
            token: Some(token),
415
        }),
416
    ))
417
}
418
419
/// `GET /api/v1/agents/grants`: the caller's grants, without tokens.
420
pub async fn grants_list(State(app): State<Shared>, headers: HeaderMap) -> Result<Json<Vec<GrantView>>, ApiError> {
421
    let account = owner(&app, &headers).await?;
422
    let grants =
423
        db::accounts::agent_grants_of(&app.db, account.id).await.map_err(|e| bad(StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
424
    Ok(Json(grants.into_iter().map(GrantView::from).collect()))
425
}
426
427
/// `DELETE /api/v1/agents/grants/{id}`: stop certifying under a grant.
428
pub async fn grants_revoke(State(app): State<Shared>, headers: HeaderMap, Path(id): Path<i64>) -> Result<StatusCode, ApiError> {
429
    let account = owner(&app, &headers).await?;
430
    let done = db::accounts::revoke_agent_grant(&app.db, account.id, id)
431
        .await
432
        .map_err(|e| bad(StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
433
    if !done {
434
        return Err(bad(StatusCode::NOT_FOUND, "no such live grant of yours"));
435
    }
436
    db::accounts::audit(&app.db, Some(&account), "agent.revoked", &format!("grant {id}"), "").await.ok();
437
    Ok(StatusCode::NO_CONTENT)
438
}