1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
//! Storage backends.
//!
//! The `container_registry` crate has somewhat modular storage backends, but currently nothing but
//! filesystem storage is supported. Contact the author if you'd like to see this change.
// Note: This module is in worse shape, documentation wise, than the rest. Cleaning this up is the
//       first step towards supporting custom implementations.
use std::{
    fmt::{self, Display},
    fs,
    io::{self, Read},
    path::{Path, PathBuf},
    str::FromStr,
};

use axum::{async_trait, http::StatusCode, response::IntoResponse};
use serde::{Deserialize, Serialize};
use sha2::Digest as Sha2Digest;
use thiserror::Error;
use tokio::io::{AsyncRead, AsyncSeekExt, AsyncWrite};
use uuid::Uuid;

use super::{types::ImageManifest, ImageDigest};

/// Length of a SHA256 hash in bytes.
pub const SHA256_LEN: usize = 32;

const BUFFER_SIZE: usize = 1024 * 1024; // 1 MiB

/// An SHA256 digest.
///
/// The `container_registry` crate supports only `sha256` digests at this time.
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Hash, Serialize)]
pub struct Digest([u8; SHA256_LEN]);

impl Digest {
    /// Creates a digest from an existing hash.
    pub const fn new(bytes: [u8; SHA256_LEN]) -> Self {
        Self(bytes)
    }

    /// Creates a digest by hashing given contents.
    pub fn from_contents(contents: &[u8]) -> Self {
        let mut hasher = sha2::Sha256::new();
        hasher.update(contents);

        Self::new(hasher.finalize().into())
    }
}

impl Display for Digest {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&hex::encode(&self.0[..]))
    }
}

#[derive(Debug, Deserialize)]
struct LayerManifest {
    #[serde(rename = "camelCase")]
    #[allow(dead_code)] // TODO
    blob_sum: String,
}

/// Location of a given image.
///
/// In an open container registry, images are stored in what `container-registry` calls
/// "repository" and "image" pairs. For example, the container image specified as
/// `bitnami/nginx:latest` would have a repository of `bitnami`, image of `nginx` and tag (which
/// is not part of [`ImageLocation`] of `latest`.
#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
pub struct ImageLocation {
    /// The repository part of the image location.
    repository: String,
    /// The image part of the image location.
    image: String,
}

impl Display for ImageLocation {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}/{}", self.repository, self.image)
    }
}

/// Refers to a specific manifest.
///
/// Combines an [`ImageLocation`] with a [`Reference`], e.g. `bitnami/nginx:latest`, which has an
/// [`ImageLocation`] portion of `bitnami/nginx` and a [`Reference::Tag`] `latest`.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ManifestReference {
    #[serde(flatten)]
    location: ImageLocation,
    reference: Reference,
}

impl Display for ManifestReference {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}:{}", self.location, self.reference)
    }
}

impl ManifestReference {
    /// Creates a new manifest reference.
    pub fn new(location: ImageLocation, reference: Reference) -> Self {
        Self {
            location,
            reference,
        }
    }

    /// Returns the location portion of the image location.
    pub fn location(&self) -> &ImageLocation {
        &self.location
    }

    /// Returns the reference portion of the image location.
    pub fn reference(&self) -> &Reference {
        &self.reference
    }
}

impl ImageLocation {
    /// Creates a new image location.
    pub fn new(repository: String, image: String) -> Self {
        Self { repository, image }
    }

    /// Returns the repository portion of the given image location.
    #[inline(always)]
    pub fn repository(&self) -> &str {
        self.repository.as_ref()
    }

    /// Returns the image portion of the given image location.
    #[inline(always)]
    pub fn image(&self) -> &str {
        self.image.as_ref()
    }
}

/// Reference to a specific version of an image.
#[derive(Clone, Debug)]
pub enum Reference {
    /// Image reference by given tag (e.g. `latest`).
    Tag(String),
    /// Image referenced by given specific hash.
    Digest(Digest),
}

