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
use std::{iter::FromIterator, str::FromStr};

use log::{debug, trace};
use reqwest::{self, header, StatusCode, Url};

use crate::{
  errors::{Error, Result},
  mediatypes,
  v2::*,
};

mod manifest_schema1;
pub use self::manifest_schema1::*;

mod manifest_schema2;
pub use self::manifest_schema2::{
  ConfigBlob, ManifestList, ManifestObj, ManifestSchema2, ManifestSchema2Spec, Platform,
};

impl Client {
  /// Fetch an image manifest.
  ///
  /// The name and reference parameters identify the image.
  /// The reference may be either a tag or digest.
  pub async fn get_manifest(&self, name: &str, reference: &str) -> Result<Manifest> {
    self
      .get_manifest_and_ref(name, reference)
      .await
      .map(|(manifest, _)| manifest)
  }

  /// Fetch an image manifest and return it with its digest.
  ///
  /// The name and reference parameters identify the image.
  /// The reference may be either a tag or digest.
  pub async fn get_manifest_and_ref(&self, name: &str, reference: &str) -> Result<(Manifest, Option<String>)> {
    let url = self.build_url(name, reference)?;

    let accept_headers = build_accept_headers(&self.accepted_types);

    let client_spare0 = self.clone();

    let res = self
      .build_reqwest(Method::GET, url.clone())
      .headers(accept_headers)
      .send()
      .await?;

    let status = res.status();
    trace!("GET '{}' status: {:?}", res.url(), status);

    match status {
      StatusCode::OK => {}
      _ => return Err(Error::UnexpectedHttpStatus(status)),
    }

    let headers = res.headers();
    let content_digest = match headers.get("docker-content-digest") {
      Some(content_digest_value) => Some(content_digest_value.to_str()?.to_string()),
      None => {
        debug!("cannot find manifestref in headers");
        None
      }
    };

    let header_content_type = headers.get(header::CONTENT_TYPE);
    let media_type = evaluate_media_type(header_content_type, &url)?;

    trace!("content-type: {:?}, media-type: {:?}", header_content_type, media_type);

    match media_type {
      mediatypes::MediaTypes::ManifestV2S1Signed => Ok((
        res.json::<ManifestSchema1Signed>().await.map(Manifest::S1Signed)?,
        content_digest,
      )),
      mediatypes::MediaTypes::ManifestV2S2 => {
        let m = res.json::<ManifestSchema2Spec>().await?;
        Ok((
          m.fetch_config_blob(client_spare0, name.to_string())
            .await
            .map(Manifest::S2)?,
          content_digest,
        ))
      }
      mediatypes::MediaTypes::ManifestList => Ok((res.json::<ManifestList>().await.map(Manifest::ML)?, content_digest)),
      unsupported => Err(Error::UnsupportedMediaType(unsupported)),
    }
  }

  fn build_url(&self, name: &str, reference: &str) -> Result<Url> {
    let ep = format!("{}/v2/{}/manifests/{}", self.base_url.clone(), name, reference);
    reqwest::Url::parse(&ep).map_err(Error::from)
  }

  /// Fetch content digest for a particular tag.
  pub async fn get_manifestref(&self, name: &str, reference: &str) -> Result<Option<String>> {
    let url = self.build_url(name, reference)?;

    let accept_headers = build_accept_headers(&self.accepted_types);

    let res = self
      .build_reqwest(Method::HEAD, url)
      .headers(accept_headers)
      .send()
      .await?;

    let status = res.status();
    trace!("HEAD '{}' status: {:?}", res.url(), status);

    match status {
      StatusCode::OK => {}
      _ => return Err(Error::UnexpectedHttpStatus(status)),
    }

    let headers = res.headers();
    let content_digest = match headers.get("docker-content-digest") {
      Some(content_digest_value) => Some(content_digest_value.to_str()?.to_string()),
      None => {
        debug!("cannot find manifestref in headers");
        None
      }
    };
    Ok(content_digest)
  }

