Skip to content

Commit

Permalink
EDGAR support using custom bridge name in managed mode
Browse files Browse the repository at this point in the history
  • Loading branch information
voelkera committed Apr 30, 2024
1 parent 6430a7b commit e152b7f
Show file tree
Hide file tree
Showing 28 changed files with 328 additions and 97 deletions.
1 change: 1 addition & 0 deletions opendut-carl/carl.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ scopes = "openid,profile,email,roles,groups"
[peer]
disconnect.timeout.ms = 30000
can.server_port_range_start = 10000
ethernet.bridge.name.default = "br-opendut"

[serve]
ui.directory = "opendut-lea/"
Expand Down
1 change: 1 addition & 0 deletions opendut-carl/src/actions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub use clusters::{
pub use peers::{
store_peer_descriptor,
StorePeerDescriptorParams,
StorePeerDescriptorOptions,
StorePeerDescriptorError,
};

Expand Down
40 changes: 34 additions & 6 deletions opendut-carl/src/actions/peers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ pub use opendut_carl_api::carl::peer::{
use opendut_carl_api::proto::services::peer_messaging_broker::{ApplyPeerConfiguration, downstream};
use opendut_types::cluster::ClusterAssignment;
use opendut_types::peer::{PeerDescriptor, PeerId, PeerName, PeerSetup};
use opendut_types::peer::configuration::PeerConfiguration;
use opendut_types::peer::configuration::{PeerConfiguration, PeerNetworkConfiguration};
use opendut_types::proto;
use opendut_types::topology::{DeviceDescriptor, DeviceId};
use opendut_types::util::net::{AuthConfig, Certificate, ClientCredentials};
use opendut_types::util::net::{AuthConfig, Certificate, ClientCredentials, NetworkInterfaceName};
use opendut_types::vpn::VpnPeerConfiguration;
use opendut_util::ErrorOr;
use crate::peer::broker::{PeerMessagingBroker, PeerMessagingBrokerRef};
Expand All @@ -31,6 +31,12 @@ pub struct StorePeerDescriptorParams {
pub resources_manager: ResourcesManagerRef,
pub vpn: Vpn,
pub peer_descriptor: PeerDescriptor,
pub options: StorePeerDescriptorOptions
}

#[derive(Clone)]
pub struct StorePeerDescriptorOptions {
pub bridge_name_default: NetworkInterfaceName
}

#[tracing::instrument(skip(params), level="trace")]
Expand Down Expand Up @@ -78,9 +84,18 @@ pub async fn store_peer_descriptor(params: StorePeerDescriptorParams) -> Result<
info!("Added device '{device_name}' <{device_id}> of peer '{peer_name}' <{peer_id}>.");
});

let peer_network_configuration = {
let bridge_name = peer_descriptor.clone().network.bridge_name
.unwrap_or_else(|| params.options.bridge_name_default);
PeerNetworkConfiguration {
bridge_name,
}
};

let peer_configuration = PeerConfiguration {
executors: Clone::clone(&peer_descriptor.executors),
cluster_assignment: None,
network: peer_network_configuration
};
resources.insert(peer_id, peer_configuration);

