Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion crates/db/src/oci_dao.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,10 @@ pub async fn find_link(
}

/// Record a use of a link, unless one was recorded less than `every` ago
/// (so pulls do not write a row on every request).
/// (so requests do not write a row each). A use keeps a link no manifest
/// names from the sweeper ([`unreferenced_links`]), so only a client that
/// may push the image records one: a pull by anyone else must not keep the
/// blobs of a deleted image pullable.
pub async fn touch_link(
pool: &PgPool,
repository_id: i64,
Expand Down
6 changes: 4 additions & 2 deletions crates/server/src/admin/images.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@
//! nothing matched. Each delete is in the audit log (`forge::audit`, "image
//! delete") with the image and reference, which the admin change log's path
//! does not name. As after a registry delete, the manifests' configs and
//! layers stay linked to the image until the registry sweeper finds them
//! unreferenced, and the blob outbox deletes a blob no image links any more.
//! layers stay linked to the image, pullable by digest, until the registry
//! sweeper finds them unreferenced and not pushed, mounted or confirmed by a
//! pusher for `docker.unreferenced_blob_ttl_seconds`; a read does not
//! prolong that. The blob outbox deletes a blob no image links any more.
//!
//! `GET /admin/repositories/{name}/images/delete-preview?image=<image>[&reference=<tag|digest>]`
//! says what that delete would remove, and deletes nothing ([`preview`]):
Expand Down
12 changes: 7 additions & 5 deletions crates/server/src/images/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -955,11 +955,13 @@ impl Conversation<'_> {
.get(&digest)
.cloned();
match found {
Some(hit) if self.permission == Permission::Read => self
.reg
.serve_blob(&hit, self.head, &self.headers)
.await
.unwrap_or_else(|failure| self.reg.failure_response(failure)),
Some(hit) if self.permission == Permission::Read => {
let confirms = self.reg.confirms_link(self.ctx, &hit).await;
self.reg
.serve_blob(&hit, confirms, self.head, &self.headers)
.await
.unwrap_or_else(|failure| self.reg.failure_response(failure))
}
_ => self.misused("serving a blob no query found"),
}
}
Expand Down
3 changes: 2 additions & 1 deletion crates/server/src/images/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ pub struct RegistryConfig {
}

impl RegistryConfig {
/// How often a pull (or `HEAD`) of a blob records a use of its link: a
/// How often a `HEAD` (or pull) of a blob by a caller who may push its
/// image records a use of its link (`Registry::confirms_link`): a
/// tenth of the unreferenced-blob TTL, at most hourly. A link a client
/// confirmed then stays at least nine tenths of the TTL, whereas an
/// hourly record with the smallest TTL (an hour) could leave it none.
Expand Down
54 changes: 48 additions & 6 deletions crates/server/src/images/pull.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ use axum::body::Body;
use axum::http::{header, HeaderMap, HeaderValue, StatusCode};
use axum::response::{IntoResponse, Response};
use bytes::Bytes;
use forge_core::authz::Level;
use forge_db::model::{RepoKind, Repository};
use forge_db::oci_dao::{self, ManifestRow};
use forge_format_api::images::{BlobInfo, ManifestInfo, Reference, TagPage, MAX_MANIFEST_BYTES};
use forge_storage::BlobRef;
use futures_util::future::BoxFuture;

use super::digest::ContentDigest;
use super::dispatch::Caller;
use super::dispatch::{mutation_credential, Caller, RequestCtx};
use super::download::Download;
use super::{header_value, Failure, ImgResult, Registry};

Expand Down Expand Up @@ -88,8 +89,9 @@ pub(crate) struct BlobHit {
pub size: i64,
pub blob_sha256: String,
pub s3_key: String,
/// Whether it is an `oci_blob_link` (touched on use) rather than a
/// manifest read as a blob.
/// Whether it is an `oci_blob_link` (whose use a caller who may push
/// the image records, [`Registry::confirms_link`]) rather than a manifest
/// read as a blob.
pub linked: bool,
/// The download it is served from while a proxy fetches it; `None` once
/// it is in the store.
Expand Down Expand Up @@ -375,15 +377,55 @@ impl Registry {
Ok(response)
}

/// Serve a blob the format asked for, with one `Range`, and record the
/// pull of its link.
/// Whether serving `hit` records a use of its link, which keeps a link
/// no manifest names from the sweeper for another
/// `docker.unreferenced_blob_ttl_seconds`: only to a caller who may push
/// the image the link is in, with a credential (and a registry token's
/// scope) a push is accepted from. Such a caller is confirming a blob it
/// is about to name in a manifest (`docker push` asks with a `HEAD`
/// whether the image has a layer and skips its upload), and could push
/// the blob again anyway. Anyone else's fetch records nothing: the grace
/// period is for uploads before their manifest, and a reader who knows
/// the digest of a deleted image's layer must not keep it pullable by
/// fetching it (#576).
pub(crate) async fn confirms_link(&self, ctx: &RequestCtx, hit: &BlobHit) -> bool {
if !hit.linked || !mutation_credential(&ctx.caller.principal) {
return false;
}
if let Some(claims) = &ctx.caller.claims {
if !claims.grants(&ctx.target.name, "push") {
return false;
}
}
// Where a push to the name the client used lands: only a link there
// is one this caller's manifest could name.
let landing = match ctx.target.repo.kind() {
RepoKind::Hosted => Some(ctx.target.repo.clone()),
RepoKind::Group => self
.writable_member(&ctx.target.repo)
.await
.unwrap_or_default(),
RepoKind::Proxy => None,
};
match landing {
Some(repo) if repo.id == hit.repository_id => {
self.rbac_allows(&ctx.caller.principal, &repo, &hit.image, Level::Write)
.await
}
_ => false,
}
}

/// Serve a blob the format asked for, with one `Range`. `confirms`
/// ([`Self::confirms_link`]): record the use of its link.
pub(crate) async fn serve_blob(
&self,
hit: &BlobHit,
confirms: bool,
head: bool,
headers: &HeaderMap,
) -> ImgResult<Response> {
if hit.linked {
if confirms && hit.linked {
if let Err(e) = oci_dao::touch_link(
&self.inner.pool,
hit.repository_id,
Expand Down
Loading
Loading