ci_id/
lib.rs

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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
//! `ci-id` provides easy access to ambient OIDC credentials in CI systems like
//! GitHub Actions.
//!
//! ```
//! match ci_id::detect_credentials(Some("my-audience")) {
//!     Ok(token) => println!("{}", token),
//!     Err(e) => eprintln!("{}", e)
//! }
//! ```
//!
//! # Environment specific setup
//!
//! Typically the CI environment needs to allow OIDC identity access.
//!
//! ## GitHub Actions
//!
//! Workflow must be given the [permission](https://docs.github.com/en/actions/security-for-github-actions/security-hardening-your-deployments/about-security-hardening-with-openid-connect#adding-permissions-settings)
//! to use the workflow identity:
//!
//! ```yaml
//! permissions:
//!     id-token: write
//! ```
//!
//! ## GitLab Pipelines
//!
//! An [ID token](https://docs.gitlab.com/ee/ci/secrets/id_token_authentication.html)
//! must be defined in the pipeline:
//!
//! ```yaml
//! id_tokens:
//!     MY_AUDIENCE_ID_TOKEN:
//!         aud: my-audience
//! ```
//!
//! The ID token name must be based on the audience so
//! that token name is either
//! * `ID_TOKEN` for default audience
//! * `<AUD>_ID_TOKEN` where `<AUD>` is the audience string sanitized for environment variable names
//!   (uppercased and all characters outside of ascii letters and digits are replaced with "_")
//!
//! ## CircleCI
//!
//! No configuration is needed.

// TODO
// * is blocking an issue?
// * less dependencies?
// * build library only with library deps -- maybe separate into two crates in a workspace?

use regex::Regex;
use serde::Deserialize;
use std::{collections::HashMap, env, fmt, process::Command};
pub type Result<T> = std::result::Result<T, CIIDError>;

#[cfg(test)]
#[macro_use]
extern crate lazy_static;

#[derive(Debug, Clone, PartialEq)]
pub enum CIIDError {
    /// No supported OIDC identity environment was detected
    EnvironmentNotDetected,
    /// Environment was found but there was a problem with acquiring the token
    EnvironmentError(String),
    /// Identity token was found but it does not look like JSON Web Token
    MalformedToken,
}
impl fmt::Display for CIIDError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            CIIDError::EnvironmentError(s) => write!(f, "credential detection failed: {}", s),
            _ => write!(f, "credential detection failed"),
        }
    }
}

type DetectFn = fn(Option<&str>) -> Result<String>;

fn validate_token(token: String) -> Result<String> {
    // very, very shallow validation: could this be a JWT token?
    match token.split(".").collect::<Vec<&str>>().len() {
        3 => Ok(token),
        _ => Err(CIIDError::MalformedToken),
    }
}

/// Returns detected OIDC identity token.
///
/// The supported environments are probed in order, the identity token
/// for the first found environment is returned.
///
/// ```
/// match ci_id::detect_credentials(Some("my-audience")) {
///     Ok(token) => println!("{}", token),
///     Err(e) => eprintln!("{}", e)
/// }
/// ```
pub fn detect_credentials(audience: Option<&str>) -> Result<String> {
    for (name, detect) in [
        ("GitHub Actions", detect_github as DetectFn),
        ("GitLab Pipelines", detect_gitlab as DetectFn),
        ("CircleCI", detect_circleci as DetectFn),
    ] {
        match detect(audience) {
            Ok(token) => {
                let token = validate_token(token)?;
                log::debug!("{}: Token found", name);
                return Ok(token);
            }
            Err(CIIDError::EnvironmentNotDetected) => {
                log::debug!("{}: Environment not detected", name);
            }
            Err(e) => return Err(e),
        }
    }

    Err(CIIDError::EnvironmentNotDetected)
}

// Github implementation

#[derive(Deserialize)]
struct GitHubTokenResponse {
    value: String,
}

