diff --git a/Readme.md b/Readme.md index df378e5..dbe6f2f 100644 --- a/Readme.md +++ b/Readme.md @@ -65,6 +65,8 @@ services: # Optional GitLab parameters # The base URL for API calls, e.g. "https://gitlab.com/" - GITLAB_URL= + # Format of the repository name on GitLab. Must contain a "#" which is replaced with the site name. Example: "bridgehead-configurations/bridgehead-config-#" + - GITLAB_REPO_FORMAT= # A long-living personal (or impersonation) access token that is used to create short-living project access tokens. Requires at least the "api" scope. Note that group access tokens and project access tokens cannot be used to create project access tokens. - GITLAB_API_ACCESS_TOKEN= ``` @@ -88,6 +90,6 @@ Create a GitLab project access token for read access (git clone/pull) to the bri Secret type: `GitLabProjectAccessToken` -The argument is always `bridgehead-configuration`. +The third value after the final `:` is unused. -Example: `GitLabProjectAccessToken:GIT_CONFIG_REPO_TOKEN:bridgehead-configuration` \ No newline at end of file +Example: `GitLabProjectAccessToken:GIT_CONFIG_REPO_TOKEN:` \ No newline at end of file diff --git a/central/src/gitlab.rs b/central/src/gitlab.rs index 2a5acb2..a860ccf 100644 --- a/central/src/gitlab.rs +++ b/central/src/gitlab.rs @@ -9,6 +9,9 @@ struct GitLabApiConfig { /// The base URL for API calls, e.g. "https://gitlab.com/" #[clap(long, env)] pub gitlab_url: Url, + /// Format of the repository name on GitLab. Must contain a "#" which is replaced with the site name. Example: "bridgehead-configurations/bridgehead-config-#" + #[clap(long, env)] + pub gitlab_repo_format: String, /// A long-living personal (or impersonation) access token that is used to create short-living project access tokens. Requires at least the "api" scope. Note that group access tokens and project access tokens cannot be used to create project access tokens. #[clap(long, env)] pub gitlab_api_access_token: String, @@ -24,28 +27,6 @@ pub struct GitLabProjectAccessTokenProvider { client: reqwest::Client, } -/// Derive the bridgehead configuration repository from the beam id of the requester. -/// Example return value: "bridgehead-configurations/bridgehead-config-berlin" -fn derive_bridgehead_config_repo_from_beam_id(requester: &AppId) -> Result { - let mut parts = requester.as_ref().splitn(3, '.'); - let (_app, proxy, broker) = (parts.next().unwrap(), parts.next().unwrap(), parts.next().unwrap()); - - let group = match broker { - "broker.ccp-it.dktk.dkfz.de" => "bridgehead-configurations", // ccp - "broker.bbmri.sample.de" => "bbmri-bridgehead-configs", // bbmri - "broker.bbmri.de" => "bbmri-bridgehead-configs", // gbn - "test-no-real-data.broker.samply.de" => "bridgehead-configurations", // cce, itcc, kr - "broker.hector.dkfz.de" => "dhki", // dhki - _ => return Err(format!("Bridgehead configuration repository group not known for broker {broker}")), - }; - - Ok(match group { - "bridgehead-configurations" => format!("{group}/bridgehead-config-{proxy}"), - "dhki" => format!("{group}/{proxy}-bk"), - _ => format!("{group}/{proxy}"), - }) -} - impl GitLabProjectAccessTokenProvider { pub fn try_init() -> Option { match GitLabApiConfig::try_parse() { @@ -65,7 +46,8 @@ impl GitLabProjectAccessTokenProvider { &self, requester: &AppId, ) -> Result { - let project_path = derive_bridgehead_config_repo_from_beam_id(requester)?; + let name = requester.as_ref().split('.').nth(1).unwrap(); + let gitlab_repo = self.config.gitlab_repo_format.replace('#', name); // Expire in 1 week let expires_at = (chrono::Local::now() + chrono::TimeDelta::weeks(1)) @@ -79,7 +61,7 @@ impl GitLabProjectAccessTokenProvider { .gitlab_url .join(&format!( "api/v4/projects/{}/access_tokens", - urlencoding::encode(&project_path) + urlencoding::encode(&gitlab_repo) )) .map_err(|e| e.to_string())?, ) @@ -115,7 +97,8 @@ impl GitLabProjectAccessTokenProvider { requester: &AppId, secret: &str, ) -> Result { - let project_path = derive_bridgehead_config_repo_from_beam_id(requester)?; + let name = requester.as_ref().split('.').nth(1).unwrap(); + let gitlab_repo = self.config.gitlab_repo_format.replace('#', name); let response = self .client @@ -124,7 +107,7 @@ impl GitLabProjectAccessTokenProvider { .gitlab_url .join(&format!( "{}.git/info/refs?service=git-upload-pack", - project_path + gitlab_repo )) .map_err(|e| e.to_string())?, ) diff --git a/local/src/config.rs b/local/src/config.rs index 996cf5a..d967d2d 100644 --- a/local/src/config.rs +++ b/local/src/config.rs @@ -69,10 +69,7 @@ impl FromStr for SecretArg { })) } "GitLabProjectAccessToken" => { - match args { - "bridgehead-configuration" => Ok(SecretRequest::GitLabProjectAccessToken), - _ => return Err(format!("Invalid GitLabProjectAccessToken parameter '{args}'")), - } + Ok(SecretRequest::GitLabProjectAccessToken) } _ => Err(format!("Unknown secret type {secret_type}")), }?;