sogar-core 0.1.0

Simple OCI Generic Artifact Registry (SOGAR)
Documentation
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
use crate::{parse_digest, Manifest};
use hyper::body::HttpBody;
use regex::Regex;
use saphir::{hyper::body::Buf, prelude::*};
use serde::Deserialize;
use slog_scope::{debug, error};
use std::{collections::HashMap, fs::create_dir_all, io, io::Write, path::Path};
use tokio_02::io::AsyncWriteExt;

const REPOSITORY: &str = "repository";
const IMAGE_NAME: &str = "image_name";
const DIGEST: &str = "digest";
const TAG: &str = "tag";
pub const ARTIFACTS_DIR: &str = "artifacts";
const ARTIFACTS_CONTENT: &str = "content.yaml";

pub const BLOB_PATH: &str = "/registry/v2/<repository>/<image_name>/blobs/<digest>";
pub const BLOB_GET_LOCATION_PATH: &str = "/registry/v2/<repository>/<image_name>/blobs/uploads";
pub const UPLOAD_BLOB_PATH: &str = "/registry/<repository>/<image_name>";
pub const MANIFEST_PATH: &str = "/registry/v2/<repository>/<image_name>/manifests/<tag>";

pub const BLOB_EXIST_ENDPOINT: &str = "is_blob_exist";
pub const BLOB_GET_LOCATION_ENDPOINT: &str = "get_blob_location";
pub const BLOB_UPLOAD_ENDPOINT: &str = "save_blob";
pub const BLOB_DOWNLOAD_ENDPOINT: &str = "get_blob";
pub const MANIFEST_EXIST_ENDPOINT: &str = "is_manifest_exist";
pub const MANIFEST_UPLOAD_ENDPOINT: &str = "save_manifest";
pub const MANIFEST_DOWNLOAD_ENDPOINT: &str = "get_manifest";

const CONTENT_TYPE_HEADER: &str = "content-type";
const ACCEPT_HEADER: &str = "accept";

pub struct SogarCustomResponse {
    headers: HashMap<String, String>,
    status: StatusCode,
    file: Option<File>,
}

impl SogarCustomResponse {
    pub fn new(status: StatusCode) -> Self {
        SogarCustomResponse {
            headers: HashMap::new(),
            status,
            file: None,
        }
    }

    pub fn header(&mut self, key: &str, value: &str) {
        self.headers.insert(key.to_string(), value.to_string());
    }

    pub fn file(&mut self, file: File) {
        self.file = Some(file);
    }
}

impl Responder for SogarCustomResponse {
    fn respond_with_builder(self, builder: Builder, _ctx: &HttpContext) -> Builder {
        let mut builder_copy = builder;
        for (key, val) in self.headers {
            builder_copy = builder_copy.header(&key, val);
        }

        builder_copy = builder_copy.status(self.status);

        if let Some(file) = self.file {
            builder_copy = builder_copy.file(file);
        }

        builder_copy
    }
}

pub struct SogarController {
    _priv: (),
}

impl SogarController {
    pub fn new(registry: &str, image_name: &str) -> Self {
        use std::fs::File;

        let path = Path::new(registry).join(image_name);
        if let Err(e) = create_dir_all(path.join(ARTIFACTS_DIR)) {
            error!("Failed to create registry! {}", e);
        }

        let content = path.join(ARTIFACTS_CONTENT);
        if !content.exists() {
            match File::create(path.join(ARTIFACTS_CONTENT)) {
                Ok(mut file) => {
                    if let Err(e) = writeln!(file, "blobs:") {
                        error!("Couldn't write to file: {}", e);
                    }
                }
                Err(e) => {
                    error!("Failed to create file with artifacts content! {}", e);
                }
            }
        }

        Self { _priv: () }
    }
}