fn detect_github(audience: Option<&str>) -> Result<String> {
    if env::var("GITHUB_ACTIONS").is_err() {
        return Err(CIIDError::EnvironmentNotDetected);
    };

    let Ok(token_token) = env::var("ACTIONS_ID_TOKEN_REQUEST_TOKEN") else {
        return Err(CIIDError::EnvironmentError(
            "GitHub Actions: ACTIONS_ID_TOKEN_REQUEST_TOKEN is not set. This could \
            imply that the job does not have 'id-token: write' permission"
                .into(),
        ));
    };
    let Ok(token_url) = env::var("ACTIONS_ID_TOKEN_REQUEST_URL") else {
        return Err(CIIDError::EnvironmentError(
            "GitHub Actions: ACTIONS_ID_TOKEN_REQUEST_URL is not set".into(),
        ));
    };
    let mut params = HashMap::new();
    if let Some(aud) = audience {
        params.insert("audience", aud);
    }

    log::debug!("GitHub Actions: Requesting token");
    let client = reqwest::blocking::Client::new();
    let http_response = match client
        .get(token_url)
        .header(
            reqwest::header::AUTHORIZATION,
            format!("bearer {}", token_token),
        )
        .query(&params)
        .send()
    {
        Ok(response) => response,
        Err(e) => {
            return Err(CIIDError::EnvironmentError(format!(
                "GitHub Actions: Token request failed: {}",
                e
            )))
        }
    };
    match http_response.json::<GitHubTokenResponse>() {
        Ok(token_response) => Ok(token_response.value),
        Err(e) => Err(CIIDError::EnvironmentError(format!(
            "GitHub Actions: Failed to parse token reponse: {}",
            e
        ))),
    }
}

fn detect_gitlab(audience: Option<&str>) -> Result<String> {
    // gitlab tokens can be in any environment variable: we require the variable name to be
    // * "ID_TOKEN" if no audience is argument is used or
    // * "<AUDIENCE>_ID_TOKEN" where <AUDIENCE> is the audience string.

    if env::var("GITLAB_CI").is_err() {
        return Err(CIIDError::EnvironmentNotDetected);
    };

    let var_name = match audience {
        None => "ID_TOKEN".into(),
        Some(audience) => {
            let upper_audience = audience.to_uppercase();
            let re = Regex::new(r"[^A-Z0-9_]|^[^A-Z_]").unwrap();
            format!("{}_ID_TOKEN", re.replace_all(&upper_audience, "_"))
        }
    };
    log::debug!("GitLab Pipelines: Looking for token in {}", var_name);
    match env::var(&var_name) {
        Ok(token) => Ok(token),
        Err(_) => Err(CIIDError::EnvironmentError(format!(
            "GitLab Pipelines: {} is not set. This could imply that the \
            pipeline does not define an id token with that name",
            var_name
        ))),
    }
}

