Skip to content

Commit 9698067

Browse files
author
mayastor-bors
committed
Try #927:
2 parents cfc4159 + d94524e commit 9698067

File tree

5 files changed

+110
-22
lines changed

5 files changed

+110
-22
lines changed

control-plane/csi-driver/src/bin/controller/client.rs

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -123,16 +123,50 @@ impl RestApiClient {
123123
let url = clients::tower::Url::parse(endpoint)
124124
.map_err(|error| anyhow!("Invalid API endpoint URL {}: {:?}", endpoint, error))?;
125125
let concurrency_limit = cfg.create_volume_limit() * 2;
126-
let tower = clients::tower::Configuration::builder()
127-
.with_timeout(cfg.io_timeout())
128-
.with_concurrency_limit(Some(concurrency_limit))
129-
.build_url(url)
130-
.map_err(|error| {
131-
anyhow::anyhow!(
132-
"Failed to create openapi configuration, Error: '{:?}'",
133-
error
134-
)
135-
})?;
126+
let ca_certificate_path = cfg.ca_certificate_path();
127+
let cert = match ca_certificate_path {
128+
Some(path) => {
129+
let cert = std::fs::read(path).expect("Failed to read certificate file");
130+
Some(cert)
131+
},
132+
None => None,
133+
};
134+
let tower = match (url.scheme(), cert) {
135+
("https", Some(cert)) => {
136+
debug!("Attempting TLS connection to {}", url);
137+
138+
// Use new_with_client method to create the configuration
139+
clients::tower::Configuration::builder()
140+
.with_timeout(Some(cfg.io_timeout()))
141+
.with_concurrency_limit(Some(concurrency_limit))
142+
.with_certificate(cert.as_slice())
143+
.build_url(url)
144+
.map_err(|error| {
145+
anyhow::anyhow!(
146+
"Failed to create openapi configuration, Error: '{:?}'",
147+
error
148+
)
149+
})?
150+
},
151+
("https", None) => {
152+
anyhow::bail!("HTTPS endpoint requires a CA certificate path");
153+
},
154+
(_, Some(_path)) => {
155+
anyhow::bail!("CA certificate path is only supported for HTTPS endpoints");
156+
},
157+
_ => {
158+
clients::tower::Configuration::builder()
159+
.with_timeout(Some(cfg.io_timeout()))
160+
.with_concurrency_limit(Some(concurrency_limit))
161+
.build_url(url)
162+
.map_err(|error| {
163+
anyhow::anyhow!(
164+
"Failed to create openapi configuration, Error: '{:?}'",
165+
error
166+
)
167+
})?
168+
}
169+
};
136170

137171
REST_CLIENT.get_or_init(|| Self {
138172
rest_client: clients::tower::ApiClient::new(tower.clone()),

control-plane/csi-driver/src/bin/controller/config.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ pub(crate) struct CsiControllerConfig {
1717
create_volume_limit: usize,
1818
/// Force unstage volume.
1919
force_unstage_volume: bool,
20+
/// Path to the CA certificate file.
21+
ca_certificate_path: Option<String>,
2022
}
2123

2224
impl CsiControllerConfig {
@@ -50,14 +52,17 @@ impl CsiControllerConfig {
5052
tracing::warn!(
5153
"Force unstage volume is disabled, can trigger potential data corruption!"
5254
);
53-
}
55+
};
56+
57+
let ca_certificate_path = args.get_one::<String>("tls-client-ca-path");
5458

5559
CONFIG.get_or_init(|| Self {
5660
rest_endpoint: rest_endpoint.into(),
5761
io_timeout: io_timeout.into(),
5862
node_selector,
5963
create_volume_limit,
6064
force_unstage_volume,
65+
ca_certificate_path: ca_certificate_path.cloned(),
6166
});
6267
Ok(())
6368
}
@@ -92,4 +97,9 @@ impl CsiControllerConfig {
9297
pub(crate) fn force_unstage_volume(&self) -> bool {
9398
self.force_unstage_volume
9499
}
100+
101+
/// Path to the CA certificate file.
102+
pub(crate) fn ca_certificate_path(&self) -> Option<&str> {
103+
self.ca_certificate_path.as_deref()
104+
}
95105
}

control-plane/csi-driver/src/bin/controller/main.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,11 @@ async fn main() -> anyhow::Result<()> {
131131
.value_parser(clap::value_parser!(bool))
132132
.help("Enable force unstage volume feature")
133133
)
134+
.arg(
135+
Arg::new("tls-client-ca-path")
136+
.long("tls-client-ca-path")
137+
.help("path to the CA certificate file"),
138+
)
134139
.get_matches();
135140

136141
utils::print_package_info!();

control-plane/rest/service/src/main.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use clap::Parser;
2121
use grpc::{client::CoreClient, operations::jsongrpc::client::JsonGrpcClient};
2222
use http::Uri;
2323
use rustls::{pki_types::PrivateKeyDer, ServerConfig};
24-
use rustls_pemfile::{certs, rsa_private_keys};
24+
use rustls_pemfile::{certs, pkcs8_private_keys};
2525
use std::{fs::File, io::BufReader, time::Duration};
2626
use stor_port::transport_api::{RequestMinTimeout, TimeoutOptions};
2727
use utils::{
@@ -180,17 +180,18 @@ fn load_certificates<R: std::io::Read>(
180180
.map_err(|_| {
181181
anyhow::anyhow!("Failed to retrieve certificates from the certificate file",)
182182
})?;
183-
let mut keys = rsa_private_keys(key_file)
183+
let mut keys: Vec<rustls::pki_types::PrivatePkcs8KeyDer<'_>> = pkcs8_private_keys(key_file)
184184
.collect::<Result<Vec<_>, _>>()
185185
.map_err(|_| {
186186
anyhow::anyhow!("Failed to retrieve the rsa private keys from the key file",)
187187
})?;
188+
188189
if keys.is_empty() {
189190
anyhow::bail!("No keys found in the keys file");
190191
}
191192
let config = config
192193
.with_no_client_auth()
193-
.with_single_cert(cert_chain, PrivateKeyDer::Pkcs1(keys.remove(0)))?;
194+
.with_single_cert(cert_chain, PrivateKeyDer::Pkcs8(keys.remove(0)))?;
194195
Ok(config)
195196
}
196197

k8s/operators/src/pool/main.rs

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use kube::{
3131
use mayastorpool::client::{check_crd, delete, list};
3232
use openapi::clients::{self, tower::Url};
3333
use std::{collections::HashMap, sync::Arc, time::Duration};
34-
use tracing::{error, info, trace, warn};
34+
use tracing::{error, info, trace, warn, debug};
3535
use utils::tracing_telemetry::{FmtLayer, FmtStyle};
3636

3737
const PAGINATION_LIMIT: u32 = 100;
@@ -129,14 +129,47 @@ async fn pool_controller(args: ArgMatches) -> anyhow::Result<()> {
129129
.expect("timeout value is invalid")
130130
.into();
131131

132-
let cfg = clients::tower::Configuration::new(url, timeout, None, None, true, None).map_err(
133-
|error| {
134-
anyhow::anyhow!(
135-
"Failed to create openapi configuration, Error: '{:?}'",
136-
error
137-
)
132+
let ca_certificate_path:Option<&str> = args.get_one::<String>("tls-client-ca-path").map(|x| x.as_str());
133+
// take in cert path and make pem file
134+
let cert = match ca_certificate_path {
135+
Some(path) => {
136+
let cert = std::fs::read(path).expect("Failed to read certificate file");
137+
Some(cert)
138138
},
139-
)?;
139+
None => None,
140+
};
141+
let cfg = match (url.scheme(), cert) {
142+
("https", Some(cert)) => {
143+
debug!("Attempting TLS connection to {}", url);
144+
145+
clients::tower::Configuration::new(url, timeout, None, Some(cert.as_slice()), true, None)
146+
.map_err(
147+
|error| {
148+
anyhow::anyhow!(
149+
"Failed to create openapi configuration, Error: '{:?}'",
150+
error
151+
)
152+
},
153+
)?
154+
},
155+
("https", None) => {
156+
anyhow::bail!("HTTPS endpoint requires a CA certificate path");
157+
},
158+
(_, Some(_path)) => {
159+
anyhow::bail!("CA certificate path is only supported for HTTPS endpoints");
160+
},
161+
_ => {
162+
clients::tower::Configuration::new(url, timeout, None, None, true, None)
163+
.map_err(
164+
|error| {
165+
anyhow::anyhow!(
166+
"Failed to create openapi configuration, Error: '{:?}'",
167+
error
168+
)
169+
},
170+
)?
171+
}
172+
};
140173
let interval = args
141174
.get_one::<String>("interval")
142175
.unwrap()
@@ -243,6 +276,11 @@ async fn main() -> anyhow::Result<()> {
243276
.value_parser(clap::value_parser!(bool))
244277
.help("Enable ansi color for logs"),
245278
)
279+
.arg(
280+
Arg::new("tls-client-ca-path")
281+
.long("tls-client-ca-path")
282+
.help("path to the CA certificate file"),
283+
)
246284
.get_matches();
247285

248286
utils::print_package_info!();

0 commit comments

Comments
 (0)