nora-registry 1.2.0

Cloud-Native Artifact Registry - Fast, lightweight, multi-protocol
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
// Copyright (c) 2026 The NORA Authors
// SPDX-License-Identifier: MIT

//! OIDC Workload Identity authentication provider.
//!
//! Validates JWT tokens from CI/CD systems (GitHub Actions, GitLab CI)
//! against configured OIDC issuers with JWKS key caching.
//!
//! Security properties:
//! - Algorithm whitelist per provider (only RS256/ES256 by default)
//! - Strict issuer binding (never follows jku/x5u from token)
//! - Per-provider namespace scoping
//! - Token lifetime ceiling enforcement
//! - Stale JWKS cache on fetch failure (availability over freshness)

use jsonwebtoken::{
    dangerous::insecure_decode, decode, decode_header, jwk::JwkSet, Algorithm, DecodingKey,
    TokenData, Validation,
};
use parking_lot::RwLock;
use reqwest::Client;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::time::{Duration, Instant};

use crate::config::{OidcConfig, OidcProvider, ScopeEnforcement};
use crate::tokens::Role;

/// Result of a successful OIDC authentication.
#[derive(Debug, Clone)]
pub struct OidcIdentity {
    /// Provider name that validated this token
    pub provider: String,
    /// Subject claim from JWT
    pub subject: String,
    /// Issuer claim
    pub issuer: String,
    /// Assigned role based on role_rules
    pub role: Role,
    /// Provider's `namespace_scope` patterns (segment-aware globs). `["*"]` = all
    /// namespaces. Enforced on writes via `auth::enforce_namespace_scope`.
    /// Always the provider's hard ceiling, whatever the matched rule says.
    pub namespace_scope: Vec<String>,
    /// The matched role rule's `namespace_scope`, if it set one. Enforced in
    /// addition to the provider scope — a write must satisfy both, so a rule
    /// can only narrow the provider ceiling, never widen past it.
    pub rule_namespace_scope: Option<Vec<String>>,
    /// Whether `namespace_scope` is enforced (403 on mismatch) or only audited
    /// for this provider.
    pub namespace_scope_enforcement: ScopeEnforcement,
}

/// Standard JWT claims we extract.
#[derive(Debug, Deserialize, Serialize)]
struct Claims {
    iss: Option<String>,
    sub: Option<String>,
    aud: Option<Audience>,
    exp: Option<u64>,
    iat: Option<u64>,
    nbf: Option<u64>,
}

/// Audience can be a string or array of strings.
#[derive(Debug, Deserialize, Serialize, Clone)]
#[serde(untagged)]
enum Audience {
    Single(String),
    Multiple(Vec<String>),
}

impl Audience {
    #[cfg(test)]
    fn contains(&self, expected: &str) -> bool {
        match self {
            Audience::Single(s) => s == expected,
            Audience::Multiple(v) => v.iter().any(|s| s == expected),
        }
    }
}

/// Cached JWKS for a provider.
struct CachedJwks {
    jwks: JwkSet,
    fetched_at: Instant,
}

/// Minimal OIDC discovery document (only the fields we need).
#[derive(Deserialize)]
struct OidcDiscoveryDoc {
    #[serde(default)]
    jwks_uri: String,
}

/// OIDC validator — thread-safe, holds cached JWKS per issuer.
pub struct OidcValidator {
    config: OidcConfig,
    http_client: Client,
    /// issuer URL → cached JWKS
    jwks_cache: RwLock<HashMap<String, CachedJwks>>,
}

/// Per-request timeout for OIDC HTTP calls (discovery + JWKS fetch).
const OIDC_REQUEST_TIMEOUT: Duration = Duration::from_secs(5);

impl OidcValidator {
    /// Create a new validator from config.
    ///
    /// The client should be pre-configured with any required TLS settings
    /// (e.g. custom CA certificates). Per-request timeouts are applied internally.
    pub fn new(config: OidcConfig, http_client: Client) -> Self {
        Self {
            config,
            http_client,
            jwks_cache: RwLock::new(HashMap::new()),
        }
    }

    /// Check if OIDC is enabled and has providers configured.
    pub fn is_active(&self) -> bool {
        self.config.enabled && !self.config.providers.is_empty()
    }

