Skip to content

Commit 140c8d1

Browse files
authored
Cherry pick "[nydusd] Support custom TLS CA" (#31)
* Add TLS and mTLS support for nydusd Signed-off-by: Etienne Carriere <etienne.carriere@datadoghq.com> * Fix format Signed-off-by: Etienne Carriere <etienne.carriere@datadoghq.com> * Handle only TLS trusted CA configuration Signed-off-by: Etienne Carriere <etienne.carriere@datadoghq.com> * Also inject ca_cert_files for HttpProxyConfig Signed-off-by: Etienne Carriere <etienne.carriere@datadoghq.com> * Also add the configuration for the proxy Signed-off-by: Etienne Carriere <etienne.carriere@datadoghq.com> * Add some unit tests for ca_cert_files parameter Signed-off-by: Etienne Carriere <etienne.carriere@datadoghq.com> --------- Signed-off-by: Etienne Carriere <etienne.carriere@datadoghq.com>
1 parent d18da01 commit 140c8d1

7 files changed

Lines changed: 57 additions & 2 deletions

File tree

api/src/config.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,9 @@ pub struct OssConfig {
504504
/// Skip SSL certificate validation for HTTPS scheme.
505505
#[serde(default)]
506506
pub skip_verify: bool,
507+
/// Paths to PEM-encoded CA certificate files to trust in addition to the system CA store.
508+
#[serde(default)]
509+
pub ca_cert_files: Vec<String>,
507510
/// Drop the read request once http request timeout, in seconds.
508511
#[serde(default = "default_http_timeout")]
509512
pub timeout: u32,
@@ -548,6 +551,9 @@ pub struct S3Config {
548551
/// Skip SSL certificate validation for HTTPS scheme.
549552
#[serde(default)]
550553
pub skip_verify: bool,
554+
/// Paths to PEM-encoded CA certificate files to trust in addition to the system CA store.
555+
#[serde(default)]
556+
pub ca_cert_files: Vec<String>,
551557
/// Drop the read request once http request timeout, in seconds.
552558
#[serde(default = "default_http_timeout")]
553559
pub timeout: u32,
@@ -577,6 +583,9 @@ pub struct HttpProxyConfig {
577583
/// Skip SSL certificate validation for HTTPS scheme.
578584
#[serde(default)]
579585
pub skip_verify: bool,
586+
/// Paths to PEM-encoded CA certificate files to trust in addition to the system CA store.
587+
#[serde(default)]
588+
pub ca_cert_files: Vec<String>,
580589
/// Drop the read request once http request timeout, in seconds.
581590
#[serde(default = "default_http_timeout")]
582591
pub timeout: u32,
@@ -611,6 +620,9 @@ pub struct RegistryConfig {
611620
/// When true, also allows automatic HTTPS-to-HTTP fallback on TLS errors.
612621
#[serde(default)]
613622
pub skip_verify: bool,
623+
/// Paths to PEM-encoded CA certificate files to trust in addition to the system CA store.
624+
#[serde(default)]
625+
pub ca_cert_files: Vec<String>,
614626
/// Drop the read request once http request timeout, in seconds.
615627
#[serde(default = "default_http_timeout")]
616628
pub timeout: u32,
@@ -1745,6 +1757,7 @@ mod tests {
17451757
let config: OssConfig = serde_json::from_str(content).unwrap();
17461758
assert_eq!(config.scheme, "https");
17471759
assert!(!config.skip_verify);
1760+
assert!(config.ca_cert_files.is_empty());
17481761
assert_eq!(config.timeout, 5);
17491762
assert_eq!(config.connect_timeout, 5);
17501763
}
@@ -1762,6 +1775,7 @@ mod tests {
17621775
let config: OssConfig = serde_json::from_str(content).unwrap();
17631776
assert_eq!(config.scheme, "https");
17641777
assert!(!config.skip_verify);
1778+
assert!(config.ca_cert_files.is_empty());
17651779
assert_eq!(config.timeout, 5);
17661780
assert_eq!(config.connect_timeout, 5);
17671781
}
@@ -1775,11 +1789,15 @@ mod tests {
17751789
"repo": "test/repo",
17761790
"auth": "base64_encoded_auth",
17771791
"registry_token": "bearer_token",
1778-
"blob_redirected_host": "blob_redirected_host"
1792+
"blob_redirected_host": "blob_redirected_host",
1793+
"ca_cert_files": ["/etc/ssl/certs/my-ca.pem","/etc/ssl/certs/my-ca2.pem"]
17791794
}"#;
17801795
let config: RegistryConfig = serde_json::from_str(content).unwrap();
17811796
assert_eq!(config.scheme, "http");
17821797
assert!(config.skip_verify);
1798+
assert_eq!(config.ca_cert_files.len(), 2);
1799+
assert_eq!(config.ca_cert_files[0], "/etc/ssl/certs/my-ca.pem");
1800+
assert_eq!(config.ca_cert_files[1], "/etc/ssl/certs/my-ca2.pem");
17831801
}
17841802

17851803
#[test]

docs/nydusd.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,9 @@ Document located at: https://github.com/adamqqqplay/nydus-localdisk/blob/master/
260260
// Skip SSL certificate validation for HTTPS scheme.
261261
// When true, also allows automatic HTTPS-to-HTTP fallback on TLS errors.
262262
"skip_verify": false,
263+
// Paths to PEM-encoded CA certificate files to trust in addition to the system CA store, optional.
264+
// Useful when the registry uses a private or self-signed CA.
265+
"ca_cert_files": ["/etc/ssl/certs/my-ca.pem"],
263266
// Use format `$namespace/$repo` (no image tag)
264267
"repo": "test/repo",
265268
// Username and password for auth

misc/configs/nydusd-blob-cache-entry-configuration-v2.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ access_key_id = "my_access_key_id"
3131
access_key_secret = "my_access_key_secret"
3232
# Skip SSL certificate validation for HTTPS scheme.
3333
skip_verify = true
34+
# Paths to PEM-encoded CA certificate files to trust in addition to the system CA store, optional.
35+
# Useful when the endpoint uses a private or self-signed CA.
36+
# ca_cert_files = ["/etc/ssl/certs/my-ca.pem"]
3437
# Drop the read request once http request timeout, in seconds.
3538
timeout = 10
3639
# Drop the read request once http connection timeout, in seconds.
@@ -74,6 +77,9 @@ auth = "base64_encoded"
7477
# Skip SSL certificate validation for HTTPS scheme.
7578
# When true, also allows automatic HTTPS-to-HTTP fallback on TLS errors.
7679
skip_verify = true
80+
# Paths to PEM-encoded CA certificate files to trust in addition to the system CA store, optional.
81+
# Useful when the registry uses a private or self-signed CA.
82+
# ca_cert_files = ["/etc/ssl/certs/my-ca.pem"]
7783
# Drop the read request once http request timeout, in seconds.
7884
timeout = 10
7985
# Drop the read request once http connection timeout, in seconds.

misc/configs/nydusd-blob-cache-entry.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ access_key_id = "my_access_key_id"
3636
access_key_secret = "my_access_key_secret"
3737
# Skip SSL certificate validation for HTTPS scheme.
3838
skip_verify = true
39+
# Paths to PEM-encoded CA certificate files to trust in addition to the system CA store, optional.
40+
# Useful when the endpoint uses a private or self-signed CA.
41+
# ca_cert_files = ["/etc/ssl/certs/my-ca.pem"]
3942
# Drop the read request once http request timeout, in seconds.
4043
timeout = 10
4144
# Drop the read request once http connection timeout, in seconds.
@@ -79,6 +82,9 @@ auth = "base64_encoded"
7982
# Skip SSL certificate validation for HTTPS scheme.
8083
# When true, also allows automatic HTTPS-to-HTTP fallback on TLS errors.
8184
skip_verify = true
85+
# Paths to PEM-encoded CA certificate files to trust in addition to the system CA store, optional.
86+
# Useful when the registry uses a private or self-signed CA.
87+
# ca_cert_files = ["/etc/ssl/certs/my-ca.pem"]
8288
# Drop the read request once http request timeout, in seconds.
8389
timeout = 10
8490
# Drop the read request once http connection timeout, in seconds.

misc/configs/nydusd-config-v2.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ access_key_id = "my_access_key_id"
2929
access_key_secret = "my_access_key_secret"
3030
# Skip SSL certificate validation for HTTPS scheme.
3131
skip_verify = true
32+
# Paths to PEM-encoded CA certificate files to trust in addition to the system CA store, optional.
33+
# Useful when the endpoint uses a private or self-signed CA.
34+
# ca_cert_files = ["/etc/ssl/certs/my-ca.pem"]
3235
# Drop the read request once http request timeout, in seconds.
3336
timeout = 10
3437
# Drop the read request once http connection timeout, in seconds.
@@ -72,6 +75,9 @@ auth = "base64_encoded"
7275
# Skip SSL certificate validation for HTTPS scheme.
7376
# When true, also allows automatic HTTPS-to-HTTP fallback on TLS errors.
7477
skip_verify = true
78+
# Paths to PEM-encoded CA certificate files to trust in addition to the system CA store, optional.
79+
# Useful when the registry uses a private or self-signed CA.
80+
# ca_cert_files = ["/etc/ssl/certs/my-ca.pem"]
7581
# Drop the read request once http request timeout, in seconds.
7682
timeout = 10
7783
# Drop the read request once http connection timeout, in seconds.

misc/configs/nydusd-config.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
"config": {
66
"timeout": 5,
77
"connect_timeout": 5,
8-
"retry_limit": 2
8+
"retry_limit": 2,
9+
"skip_verify": false,
10+
"ca_cert_files": ["/etc/ssl/certs/my-ca.pem"]
911
}
1012
},
1113
"cache": {

storage/src/backend/connection.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ pub(crate) struct ConnectionConfig {
7474
pub timeout: u32,
7575
pub connect_timeout: u32,
7676
pub retry_limit: u8,
77+
/// Paths to PEM-encoded CA certificate files to trust in addition to the system CA store.
78+
pub ca_cert_files: Vec<String>,
7779
}
7880

7981
impl Default for ConnectionConfig {
@@ -85,6 +87,7 @@ impl Default for ConnectionConfig {
8587
timeout: 5,
8688
connect_timeout: 5,
8789
retry_limit: 0,
90+
ca_cert_files: Vec::new(),
8891
}
8992
}
9093
}
@@ -98,6 +101,7 @@ impl From<OssConfig> for ConnectionConfig {
98101
timeout: c.timeout,
99102
connect_timeout: c.connect_timeout,
100103
retry_limit: c.retry_limit,
104+
ca_cert_files: c.ca_cert_files,
101105
}
102106
}
103107
}
@@ -111,6 +115,7 @@ impl From<S3Config> for ConnectionConfig {
111115
timeout: c.timeout,
112116
connect_timeout: c.connect_timeout,
113117
retry_limit: c.retry_limit,
118+
ca_cert_files: c.ca_cert_files,
114119
}
115120
}
116121
}
@@ -124,6 +129,7 @@ impl From<RegistryConfig> for ConnectionConfig {
124129
timeout: c.timeout,
125130
connect_timeout: c.connect_timeout,
126131
retry_limit: c.retry_limit,
132+
ca_cert_files: c.ca_cert_files,
127133
}
128134
}
129135
}
@@ -137,6 +143,7 @@ impl From<HttpProxyConfig> for ConnectionConfig {
137143
timeout: c.timeout,
138144
connect_timeout: c.connect_timeout,
139145
retry_limit: c.retry_limit,
146+
ca_cert_files: c.ca_cert_files,
140147
}
141148
}
142149
}
@@ -660,6 +667,12 @@ impl Connection {
660667
cb = cb.danger_accept_invalid_certs(true);
661668
}
662669

670+
for ca_cert_file in &config.ca_cert_files {
671+
let pem = std::fs::read(ca_cert_file).map_err(|e| einval!(e))?;
672+
let cert = reqwest::Certificate::from_pem(&pem).map_err(|e| einval!(e))?;
673+
cb = cb.add_root_certificate(cert);
674+
}
675+
663676
if !proxy.is_empty() {
664677
cb = cb.proxy(reqwest::Proxy::all(proxy).map_err(|e| einval!(e))?)
665678
}
@@ -789,5 +802,6 @@ mod tests {
789802
assert_eq!(config.proxy.ping_url, "");
790803
assert_eq!(config.proxy.url, "");
791804
assert!(config.mirrors.is_empty());
805+
assert!(config.ca_cert_files.is_empty());
792806
}
793807
}

0 commit comments

Comments
 (0)