#[controller(name = "registry")]
impl SogarController {
    #[head("/v2/<repository>/<image_name>/blobs/<digest>")]
    async fn is_blob_exist(&self, req: Request) -> impl Responder {
        debug!("Head request for blob");
        let map = req.captures();

        if let (Some(repository), Some(image_name), Some(digest)) =
            (map.get(REPOSITORY), map.get(IMAGE_NAME), map.get(DIGEST))
        {
            if let Some(digest) = parse_digest(digest.to_string()) {
                let path = Path::new(repository)
                    .join(image_name)
                    .join(ARTIFACTS_DIR)
                    .join(&digest.digest_type)
                    .join(&digest.value);

                if path.exists() {
                    let mut response = SogarCustomResponse::new(StatusCode::OK);
                    response.header(
                        "Docker-Content-Digest",
                        format!("{}:{}", digest.digest_type, digest.value).as_str(),
                    );
                    return response;
                }
            }
        }

        SogarCustomResponse::new(StatusCode::NOT_FOUND)
    }

    #[post("/v2/<repository>/<image_name>/blobs/uploads/")]
    async fn get_blob_location(&self, req: Request) -> impl Responder {
        debug!("Post request for blob");
        let map = req.captures();

        if let (Some(repository), Some(image_name)) = (map.get(REPOSITORY), map.get(IMAGE_NAME)) {
            let path = Path::new(repository).join(image_name);
            if path.exists() {
                let mut response = SogarCustomResponse::new(StatusCode::ACCEPTED);
                response.header("Location", format!("/{}/{}", repository, image_name).as_str());
                return response;
            }
        }

        SogarCustomResponse::new(StatusCode::NOT_FOUND)
    }

    // Full url example: /<repository>/<image_name>&digest=<digest>
    #[put("/<repository>/<image_name>")]
    async fn save_blob(&self, mut req: Request) -> impl Responder {
        debug!("Put request for blob");

        let body: hyper::Body = req.body_mut().take().into();

        let digest = parse_digest_uri(req.uri());
        let map = req.captures();

        if let (Some(repository), Some(image_name), Some(digest)) = (map.get(REPOSITORY), map.get(IMAGE_NAME), digest) {
            let path = Path::new(repository).join(image_name).join(ARTIFACTS_DIR);
            if path.exists() {
                if let Some(blob_digest) = parse_digest(digest.clone()) {
                    let path = path.join(blob_digest.digest_type.as_str());
                    if !path.exists() {
                        if let Err(e) = create_dir_all(path.as_path()) {
                            error!("Failed to create directory for saving blob {}", e);
                            return SogarCustomResponse::new(StatusCode::BAD_REQUEST);
                        }
                    }

                    let path = path.join(blob_digest.value.as_str());

                    if let Err(error_response) = remove_file_if_exists(path.as_path()) {
                        return error_response;
                    }

                    if let Err(e) = write_body_to_file(body, path.as_path()).await {
                        error!("Failed to write data to the file {}", e);
                        return SogarCustomResponse::new(StatusCode::BAD_REQUEST);
                    }

                    let mut response = SogarCustomResponse::new(StatusCode::CREATED);
                    response.header(
                        "Location",
                        format!("/v2/{}/{}/blobs/{}", repository, image_name, digest).as_str(),
                    );

                    return response;
                }
            }
        }

        SogarCustomResponse::new(StatusCode::NOT_FOUND)
    }

    #[head("/v2/<repository>/<image_name>/manifests/<tag>")]
    async fn is_manifest_exist(&self, req: Request) -> (StatusCode, ()) {
        debug!("Head request for manifest");

        let map = req.captures();

        if let (Some(repository), Some(image_name), Some(tag)) =
            (map.get(REPOSITORY), map.get(IMAGE_NAME), map.get(TAG))
        {
            let path = Path::new(repository).join(image_name).join(ARTIFACTS_DIR).join(tag);
            if path.exists() {
                return (StatusCode::OK, ());
            }
        }

        (StatusCode::NOT_FOUND, ())
    }

