Skip to content

Commit 106b4ca

Browse files
committed
Add expires_at field for impersonation_tokens POST request
1 parent 3e8a188 commit 106b4ca

File tree

7 files changed

+27
-8
lines changed

7 files changed

+27
-8
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ serde = { version = "1.0", features = ["derive"] }
3131
serde_json = "1"
3232
sha1 = "0.10"
3333
shlex = "1.1"
34+
time = { version = "0.3", features = ["serde"] }
3435
tracing = "0.1"
3536
tracing-subscriber = "0.3"
3637
thrussh = "0.33"

src/config.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use clap::Parser;
44
use serde::{de::DeserializeOwned, Deserialize};
55
use std::{net::SocketAddr, path::PathBuf};
6+
use time::Duration;
67
use url::Url;
78

89
#[derive(Parser)]
@@ -25,6 +26,15 @@ pub struct Config {
2526
pub struct GitlabConfig {
2627
pub uri: Url,
2728
pub admin_token: String,
29+
#[serde(default = "GitlabConfig::default_token_expiry")]
30+
pub token_expiry: Duration,
31+
}
32+
33+
impl GitlabConfig {
34+
#[must_use]
35+
const fn default_token_expiry() -> Duration {
36+
Duration::days(30)
37+
}
2838
}
2939

3040
pub fn from_toml_path<T: DeserializeOwned>(path: &str) -> Result<T, std::io::Error> {

src/git_command_handlers/ls_refs.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ pub fn handle<U: UserProvider + PackageProvider + Send + Sync + 'static>(
1919
_metadata: &[Bytes],
2020
commit_hash: &HashOutput,
2121
) -> Result<(), anyhow::Error> {
22-
let commit_hash = hex::encode(&commit_hash);
22+
let commit_hash = hex::encode(commit_hash);
2323

2424
handle.write(PktLine::Data(
25-
format!("{} HEAD symref-target:refs/heads/master", commit_hash).as_bytes(),
25+
format!("{commit_hash} HEAD symref-target:refs/heads/master").as_bytes(),
2626
))?;
2727
handle.write(PktLine::Flush)?;
2828
handle.flush(session, channel);

src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ async fn main() -> anyhow::Result<()> {
8484
.ok_or_else(|| anyhow!("failed to generate server private key"))?;
8585
let thrussh_keys::key::KeyPair::Ed25519(key) = key;
8686

87-
std::fs::write(server_private_key, &key.key)?;
87+
std::fs::write(server_private_key, key.key)?;
8888

8989
thrussh_keys::key::KeyPair::Ed25519(key)
9090
};
@@ -350,7 +350,7 @@ type AsyncHandlerFut<T, U> =
350350
dyn Future<Output = Result<T, <Handler<U> as thrussh::server::Handler>::Error>> + Send;
351351

352352
#[allow(clippy::type_complexity)]
353-
impl<'a, U: UserProvider + PackageProvider + Send + Sync + 'static> thrussh::server::Handler
353+
impl<U: UserProvider + PackageProvider + Send + Sync + 'static> thrussh::server::Handler
354354
for Handler<U>
355355
{
356356
type Error = anyhow::Error;

src/providers/gitlab.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ use percent_encoding::{utf8_percent_encode, NON_ALPHANUMERIC};
88
use reqwest::header;
99
use serde::{Deserialize, Serialize};
1010
use std::{borrow::Cow, sync::Arc};
11+
use time::{Duration, OffsetDateTime};
1112
use tracing::{info_span, instrument, Instrument};
1213
use url::Url;
1314

1415
pub struct Gitlab {
1516
client: reqwest::Client,
1617
base_url: Url,
18+
token_expiry: Duration,
1719
}
1820

1921
impl Gitlab {
@@ -29,6 +31,7 @@ impl Gitlab {
2931
.default_headers(headers)
3032
.build()?,
3133
base_url: config.uri.join("api/v4/")?,
34+
token_expiry: config.token_expiry,
3235
})
3336
}
3437
}
@@ -41,9 +44,8 @@ impl super::UserProvider for Gitlab {
4144
username_password: &str,
4245
) -> anyhow::Result<Option<User>> {
4346
let mut splitter = username_password.splitn(2, ':');
44-
let (username, password) = match (splitter.next(), splitter.next()) {
45-
(Some(username), Some(password)) => (username, password),
46-
_ => return Ok(None),
47+
let (Some(username), Some(password)) = (splitter.next(), splitter.next()) else {
48+
return Ok(None);
4749
};
4850

4951
if username == "gitlab-ci-token" {
@@ -95,6 +97,9 @@ impl super::UserProvider for Gitlab {
9597
)
9698
.json(&GitlabImpersonationTokenRequest {
9799
name: env!("CARGO_PKG_NAME"),
100+
expires_at: (OffsetDateTime::now_utc() + self.token_expiry)
101+
.date()
102+
.to_string(),
98103
scopes: vec!["api"],
99104
})
100105
.send()
@@ -273,6 +278,7 @@ impl GitlabCratePath {
273278
#[derive(Serialize)]
274279
pub struct GitlabImpersonationTokenRequest {
275280
name: &'static str,
281+
expires_at: String,
276282
scopes: Vec<&'static str>,
277283
}
278284

src/util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use ustr::ustr;
99

1010
#[must_use]
1111
pub fn format_fingerprint(fingerprint: &str) -> String {
12-
format!("SHA256:{}", fingerprint)
12+
format!("SHA256:{fingerprint}")
1313
}
1414

1515
/// Crates with a total of 1, 2 characters in the same are written out to directories named

0 commit comments

Comments
 (0)