  /// Check if an image manifest exists.
  ///
  /// The name and reference parameters identify the image.
  /// The reference may be either a tag or digest.
  pub async fn has_manifest(
    &self,
    name: &str,
    reference: &str,
    mediatypes: Option<&[&str]>,
  ) -> Result<Option<mediatypes::MediaTypes>> {
    let url = self.build_url(name, reference)?;
    let accept_types = match mediatypes {
      None => {
        let m = mediatypes::MediaTypes::ManifestV2S2.to_mime();
        vec![m]
      }
      Some(v) => to_mimes(v),
    };

    let mut accept_headers = header::HeaderMap::with_capacity(accept_types.len());
    for accept_type in accept_types {
      let header_value =
        header::HeaderValue::from_str(accept_type.as_ref()).expect("mime type is always valid header value");
      accept_headers.insert(header::ACCEPT, header_value);
    }

    trace!("HEAD {:?}", url);

    let r = self
      .build_reqwest(Method::HEAD, url.clone())
      .headers(accept_headers)
      .send()
      .await
      .map_err(Error::from)?;

    let status = r.status();

    trace!("Manifest check status '{:?}', headers '{:?}", r.status(), r.headers(),);

    match status {
      StatusCode::MOVED_PERMANENTLY | StatusCode::TEMPORARY_REDIRECT | StatusCode::FOUND | StatusCode::OK => {
        let media_type = evaluate_media_type(r.headers().get(header::CONTENT_TYPE), r.url())?;
        trace!("Manifest media-type: {:?}", media_type);
        Ok(Some(media_type))
      }
      StatusCode::NOT_FOUND => Ok(None),
      _ => Err(Error::UnexpectedHttpStatus(status)),
    }
  }
}

fn to_mimes(v: &[&str]) -> Vec<mime::Mime> {
  let res = v
    .iter()
    .filter_map(|x| {
      let mtype = mediatypes::MediaTypes::from_str(x);
      match mtype {
        Ok(m) => Some(m.to_mime()),
        _ => None,
      }
    })
    .collect();
  res
}

// Evaluate the `MediaTypes` from the the request header.
fn evaluate_media_type(
  content_type: Option<&reqwest::header::HeaderValue>,
  url: &Url,
) -> Result<mediatypes::MediaTypes> {
  let header_content_type = content_type
    .map(|hv| hv.to_str())
    .map(std::result::Result::unwrap_or_default);

  let is_pulp_based = url.path().starts_with("/pulp/docker/v2");

  match (header_content_type, is_pulp_based) {
    (Some(header_value), false) => mediatypes::MediaTypes::from_str(header_value).map_err(strum::ParseError::into),
    (None, false) => Err(Error::MediaTypeSniff),
    (Some(header_value), true) => {
      // TODO: remove this workaround once Satellite returns a proper content-type here
      match header_value {
        "application/x-troff-man" => {
          trace!("Applying workaround for pulp-based registries, e.g. Satellite");
          mediatypes::MediaTypes::from_str("application/vnd.docker.distribution.manifest.v1+prettyjws")
            .map_err(strum::ParseError::into)
        }
        _ => {
          debug!(
            "Received content-type '{}' from pulp-based registry. Feeling lucky and trying to parse it...",
            header_value
          );
          mediatypes::MediaTypes::from_str(header_value).map_err(strum::ParseError::into)
        }
      }
    }
    (None, true) => {
      trace!("Applying workaround for pulp-based registries, e.g. Satellite");
      mediatypes::MediaTypes::from_str("application/vnd.docker.distribution.manifest.v1+prettyjws")
        .map_err(strum::ParseError::into)
    }
  }
}

fn build_accept_headers(accepted_types: &[(MediaTypes, Option<f64>)]) -> header::HeaderMap {
  let accepted_types_string = accepted_types
    .iter()
    .map(|(ty, q)| {
      format!(
        "{}{}",
        ty,
        match q {
          None => String::default(),
          Some(v) => format!("; q={}", v),
        }
      )
    })
    .collect::<Vec<_>>()
    .join(",");

  header::HeaderMap::from_iter(vec![(
    header::ACCEPT,
    header::HeaderValue::from_str(&accepted_types_string)
      .expect("should be always valid because both float and mime type only use allowed ASCII chard"),
  )])
}

/// Umbrella type for common actions on the different manifest schema types
#[derive(Debug)]
pub enum Manifest {
  S1Signed(manifest_schema1::ManifestSchema1Signed),
  S2(manifest_schema2::ManifestSchema2),
  ML(manifest_schema2::ManifestList),
}

#[derive(Debug, thiserror::Error)]
pub enum ManifestError {
  #[error("no architecture in manifest")]
  NoArchitecture,
  #[error("architecture mismatch")]
  ArchitectureMismatch,
  #[error("manifest {0} does not support the 'layer_digests' method")]
  LayerDigestsUnsupported(String),
  #[error("manifest {0} does not support the 'architecture' method")]
  ArchitectureNotSupported(String),
}

impl Manifest {
  /// List digests of all layers referenced by this manifest, if available.
  /// For ManifestList, returns the digests of all the manifest list images.
  ///
  /// As manifest list images only contain digests of the
  /// images contained in the manifest, the `layers_digests`
  /// function returns the digests of all the images
  /// contained in the ManifestList instead of individual
  /// layers of the manifests.
  /// The layers of a specific image from manifest list can
  /// be obtained using the digest of the image from the
  /// manifest list and getting its manifest and manifestref
  /// (get_manifest_and_ref()) and using this manifest of
  /// the individual image to get the layers.
  ///
  /// The returned layers list for non ManifestList images is ordered starting with the base image first.
  pub fn layers_digests(&self, architecture: Option<&str>) -> Result<Vec<String>> {
    match (self, self.architectures(), architecture) {
      (Manifest::S1Signed(m), _, None) => Ok(m.get_layers()),
      (Manifest::S2(m), _, None) => Ok(m.get_layers()),
      (Manifest::S1Signed(m), Ok(ref self_architectures), Some(ref a)) => {
        let self_a = self_architectures.first().ok_or(ManifestError::NoArchitecture)?;
        if self_a != a {
          return Err(ManifestError::ArchitectureMismatch.into());
        }
        Ok(m.get_layers())
      }
      (Manifest::S2(m), Ok(ref self_architectures), Some(ref a)) => {
        let self_a = self_architectures.first().ok_or(ManifestError::NoArchitecture)?;
        if self_a != a {
          return Err(ManifestError::ArchitectureMismatch.into());
        }
        Ok(m.get_layers())
      }
      (Manifest::ML(m), _, _) => Ok(m.get_digests()),
      _ => Err(ManifestError::LayerDigestsUnsupported(format!("{:?}", self)).into()),
    }
  }

  /// The architectures of the image the manifest points to, if available.
  pub fn architectures(&self) -> Result<Vec<String>> {
    match self {
      Manifest::S1Signed(m) => Ok([m.architecture.clone()].to_vec()),
      Manifest::S2(m) => Ok([m.architecture()].to_vec()),
      Manifest::ML(m) => Ok(m.architectures()),
    }
  }
}

#[cfg(test)]
mod tests {
  use test_case::test_case;

  use super::*;
  use crate::v2::Client;

  #[test_case("not-gcr.io" => "application/vnd.docker.distribution.manifest.v2+json; q=0.5,application/vnd.docker.distribution.manifest.v1+prettyjws; q=0.4,application/vnd.docker.distribution.manifest.list.v2+json; q=0.5"; "Not gcr registry")]
  #[test_case("gcr.io" => "application/vnd.docker.distribution.manifest.v2+json,application/vnd.docker.distribution.manifest.v1+prettyjws,application/vnd.docker.distribution.manifest.list.v2+json"; "gcr.io")]
  #[test_case("foobar.gcr.io" => "application/vnd.docker.distribution.manifest.v2+json,application/vnd.docker.distribution.manifest.v1+prettyjws,application/vnd.docker.distribution.manifest.list.v2+json"; "Custom gcr.io registry")]
  fn gcr_io_accept_headers(registry: &str) -> String {
    let client_builder = Client::configure().registry(registry);
    let client = client_builder.build().unwrap();
    let header_map = build_accept_headers(&client.accepted_types);
    header_map.get(header::ACCEPT).unwrap().to_str().unwrap().to_string()
  }
  #[test_case(None => "application/vnd.docker.distribution.manifest.v2+json; q=0.5,application/vnd.docker.distribution.manifest.v1+prettyjws; q=0.4,application/vnd.docker.distribution.manifest.list.v2+json; q=0.5"; "Default settings")]
  #[test_case(Some(vec![
        (MediaTypes::ManifestV2S2, Some(0.5)),
        (MediaTypes::ManifestV2S1Signed, Some(0.2)),
        (MediaTypes::ManifestList, Some(0.5)),
    ]) => "application/vnd.docker.distribution.manifest.v2+json; q=0.5,application/vnd.docker.distribution.manifest.v1+prettyjws; q=0.2,application/vnd.docker.distribution.manifest.list.v2+json; q=0.5"; "Custom accept types with weight")]
  #[test_case(Some(vec![
        (MediaTypes::ManifestV2S2, None),
        (MediaTypes::ManifestList, None),
    ]) => "application/vnd.docker.distribution.manifest.v2+json,application/vnd.docker.distribution.manifest.list.v2+json"; "Custom accept types, no weight")]
  fn custom_accept_headers(accept_headers: Option<Vec<(MediaTypes, Option<f64>)>>) -> String {
    let registry = "https://example.com";

    let client_builder = Client::configure().registry(registry).accepted_types(accept_headers);
    let client = client_builder.build().unwrap();
    let header_map = build_accept_headers(&client.accepted_types);
    header_map.get(header::ACCEPT).unwrap().to_str().unwrap().to_string()
  }
}