fn detect_circleci(audience: Option<&str>) -> Result<String> {
    if env::var("CIRCLECI").is_err() {
        return Err(CIIDError::EnvironmentNotDetected);
    };
    let payload;
    match audience {
        None => match env::var("CIRCLE_OIDC_TOKEN_V2") {
            Ok(token) => Ok(token),
            Err(_) => Err(CIIDError::EnvironmentError(
                "CircleCI: CIRCLE_OIDC_TOKEN_V2 is not set.".into(),
            )),
        },
        Some(audience) => {
            // TODO Use serde here? the audience string could be anything...
            payload = format!("{{\"aud\":\"{}\"}}", audience);
            let args = ["run", "oidc", "get", "--claims", &payload];
            match Command::new("circleci").args(args).output() {
                Ok(output) => match String::from_utf8(output.stdout) {
                    Ok(token) => Ok(token),
                    Err(_) => Err(CIIDError::EnvironmentError(
                        "CircleCI; Failed to read token".into(),
                    )),
                },
                Err(e) => Err(CIIDError::EnvironmentError(format!(
                    "CircleCI: Call to circle CLI failed: {}",
                    e
                ))),
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use std::{
        fs::{self, File},
        io::Write,
        os::unix::fs::PermissionsExt,
        sync::{Mutex, MutexGuard},
    };

    const TOKEN: &str = "eyJhbGciOiJSUzI1NiIsImtpZCI6IjMxNjA2OGMzM2ZhMjg2OTZhZmI5YzM5YWI2OTMxMjY1ZDk0Y2I3NTUifQ.eyJpc3MiOiJodHRwczovL29hdXRoMi5zaWdzdG9yZS5kZXYvYXV0aCIsInN1YiI6IkNnVXpNVGc0T1JJbWFIUjBjSE02SlRKR0pUSkdaMmwwYUhWaUxtTnZiU1V5Um14dloybHVKVEpHYjJGMWRHZyIsImF1ZCI6InNpZ3N0b3JlIiwiZXhwIjoxNzI5NTEyOTMwLCJpYXQiOjE3Mjk1MTI4NzAsIm5vbmNlIjoiNTI3NjM3Y2UtN2Q2MS00MDA5LThkM2EtNGNjZGM3OGJiZDg1IiwiYXRfaGFzaCI6IktmMUNPTXB5TVJDTkdzWWp1QXczclEiLCJlbWFpbCI6ImprdUBnb3RvLmZpIiwiZW1haWxfdmVyaWZpZWQiOnRydWUsImZlZGVyYXRlZF9jbGFpbXMiOnsiY29ubmVjdG9yX2lkIjoiaHR0cHM6Ly9naXRodWIuY29tL2xvZ2luL29hdXRoIiwidXNlcl9pZCI6IjMxODg5In19.s27uZ3vpIzRS4eWdC3pM0FSsYkHNvScQoii_TcSRVZhtrcPAbA4D95Pw_R_UB-qRquMK1BHepKmeN1b1-CQ00jiFZgUOf9sDLC3Hy3oQejGJsYKb-7oeHs7amLz3SBzPwDwVd09e-7Yu1x9YV5k6aezqruLLt42C_kyOTsHeCIWWMEVmGp32105Jkj8YT5uEYXS-aOEvQFvAYsDfKgGuiJtGybUycVcJEfqyWI3cami7fkjU5PcCx8oFyP2E7YNRw4UeNWCTn7WFtL2onrgDm0oa2AqF3gtH4Q-9ByksVq3y6xQdoLj1ydzWcoCzsF43oZ6O6DkLmWk5fu3FxNyewg";

    // Mutex for all tests that modify environment variables
    lazy_static! {
        static ref ENV_MUTEX: Mutex<()> = Mutex::new(());
    }

    struct SavedEnv<'a> {
        old_env: HashMap<&'a str, Option<String>>,
        _guard: MutexGuard<'a, ()>,
    }

    impl<'a> SavedEnv<'a> {
        fn new<T>(test_env: T) -> Self
        where
            T: IntoIterator<Item = (&'a str, Option<&'a str>)>,
        {
            // Tests can panic: assume our lock is still fine
            let guard = match ENV_MUTEX.lock() {
                Ok(guard) => guard,
                Err(poison) => poison.into_inner(),
            };

            // Store current env values, set the test values as the environment
            let mut old_env = HashMap::new();
            for (key, val) in test_env {
                let old_val = env::var(key).ok();
                old_env.insert(key, old_val);
                match val {
                    Some(val) => env::set_var(key, val),
                    None => env::remove_var(key),
                }
            }

            Self {
                old_env,
                _guard: guard,
            }
        }
    }

    impl<'a> Drop for SavedEnv<'a> {
        fn drop(&mut self) {
            for (key, val) in self.old_env.drain() {
                match val {
                    Some(val) => env::set_var(key, val),
                    None => env::remove_var(key),
                }
            }
        }
    }

    fn run_with_env<'a, T, F>(test_env: T, f: F)
    where
        F: Fn(),
        T: IntoIterator<Item = (&'a str, Option<&'a str>)>,
    {
        // Prepares env variables according to `env`, runs the function, then returns environment
        // to old values
        let saved_env = SavedEnv::new(test_env);
        f();
        drop(saved_env);
    }

    #[test]
    fn circle_not_detected() {
        run_with_env([("CIRCLECI", None)], || {
            assert_eq!(
                detect_circleci(None),
                Err(CIIDError::EnvironmentNotDetected)
            );
        });
    }

    #[test]
    fn circleci_env_failure() {
        run_with_env(
            // empty the path so that this does not accidentally succeed on CircleCI
            [("CIRCLECI", Some("1")), ("PATH", Some(""))],
            || {
                assert!(matches!(
                    detect_circleci("my-audience".into()).unwrap_err(),
                    CIIDError::EnvironmentError(_)
                ));
            },
        );

        run_with_env(
            // default audience uses specific env var
            [("CIRCLECI", Some("1")), ("CIRCLE_OIDC_TOKEN_V2", None)],
            || {
                assert!(matches!(
                    detect_circleci(None).unwrap_err(),
                    CIIDError::EnvironmentError(_)
                ));
            },
        );
    }

    #[test]
    fn circleci_success() {
        // create a fake 'circleci' executable
        let tmpdir = tempfile::tempdir().unwrap();
        let dir_path = tmpdir.into_path();
        let path = dir_path.join("circleci");
        let mut f = File::create(&path).unwrap();
        let script = format!("#!/bin/sh\necho -n {}\n", TOKEN);
        f.write_all(script.as_bytes()).unwrap();
        let mut permissions = f.metadata().unwrap().permissions();
        drop(f);
        permissions.set_mode(0o744);
        fs::set_permissions(path, permissions).unwrap();

        // Make sure the fake executable is in PATH, then test non-default audience
        run_with_env(
            // empty the path so that this does not accidentally succeed on CircleCI
            [
                ("CIRCLECI", Some("1")),
                ("PATH", Some(dir_path.to_str().unwrap())),
            ],
            || {
                assert_eq!(detect_circleci("my-audience".into()), Ok(TOKEN.into()));
            },
        );

        run_with_env(
            [
                ("CIRCLECI", Some("1")),
                ("CIRCLE_OIDC_TOKEN_V2", Some(TOKEN)),
            ],
            || {
                assert_eq!(detect_circleci(None), Ok(TOKEN.into()));
            },
        );
    }

    #[test]
    fn github_not_detected() {
        run_with_env([("GITHUB_ACTIONS", None)], || {
            assert_eq!(detect_github(None), Err(CIIDError::EnvironmentNotDetected));
        });
    }

    #[test]
    fn github_env_failure() {
        // Missing env variables
        run_with_env(
            [
                ("GITHUB_ACTIONS", Some("1")),
                ("ACTIONS_ID_TOKEN_REQUEST_TOKEN", None),
            ],
            || {
                assert!(matches!(
                    detect_github(None).unwrap_err(),
                    CIIDError::EnvironmentError(_)
                ));
            },
        );
        run_with_env(
            [
                ("GITHUB_ACTIONS", Some("1")),
                ("ACTIONS_ID_TOKEN_REQUEST_TOKEN", Some("token")),
                ("ACTIONS_ID_TOKEN_REQUEST_URL", None),
            ],
            || {
                assert!(matches!(
                    detect_github(None).unwrap_err(),
                    CIIDError::EnvironmentError(_)
                ));
            },
        );

        // request fails
        run_with_env(
            [
                ("GITHUB_ACTIONS", Some("1")),
                ("ACTIONS_ID_TOKEN_REQUEST_TOKEN", Some("token")),
                ("ACTIONS_ID_TOKEN_REQUEST_URL", Some("http://invalid")),
            ],
            || {
                assert_eq!(
                    detect_github(None).unwrap_err(),
                    CIIDError::EnvironmentError("GitHub Actions: Token request failed: error sending request for url (http://invalid/)".into())
                );
            },
        );
    }

    // TODO This requires mocking reqwest response
    // fn github_success() { }

    #[test]
    fn gitlab_not_detected() {
        run_with_env([("GITLAB_CI", None)], || {
            assert_eq!(detect_gitlab(None), Err(CIIDError::EnvironmentNotDetected));
        });
    }

    #[test]
    fn gitlab_env_failure() {
        // Missing token variable for default audience
        run_with_env([("GITLAB_CI", Some("1")), ("ID_TOKEN", None)], || {
            assert!(matches!(
                detect_gitlab(None).unwrap_err(),
                CIIDError::EnvironmentError(_)
            ));
        });

        // Missing token variable for non-default audience
        run_with_env(
            [("GITLAB_CI", Some("1")), ("MY_AUD_ID_TOKEN", None)],
            || {
                assert!(matches!(
                    detect_gitlab(Some("my-aud")).unwrap_err(),
                    CIIDError::EnvironmentError(_)
                ));
            },
        );
    }

    #[test]
    fn gitlab_success() {
        run_with_env(
            [("GITLAB_CI", Some("1")), ("ID_TOKEN", Some(TOKEN))],
            || {
                assert_eq!(detect_gitlab(None), Ok(TOKEN.into()));
            },
        );

        run_with_env(
            [("GITLAB_CI", Some("1")), ("MY_AUD_ID_TOKEN", Some(TOKEN))],
            || {
                assert_eq!(detect_gitlab(Some("my-aud")), Ok(TOKEN.into()));
            },
        );
    }

    #[test]
    fn detect_credentials_no_environments() {
        run_with_env(
            [
                ("CIRCLECI", None),
                ("GITLAB_CI", None),
                ("GITHUB_ACTIONS", None),
            ],
            || {
                assert_eq!(
                    detect_credentials(None),
                    Err(CIIDError::EnvironmentNotDetected)
                );
            },
        );
    }

    #[test]
    fn detect_credentials_failure() {
        // Unexpected failure in any detector leads to detect_credentials failure.
        run_with_env(
            [
                ("GITHUB_ACTIONS", Some("1")),
                ("ACTIONS_ID_TOKEN_REQUEST_TOKEN", None),
            ],
            || {
                assert!(matches!(
                    detect_credentials(None).unwrap_err(),
                    CIIDError::EnvironmentError(_)
                ));
            },
        );
    }

    #[test]
    fn detect_credentials_malformed_token() {
        // need to disable GitHub, otherwise we get a "false" positive on CI...
        run_with_env(
            [
                ("GITHUB_ACTIONS", None),
                ("GITLAB_CI", Some("1")),
                ("ID_TOKEN", Some("token value")),
            ],
            || {
                assert_eq!(detect_credentials(None), Err(CIIDError::MalformedToken));
            },
        );
    }

    #[test]
    fn detect_credentials_success() {
        // need to disable GitHub, otherwise we get a "false" positive on CI...
        run_with_env(
            [
                ("GITHUB_ACTIONS", None),
                ("GITLAB_CI", Some("1")),
                ("ID_TOKEN", Some(TOKEN)),
            ],
            || {
                assert_eq!(detect_credentials(None), Ok(TOKEN.into()));
            },
        );

        run_with_env(
            [
                ("GITHUB_ACTIONS", None),
                ("GITLAB_CI", Some("1")),
                ("MY_AUD_ID_TOKEN", Some(TOKEN)),
            ],
            || {
                assert_eq!(detect_credentials(Some("my-aud")), Ok(TOKEN.into()));
            },
        );
    }
}