    #[put("/v2/<repository>/<image_name>/manifests/<tag>")]
    async fn save_manifest(&self, mut req: Request) -> impl Responder {
        debug!("Put request for manifest");
        let body: hyper::Body = req.body_mut().take().into();

        let map = req.captures();
        let headers = req.headers();

        if let (Some(repository), Some(image_name), Some(tag)) =
            (map.get(REPOSITORY), map.get(IMAGE_NAME), map.get(TAG))
        {
            let image_path = Path::new(repository).join(image_name);
            let path = image_path.join(ARTIFACTS_DIR);
            if path.exists() {
                let path = path.join(tag);

                if let Err(error_response) = remove_file_if_exists(path.as_path()) {
                    return error_response;
                }

                if let Err(e) = write_body_to_file(body, path.as_path()).await {
                    error!("Failed to write data to the file {}", e);
                    return SogarCustomResponse::new(StatusCode::BAD_REQUEST);
                }

                let mut manifest_mime_type = None;
                if headers.contains_key(CONTENT_TYPE_HEADER) {
                    manifest_mime_type = headers
                        .get(CONTENT_TYPE_HEADER)
                        .and_then(|header| header.to_str().map_or(None, |result| Some(result.to_string())));
                }

                add_artifacts_info(tag.clone(), manifest_mime_type, image_path.as_path());

                let mut response = SogarCustomResponse::new(StatusCode::CREATED);
                response.header(
                    "Location",
                    format!("/v2/{}/{}/manifests/{}", repository, image_name, tag).as_str(),
                );

                return response;
            }
        }

        SogarCustomResponse::new(StatusCode::BAD_REQUEST)
    }

    #[get("/v2/<repository>/<image_name>/manifests/<tag>")]
    async fn get_manifest(&self, req: Request) -> (StatusCode, Option<File>) {
        debug!("Get request for manifest");
        let map = req.captures();
        let headers = req.headers();

        if let (Some(repository), Some(image_name), Some(tag)) =
            (map.get(REPOSITORY), map.get(IMAGE_NAME), map.get(TAG))
        {
            let image_path = Path::new(repository).join(image_name);
            let path = image_path.join(ARTIFACTS_DIR).join(tag);
            let content_type = read_artifact_info(tag.to_string(), image_path.as_path());

            if headers.contains_key(ACCEPT_HEADER) {
                if let (Some(accept_value), Some(content_type)) = (
                    headers.get(ACCEPT_HEADER).and_then(|header| header.to_str().ok()),
                    content_type,
                ) {
                    if accept_value.contains(&content_type) || accept_value.contains("*/*") {
                        return get_file_if_exists(path.as_path()).await;
                    }
                }
            } else {
                return get_file_if_exists(path.as_path()).await;
            }
        }

        (StatusCode::NOT_FOUND, None)
    }

    #[get("/v2/<repository>/<image_name>/blobs/<digest>")]
    async fn get_blob(&self, req: Request) -> impl Responder {
        debug!("Get request for blob");

        let map = req.captures();

        if let (Some(repository), Some(image_name), Some(digest)) =
            (map.get(REPOSITORY), map.get(IMAGE_NAME), map.get(DIGEST))
        {
            if let Some(digest) = parse_digest(digest.to_string()) {
                let image_path = Path::new(repository).join(image_name);
                let path = image_path
                    .join(ARTIFACTS_DIR)
                    .join(&digest.digest_type)
                    .join(&digest.value);
                let (status_code, file_result) = get_file_if_exists(path.as_path()).await;

                if let Some(file) = file_result {
                    let content_type = read_artifact_info(digest.value, image_path.as_path());

                    let mut response = SogarCustomResponse::new(status_code);
                    response.file(file);

                    if let Some(content_type) = content_type {
                        response.header(CONTENT_TYPE_HEADER, content_type.as_str());
                    }

                    return response;
                }
            }
        }

        SogarCustomResponse::new(StatusCode::NOT_FOUND)
    }
}

