forge
Clone
https://git.tahinli.com/Tahinli/forge.git
0 watching·0 forks·Rust·AGPL-3.0·7.1 MiB·490f099·Tahinli·7 hours ago·322 commits
| 1 | # Package registries |
| 2 | |
| 3 | Every user and organization has a package registry on this forge. It speaks the Cargo registry |
| 4 | protocol (the sparse index), so `cargo` can publish to it, install from it and yank versions, and |
| 5 | the OCI container protocol, so `docker` can push and pull images (see |
| 6 | [Container images](#container-images)). Packages show on the owner's profile under the **Packages** tab, and on the |
| 7 | package page with its README, versions and an install snippet. |
| 8 | |
| 9 | The registry of an owner is named after the owner. Below, `tahinli` is the owner and |
| 10 | `git.tahinli.com` is the forge. |
| 11 | |
| 12 | ## Use a registry in Cargo |
| 13 | |
| 14 | Add the registry to `~/.cargo/config.toml` (or to a project's `.cargo/config.toml`): |
| 15 | |
| 16 | ```toml |
| 17 | [registries.tahinli] |
| 18 | index = "sparse+https://git.tahinli.com/api/packages/tahinli/cargo/" |
| 19 | ``` |
| 20 | |
| 21 | Then add a crate from it: |
| 22 | |
| 23 | ```sh |
| 24 | cargo add --registry tahinli my-crate |
| 25 | ``` |
| 26 | |
| 27 | Or refer to it in `Cargo.toml`: |
| 28 | |
| 29 | ```toml |
| 30 | [dependencies] |
| 31 | my-crate = { version = "0.1", registry = "tahinli" } |
| 32 | ``` |
| 33 | |
| 34 | Packages marked private need a token for every request to that registry, so Cargo asks for |
| 35 | credentials when it reads the index (see below). |
| 36 | |
| 37 | ## Tokens |
| 38 | |
| 39 | Create a personal access token under **Settings, Tokens** and tick **Packages**. A token with |
| 40 | the `packages` scope can read private packages in the registries of the owners it may act for, |
| 41 | publish, yank and unyank. A token without it is treated as anonymous for public reads and is |
| 42 | refused for writes. |
| 43 | |
| 44 | Log in once on your machine: |
| 45 | |
| 46 | ```sh |
| 47 | cargo login --registry tahinli |
| 48 | ``` |
| 49 | |
| 50 | Cargo asks for the token and stores it in `~/.cargo/credentials.toml`. Cargo sends the bare |
| 51 | token in the `Authorization` header, which is what this forge expects. |
| 52 | |
| 53 | ## Publish |
| 54 | |
| 55 | Set the registry in the crate's `Cargo.toml` so that `cargo publish` does not go to crates.io: |
| 56 | |
| 57 | ```toml |
| 58 | [package] |
| 59 | name = "my-crate" |
| 60 | version = "0.1.0" |
| 61 | repository = "https://git.tahinli.com/tahinli/my-crate" # links the package to this repo |
| 62 | publish = ["tahinli"] |
| 63 | ``` |
| 64 | |
| 65 | Then publish: |
| 66 | |
| 67 | ```sh |
| 68 | cargo publish --registry tahinli |
| 69 | ``` |
| 70 | |
| 71 | Rules: |
| 72 | |
| 73 | - The crate name starts with a letter and holds letters, digits, `-` and `_`, at most 64 characters. |
| 74 | Names are case-insensitive in the registry: `My_Crate` and `my_crate` are the same package. |
| 75 | - The version must be valid semver, and it can be published only once. A second publish of the |
| 76 | same version fails with `409 Conflict` and the message `crate version ... is already uploaded`. |
| 77 | - A `.crate` file is at most 10 MiB. |
| 78 | - Only the owner, the instance admin, or an owner of an organization may publish. Members of an |
| 79 | organization without the owner role may read but not publish. |
| 80 | - The package links to a repository when its `repository` URL points to a repo of the same owner |
| 81 | on this forge. The package then shows on that repo's page. Links to other owners' repos are |
| 82 | ignored. |
| 83 | - The README is read from the `.crate` (`README*` at the top of the package directory), or from |
| 84 | the `readme` field of the publish metadata when the file is missing. |
| 85 | |
| 86 | ## Yank |
| 87 | |
| 88 | ```sh |
| 89 | cargo yank --registry tahinli --version 0.1.0 my-crate |
| 90 | cargo yank --registry tahinli --version 0.1.0 --undo my-crate |
| 91 | ``` |
| 92 | |
| 93 | A yanked version stays in the index and can still be downloaded, so existing `Cargo.lock` files |
| 94 | keep working. Cargo stops choosing it for new builds. |
| 95 | |
| 96 | ## Continuous integration |
| 97 | |
| 98 | Create a token with the `packages` scope and store it as a secret of the CI system, for |
| 99 | example `FORGE_PACKAGES_TOKEN`. Then pass it to Cargo through an environment variable, which |
| 100 | Cargo reads for a registry named `tahinli`: |
| 101 | |
| 102 | ```yaml |
| 103 | env: |
| 104 | CARGO_REGISTRIES_TAHINLI_INDEX: sparse+https://git.tahinli.com/api/packages/tahinli/cargo/ |
| 105 | CARGO_REGISTRIES_TAHINLI_TOKEN: ${{ secrets.FORGE_PACKAGES_TOKEN }} |
| 106 | steps: |
| 107 | - run: cargo publish --registry tahinli |
| 108 | ``` |
| 109 | |
| 110 | Keep the token out of the repository. Use a dedicated token for CI, and revoke it when the job |
| 111 | is no longer needed. |
| 112 | |
| 113 | ## Private packages |
| 114 | |
| 115 | A package is public unless it is marked private. Public packages install without a token. |
| 116 | When an owner has any private package, the registry's `config.json` sets `auth-required`, and |
| 117 | Cargo then sends the token for every request to that registry, so a token is needed for public |
| 118 | packages too. |
| 119 | |
| 120 | ## Container images |
| 121 | |
| 122 | Every owner also has a container registry. It speaks the OCI Distribution protocol (v1.1) under |
| 123 | `/v2/`, so `docker`, `podman` and other OCI clients push and pull images with no extra setup. |
| 124 | Container images show in the owner's **Packages** tab with a `container` badge, next to the |
| 125 | Cargo crates. |
| 126 | |
| 127 | ### Log in, push and pull |
| 128 | |
| 129 | Log in with your username and a personal access token that has the **packages** scope: |
| 130 | |
| 131 | ```sh |
| 132 | docker login git.tahinli.com |
| 133 | ``` |
| 134 | |
| 135 | Tag an image with the owner and a name below the owner, then push it: |
| 136 | |
| 137 | ```sh |
| 138 | docker tag myapp:latest git.tahinli.com/tahinli/myapp:1.0.0 |
| 139 | docker push git.tahinli.com/tahinli/myapp:1.0.0 |
| 140 | ``` |
| 141 | |
| 142 | A name may have several parts, such as `tahinli/tools/cli`. Pull a public image with no login: |
| 143 | |
| 144 | ```sh |
| 145 | docker pull git.tahinli.com/tahinli/myapp:1.0.0 |
| 146 | ``` |
| 147 | |
| 148 | Pulls of a public image need no token. A private image needs a token with the packages scope, and |
| 149 | a pull of a private image without one is answered with `401` and a `WWW-Authenticate` challenge. |
| 150 | Pushing needs write access to the owner: the owner, or a member of the organization with write |
| 151 | access. The web page of an image shows the pull command, the tags with their digest, size and |
| 152 | date, and the delete control for tags. |
| 153 | |
| 154 | ### Token endpoint |
| 155 | |
| 156 | Clients that use the Bearer challenge get their token from `/v2/token`. The request uses HTTP |
| 157 | Basic with the username and the token. The response is `{"token": ..., "access_token": ..., |
| 158 | "expires_in": 300, ...}`, and the token is good for five minutes. Basic auth also works on each |
| 159 | `/v2/` request directly, so simple clients need no token step. |
| 160 | |
| 161 | ### Deleting |
| 162 | |
| 163 | Deleting a tag, with the owner's delete control or `DELETE /v2/{name}/manifests/{tag}`, removes |
| 164 | the tag. A manifest that no tag names any more, and that no index names, is removed as well. |
| 165 | Deleting by digest removes the manifest and all its tags. Blobs that no image uses are then |
| 166 | swept from the store. The sweep runs on the request that deleted something, under a lock, so the |
| 167 | registry has no background timer. A push that is still in flight when a sweep runs can lose its |
| 168 | blobs; the client then gets `MANIFEST_BLOB_UNKNOWN` and pushes again. |
| 169 | |
| 170 | ### Proxy in front of the forge |
| 171 | |
| 172 | Blobs are large, so the reverse proxy in front of the forge must accept request bodies of |
| 173 | gigabytes and must not buffer request bodies. Route `/v2/` to the forge with a large body size |
| 174 | limit and with request buffering turned off. A proxy that buffers streams every layer to disk |
| 175 | before forwarding, and a small limit rejects the first layer. |
| 176 | |
| 177 | ### Limits and storage |
| 178 | |
| 179 | Blobs are stored once per digest under the blob store (`oci/<hex>`), however many images use |
| 180 | them. Upload sessions are temporary files under the data directory and are removed when the |
| 181 | upload completes. A session left behind by a crash is removed by the next upload, once it is |
| 182 | older than a day. Manifests and tags are in the database. A manifest is at most 4 MiB, and a |
| 183 | blob at most 10 GiB. |
| 184 | |
| 185 | Not supported yet: the referrers API (`/v2/{name}/referrers/{digest}`). Blobs can be mounted from |
| 186 | one image into another, so a shared base layer is uploaded once. |
| 187 | |
| 188 | ## What is not supported yet |
| 189 | |
| 190 | - `cargo search` and the search endpoint. |
| 191 | - Adding or removing owners with `cargo owner`. The owners list (`cargo owner --list`) works and |
| 192 | shows the owner, or the owners of an organization. |
| 193 | - Managing package visibility from the web page. |
| 194 | |
| 195 | ## Storage |
| 196 | |
| 197 | Each `.crate` file is stored with the other release and artifact blobs (see `[storage]` in |
| 198 | [backup.md](backup.md)), under `packages/<version id>`. The metadata, the index lines, the |
| 199 | checksums and the READMEs are in the database, so the index is served without reading any file. |
| 200 | Container images are rows of the same packages table with the kind `oci`. Their tags, manifests |
| 201 | and blob links are in their own tables, and their blob bytes are stored under `oci/<hex>`. |
| 202 | |
| 203 | ## Protocol endpoints |
| 204 | |
| 205 | All paths are under `/api/packages/{owner}/cargo/`: |
| 206 | |
| 207 | | Method | Path | Purpose | |
| 208 | | --- | --- | --- | |
| 209 | | GET | `config.json` | Registry config: `dl`, `api` and `auth-required`. | |
| 210 | | GET | `1/{name}`, `2/{name}`, `{ab}/{cd}/{name}` | Sparse index file of a crate. | |
| 211 | | GET | `download/{name}/{version}` | The `.crate` file. | |
| 212 | | PUT | `api/v1/crates/new` | Publish. The body is the length-prefixed JSON metadata and `.crate`. | |
| 213 | | DELETE | `api/v1/crates/{name}/{version}/yank` | Yank a version. | |
| 214 | | PUT | `api/v1/crates/{name}/{version}/unyank` | Unyank a version. | |
| 215 | | GET | `api/v1/crates/{name}/owners` | The people who may publish. | |
| 216 | |
| 217 | Errors use the body Cargo expects: `{"errors":[{"detail":"..."}]}`. |