Expand Down Expand Up @@ -361,7 +376,7 @@ mod test {
use googletest::prelude::*;
use rstest::*;

use opendut_types::peer::{PeerLocation, PeerName, PeerNetworkConfiguration};
use opendut_types::peer::{PeerLocation, PeerName, PeerNetworkDescriptor};
use opendut_types::peer::executor::ExecutorDescriptors;
use opendut_types::topology::{DeviceDescription, DeviceName, Topology};
use opendut_types::util::net::{NetworkInterfaceConfiguration, NetworkInterfaceDescriptor, NetworkInterfaceName};
Expand All @@ -378,14 +393,15 @@ mod test {

#[rstest]
#[tokio::test]
async fn should_update_expected_resources(fixture: Fixture) -> anyhow::Result<()> {
async fn should_update_expected_resources(fixture: Fixture, store_peer_descriptor_options: StorePeerDescriptorOptions) -> anyhow::Result<()> {

let resources_manager = fixture.resources_manager;

store_peer_descriptor(StorePeerDescriptorParams {
resources_manager: Arc::clone(&resources_manager),
vpn: Clone::clone(&fixture.vpn),
peer_descriptor: Clone::clone(&fixture.peer_a_descriptor),
options: store_peer_descriptor_options.clone(),
}).await?;

assert_that!(resources_manager.get::<PeerDescriptor>(fixture.peer_a_id).await.as_ref(), some(eq(&fixture.peer_a_descriptor)));
Expand Down Expand Up @@ -418,6 +434,7 @@ mod test {
resources_manager: Arc::clone(&resources_manager),
vpn: Clone::clone(&fixture.vpn),
peer_descriptor: Clone::clone(&changed_descriptor),
options: store_peer_descriptor_options,
}).await?;

assert_that!(resources_manager.get::<PeerDescriptor>(fixture.peer_a_id).await.as_ref(), some(eq(&changed_descriptor)));
Expand Down Expand Up @@ -453,6 +470,9 @@ mod test {
let peer_configuration = PeerConfiguration {
executors: ExecutorDescriptors { executors: vec![] },
cluster_assignment: None,
network: PeerNetworkConfiguration {
bridge_name: NetworkInterfaceName::try_from("br-opendut-1").unwrap()
}
};
resources_manager.resources_mut(|resources| {
resources.insert(peer_id, Clone::clone(&peer_configuration));
Expand Down Expand Up @@ -527,13 +547,14 @@ mod test {
id: peer_a_id,
name: PeerName::try_from("PeerA").unwrap(),
location: PeerLocation::try_from("Ulm").ok(),
network_configuration: PeerNetworkConfiguration {
network: PeerNetworkDescriptor {
interfaces: vec![
NetworkInterfaceDescriptor {
name: NetworkInterfaceName::try_from("eth0").unwrap(),
configuration: NetworkInterfaceConfiguration::Ethernet,
},
]
],
bridge_name: Some(NetworkInterfaceName::try_from("br-opendut-1").unwrap()),
},
topology: Topology {
devices: vec![
Expand Down Expand Up @@ -572,4 +593,11 @@ mod test {
peer_a_device_2,
}
}

#[fixture]
fn store_peer_descriptor_options() -> StorePeerDescriptorOptions {
StorePeerDescriptorOptions {
bridge_name_default: NetworkInterfaceName::try_from("br-opendut").unwrap(),
}
}
}
18 changes: 13 additions & 5 deletions opendut-carl/src/cluster/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ mod test {
use opendut_carl_api::proto::services::peer_messaging_broker::Downstream;
use opendut_carl_api::proto::services::peer_messaging_broker::downstream;
use opendut_types::cluster::ClusterName;
use opendut_types::peer::{PeerDescriptor, PeerId, PeerLocation, PeerName, PeerNetworkConfiguration};
use opendut_types::peer::{PeerDescriptor, PeerId, PeerLocation, PeerName, PeerNetworkDescriptor};
use opendut_types::peer::executor::{ContainerCommand, ContainerImage, ContainerName, Engine, ExecutorDescriptor, ExecutorDescriptors};
use opendut_types::topology::{DeviceDescription, DeviceDescriptor, DeviceId, DeviceName, Topology};
use opendut_types::util::net::{NetworkInterfaceConfiguration, NetworkInterfaceName};
Expand All @@ -300,6 +300,7 @@ mod test {
mod deploy_cluster {
use opendut_carl_api::proto::services::peer_messaging_broker::ApplyPeerConfiguration;
use opendut_types::peer::configuration::PeerConfiguration;
use crate::actions::StorePeerDescriptorOptions;

use super::*;

Expand All @@ -320,16 +321,21 @@ mod test {
devices: HashSet::from([peer_a.device, peer_b.device]),
};

let store_peer_descriptor_options = StorePeerDescriptorOptions {
bridge_name_default: NetworkInterfaceName::try_from("br-opendut").unwrap(),
};
actions::store_peer_descriptor(StorePeerDescriptorParams {
resources_manager: Arc::clone(&fixture.resources_manager),
vpn: Vpn::Disabled,
peer_descriptor: Clone::clone(&peer_a.descriptor),
options: store_peer_descriptor_options.clone(),
}).await?;

actions::store_peer_descriptor(StorePeerDescriptorParams {
resources_manager: Arc::clone(&fixture.resources_manager),
vpn: Vpn::Disabled,
peer_descriptor: Clone::clone(&peer_b.descriptor),
options: store_peer_descriptor_options,
}).await?;


Expand Down Expand Up @@ -449,11 +455,12 @@ mod test {
id,
name: PeerName::try_from(format!("peer-{id}")).unwrap(),
location: PeerLocation::try_from("Ulm").ok(),
network_configuration: PeerNetworkConfiguration {
network: PeerNetworkDescriptor {
interfaces: vec!(NetworkInterfaceDescriptor {
name: NetworkInterfaceName::try_from("eth0").unwrap(),
configuration: NetworkInterfaceConfiguration::Ethernet,
})
}),
bridge_name: Some(NetworkInterfaceName::try_from("br-custom").unwrap()),
},
topology: Topology {
devices,
Expand Down Expand Up @@ -539,13 +546,14 @@ mod test {
id,
name: PeerName::try_from(peer_name).unwrap(),
location: PeerLocation::try_from("Ulm").ok(),
network_configuration: PeerNetworkConfiguration {
network: PeerNetworkDescriptor {
interfaces: vec![
NetworkInterfaceDescriptor {
name: NetworkInterfaceName::try_from("eth0").unwrap(),
configuration: NetworkInterfaceConfiguration::Ethernet,
}
]
],
bridge_name: Some(NetworkInterfaceName::try_from("br-opendut-1").unwrap()),
},
topology: Topology {
devices: vec![
Expand Down
2 changes: 1 addition & 1 deletion opendut-carl/src/grpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::fmt::Display;

pub use cluster_manager::ClusterManagerFacade;
pub use metadata_provider::MetadataProviderFacade;
pub use peer_manager::PeerManagerFacade;
pub use peer_manager::{PeerManagerFacade, PeerManagerFacadeOptions};
pub use peer_messaging_broker::PeerMessagingBrokerFacade;

mod cluster_manager;
Expand Down
55 changes: 50 additions & 5 deletions opendut-carl/src/grpc/peer_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ use opendut_carl_api::proto::services::peer_manager;
use opendut_carl_api::proto::services::peer_manager::*;
use opendut_carl_api::proto::services::peer_manager::peer_manager_server::{PeerManager as PeerManagerService, PeerManagerServer};
use opendut_types::peer::{PeerDescriptor, PeerId};
use opendut_types::util::net::NetworkInterfaceName;

use crate::actions;
use crate::actions::{DeletePeerDescriptorParams, GeneratePeerSetupParams, ListDevicesParams, ListPeerDescriptorsParams, StorePeerDescriptorParams};
use crate::actions::{DeletePeerDescriptorParams, GeneratePeerSetupParams, ListDevicesParams, ListPeerDescriptorsParams, StorePeerDescriptorOptions, StorePeerDescriptorParams};
use crate::grpc::extract;
use crate::peer::oidc_client_manager::OpenIdConnectClientManager;
use crate::resources::manager::ResourcesManagerRef;
Expand All @@ -25,17 +26,26 @@ pub struct PeerManagerFacade {
carl_url: Url,
ca: Pem,
oidc_client_manager: Option<OpenIdConnectClientManager>,
options: PeerManagerFacadeOptions,
}

impl PeerManagerFacade {

pub fn new(resources_manager: ResourcesManagerRef, vpn: Vpn, carl_url: Url, ca: Pem, oidc_client_manager: Option<OpenIdConnectClientManager>) -> Self {
pub fn new(
resources_manager: ResourcesManagerRef,
vpn: Vpn,
carl_url: Url,
ca: Pem,
oidc_client_manager: Option<OpenIdConnectClientManager>,
options: PeerManagerFacadeOptions
) -> Self {
PeerManagerFacade {
resources_manager,
vpn,
carl_url,
ca,
oidc_client_manager,
options
}
}

Expand All @@ -59,6 +69,9 @@ impl PeerManagerService for PeerManagerFacade {
resources_manager: Arc::clone(&self.resources_manager),
vpn: Clone::clone(&self.vpn),
peer_descriptor: Clone::clone(&peer_descriptor),
options: StorePeerDescriptorOptions {
bridge_name_default: Clone::clone(&self.options.bridge_name_default),
}
}).await;

match result {
Expand Down Expand Up @@ -226,6 +239,30 @@ impl PeerManagerService for PeerManagerFacade {
}
}

#[derive(Clone)]
pub struct PeerManagerFacadeOptions {
pub bridge_name_default: NetworkInterfaceName,
}
impl PeerManagerFacadeOptions {
pub fn load(config: &config::Config) -> Result<Self, PeerManagerFacadeOptionsLoadError> {
let bridge_name_default = config.get_string("peer.ethernet.bridge.name.default")
.map_err(|cause| PeerManagerFacadeOptionsLoadError { message: cause.to_string() })?;

let bridge_name_default = NetworkInterfaceName::try_from(bridge_name_default)
.map_err(|cause| PeerManagerFacadeOptionsLoadError { message: cause.to_string() })?;

Ok(PeerManagerFacadeOptions {
bridge_name_default,
})
}
}

#[derive(thiserror::Error, Debug)]
#[error("Error while loading PeerManagerFacadeOptions: {message}")]
pub struct PeerManagerFacadeOptionsLoadError {
message: String,
}

#[cfg(test)]
mod tests {
use std::str::FromStr;
Expand All @@ -236,7 +273,7 @@ mod tests {
use crate::peer::oidc_client_manager::tests::oidc_client_manager;
use url::Url;

use opendut_types::peer::{PeerLocation, PeerName, PeerNetworkConfiguration};
use opendut_types::peer::{PeerLocation, PeerName, PeerNetworkDescriptor};
use opendut_types::peer::executor::{ContainerCommand, ContainerImage, ContainerName, Engine, ExecutorDescriptor, ExecutorDescriptors};
use opendut_types::proto;
use opendut_types::topology::Topology;
Expand All @@ -260,7 +297,7 @@ mod tests {
#[tokio::test]
async fn test_successful_create_delete(oidc_client_manager: OpenIdConnectClientManager) -> Result<()> {


let settings = crate::settings::load_defaults()?;

let resources_manager = ResourcesManager::new();
let testee = PeerManagerFacade::new(
Expand All @@ -269,20 +306,22 @@ mod tests {
Url::parse("https://example.com:1234").unwrap(),
get_cert(),
Some(oidc_client_manager),
PeerManagerFacadeOptions::load(&settings.config)?
);

let peer_id = PeerId::random();
let peer_descriptor = PeerDescriptor {
id: peer_id,
name: PeerName::try_from("TestPeer").unwrap(),
location: PeerLocation::try_from("SiFi").ok(),
network_configuration: PeerNetworkConfiguration {
network: PeerNetworkDescriptor {
interfaces: vec![
NetworkInterfaceDescriptor {
name: NetworkInterfaceName::try_from("eth0").unwrap(),
configuration: NetworkInterfaceConfiguration::Ethernet,
},
],
bridge_name: Some(NetworkInterfaceName::try_from("br-opendut-1").unwrap()),
},
topology: Topology::default(),
executors: ExecutorDescriptors {
Expand Down Expand Up @@ -354,13 +393,16 @@ mod tests {
#[tokio::test]
async fn register_fails_when_no_id_specified(oidc_client_manager: OpenIdConnectClientManager) -> Result<()> {

let settings = crate::settings::load_defaults()?;

let resources_manager = ResourcesManager::new();
let testee = PeerManagerFacade::new(
Arc::clone(&resources_manager),
Vpn::Disabled,
Url::parse("https://example.com:1234").unwrap(),
get_cert(),
Some(oidc_client_manager),
PeerManagerFacadeOptions::load(&settings.config)?
);

let create_peer_reply = testee.store_peer_descriptor(Request::new(
Expand Down Expand Up @@ -394,13 +436,16 @@ mod tests {
#[tokio::test]
async fn unregister_fails_when_no_id_specified(oidc_client_manager: OpenIdConnectClientManager) -> Result<()> {

let settings = crate::settings::load_defaults()?;

let resources_manager = ResourcesManager::new();
let testee = PeerManagerFacade::new(
Arc::clone(&resources_manager),
Vpn::Disabled,
Url::parse("https://example.com:1234").unwrap(),
get_cert(),
Some(oidc_client_manager),
PeerManagerFacadeOptions::load(&settings.config)?
);

let delete_peer_reply = testee.delete_peer_descriptor(Request::new(
Expand Down
Loading

0 comments on commit e152b7f

Please sign in to comment.