    /// Validate a Bearer token as an OIDC JWT.
    /// Returns the identity on success, or an error string.
    pub async fn validate_token(&self, token: &str) -> Result<OidcIdentity, String> {
        if !self.is_active() {
            return Err("OIDC not enabled".to_string());
        }

        // Decode header WITHOUT validation to determine issuer and kid
        let header = decode_header(token).map_err(|e| format!("Invalid JWT header: {}", e))?;

        // Security: reject alg=none and symmetric algorithms globally
        match header.alg {
            Algorithm::HS256 | Algorithm::HS384 | Algorithm::HS512 => {
                return Err("Symmetric algorithms not allowed for OIDC".to_string());
            }
            _ => {}
        }

        // Peek at claims to find issuer (unverified — for routing only)
        let unverified: Claims = insecure_decode::<Claims>(token)
            .map_err(|e| format!("Cannot decode claims: {}", e))?
            .claims;

        let issuer = unverified.iss.as_deref().ok_or("Token missing iss claim")?;

        // Find matching provider
        let provider = self
            .config
            .providers
            .iter()
            .find(|p| p.enabled && p.issuer == issuer)
            .ok_or_else(|| format!("No matching OIDC provider for issuer: {}", issuer))?;

        // Verify algorithm is in provider's whitelist
        let alg_str = format!("{:?}", header.alg);
        if !provider.algorithms.iter().any(|a| a == &alg_str) {
            return Err(format!(
                "Algorithm {} not in provider whitelist: {:?}",
                alg_str, provider.algorithms
            ));
        }

        // Get JWKS (cached or fetch)
        let jwks = self.get_jwks(provider).await?;

        // Find the matching key by kid
        let kid = header.kid.as_deref().unwrap_or("");
        let jwk = jwks
            .keys
            .iter()
            .find(|k| {
                k.common.key_id.as_deref() == Some(kid) || (kid.is_empty() && jwks.keys.len() == 1)
            })
            .ok_or_else(|| {
                format!(
                    "No matching key (kid={}) in JWKS for provider {}",
                    kid, provider.name
                )
            })?;

        // Build decoding key from JWK
        let decoding_key =
            DecodingKey::from_jwk(jwk).map_err(|e| format!("Cannot build key from JWK: {}", e))?;

        // Full validation
        let mut validation = Validation::new(header.alg);
        validation.set_issuer(&[&provider.issuer]);
        validation.leeway = self.config.leeway_secs;

        if !provider.audience.is_empty() {
            validation.set_audience(&[&provider.audience]);
        } else {
            validation.validate_aud = false;
        }

        let token_data: TokenData<Claims> = decode(token, &decoding_key, &validation)
            .map_err(|e| format!("JWT validation failed: {}", e))?;

        let claims = token_data.claims;

        // Enforce token lifetime ceiling
        if let (Some(iat), Some(exp)) = (claims.iat, claims.exp) {
            let lifetime = exp.saturating_sub(iat);
            if lifetime > provider.max_token_lifetime_secs {
                return Err(format!(
                    "Token lifetime {} exceeds max {} for provider {}",
                    lifetime, provider.max_token_lifetime_secs, provider.name
                ));
            }
        }

        // Map claims to role via role_rules
        let subject = claims.sub.unwrap_or_default();
        let (role, rule_scope) = self.match_role(provider, &subject).ok_or_else(|| {
            format!(
                "No role rule matches sub='{}' for provider {}",
                subject, provider.name
            )
        })?;

        Ok(OidcIdentity {
            provider: provider.name.clone(),
            subject,
            issuer: provider.issuer.clone(),
            role,
            namespace_scope: provider.namespace_scope.clone(),
            rule_namespace_scope: rule_scope,
            namespace_scope_enforcement: provider.namespace_scope_enforcement,
        })
    }