impl<'de> Deserialize<'de> for Reference {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let raw = <&str>::deserialize(deserializer)?;

        match ImageDigest::from_str(raw) {
            Ok(digest) => Ok(Self::Digest(digest.digest)),
            Err(_) => Ok(Self::Tag(raw.to_owned())),
        }
    }
}

impl Serialize for Reference {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        match self {
            Reference::Tag(tag) => tag.serialize(serializer),
            Reference::Digest(digest) => ImageDigest::new(*digest).serialize(serializer),
        }
    }
}

impl Reference {
    /// Creates a new by-tag reference.
    #[inline(always)]
    pub fn new_tag<S: ToString>(s: S) -> Self {
        Reference::Tag(s.to_string())
    }

    /// Creats a new by-hash reference.
    #[inline(always)]
    pub fn new_digest(d: Digest) -> Self {
        Reference::Digest(d)
    }

    /// Returns reference as naked tag, if it is a tag.
    pub fn as_tag(&self) -> Option<&str> {
        match self {
            Reference::Tag(tag) => Some(tag),
            Reference::Digest(_) => None,
        }
    }
}

impl Display for Reference {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Reference::Tag(tag) => Display::fmt(tag, f),
            Reference::Digest(digest) => Display::fmt(digest, f),
        }
    }
}

