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
use anyhow::anyhow;
use std::borrow::Cow;
use std::convert::{TryFrom, TryInto};
use std::ops::Deref;
pub const OSTREE_COMMIT_LABEL: &str = "ostree.commit";
type Result<T> = anyhow::Result<T>;
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum Transport {
Registry,
OciDir,
OciArchive,
ContainerStorage,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ImageReference {
pub transport: Transport,
pub name: String,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SignatureSource {
OstreeRemote(String),
ContainerPolicy,
ContainerPolicyAllowInsecure,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OstreeImageReference {
pub sigverify: SignatureSource,
pub imgref: ImageReference,
}
impl TryFrom<&str> for Transport {
type Error = anyhow::Error;
fn try_from(value: &str) -> Result<Self> {
Ok(match value {
"registry" | "docker" => Self::Registry,
"oci" => Self::OciDir,
"oci-archive" => Self::OciArchive,
"containers-storage" => Self::ContainerStorage,
o => return Err(anyhow!("Unknown transport '{}'", o)),
})
}
}
impl TryFrom<&str> for ImageReference {
type Error = anyhow::Error;
fn try_from(value: &str) -> Result<Self> {
let (transport_name, mut name) = value
.split_once(':')
.ok_or_else(|| anyhow!("Missing ':' in {}", value))?;
let transport: Transport = transport_name.try_into()?;
if name.is_empty() {
return Err(anyhow!("Invalid empty name in {}", value));
}
if transport_name == "docker" {
name = name
.strip_prefix("//")
.ok_or_else(|| anyhow!("Missing // in docker:// in {}", value))?;
}
Ok(Self {
transport,
name: name.to_string(),
})
}
}
impl TryFrom<&str> for SignatureSource {
type Error = anyhow::Error;
fn try_from(value: &str) -> Result<Self> {
match value {
"ostree-image-signed" => Ok(Self::ContainerPolicy),
"ostree-unverified-image" => Ok(Self::ContainerPolicyAllowInsecure),
o => match o.strip_prefix("ostree-remote-image:") {
Some(rest) => Ok(Self::OstreeRemote(rest.to_string())),
_ => Err(anyhow!("Invalid signature source: {}", o)),
},
}
}
}
impl TryFrom<&str> for OstreeImageReference {
type Error = anyhow::Error;
fn try_from(value: &str) -> Result<Self> {
let (first, second) = value
.split_once(':')
.ok_or_else(|| anyhow!("Missing ':' in {}", value))?;
let (sigverify, rest) = match first {
"ostree-image-signed" => (SignatureSource::ContainerPolicy, Cow::Borrowed(second)),
"ostree-unverified-image" => (
SignatureSource::ContainerPolicyAllowInsecure,
Cow::Borrowed(second),
),
"ostree-unverified-registry" => (
SignatureSource::ContainerPolicyAllowInsecure,
Cow::Owned(format!("registry:{}", second)),
),
"ostree-remote-registry" => {
let (remote, rest) = second
.split_once(':')
.ok_or_else(|| anyhow!("Missing second ':' in {}", value))?;
(
SignatureSource::OstreeRemote(remote.to_string()),
Cow::Owned(format!("registry:{}", rest)),
)
}
"ostree-remote-image" => {
let (remote, rest) = second
.split_once(':')
.ok_or_else(|| anyhow!("Missing second ':' in {}", value))?;
(
SignatureSource::OstreeRemote(remote.to_string()),
Cow::Borrowed(rest),
)
}
o => {
return Err(anyhow!("Invalid ostree image reference scheme: {}", o));
}
};
let imgref = rest.deref().try_into()?;
Ok(Self { sigverify, imgref })
}
}
impl std::fmt::Display for Transport {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let s = match self {
Self::Registry => "docker://",
Self::OciArchive => "oci-archive:",
Self::OciDir => "oci:",
Self::ContainerStorage => "containers-storage:",
};
f.write_str(s)
}
}
impl std::fmt::Display for ImageReference {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}{}", self.transport, self.name)
}
}
impl std::fmt::Display for OstreeImageReference {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match &self.sigverify {
SignatureSource::OstreeRemote(r) => {
write!(f, "ostree-remote-image:{}:{}", r, self.imgref)
}
SignatureSource::ContainerPolicy => write!(f, "ostree-image-signed:{}", self.imgref),
SignatureSource::ContainerPolicyAllowInsecure => {
write!(f, "ostree-unverified-image:{}", self.imgref)
}
}
}
}
pub fn merge_default_container_proxy_opts(
config: &mut containers_image_proxy::ImageProxyConfig,
) -> Result<()> {
if !config.auth_anonymous && config.authfile.is_none() {
config.authfile = crate::globals::get_global_authfile_path()?;
}
Ok(())
}
pub mod deploy;
mod encapsulate;
pub use encapsulate::*;
mod unencapsulate;
pub use unencapsulate::*;
pub(crate) mod ocidir;
mod skopeo;
pub mod store;
#[cfg(test)]
mod tests {
use super::*;
const INVALID_IRS: &[&str] = &["", "foo://", "docker:blah", "registry:", "foo:bar"];
const VALID_IRS: &[&str] = &[
"containers-storage:localhost/someimage",
"docker://quay.io/exampleos/blah:sometag",
];
#[test]
fn test_imagereference() {
let ir: ImageReference = "registry:quay.io/exampleos/blah".try_into().unwrap();
assert_eq!(ir.transport, Transport::Registry);
assert_eq!(ir.name, "quay.io/exampleos/blah");
assert_eq!(ir.to_string(), "docker://quay.io/exampleos/blah");
for &v in VALID_IRS {
ImageReference::try_from(v).unwrap();
}
for &v in INVALID_IRS {
if ImageReference::try_from(v).is_ok() {
panic!("Should fail to parse: {}", v)
}
}
let ir: ImageReference = "oci:somedir".try_into().unwrap();
assert_eq!(ir.transport, Transport::OciDir);
assert_eq!(ir.name, "somedir");
}
#[test]
fn test_ostreeimagereference() {
let ir_s = "ostree-remote-image:myremote:registry:quay.io/exampleos/blah";
let ir_registry = "ostree-remote-registry:myremote:quay.io/exampleos/blah";
for &ir_s in &[ir_s, ir_registry] {
let ir: OstreeImageReference = ir_s.try_into().unwrap();
assert_eq!(
ir.sigverify,
SignatureSource::OstreeRemote("myremote".to_string())
);
assert_eq!(ir.imgref.transport, Transport::Registry);
assert_eq!(ir.imgref.name, "quay.io/exampleos/blah");
assert_eq!(
ir.to_string(),
"ostree-remote-image:myremote:docker://quay.io/exampleos/blah"
);
}
let ir: OstreeImageReference = ir_s.try_into().unwrap();
assert_eq!(&ir, &OstreeImageReference::try_from(ir_registry).unwrap());
let ir_s = "ostree-image-signed:docker://quay.io/exampleos/blah";
let ir: OstreeImageReference = ir_s.try_into().unwrap();
assert_eq!(ir.sigverify, SignatureSource::ContainerPolicy);
assert_eq!(ir.imgref.transport, Transport::Registry);
assert_eq!(ir.imgref.name, "quay.io/exampleos/blah");
assert_eq!(
ir.to_string(),
"ostree-image-signed:docker://quay.io/exampleos/blah"
);
let ir_s = "ostree-unverified-image:docker://quay.io/exampleos/blah";
let ir: OstreeImageReference = ir_s.try_into().unwrap();
assert_eq!(ir.sigverify, SignatureSource::ContainerPolicyAllowInsecure);
assert_eq!(ir.imgref.transport, Transport::Registry);
assert_eq!(ir.imgref.name, "quay.io/exampleos/blah");
assert_eq!(
ir.to_string(),
"ostree-unverified-image:docker://quay.io/exampleos/blah"
);
let ir_shorthand =
OstreeImageReference::try_from("ostree-unverified-registry:quay.io/exampleos/blah")
.unwrap();
assert_eq!(&ir_shorthand, &ir);
}
}