    /// Fetch or return cached JWKS for a provider.
    async fn get_jwks(&self, provider: &OidcProvider) -> Result<JwkSet, String> {
        let cache_ttl = Duration::from_secs(self.config.jwks_cache_secs);

        // Try cache first
        {
            let cache = self.jwks_cache.read();
            if let Some(cached) = cache.get(&provider.issuer) {
                if cached.fetched_at.elapsed() < cache_ttl {
                    return Ok(cached.jwks.clone());
                }
            }
        }

        // Resolve JWKS URI: explicit config > OIDC discovery > legacy fallback
        let jwks_uri = self.resolve_jwks_uri(provider).await?;

        match self.fetch_jwks(&jwks_uri).await {
            Ok(jwks) => {
                let mut cache = self.jwks_cache.write();
                cache.insert(
                    provider.issuer.clone(),
                    CachedJwks {
                        jwks: jwks.clone(),
                        fetched_at: Instant::now(),
                    },
                );
                Ok(jwks)
            }
            Err(e) => {
                // Stale fallback: return expired cache if available
                let cache = self.jwks_cache.read();
                if let Some(cached) = cache.get(&provider.issuer) {
                    tracing::warn!(
                        provider = %provider.name,
                        error = %e,
                        stale_age_secs = cached.fetched_at.elapsed().as_secs(),
                        "JWKS fetch failed, serving stale keys"
                    );
                    return Ok(cached.jwks.clone());
                }
                Err(format!("JWKS fetch failed for {}: {}", provider.name, e))
            }
        }
    }

    /// Resolve the JWKS URI for a provider using (in order):
    /// 1. Explicit `jwks_uri` from config
    /// 2. OIDC discovery via `.well-known/openid-configuration`
    /// 3. Fallback to `{issuer}/.well-known/jwks.json`
    async fn resolve_jwks_uri(&self, provider: &OidcProvider) -> Result<String, String> {
        // 1. Explicit override
        if let Some(ref uri) = provider.jwks_uri {
            return Ok(uri.clone());
        }

        // 2. OIDC discovery
        let discovery_url = format!(
            "{}/.well-known/openid-configuration",
            provider.issuer.trim_end_matches('/')
        );

        if let Ok(resp) = self
            .http_client
            .get(&discovery_url)
            .timeout(OIDC_REQUEST_TIMEOUT)
            .send()
            .await
        {
            if resp.status().is_success() {
                if let Ok(config) = resp.json::<OidcDiscoveryDoc>().await {
                    if !config.jwks_uri.is_empty() {
                        tracing::debug!(
                            provider = %provider.name,
                            jwks_uri = %config.jwks_uri,
                            "Discovered JWKS URI via openid-configuration"
                        );
                        return Ok(config.jwks_uri);
                    }
                }
            }
        }

        // 3. Legacy fallback
        Ok(format!(
            "{}/.well-known/jwks.json",
            provider.issuer.trim_end_matches('/')
        ))
    }

    /// HTTP fetch of JWKS from provider.
    async fn fetch_jwks(&self, url: &str) -> Result<JwkSet, String> {
        let resp = self
            .http_client
            .get(url)
            .timeout(OIDC_REQUEST_TIMEOUT)
            .send()
            .await
            .map_err(|e| format!("HTTP error: {}", e))?;

        if !resp.status().is_success() {
            return Err(format!("JWKS endpoint returned {}", resp.status()));
        }

        resp.json::<JwkSet>()
            .await
            .map_err(|e| format!("JWKS parse error: {}", e))
    }

    /// Match subject against provider's role_rules. First match wins.
    /// Returns the role plus the rule's namespace-scope override, if any.
    fn match_role(
        &self,
        provider: &OidcProvider,
        subject: &str,
    ) -> Option<(Role, Option<Vec<String>>)> {
        for rule in &provider.role_rules {
            if glob_match(&rule.pattern, subject) {
                let role = match rule.role.as_str() {
                    "admin" => Role::Admin,
                    "write" => Role::Write,
                    "read" => Role::Read,
                    _ => return None,
                };
                return Some((role, rule.namespace_scope.clone()));
            }
        }
        None
    }
}

