forge
Explore
Log in

Don't you dare go Hollow

forge

Clone
★ 0

0 watching0 forksRustAGPL-3.07.1 MiB490f099Tahinli7 hours ago322 commits

packages.md217 lines, 8.6 KiBHistoryRawDownload
1# Package registries
2
3Every user and organization has a package registry on this forge. It speaks the Cargo registry
4protocol (the sparse index), so `cargo` can publish to it, install from it and yank versions, and
5the 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
7package page with its README, versions and an install snippet.
8
9The 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
14Add the registry to `~/.cargo/config.toml` (or to a project's `.cargo/config.toml`):
15
16```toml
17[registries.tahinli]
18index = "sparse+https://git.tahinli.com/api/packages/tahinli/cargo/"
19```
20
21Then add a crate from it:
22
23```sh
24cargo add --registry tahinli my-crate
25```
26
27Or refer to it in `Cargo.toml`:
28
29```toml
30[dependencies]
31my-crate = { version = "0.1", registry = "tahinli" }
32```
33
34Packages marked private need a token for every request to that registry, so Cargo asks for
35credentials when it reads the index (see below).
36
37## Tokens
38
39Create a personal access token under **Settings, Tokens** and tick **Packages**. A token with
40the `packages` scope can read private packages in the registries of the owners it may act for,
41publish, yank and unyank. A token without it is treated as anonymous for public reads and is
42refused for writes.
43
44Log in once on your machine:
45
46```sh
47cargo login --registry tahinli
48```
49
50Cargo asks for the token and stores it in `~/.cargo/credentials.toml`. Cargo sends the bare
51token in the `Authorization` header, which is what this forge expects.
52
53## Publish
54
55Set the registry in the crate's `Cargo.toml` so that `cargo publish` does not go to crates.io:
56
57```toml
58[package]
59name = "my-crate"
60version = "0.1.0"
61repository = "https://git.tahinli.com/tahinli/my-crate" # links the package to this repo
62publish = ["tahinli"]
63```
64
65Then publish:
66
67```sh
68cargo publish --registry tahinli
69```
70
71Rules:
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
89cargo yank --registry tahinli --version 0.1.0 my-crate
90cargo yank --registry tahinli --version 0.1.0 --undo my-crate
91```
92
93A yanked version stays in the index and can still be downloaded, so existing `Cargo.lock` files
94keep working. Cargo stops choosing it for new builds.
95
96## Continuous integration
97
98Create a token with the `packages` scope and store it as a secret of the CI system, for
99example `FORGE_PACKAGES_TOKEN`. Then pass it to Cargo through an environment variable, which
100Cargo reads for a registry named `tahinli`:
101
102```yaml
103env:
104 CARGO_REGISTRIES_TAHINLI_INDEX: sparse+https://git.tahinli.com/api/packages/tahinli/cargo/
105 CARGO_REGISTRIES_TAHINLI_TOKEN: ${{ secrets.FORGE_PACKAGES_TOKEN }}
106steps:
107 - run: cargo publish --registry tahinli
108```
109
110Keep the token out of the repository. Use a dedicated token for CI, and revoke it when the job
111is no longer needed.
112
113## Private packages
114
115A package is public unless it is marked private. Public packages install without a token.
116When an owner has any private package, the registry's `config.json` sets `auth-required`, and
117Cargo then sends the token for every request to that registry, so a token is needed for public
118packages too.
119
120## Container images
121
122Every 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.
124Container images show in the owner's **Packages** tab with a `container` badge, next to the
125Cargo crates.
126
127### Log in, push and pull
128
129Log in with your username and a personal access token that has the **packages** scope:
130
131```sh
132docker login git.tahinli.com
133```
134
135Tag an image with the owner and a name below the owner, then push it:
136
137```sh
138docker tag myapp:latest git.tahinli.com/tahinli/myapp:1.0.0
139docker push git.tahinli.com/tahinli/myapp:1.0.0
140```
141
142A name may have several parts, such as `tahinli/tools/cli`. Pull a public image with no login:
143
144```sh
145docker pull git.tahinli.com/tahinli/myapp:1.0.0
146```
147
148Pulls of a public image need no token. A private image needs a token with the packages scope, and
149a pull of a private image without one is answered with `401` and a `WWW-Authenticate` challenge.
150Pushing needs write access to the owner: the owner, or a member of the organization with write
151access. The web page of an image shows the pull command, the tags with their digest, size and
152date, and the delete control for tags.
153
154### Token endpoint
155
156Clients that use the Bearer challenge get their token from `/v2/token`. The request uses HTTP
157Basic 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
163Deleting a tag, with the owner's delete control or `DELETE /v2/{name}/manifests/{tag}`, removes
164the tag. A manifest that no tag names any more, and that no index names, is removed as well.
165Deleting by digest removes the manifest and all its tags. Blobs that no image uses are then
166swept from the store. The sweep runs on the request that deleted something, under a lock, so the
167registry has no background timer. A push that is still in flight when a sweep runs can lose its
168blobs; the client then gets `MANIFEST_BLOB_UNKNOWN` and pushes again.
169
170### Proxy in front of the forge
171
172Blobs are large, so the reverse proxy in front of the forge must accept request bodies of
173gigabytes and must not buffer request bodies. Route `/v2/` to the forge with a large body size
174limit and with request buffering turned off. A proxy that buffers streams every layer to disk
175before forwarding, and a small limit rejects the first layer.
176
177### Limits and storage
178
179Blobs are stored once per digest under the blob store (`oci/<hex>`), however many images use
180them. Upload sessions are temporary files under the data directory and are removed when the
181upload completes. A session left behind by a crash is removed by the next upload, once it is
182older than a day. Manifests and tags are in the database. A manifest is at most 4 MiB, and a
183blob at most 10 GiB.
184
185Not supported yet: the referrers API (`/v2/{name}/referrers/{digest}`). Blobs can be mounted from
186one 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
197Each `.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
199checksums and the READMEs are in the database, so the index is served without reading any file.
200Container images are rows of the same packages table with the kind `oci`. Their tags, manifests
201and blob links are in their own tables, and their blob bytes are stored under `oci/<hex>`.
202
203## Protocol endpoints
204
205All 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
217Errors use the body Cargo expects: `{"errors":[{"detail":"..."}]}`.