Periodically fetch JWT only in GHA
This commit is contained in:
parent
90180e31ef
commit
136a3d43d6
|
@ -7,6 +7,16 @@ pub enum Environment {
|
||||||
Other,
|
Other,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Environment {
|
||||||
|
pub fn is_github_actions(&self) -> bool {
|
||||||
|
matches!(self, Self::GitHubActions)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_gitlab_ci(&self) -> bool {
|
||||||
|
matches!(self, Self::GitLabCI)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl ToString for Environment {
|
impl ToString for Environment {
|
||||||
fn to_string(&self) -> String {
|
fn to_string(&self) -> String {
|
||||||
use Environment::*;
|
use Environment::*;
|
||||||
|
@ -14,7 +24,7 @@ impl ToString for Environment {
|
||||||
String::from(match self {
|
String::from(match self {
|
||||||
GitHubActions => "GitHub Actions",
|
GitHubActions => "GitHub Actions",
|
||||||
GitLabCI => "GitLab CI",
|
GitLabCI => "GitLab CI",
|
||||||
_ => "unspecified",
|
_ => "an unspecified environment",
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
use crate::env::Environment;
|
||||||
use crate::error::{Error, Result};
|
use crate::error::{Error, Result};
|
||||||
use attic::cache::CacheName;
|
use attic::cache::CacheName;
|
||||||
use attic::nix_store::{NixStore, StorePath};
|
use attic::nix_store::{NixStore, StorePath};
|
||||||
|
@ -27,6 +28,7 @@ pub struct State {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn init_cache(
|
pub async fn init_cache(
|
||||||
|
environment: Environment,
|
||||||
flakehub_api_server: &Url,
|
flakehub_api_server: &Url,
|
||||||
flakehub_api_server_netrc: &Path,
|
flakehub_api_server_netrc: &Path,
|
||||||
flakehub_cache_server: &Url,
|
flakehub_cache_server: &Url,
|
||||||
|
@ -102,6 +104,8 @@ pub async fn init_cache(
|
||||||
let api_inner = ApiClient::from_server_config(server_config)?;
|
let api_inner = ApiClient::from_server_config(server_config)?;
|
||||||
let api = Arc::new(RwLock::new(api_inner));
|
let api = Arc::new(RwLock::new(api_inner));
|
||||||
|
|
||||||
|
// Periodically refresh JWT in GitHub Actions environment
|
||||||
|
if environment.is_github_actions() {
|
||||||
// NOTE(cole-h): This is a workaround -- at the time of writing, GitHub Actions JWTs are only
|
// NOTE(cole-h): This is a workaround -- at the time of writing, GitHub Actions JWTs are only
|
||||||
// valid for 5 minutes after being issued. FlakeHub uses these JWTs for authentication, which
|
// valid for 5 minutes after being issued. FlakeHub uses these JWTs for authentication, which
|
||||||
// means that after those 5 minutes have passed and the token is expired, FlakeHub (and by
|
// means that after those 5 minutes have passed and the token is expired, FlakeHub (and by
|
||||||
|
@ -112,12 +116,14 @@ pub async fn init_cache(
|
||||||
let initial_github_jwt_clone = flakehub_password.clone();
|
let initial_github_jwt_clone = flakehub_password.clone();
|
||||||
let flakehub_cache_server_clone = flakehub_cache_server.to_string();
|
let flakehub_cache_server_clone = flakehub_cache_server.to_string();
|
||||||
let api_clone = api.clone();
|
let api_clone = api.clone();
|
||||||
|
|
||||||
tokio::task::spawn(refresh_github_actions_jwt_worker(
|
tokio::task::spawn(refresh_github_actions_jwt_worker(
|
||||||
netrc_path_clone,
|
netrc_path_clone,
|
||||||
initial_github_jwt_clone,
|
initial_github_jwt_clone,
|
||||||
flakehub_cache_server_clone,
|
flakehub_cache_server_clone,
|
||||||
api_clone,
|
api_clone,
|
||||||
));
|
));
|
||||||
|
}
|
||||||
|
|
||||||
// Get the cache UUID for this project.
|
// Get the cache UUID for this project.
|
||||||
let cache_name = {
|
let cache_name = {
|
||||||
|
|
|
@ -32,9 +32,11 @@ use ::attic::nix_store::NixStore;
|
||||||
use anyhow::{anyhow, Context, Result};
|
use anyhow::{anyhow, Context, Result};
|
||||||
use axum::{extract::Extension, routing::get, Router};
|
use axum::{extract::Extension, routing::get, Router};
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
|
use env::Environment;
|
||||||
use tempfile::NamedTempFile;
|
use tempfile::NamedTempFile;
|
||||||
use tokio::process::Command;
|
use tokio::process::Command;
|
||||||
use tokio::sync::{oneshot, Mutex, RwLock};
|
use tokio::sync::{oneshot, Mutex, RwLock};
|
||||||
|
use tracing_subscriber::field::debug;
|
||||||
use tracing_subscriber::filter::EnvFilter;
|
use tracing_subscriber::filter::EnvFilter;
|
||||||
|
|
||||||
use gha_cache::Credentials;
|
use gha_cache::Credentials;
|
||||||
|
@ -115,6 +117,18 @@ struct Args {
|
||||||
startup_notification_url: Option<reqwest::Url>,
|
startup_notification_url: Option<reqwest::Url>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Args {
|
||||||
|
fn validate(&self, environment: Environment) -> Result<(), error::Error> {
|
||||||
|
if environment.is_gitlab_ci() && self.use_gha_cache {
|
||||||
|
return Err(Error::Config(String::from(
|
||||||
|
"the --use-gha-cache flag should not be applied in GitLab CI",
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// The global server state.
|
/// The global server state.
|
||||||
struct StateInner {
|
struct StateInner {
|
||||||
/// State for uploading to the GHA cache.
|
/// State for uploading to the GHA cache.
|
||||||
|
@ -143,8 +157,9 @@ async fn main_cli() -> Result<()> {
|
||||||
init_logging();
|
init_logging();
|
||||||
|
|
||||||
let args = Args::parse();
|
let args = Args::parse();
|
||||||
|
let environment = determine_environment();
|
||||||
let _environment = determine_environment();
|
tracing::debug!("Running in {environment}");
|
||||||
|
args.validate(environment)?;
|
||||||
|
|
||||||
let metrics = Arc::new(telemetry::TelemetryReport::new());
|
let metrics = Arc::new(telemetry::TelemetryReport::new());
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue