Skip to content

Commit

Permalink
feature: shared SslConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
leon3s committed Jan 31, 2024
1 parent 9a630d1 commit 87e1ff6
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 40 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion bin/nanocld/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ release = []
clap = { version = "4.4", features = ["derive"] }
clap_mangen = { version = "0.2" }
nanocl_utils = { version = "0.4", features = ["build_tools"] }
nanocl_stubs = { version = "0.13", features = ["clap"] }

[dev-dependencies]
serde_yaml = "0.9"
Expand Down Expand Up @@ -79,7 +80,7 @@ chrono = { version = "0.4", default-features = false, features = [
jsonschema = { version = "0.17", default-features = false }
nanocld_client = { version = "0.13", features = ["tokio"] }
metrsd_client = "0.5"
nanocl_stubs = { version = "0.13", features = ["serde"] }
nanocl_stubs = { version = "0.13", features = ["serde", "clap"] }
nanocl_utils = { version = "0.4", features = ["unix", "ntex", "logger"] }
utoipa = { version = "4.2", features = ["yaml"], optional = true }
notify = "6.1"
Expand Down
14 changes: 3 additions & 11 deletions bin/nanocld/specs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2854,17 +2854,9 @@ components:
format: int32
description: Group id
minimum: 0
cert:
type: string
description: Certificate path
nullable: true
cert_key:
type: string
description: Certificate key path
nullable: true
cert_ca:
type: string
description: Ca certificate path
ssl:
allOf:
- $ref: '#/components/schemas/SslConfig'
nullable: true
DeviceMapping:
type: object
Expand Down
18 changes: 6 additions & 12 deletions bin/nanocld/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use clap::Parser;

use nanocl_stubs::system::SslConfig;

/// Nanocl Daemon - Self Sufficient Orchestrator
#[derive(Debug, Clone, Parser)]
#[command(name = "Nanocl")]
Expand Down Expand Up @@ -34,15 +36,9 @@ pub struct Cli {
/// Group id
#[clap(long, default_value = "0")]
pub gid: u32,
/// Optional certificate path
#[clap(long)]
pub cert: Option<String>,
/// Optional certificate key path
#[clap(long)]
pub cert_key: Option<String>,
/// Optional ca certificate path
#[clap(long)]
pub cert_ca: Option<String>,
/// Optional ssl options
#[clap(flatten)]
pub ssl: Option<SslConfig>,
}

impl Default for Cli {
Expand All @@ -57,9 +53,7 @@ impl Default for Cli {
nodes: vec![],
advertise_addr: None,
gid: 0,
cert: None,
cert_key: None,
cert_ca: None,
ssl: None,
}
}
}
Expand Down
4 changes: 1 addition & 3 deletions bin/nanocld/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ fn gen_daemon_conf(
advertise_addr,
nodes: args.nodes.clone(),
conf_dir: args.conf_dir.clone(),
cert: args.cert.clone(),
cert_key: args.cert_key.clone(),
cert_ca: args.cert_ca.clone(),
ssl: args.ssl.clone(),
})
}

Expand Down
10 changes: 6 additions & 4 deletions bin/nanocld/src/utils/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,11 @@ pub async fn gen(
};
} else if host.starts_with("tcp://") {
let addr = host.replace("tcp://", "");
if let Some(cert) = config.cert.clone() {
if let Some(ssl) = config.ssl.clone() {
log::debug!("server::gen: {addr}: with ssl");
let cert_key = config.cert_key.clone().unwrap();
let cert_ca = config.cert_ca.clone().unwrap();
let cert = ssl.cert.clone().unwrap();
let cert_key = ssl.cert_key.clone().unwrap();
let cert_ca = ssl.cert_ca.clone().unwrap();
server = match server.bind_openssl(&addr, {
let mut builder =
SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap();
Expand Down Expand Up @@ -207,7 +208,8 @@ mod tests {
builder
.set_certificate_file("../../tests/client.crt", SslFiletype::PEM)
.unwrap();
builder.set_private_key_file("../../tests/client.key", SslFiletype::PEM)
builder
.set_private_key_file("../../tests/client.key", SslFiletype::PEM)
.unwrap();
let client = ntex::http::client::Client::build()
.connector(Connector::default().openssl(builder.build()).finish())
Expand Down
2 changes: 2 additions & 0 deletions crates/nanocl_stubs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ default = ["serde"]
serde = ["dep:serde", "uuid/serde", "chrono/serde"]
utoipa = ["dep:utoipa"]
schemars = ["dep:schemars", "bollard-next/schemars"]
clap = ["dep:clap"]
test = []

[dependencies]
Expand All @@ -30,3 +31,4 @@ chrono = { version = "0.4", default-features = false, features = [
serde = { version = "1.0", features = ["derive"], optional = true }
utoipa = { version = "4", features = ["uuid", "chrono"], optional = true }
schemars = { version = "0.8", features = ["uuid1", "chrono"], optional = true }
clap = { version = "4.4", features = ["derive", "cargo"], optional = true }
14 changes: 5 additions & 9 deletions crates/nanocl_stubs/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#[cfg(feature = "serde")]
use serde::{Serialize, Deserialize};

use super::system::SslConfig;

/// Configuration of the daemon
/// It is used to configure the daemon
#[derive(Debug, Clone)]
Expand All @@ -27,12 +29,8 @@ pub struct DaemonConfig {
pub conf_dir: String,
/// Group id
pub gid: u32,
/// Certificate path
pub cert: Option<String>,
/// Certificate key path
pub cert_key: Option<String>,
/// Ca certificate path
pub cert_ca: Option<String>,
/// Optional ssl configuration
pub ssl: Option<SslConfig>,
}

/// Configuration File of the daemon
Expand Down Expand Up @@ -64,9 +62,7 @@ impl Default for DaemonConfig {
gateway: String::default(),
nodes: Vec::default(),
advertise_addr: String::default(),
cert: None,
cert_key: None,
cert_ca: None,
ssl: None,
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions crates/nanocl_stubs/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@ use serde::{Serialize, Deserialize};

use crate::config::DaemonConfig;

#[derive(Clone, Debug, Default, Eq, PartialEq)]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]

Check warning on line 13 in crates/nanocl_stubs/src/system.rs

View check run for this annotation

Codecov / codecov/patch

crates/nanocl_stubs/src/system.rs#L13

Added line #L13 was not covered by tests
#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))]
#[cfg_attr(feature = "clap", derive(clap::Parser))]

Check warning on line 15 in crates/nanocl_stubs/src/system.rs

View check run for this annotation

Codecov / codecov/patch

crates/nanocl_stubs/src/system.rs#L15

Added line #L15 was not covered by tests
pub struct SslConfig {
#[cfg_attr(feature = "clap", clap(long))]
pub cert: Option<String>,
#[cfg_attr(feature = "clap", clap(long))]
pub cert_key: Option<String>,
#[cfg_attr(feature = "clap", clap(long))]
pub cert_ca: Option<String>,
}

#[derive(Clone, Debug, Default, Eq, PartialEq)]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
Expand Down

0 comments on commit 87e1ff6

Please sign in to comment.