Skip to main content

deslicer_cli/ci/
mod.rs

1pub mod azure;
2pub mod bitbucket;
3pub mod github;
4pub mod gitlab;
5pub mod local;
6
7pub const AUDIENCE: &str = "https://api.deslicer.ai";
8
9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10pub enum CiPlatform {
11    Github,
12    Gitlab,
13    Azure,
14    Bitbucket,
15    Local,
16}
17
18impl CiPlatform {
19    pub fn header_value(self) -> &'static str {
20        match self {
21            CiPlatform::Github => "github",
22            CiPlatform::Gitlab => "gitlab",
23            CiPlatform::Azure => "azure",
24            CiPlatform::Bitbucket => "bitbucket",
25            CiPlatform::Local => "local",
26        }
27    }
28}
29
30#[derive(Debug, thiserror::Error)]
31pub enum OidcError {
32    #[error("missing environment variable: {0}")]
33    MissingEnv(String),
34    #[error("HTTP error: {0}")]
35    Http(String),
36    #[error("unsupported: {0}")]
37    Unsupported(String),
38    #[error("{0}")]
39    Other(String),
40}
41
42#[async_trait::async_trait]
43pub trait OidcTokenProvider: Send + Sync {
44    async fn fetch_token(&self, audience: &str) -> Result<String, OidcError>;
45}
46
47pub fn detect_platform(override_platform: Option<CiPlatform>) -> CiPlatform {
48    if let Some(platform) = override_platform {
49        return platform;
50    }
51    if std::env::var("GITHUB_ACTIONS").is_ok() {
52        return CiPlatform::Github;
53    }
54    if std::env::var("GITLAB_CI").is_ok() {
55        return CiPlatform::Gitlab;
56    }
57    if std::env::var("TF_BUILD").is_ok() {
58        return CiPlatform::Azure;
59    }
60    if std::env::var("BITBUCKET_BUILD_NUMBER").is_ok() {
61        return CiPlatform::Bitbucket;
62    }
63    CiPlatform::Local
64}
65
66pub fn provider_for(platform: CiPlatform) -> Box<dyn OidcTokenProvider> {
67    match platform {
68        CiPlatform::Github => Box::new(github::GithubProvider),
69        CiPlatform::Gitlab => Box::new(gitlab::GitlabProvider),
70        CiPlatform::Azure => Box::new(azure::AzureProvider),
71        CiPlatform::Bitbucket => Box::new(bitbucket::BitbucketProvider),
72        CiPlatform::Local => Box::new(local::LocalProvider),
73    }
74}