async fn write_body_to_file(mut body: hyper::Body, path: &Path) -> io::Result<()> {
    use tokio_02::fs::File;

    let mut file = File::create(path).await?;

    while let Some(chunk_res) = body.data().await {
        match chunk_res {
            Ok(chunk) => {
                debug!("Got a chunk [len = {}]", chunk.len());
                file.write_all(chunk.bytes()).await?
            }

            Err(e) => {
                return Err(io::Error::new(
                    io::ErrorKind::InvalidData,
                    format!("Failed to read chunk! Error is {}", e),
                ));
            }
        }
    }

    file.flush().await?;

    Ok(())
}

fn parse_digest_uri(uri: &Uri) -> Option<String> {
    if let Some(digest_uri) = uri.query() {
        let digest_index = 1;
        let digest_re = Regex::new(r"(.*)=(.*)").unwrap();

        if digest_re.is_match(digest_uri) {
            let split = digest_uri.split('=');
            let result = split.into_iter().map(ToString::to_string).collect::<Vec<String>>();
            return Some(result[digest_index].clone());
        }
    }

    None
}

fn remove_file_if_exists(path: &Path) -> std::result::Result<(), SogarCustomResponse> {
    if path.exists() {
        if let Err(e) = std::fs::remove_file(path) {
            error!("Failed to delete existed file {:?} with error {}", path, e);
            return Err(SogarCustomResponse::new(StatusCode::BAD_REQUEST));
        }
    }

    Ok(())
}

async fn get_file_if_exists(path: &Path) -> (StatusCode, Option<File>) {
    if path.exists() && path.to_str().is_some() {
        if let Ok(file) = File::open(path.to_str().unwrap()).await {
            return (StatusCode::OK, Some(file));
        }
    }

    (StatusCode::NOT_FOUND, None)
}

pub fn add_artifacts_info(filename: String, manifest_mime: Option<String>, image_path: &Path) {
    use std::fs::File;
    use std::fs::OpenOptions;

    let content_path = image_path.join(ARTIFACTS_CONTENT);
    let filepath = image_path.join(ARTIFACTS_DIR).join(&filename);

    let file = File::open(filepath);

    if let Ok(file) = file {
        let json = serde_json::from_reader(file);
        if let Err(e) = json {
            error!("Failed to convert manifest data to json with error: {}", e);
            return;
        }

        let artifacts_content_file = OpenOptions::new().write(true).append(true).open(content_path);

        let manifest: Manifest = json.unwrap();

        if let Ok(mut file) = artifacts_content_file {
            for layer in manifest.layers {
                if let Some(digest) = parse_digest(layer.digest.clone()) {
                    if let Err(e) = writeln!(file, "{}", format!("  {}: {}", digest.value, layer.media_type)) {
                        error!("Couldn't write to file: {}", e);
                    }
                }
            }

            if let Some(manifest_mime) = manifest_mime {
                if let Err(e) = writeln!(file, "{}", format!("  {}: {}", filename, manifest_mime)) {
                    error!("Couldn't write to file: {}", e);
                }
            }
        }
    }
}

fn read_artifact_info(digest_value: String, image_path: &Path) -> Option<String> {
    use std::fs::File;

    let content_path = image_path.join(ARTIFACTS_CONTENT);
    let file = File::open(content_path);

    #[derive(Deserialize)]
    struct ArtifactsData {
        artifacts: HashMap<String, String>,
    }

    if let Ok(file) = file {
        let yaml = serde_yaml::from_reader(file);
        if let Err(e) = yaml {
            error!("Failed to convert manifest data to json with error: {}", e);
            return None;
        }

        let blobs_data: ArtifactsData = yaml.unwrap();
        if blobs_data.artifacts.contains_key(digest_value.as_str()) {
            return blobs_data
                .artifacts
                .get(digest_value.as_str())
                .map(|mime_type| mime_type.to_string());
        }
    }

    None
}