/// A storage error.
#[derive(Debug, Error)]
pub enum Error {
    /// Attempted to submit data to an upload that does not exist.
    #[error("given upload does not exist")]
    UploadDoesNotExit,
    /// A content hash mismatched.
    #[error("digest did not match")]
    DigestMismatch,
    /// An IO error.
    // TODO: Not great to have a catch-all IO error, to be replaced later.
    #[error("io error")]
    Io(io::Error),
    /// A background task panicked.
    #[error("background task panicked")]
    BackgroundTaskPanicked(#[source] tokio::task::JoinError),
    /// Invalid image manifest submitted.
    #[error("invalid image manifest")]
    InvalidManifest(#[source] serde_json::Error),
    /// Attempted to store a manifest under a digest instead of a tag.
    #[error("cannot store manifest under hash")]
    NotATag,
}

impl IntoResponse for Error {
    #[inline]
    fn into_response(self) -> axum::response::Response {
        match self {
            Error::UploadDoesNotExit => StatusCode::NOT_FOUND.into_response(),
            Error::InvalidManifest(_) | Error::NotATag => StatusCode::BAD_REQUEST.into_response(),
            Error::DigestMismatch | Error::Io(_) | Error::BackgroundTaskPanicked(_) => {
                StatusCode::INTERNAL_SERVER_ERROR.into_response()
            }
        }
    }
}

#[derive(Debug)]
pub(crate) struct BlobMetadata {
    #[allow(dead_code)] // TODO
    digest: Digest,
    size: u64,
}

impl BlobMetadata {
    #[allow(dead_code)] // TODO
    pub(crate) fn digest(&self) -> Digest {
        self.digest
    }

    pub(crate) fn size(&self) -> u64 {
        self.size
    }
}

#[async_trait]
pub(crate) trait RegistryStorage: Send + Sync {
    async fn begin_new_upload(&self) -> Result<Uuid, Error>;

    async fn get_blob_reader(
        &self,
        digest: Digest,
    ) -> Result<Option<Box<dyn AsyncRead + Send + Unpin>>, Error>;

    async fn get_blob_metadata(&self, digest: Digest) -> Result<Option<BlobMetadata>, Error>;

    async fn get_upload_writer(
        &self,
        start_at: u64,
        upload: Uuid,
    ) -> Result<Box<dyn AsyncWrite + Send + Unpin>, Error>;

    async fn finalize_upload(&self, upload: Uuid, hash: Digest) -> Result<(), Error>;

    async fn get_manifest(
        &self,
        manifest_reference: &ManifestReference,
    ) -> Result<Option<Vec<u8>>, Error>;

    async fn put_manifest(
        &self,
        manifest_reference: &ManifestReference,
        manifest: &[u8],
    ) -> Result<Digest, Error>;
}

/// A filesystem backend error.
#[derive(Debug, Error)]
pub enum FilesystemStorageError {
    /// The storage path given could not be canonicalized.
    #[error("could not canonicalize root {}", path.display())]
    CouldNotCanonicalizeRoot {
        path: PathBuf,
        #[source]
        err: io::Error,
    },
    /// Failed to create directory in storage path.
    #[error("could not create directory {}", path.display())]
    FailedToCreateDir {
        path: PathBuf,
        #[source]
        err: io::Error,
    },
}

#[derive(Debug)]
pub(crate) struct FilesystemStorage {
    uploads: PathBuf,
    blobs: PathBuf,
    manifests: PathBuf,
    tags: PathBuf,
    rel_manifest_to_blobs: PathBuf,
}

impl FilesystemStorage {
    pub(crate) fn new<P: AsRef<Path>>(root: P) -> Result<Self, FilesystemStorageError> {
        let raw_root = root.as_ref();
        let root = raw_root.canonicalize().map_err(|err| {
            FilesystemStorageError::CouldNotCanonicalizeRoot {
                path: raw_root.to_owned(),
                err,
            }
        })?;

        let uploads = root.join("uploads");
        let blobs = root.join("blobs");
        let manifests = root.join("manifests");
        let tags = root.join("tags");
        let rel_manifest_to_blobs = PathBuf::from("../../../manifests");

        for dir in [&uploads, &blobs, &manifests, &tags] {
            if !dir.exists() {
                fs::create_dir(dir).map_err(|err| FilesystemStorageError::FailedToCreateDir {
                    path: dir.to_owned(),
                    err,
                })?;
            }
        }

        Ok(FilesystemStorage {
            uploads,
            blobs,
            manifests,
            tags,
            rel_manifest_to_blobs,
        })
    }
    fn blob_path(&self, digest: Digest) -> PathBuf {
        self.blobs.join(format!("{}", digest))
    }
    fn upload_path(&self, upload: Uuid) -> PathBuf {
        self.uploads.join(format!("{}.partial", upload))
    }

    fn manifest_path(&self, digest: Digest) -> PathBuf {
        self.manifests.join(format!("{}", digest))
    }

    fn blob_rel_path(&self, digest: Digest) -> PathBuf {
        self.rel_manifest_to_blobs.join(format!("{}", digest))
    }

    fn tag_path(&self, location: &ImageLocation, tag: &str) -> PathBuf {
        self.tags
            .join(location.repository())
            .join(location.image())
            .join(tag)
    }

    fn temp_tag_path(&self) -> PathBuf {
        self.tags.join(Uuid::new_v4().to_string())
    }
}

#[async_trait]
impl RegistryStorage for FilesystemStorage {
    async fn begin_new_upload(&self) -> Result<Uuid, Error> {
        let upload = Uuid::new_v4();
        let out_path = self.upload_path(upload);

        // Write zero-sized file.
        let _file = tokio::fs::File::create(out_path).await.map_err(Error::Io)?;

        Ok(upload)
    }

    async fn get_blob_metadata(&self, digest: Digest) -> Result<Option<BlobMetadata>, Error> {
        let blob_path = self.blob_path(digest);

        if !blob_path.exists() {
            return Ok(None);
        }

        let metadata = tokio::fs::metadata(blob_path).await.map_err(Error::Io)?;

        Ok(Some(BlobMetadata {
            digest,
            size: metadata.len(),
        }))
    }

    async fn get_blob_reader(
        &self,
        digest: Digest,
    ) -> Result<Option<Box<dyn AsyncRead + Send + Unpin>>, Error> {
        let blob_path = self.blob_path(digest);

        if !blob_path.exists() {
            return Ok(None);
        }

        let reader = tokio::fs::File::open(blob_path).await.map_err(Error::Io)?;

        Ok(Some(Box::new(reader)))
    }

    async fn get_upload_writer(
        &self,
        start_at: u64,
        upload: Uuid,
    ) -> Result<Box<dyn AsyncWrite + Send + Unpin>, Error> {
        let location = self.upload_path(upload);

        if !location.exists() {
            return Err(Error::UploadDoesNotExit);
        }

        let mut file = tokio::fs::OpenOptions::new()
            .append(true)
            .truncate(false)
            .open(location)
            .await
            .map_err(Error::Io)?;

        file.seek(io::SeekFrom::Start(start_at))
            .await
            .map_err(Error::Io)?;

        Ok(Box::new(file))
    }

    async fn finalize_upload(&self, upload: Uuid, digest: Digest) -> Result<(), Error> {
        // We are to validate the uploaded partial, then move it into the proper store.
        // TODO: Lock in place so that the hash cannot be corrupted/attacked.

        let upload_path = self.upload_path(upload);

        if !upload_path.exists() {
            return Err(Error::UploadDoesNotExit);
        }

        // We offload hashing to a blocking thread.
        let actual = {
            let upload_path = upload_path.clone();
            tokio::task::spawn_blocking::<_, Result<Digest, Error>>(move || {
                let mut src = fs::File::open(upload_path).map_err(Error::Io)?;

                // Uses `vec!` instead of `Box`, as initializing the latter blows the stack:
                let mut buf = vec![0; BUFFER_SIZE];
                let mut hasher = sha2::Sha256::new();

                loop {
                    let read = src.read(buf.as_mut()).map_err(Error::Io)?;
                    if read == 0 {
                        break;
                    }
                    hasher.update(&buf[..read]);
                }

                let actual = hasher.finalize();
                Ok(Digest::new(actual.into()))
            })
        }
        .await
        .map_err(Error::BackgroundTaskPanicked)??;

        if actual != digest {
            return Err(Error::DigestMismatch);
        }

        // The uploaded file matches, we can rename it now.
        let dest = self.blob_path(digest);
        tokio::fs::rename(upload_path, dest)
            .await
            .map_err(Error::Io)?;

        // All good.
        Ok(())
    }

    async fn get_manifest(
        &self,
        manifest_reference: &ManifestReference,
    ) -> Result<Option<Vec<u8>>, Error> {
        let manifest_path = match manifest_reference.reference() {
            Reference::Tag(ref tag) => self.tag_path(manifest_reference.location(), tag),
            Reference::Digest(digest) => self.manifest_path(*digest),
        };

        match tokio::fs::read(manifest_path).await {
            Ok(data) => Ok(Some(data)),
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None),
            Err(e) => Err(Error::Io(e)),
        }
    }

    async fn put_manifest(
        &self,
        manifest_reference: &ManifestReference,
        manifest: &[u8],
    ) -> Result<Digest, Error> {
        // TODO: Validate all blobs are completely uploaded.
        let _manifest: ImageManifest =
            serde_json::from_slice(manifest).map_err(Error::InvalidManifest)?;

        let digest = Digest::from_contents(manifest);
        let dest = self.manifest_path(digest);
        tokio::fs::write(dest, &manifest).await.map_err(Error::Io)?;

        let tag = self.tag_path(
            manifest_reference.location(),
            manifest_reference
                .reference()
                .as_tag()
                .ok_or(Error::NotATag)?,
        );

        let tag_parent = tag.parent().expect("should have parent");

        if !tag_parent.exists() {
            tokio::fs::create_dir_all(tag_parent)
                .await
                .map_err(Error::Io)?;
        }

        let tmp_tag = self.temp_tag_path();

        tokio::fs::symlink(self.blob_rel_path(digest), &tmp_tag)
            .await
            .map_err(Error::Io)?;
        tokio::fs::rename(tmp_tag, tag).await.map_err(Error::Io)?;

        Ok(digest)
    }
}