/// Simple glob matching: supports `*` (any chars within segment) and `**` is not needed
/// since sub claims use `:` as separator, not `/`.
/// Patterns: "repo:org/*" matches "repo:org/myrepo:ref:refs/heads/main"
fn glob_match(pattern: &str, value: &str) -> bool {
    if pattern == "*" {
        return true;
    }

    let parts: Vec<&str> = pattern.split('*').collect();
    if parts.len() == 1 {
        // No wildcard — exact match
        return pattern == value;
    }

    // First part must be prefix, last part must be suffix
    let mut remaining = value;

    for (i, part) in parts.iter().enumerate() {
        if part.is_empty() {
            continue;
        }
        if i == 0 {
            // Must start with this
            if !remaining.starts_with(part) {
                return false;
            }
            remaining = &remaining[part.len()..];
        } else if i == parts.len() - 1 {
            // Must end with this
            if !remaining.ends_with(part) {
                return false;
            }
            return true;
        } else {
            // Must contain this
            match remaining.find(part) {
                Some(pos) => remaining = &remaining[pos + part.len()..],
                None => return false,
            }
        }
    }

    true
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
    use super::*;
    use crate::config::{OidcProvider, OidcRoleRule};

    #[test]
    fn test_glob_match_exact() {
        assert!(glob_match("repo:org/myrepo", "repo:org/myrepo"));
        assert!(!glob_match("repo:org/myrepo", "repo:org/other"));
    }

    #[test]
    fn test_glob_match_wildcard() {
        assert!(glob_match("repo:org/*", "repo:org/myrepo"));
        assert!(glob_match(
            "repo:org/*",
            "repo:org/myrepo:ref:refs/heads/main"
        ));
        assert!(!glob_match("repo:org/*", "repo:other/myrepo"));
    }

    #[test]
    fn test_glob_match_star_only() {
        assert!(glob_match("*", "anything"));
        assert!(glob_match("*", ""));
    }

    #[test]
    fn test_glob_match_middle() {
        assert!(glob_match(
            "repo:*:ref:refs/heads/main",
            "repo:org/myrepo:ref:refs/heads/main"
        ));
        assert!(!glob_match(
            "repo:*:ref:refs/heads/main",
            "repo:org/myrepo:ref:refs/heads/dev"
        ));
    }

    #[test]
    fn test_match_role_first_wins() {
        let config = OidcConfig::default();
        let validator = OidcValidator::new(config, Client::new());

        let provider = OidcProvider {
            name: "test".to_string(),
            issuer: "https://test".to_string(),
            jwks_uri: None,
            audience: "nora".to_string(),
            algorithms: vec!["RS256".to_string()],
            max_token_lifetime_secs: 900,
            namespace_scope: vec!["*".to_string()],
            namespace_scope_enforcement: crate::config::ScopeEnforcement::Enforce,
            enabled: true,
            role_rules: vec![
                OidcRoleRule {
                    pattern: "repo:myorg/*:ref:refs/heads/main".to_string(),
                    role: "write".to_string(),
                    namespace_scope: None,
                },
                OidcRoleRule {
                    pattern: "repo:myorg/*:pull_request".to_string(),
                    role: "write".to_string(),
                    namespace_scope: Some(vec!["ci-transport/**".to_string()]),
                },
                OidcRoleRule {
                    pattern: "repo:myorg/*".to_string(),
                    role: "read".to_string(),
                    namespace_scope: None,
                },
            ],
        };

        // main branch → write (first rule), inherits provider scope
        let m = validator.match_role(&provider, "repo:myorg/app:ref:refs/heads/main");
        assert!(matches!(m, Some((Role::Write, None))));

        // pull_request → write narrowed to the rule's scope
        let m = validator.match_role(&provider, "repo:myorg/app:pull_request");
        let (role, scope) = m.unwrap();
        assert_eq!(role, Role::Write);
        assert_eq!(scope.as_deref(), Some(&["ci-transport/**".to_string()][..]));

        // other branch → read (last rule)
        let m = validator.match_role(&provider, "repo:myorg/app:ref:refs/heads/dev");
        assert!(matches!(m, Some((Role::Read, None))));

        // different org → no match
        let m = validator.match_role(&provider, "repo:other/app:ref:refs/heads/main");
        assert!(m.is_none());
    }

    #[test]
    fn test_audience_contains() {
        let single = Audience::Single("nora".to_string());
        assert!(single.contains("nora"));
        assert!(!single.contains("other"));

        let multi = Audience::Multiple(vec!["nora".to_string(), "api".to_string()]);
        assert!(multi.contains("nora"));
        assert!(multi.contains("api"));
        assert!(!multi.contains("other"));
    }
}