Skip to content
Snippets Groups Projects
Unverified Commit 445a5163 authored by Geoffrey Arthaud's avatar Geoffrey Arthaud
Browse files

api: add specific connection checking with job token

parent 3657f7c9
No related branches found
No related tags found
No related merge requests found
Pipeline #390450 passed
# v0.1609.2 (unreleased)
## Fixes
* Job token authentication is now compliant with its limited permissions.
# v0.1609.1 # v0.1609.1
## Fixes ## Fixes
......
...@@ -7,8 +7,10 @@ ...@@ -7,8 +7,10 @@
use http::{HeaderMap, HeaderValue}; use http::{HeaderMap, HeaderValue};
use log::error; use log::error;
use serde::Deserialize; use serde::Deserialize;
use std::env;
use thiserror::Error; use thiserror::Error;
use crate::api::projects::packages::Packages;
use crate::api::users::CurrentUser; use crate::api::users::CurrentUser;
use crate::api::{self, AsyncQuery, Query}; use crate::api::{self, AsyncQuery, Query};
...@@ -20,6 +22,22 @@ pub enum AuthError { ...@@ -20,6 +22,22 @@ pub enum AuthError {
#[from] #[from]
source: http::header::InvalidHeaderValue, source: http::header::InvalidHeaderValue,
}, },
/// The CI environment is misconfigured
#[error("CI environment is missing `CI_PROJECT_ID`")]
CiEnvironmentMissing,
/// The CI environment is misconfigured
#[error("CI environment has a non-integer `CI_PROJECT_ID`")]
CiEnvironmentInvalid,
}
impl AuthError {
fn ci_environment_missing() -> Self {
Self::CiEnvironmentMissing
}
fn ci_environment_invalid() -> Self {
Self::CiEnvironmentInvalid
}
} }
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
...@@ -85,11 +103,18 @@ impl Auth { ...@@ -85,11 +103,18 @@ impl Auth {
where where
C: api::Client, C: api::Client,
{ {
if let Self::None = self { match self {
// There does not seem to be an unparameterized endpoint that can be used to reliably Self::None => {
// detect whether the connection will work or not. // There does not seem to be an unparameterized endpoint that can be used to reliably
} else { // detect whether the connection will work or not.
let _: UserPublic = CurrentUser::builder().build().unwrap().query(api)?; },
Self::JobToken(_) => {
let project_id = Self::_ci_project_id()?;
api::ignore(Packages::builder().project(project_id).build().unwrap()).query(api)?;
},
Self::Token(_) | Self::OAuth2(_) => {
let _: UserPublic = CurrentUser::builder().build().unwrap().query(api)?;
},
} }
Ok(()) Ok(())
...@@ -99,17 +124,33 @@ impl Auth { ...@@ -99,17 +124,33 @@ impl Auth {
where where
C: api::AsyncClient + Sync, C: api::AsyncClient + Sync,
{ {
if let Self::None = self { match self {
// There does not seem to be an unparameterized endpoint that can be used to reliably Self::None => {
// detect whether the connection will work or not. // There does not seem to be an unparameterized endpoint that can be used to reliably
} else { // detect whether the connection will work or not.
let _: UserPublic = CurrentUser::builder() },
.build() Self::JobToken(_) => {
.unwrap() let project_id = Self::_ci_project_id()?;
.query_async(api) api::ignore(Packages::builder().project(project_id).build().unwrap())
.await?; .query_async(api)
.await?;
},
Self::Token(_) | Self::OAuth2(_) => {
let _: UserPublic = CurrentUser::builder()
.build()
.unwrap()
.query_async(api)
.await?;
},
} }
Ok(()) Ok(())
} }
fn _ci_project_id() -> Result<u64, AuthError> {
env::var("CI_PROJECT_ID")
.map_err(|_| AuthError::ci_environment_missing())?
.parse::<u64>()
.map_err(|_| AuthError::ci_environment_invalid())
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment