Skip to content

Commit 8a17525

Browse files
author
mayastor-bors
committed
Try #927:
2 parents cfc4159 + 3e15568 commit 8a17525

File tree

5 files changed

+122
-23
lines changed

5 files changed

+122
-23
lines changed

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

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -123,16 +123,53 @@ 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).map_err(|error| {
130+
anyhow::anyhow!(
131+
"Failed to create openapi configuration, Error: '{:?}'",
132+
error
133+
)
134+
})?;
135+
Some(cert)
136+
}
137+
None => None,
138+
};
139+
let tower = match (url.scheme(), cert) {
140+
("https", Some(cert)) => {
141+
debug!("Attempting TLS connection to {}", url);
142+
143+
// Use new_with_client method to create the configuration
144+
clients::tower::Configuration::builder()
145+
.with_timeout(Some(cfg.io_timeout()))
146+
.with_concurrency_limit(Some(concurrency_limit))
147+
.with_certificate(cert.as_slice())
148+
.build_url(url)
149+
.map_err(|error| {
150+
anyhow::anyhow!(
151+
"Failed to create openapi configuration, Error: '{:?}'",
152+
error
153+
)
154+
})?
155+
}
156+
("https", None) => {
157+
anyhow::bail!("HTTPS endpoint requires a CA certificate path");
158+
}
159+
(_, Some(_path)) => {
160+
anyhow::bail!("CA certificate path is only supported for HTTPS endpoints");
161+
}
162+
_ => clients::tower::Configuration::builder()
163+
.with_timeout(Some(cfg.io_timeout()))
164+
.with_concurrency_limit(Some(concurrency_limit))
165+
.build_url(url)
166+
.map_err(|error| {
167+
anyhow::anyhow!(
168+
"Failed to create openapi configuration, Error: '{:?}'",
169+
error
170+
)
171+
})?,
172+
};
136173

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

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
use anyhow::Context;
22
use clap::ArgMatches;
33
use once_cell::sync::OnceCell;
4-
use std::{collections::HashMap, time::Duration};
4+
use std::{
5+
collections::HashMap,
6+
path::{Path, PathBuf},
7+
time::Duration,
8+
};
59

610
static CONFIG: OnceCell<CsiControllerConfig> = OnceCell::new();
711

@@ -17,6 +21,8 @@ pub(crate) struct CsiControllerConfig {
1721
create_volume_limit: usize,
1822
/// Force unstage volume.
1923
force_unstage_volume: bool,
24+
/// Path to the CA certificate file.
25+
ca_certificate_path: Option<PathBuf>,
2026
}
2127

2228
impl CsiControllerConfig {
@@ -50,14 +56,17 @@ impl CsiControllerConfig {
5056
tracing::warn!(
5157
"Force unstage volume is disabled, can trigger potential data corruption!"
5258
);
53-
}
59+
};
60+
61+
let ca_certificate_path: Option<&PathBuf> = args.get_one::<PathBuf>("tls-client-ca-path");
5462

5563
CONFIG.get_or_init(|| Self {
5664
rest_endpoint: rest_endpoint.into(),
5765
io_timeout: io_timeout.into(),
5866
node_selector,
5967
create_volume_limit,
6068
force_unstage_volume,
69+
ca_certificate_path: ca_certificate_path.cloned(),
6170
});
6271
Ok(())
6372
}
@@ -92,4 +101,9 @@ impl CsiControllerConfig {
92101
pub(crate) fn force_unstage_volume(&self) -> bool {
93102
self.force_unstage_volume
94103
}
104+
105+
/// Path to the CA certificate file.
106+
pub(crate) fn ca_certificate_path(&self) -> Option<&Path> {
107+
self.ca_certificate_path.as_deref()
108+
}
95109
}

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: 50 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::{debug, error, info, trace, warn};
3535
use utils::tracing_telemetry::{FmtLayer, FmtStyle};
3636

3737
const PAGINATION_LIMIT: u32 = 100;
@@ -129,14 +129,51 @@ 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
132+
let ca_certificate_path: Option<&str> = args
133+
.get_one::<String>("tls-client-ca-path")
134+
.map(|x| x.as_str());
135+
// take in cert path and make pem file
136+
let cert = match ca_certificate_path {
137+
Some(path) => {
138+
let cert = std::fs::read(path).expect("Failed to read certificate file");
139+
Some(cert)
140+
}
141+
None => None,
142+
};
143+
let cfg = match (url.scheme(), cert) {
144+
("https", Some(cert)) => {
145+
debug!("Attempting TLS connection to {}", url);
146+
147+
clients::tower::Configuration::new(
148+
url,
149+
timeout,
150+
None,
151+
Some(cert.as_slice()),
152+
true,
153+
None,
137154
)
138-
},
139-
)?;
155+
.map_err(|error| {
156+
anyhow::anyhow!(
157+
"Failed to create openapi configuration, Error: '{:?}'",
158+
error
159+
)
160+
})?
161+
}
162+
("https", None) => {
163+
anyhow::bail!("HTTPS endpoint requires a CA certificate path");
164+
}
165+
(_, Some(_path)) => {
166+
anyhow::bail!("CA certificate path is only supported for HTTPS endpoints");
167+
}
168+
_ => clients::tower::Configuration::new(url, timeout, None, None, true, None).map_err(
169+
|error| {
170+
anyhow::anyhow!(
171+
"Failed to create openapi configuration, Error: '{:?}'",
172+
error
173+
)
174+
},
175+
)?,
176+
};
140177
let interval = args
141178
.get_one::<String>("interval")
142179
.unwrap()
@@ -243,6 +280,11 @@ async fn main() -> anyhow::Result<()> {
243280
.value_parser(clap::value_parser!(bool))
244281
.help("Enable ansi color for logs"),
245282
)
283+
.arg(
284+
Arg::new("tls-client-ca-path")
285+
.long("tls-client-ca-path")
286+
.help("path to the CA certificate file"),
287+
)
246288
.get_matches();
247289

248290
utils::print_package_info!();

0 commit comments

Comments
 (0)