🏡 index : ~doyle/gitlab-cargo-shim.git

author Jordan Doyle <jordan@doyle.la> 2022-03-18 2:37:09.0 +00:00:00
committer Jordan Doyle <jordan@doyle.la> 2022-03-18 2:41:02.0 +00:00:00
commit
faf7542de7e2dd4b1b7a4e1302c782ff2bdb2b17 [patch]
tree
96eb7d55de6603bdea47eb9757b3d08571730170
parent
ad4cd6b795b38de3e563372254e8fd287ef684f7
download
faf7542de7e2dd4b1b7a4e1302c782ff2bdb2b17.tar.gz

Replace GitLab patch with Cargo patch



Diff

 Cargo.lock              | 37 +++++++++++++++++++++++++------------
 README.md               |  8 ++++----
 src/main.rs             | 22 +++++++++++++---------
 src/metadata.rs         |  8 +++++---
 src/providers/gitlab.rs | 20 ++++++++++++++++----
 src/providers/mod.rs    |  7 ++++++-
 6 files changed, 59 insertions(+), 43 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock
index 13a5b56..5c6db58 100644
--- a/Cargo.lock
+++ a/Cargo.lock
@@ -381,9 +381,9 @@

[[package]]
name = "dirs-sys"
version = "0.3.6"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "03d86534ed367a67548dc68113a0f5db55432fdfbb6e6f9d77704397d95d5780"
checksum = "1b1d1d91c932ef41c0f2663aa8b0ca0342d444d842c06914aa0a7e352d0bada6"
dependencies = [
 "libc",
 "redox_users",
@@ -795,9 +795,9 @@

[[package]]
name = "libc"
version = "0.2.119"
version = "0.2.120"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1bf2e165bb3457c8e098ea76f3e3bc9db55f87aa90d52d0e6be741470916aaa4"
checksum = "ad5c14e80759d0939d013e6ca49930e59fc53dd8e5009132f76240c179380c09"

[[package]]
name = "libsodium-sys"
@@ -865,9 +865,9 @@

[[package]]
name = "mio"
version = "0.8.1"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ba42135c6a5917b9db9cd7b293e5409e1c6b041e6f9825e92e55a894c63b6f8"
checksum = "52da4364ffb0e4fe33a9841a98a3f3014fb964045ce4f7a45a398243c8d6b0c9"
dependencies = [
 "libc",
 "log",
@@ -955,9 +955,9 @@

[[package]]
name = "num_threads"
version = "0.1.4"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c539a50b93a303167eded6e8dff5220cd39447409fb659f4cd24b1f72fe4f133"
checksum = "aba1801fb138d8e85e11d0fc70baf4fe1cdfffda7c6cd34a854905df588e5ed0"
dependencies = [
 "libc",
]
@@ -1164,9 +1164,9 @@

[[package]]
name = "quote"
version = "1.0.15"
version = "1.0.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "864d3e96a899863136fc6e99f3d7cae289dafe43bf2c5ac19b70df7210c0a145"
checksum = "b4af2ec4714533fcdf07e886f17025ace8b997b9ce51204ee69b6da831c3da57"
dependencies = [
 "proc-macro2",
]
@@ -1212,12 +1212,13 @@

[[package]]
name = "redox_users"
version = "0.4.0"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "528532f3d801c87aec9def2add9ca802fe569e44a544afe633765267840abe64"
checksum = "7776223e2696f1aa4c6b0170e83212f47296a00424305117d013dfe86fb0fe55"
dependencies = [
 "getrandom",
 "redox_syscall",
 "thiserror",
]

[[package]]
@@ -1248,9 +1249,9 @@

[[package]]
name = "reqwest"
version = "0.11.9"
version = "0.11.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "87f242f1488a539a79bac6dbe7c8609ae43b7914b7736210f239a37cccb32525"
checksum = "46a1f7aa4f35e5e8b4160449f51afc758f0ce6454315a9fa7d0d113e958c41eb"
dependencies = [
 "base64",
 "bytes",
@@ -1472,9 +1473,9 @@

[[package]]
name = "syn"
version = "1.0.86"
version = "1.0.89"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a65b3f4ffa0092e9887669db0eae07941f023991ab58ea44da8fe8e2d511c6b"
checksum = "ea297be220d52398dcc07ce15a209fce436d361735ac1db700cab3b6cdfb9f54"
dependencies = [
 "proc-macro2",
 "quote",
@@ -2060,9 +2061,9 @@

[[package]]
name = "winreg"
version = "0.7.0"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0120db82e8a1e0b9fb3345a539c478767c0048d842860994d96113d5b667bd69"
checksum = "80d0f4e272c85def139476380b12f9ac60926689dd2e01d4923222f40580869d"
dependencies = [
 "winapi",
]
diff --git a/README.md b/README.md
index 256c8c7..3211701 100644
--- a/README.md
+++ a/README.md
@@ -1,7 +1,7 @@
# gitlab-cargo-shim

To use `github-cargo-shim` at the moment you will have to apply [this patch][patch] ([MR][MR])
to your GitLab instance.
To use `github-cargo-shim` at the moment you will have to build Cargo with
[this patch][patch] ([MR][MR]).

Say goodbye to your Git dependencies, `gitlab-cargo-shim` is an SSH server
that serves crates just like a standard Cargo registry but from a
@@ -69,5 +69,5 @@
[gitlab-package-registry]: https://docs.gitlab.com/ee/user/packages/package_registry/index.html
[imp-token]: https://docs.gitlab.com/ee/api/index.html#impersonation-tokens
[envvar]: https://doc.rust-lang.org/cargo/reference/registries.html#using-an-alternate-registry
[patch]: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/82663.patch
[MR]: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/82663
[patch]: https://github.com/rust-lang/cargo/pull/10484.patch
[MR]: https://github.com/rust-lang/cargo/pull/10484
diff --git a/src/main.rs b/src/main.rs
index 796d813..242f407 100644
--- a/src/main.rs
+++ a/src/main.rs
@@ -10,7 +10,7 @@

use crate::{
    config::Args,
    metadata::{CargoConfig, CargoIndexCrateMetadata},
    metadata::{CargoIndexCrateMetadata, CargoIndexCrateMetadataWithDlOverride},
    protocol::{
        codec::{Encoder, GitCodec},
        high_level::GitRepository,
@@ -162,11 +162,11 @@

impl<U: UserProvider + PackageProvider + Send + Sync + 'static> Handler<U> {
    fn user(&self) -> anyhow::Result<&Arc<User>> {
        self.user.as_ref().ok_or(anyhow::anyhow!("no user set"))
        self.user.as_ref().ok_or_else(|| anyhow::anyhow!("no user set"))
    }

    fn group(&self) -> anyhow::Result<&Group> {
        self.group.as_ref().ok_or(anyhow::anyhow!("no group set"))
        self.group.as_ref().ok_or_else(|| anyhow::anyhow!("no group set"))
    }

    /// Writes a Git packet line response to the buffer, this should only

@@ -272,18 +272,13 @@
        // create the high-level packfile generator
        let mut packfile = GitRepository::default();

        let user = self.user()?;
        let group = self.group()?;

        // fetch the impersonation token for the user we'll embed
        // the `dl` string.
        let token = self.gitlab.fetch_token_for_user(user).await?;
        let token = self.gitlab.fetch_token_for_user(self.user()?).await?;

        // generate the config for the user, containing the download
        // url template from gitlab and the impersonation token embedded
        let config_json = Bytes::from(serde_json::to_vec(&CargoConfig {
            dl: self.gitlab.cargo_dl_uri(group, &token)?,
        })?);
        let config_json = Bytes::from_static(b"{}");

        // write config.json to the root of the repo
        packfile.insert(&[], "config.json".into(), config_json)?;
@@ -310,7 +305,12 @@

                // each crates file in the index is a metadata blob for
                // each version separated by a newline
                buffer.extend_from_slice(&serde_json::to_vec(&*meta)?);
                buffer.extend_from_slice(&serde_json::to_vec(
                    &CargoIndexCrateMetadataWithDlOverride {
                        meta: &meta,
                        dl: &self.gitlab.cargo_dl_uri(crate_path, version, &token)?,
                    },
                )?);
                buffer.put_u8(b'\n');
            }

diff --git a/src/metadata.rs b/src/metadata.rs
index c996ad3..ea187e9 100644
--- a/src/metadata.rs
+++ a/src/metadata.rs
@@ -48,9 +48,11 @@
    })
}

#[derive(Serialize)]
pub struct CargoConfig {
    pub dl: String,
#[derive(Serialize, Debug)]
pub struct CargoIndexCrateMetadataWithDlOverride<'a> {
    #[serde(flatten)]
    pub meta: &'a CargoIndexCrateMetadata,
    pub dl: &'a str,
}

#[derive(Serialize, Deserialize, Debug)]
diff --git a/src/providers/gitlab.rs b/src/providers/gitlab.rs
index a2c4b6b..74fc63f 100644
--- a/src/providers/gitlab.rs
+++ a/src/providers/gitlab.rs
@@ -238,12 +238,20 @@
            .await?)
    }

    fn cargo_dl_uri(&self, group: &Group, token: &str) -> anyhow::Result<String> {
        let uri = self
            .base_url
            .join("groups/")?
            .join(&format!("{}/", group.id))?;
        Ok(format!("{uri}packages/generic/{{sha256-checksum}}/{{crate}}-{{version}}.crate?private_token={token}"))
    fn cargo_dl_uri(
        &self,
        path: &Self::CratePath,
        version: &str,
        token: &str,
    ) -> anyhow::Result<String> {
        Ok(format!(
            "{uri}projects/{project}/packages/generic/{crate_name}/{version}/{crate_name}-{version}.crate?private_token={token}",
            uri = self.base_url,
            project = path.project,
            crate_name = path.package_name,
            version = version,
            token = token,
        ))
    }
}

diff --git a/src/providers/mod.rs b/src/providers/mod.rs
index 7ad56a1..375eac8 100644
--- a/src/providers/mod.rs
+++ a/src/providers/mod.rs
@@ -35,7 +35,12 @@
        version: &str,
    ) -> anyhow::Result<cargo_metadata::Metadata>;

    fn cargo_dl_uri(&self, group: &Group, token: &str) -> anyhow::Result<String>;
    fn cargo_dl_uri(
        &self,
        path: &Self::CratePath,
        version: &str,
        token: &str,
    ) -> anyhow::Result<String>;
}

#[derive(Debug, Clone)]