Skip to content

Commit

Permalink
use the gitlab server cert for non-admin requests
Browse files Browse the repository at this point in the history
  • Loading branch information
fdbastionamio committed Jul 19, 2023
1 parent f3ad70f commit c4885bd
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub struct GitlabConfig {
#[serde(default = "GitlabConfig::default_token_expiry")]
pub token_expiry: Duration,
#[serde(default)]
pub ssl_cert : Option<String>
pub ssl_cert: Option<String>,
}

impl GitlabConfig {
Expand Down
55 changes: 32 additions & 23 deletions src/providers/gitlab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::providers::{Release, User};
use async_trait::async_trait;
use futures::{stream::FuturesUnordered, StreamExt, TryStreamExt};
use percent_encoding::{utf8_percent_encode, NON_ALPHANUMERIC};
use reqwest::{Certificate, header};
use reqwest::{header, Certificate};
use serde::{Deserialize, Serialize};
use std::{borrow::Cow, sync::Arc};
use time::{Duration, OffsetDateTime};
Expand All @@ -16,6 +16,7 @@ pub struct Gitlab {
client: reqwest::Client,
base_url: Url,
token_expiry: Duration,
ssl_cert: Option<Certificate>,
}

impl Gitlab {
Expand All @@ -26,20 +27,23 @@ impl Gitlab {
header::HeaderValue::from_str(&config.admin_token)?,
);

let mut client_builder = reqwest::ClientBuilder::new()
.default_headers(headers);
let mut client_builder = reqwest::ClientBuilder::new().default_headers(headers);

if let Some(cert_path) = &config.ssl_cert {
let gitlab_cert_bytes = std::fs::read(&cert_path)?;
let gitlab_cert = Certificate::from_pem(&gitlab_cert_bytes)?;
client_builder = client_builder.add_root_certificate(gitlab_cert);
}
let ssl_cert = match &config.ssl_cert {
Some(cert_path) => {
let gitlab_cert_bytes = std::fs::read(&cert_path)?;

Check warning on line 34 in src/providers/gitlab.rs

View workflow job for this annotation

GitHub Actions / Clippy

the borrowed expression implements the required traits

Check warning on line 34 in src/providers/gitlab.rs

View workflow job for this annotation

GitHub Actions / Clippy

the borrowed expression implements the required traits
let gitlab_cert = Certificate::from_pem(&gitlab_cert_bytes)?;
client_builder = client_builder.add_root_certificate(gitlab_cert.clone());
Some(gitlab_cert)
}
_ => None,
};

Ok(Self {
client: client_builder
.build()?,
client: client_builder.build()?,
base_url: config.uri.join("api/v4/")?,
token_expiry: config.token_expiry,
ssl_cert,
})
}
}
Expand All @@ -57,18 +61,23 @@ impl super::UserProvider for Gitlab {
};

if username == "gitlab-ci-token" {
// we're purposely not using `self.client` here as we don't
// want to use our admin token for this request but still want to use any ssl cert provided.
let mut client_builder = reqwest::Client::builder();
if let Some(cert) = &self.ssl_cert {
client_builder = client_builder.add_root_certificate(cert.clone())

Check failure on line 68 in src/providers/gitlab.rs

View workflow job for this annotation

GitHub Actions / Clippy

consider adding a `;` to the last statement for consistent formatting

Check failure on line 68 in src/providers/gitlab.rs

View workflow job for this annotation

GitHub Actions / Clippy

consider adding a `;` to the last statement for consistent formatting
}
let client = client_builder.build();
let res: GitlabJobResponse = handle_error(
// we're purposely not using `self.client` here as we don't
// want to use our admin token for this request
reqwest::Client::new()
client?
.get(self.base_url.join("job/")?)
.header("JOB-TOKEN", password)
.send()
.await?,
)
.await?
.json()
.await?;
.await?
.json()
.await?;

Ok(Some(User {
id: res.user.id,
Expand Down Expand Up @@ -113,9 +122,9 @@ impl super::UserProvider for Gitlab {
.send()
.await?,
)
.await?
.json()
.await?;
.await?
.json()
.await?;

Ok(impersonation_token.token)
}
Expand Down Expand Up @@ -190,9 +199,9 @@ impl super::PackageProvider for Gitlab {
.send()
.await?,
)
.await?
.json()
.await?;
.await?
.json()
.await?;

let expected_file_name =
format!("{}-{}.crate", release.name, release.version);
Expand All @@ -213,7 +222,7 @@ impl super::PackageProvider for Gitlab {
}),
)
}
.instrument(info_span!("fetch_package_files")),
.instrument(info_span!("fetch_package_files")),
));
}
}
Expand Down

0 comments on commit c4885bd

Please sign in to comment.