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 authored and mbfm committed Apr 29, 2024
1 parent 2ef16cd commit e1e3dba
Show file tree
Hide file tree
Showing 23 changed files with 252 additions and 75 deletions.
24 changes: 19 additions & 5 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 Down Expand Up @@ -78,9 +78,19 @@ 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 = match peer_descriptor.clone().network.bridge_name {
Some(bridge_name) => {
PeerNetworkConfiguration {
bridge_name,
}
}
None => { PeerNetworkConfiguration::default() }
};

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 +371,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 Down Expand Up @@ -453,6 +463,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 +540,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
12 changes: 7 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 Down Expand Up @@ -449,11 +449,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 +540,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
5 changes: 3 additions & 2 deletions opendut-carl/src/grpc/peer_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,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 Down Expand Up @@ -276,13 +276,14 @@ mod tests {
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
7 changes: 4 additions & 3 deletions opendut-carl/src/resources/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ mod test {
use googletest::prelude::*;

use opendut_types::cluster::{ClusterConfiguration, ClusterId, 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::Topology;
use opendut_types::util::net::{NetworkInterfaceConfiguration, NetworkInterfaceDescriptor, NetworkInterfaceName};
Expand All @@ -97,13 +97,14 @@ mod test {
id: peer_resource_id,
name: PeerName::try_from("TestPeer").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::default(),
executors: ExecutorDescriptors {
Expand Down
6 changes: 3 additions & 3 deletions opendut-cleo/src/commands/device/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl CreateDeviceCli {

let mut peer_descriptor = carl.peers.get_peer_descriptor(peer_id).await
.map_err(|_| format!("Failed to get peer with ID <{}>.", peer_id))?;
let peer_network_interface_names = peer_descriptor.network_configuration.interfaces.iter().map(|peer_interface| {
let peer_network_interface_names = peer_descriptor.network.interfaces.iter().map(|peer_interface| {
peer_interface.name.clone()
}).collect::<Vec<_>>();
let maybe_existing_device = peer_descriptor.topology.devices.iter_mut().find(|device| device.id == device_id);
Expand All @@ -46,7 +46,7 @@ impl CreateDeviceCli {
let name = self.name.ok_or(String::from("Cannot create new device because of missing device name."))?;
let interface_name = self.interface.ok_or(String::from("Cannot create new device because of missing interface name."))?;

let interface = match peer_descriptor.network_configuration.interfaces.iter().find(|descriptor| descriptor.name == interface_name) {
let interface = match peer_descriptor.network.interfaces.iter().find(|descriptor| descriptor.name == interface_name) {
Some(network_interface_descriptor) => network_interface_descriptor.clone(),
None => {
Err(format!("Cannot create new device because interface is not one of the allowed values: {} \nAllowed interfaces are configured on the peer.",
Expand Down Expand Up @@ -83,7 +83,7 @@ impl CreateDeviceCli {
.ok();
}
if let Some(interface_name) = self.interface {
device.interface = match peer_descriptor.network_configuration.interfaces.iter().find(|descriptor| descriptor.name == interface_name) {
device.interface = match peer_descriptor.network.interfaces.iter().find(|descriptor| descriptor.name == interface_name) {
Some(network_interface_descriptor) => network_interface_descriptor.clone(),
None => {
Err(format!("Cannot create new device because interface is not one of the allowed values: {} \nAllowed interfaces are configured on the peer.",
Expand Down
4 changes: 2 additions & 2 deletions opendut-cleo/src/commands/network_interface/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl CreateNetworkInterfaceCli {
let mut peer_descriptor = carl.peers.get_peer_descriptor(peer_id).await
.map_err(|_| format!("Failed to get peer with ID <{}>.", peer_id))?;

let peer_interface_names = peer_descriptor.network_configuration.interfaces
let peer_interface_names = peer_descriptor.network.interfaces
.iter().map(|interface| interface.name.clone()).collect::<Vec<_>>();

let interface_name = NetworkInterfaceName::try_from(self.interface_name).map_err(|error| error.to_string())?;
Expand All @@ -46,7 +46,7 @@ impl CreateNetworkInterfaceCli {
if peer_interface_names.contains(&interface_name) {
Err(format!("Could not create peer network configuration with name '{}' because it already exists", &interface_name))?
} else {
peer_descriptor.network_configuration.interfaces.push(
peer_descriptor.network.interfaces.push(
NetworkInterfaceDescriptor {
name: interface_name,
configuration: interface_configuration,
Expand Down
2 changes: 1 addition & 1 deletion opendut-cleo/src/commands/network_interface/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl DeleteNetworkInterfaceCli {
Err(format!("Network interface '{}' could not be deleted due to it being used in following devices: {}", name,
device_interfaces_map.get(&name).unwrap().join(", ")))?
}
peer.network_configuration.interfaces.retain(|interface| interface.name.name() != name.name())
peer.network.interfaces.retain(|interface| interface.name.name() != name.name())
};

carl.peers.store_peer_descriptor(peer).await
Expand Down
15 changes: 13 additions & 2 deletions opendut-cleo/src/commands/peer/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ use uuid::Uuid;

use crate::{CreateOutputFormat};
use opendut_carl_api::carl::CarlClient;
use opendut_types::peer::{PeerDescriptor, PeerId, PeerLocation, PeerName};
use opendut_types::peer::{PeerDescriptor, PeerId, PeerLocation, PeerName, PeerNetworkDescriptor};
use opendut_types::peer::executor::{ExecutorDescriptors};
use opendut_types::util::net::NetworkInterfaceName;

/// Create a peer
#[derive(clap::Parser)]
Expand All @@ -18,6 +19,11 @@ pub struct CreatePeerCli {
///Location of peer
#[arg(long)]
location: Option<String>,
///Custom bridge name;
/// Please note bridges with custom names are not automatically removed and need to be removed manually.
/// Not removing the bridge could lead to network traffic being misdirected!
#[arg(long)]
bridge_name: Option<NetworkInterfaceName>,
}

impl CreatePeerCli {
Expand All @@ -32,11 +38,16 @@ impl CreatePeerCli {
.transpose()
.map_err(|error| format!("Could not create peer.\n {}", error))?;

let bridge_name = self.bridge_name;

let descriptor: PeerDescriptor = PeerDescriptor {
id,
name: Clone::clone(&name),
location,
network_configuration: Default::default(),
network: PeerNetworkDescriptor {
interfaces: vec![],
bridge_name,
},
topology: Default::default(),
executors: ExecutorDescriptors {
executors: vec![],
Expand Down
22 changes: 15 additions & 7 deletions opendut-cleo/src/commands/peer/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ struct PeerTable {
status: PeerStatus,
#[table(title = "Location")]
location: PeerLocation,
#[table(title = "NetworkConfiguration")]
network_configuration: String
#[table(title = "NetworkInterfaces")]
network_interfaces: String,
#[table(title = "BridgeName")]
bridge_name: String,
}

#[derive(Debug, PartialEq, Serialize)]
Expand Down Expand Up @@ -85,13 +87,18 @@ fn filter_connected_peers(
} else {
PeerStatus::Disconnected
};
let network_interfaces = Clone::clone(&peer.network_configuration.interfaces);
let network_interfaces = Clone::clone(&peer.network.interfaces);
let interfaces = network_interfaces.into_iter().map(|interface| interface.name.to_string()).collect::<Vec<_>>();
let bridge_name = match Clone::clone(&peer.network.bridge_name) {
Some(bridge_name) => bridge_name.name(),
None => String::new()
};
PeerTable {
name: Clone::clone(&peer.name),
id: peer.id,
location: Clone::clone(&peer.location.clone().unwrap_or_default()),
network_configuration: interfaces.join(", "),
network_interfaces: interfaces.join(", "),
bridge_name,
status
}
})
Expand All @@ -102,7 +109,7 @@ fn filter_connected_peers(
mod test {
use googletest::prelude::*;

use opendut_types::peer::{PeerDescriptor, PeerId, PeerLocation, PeerName, PeerNetworkConfiguration};
use opendut_types::peer::{PeerDescriptor, PeerId, PeerLocation, PeerName, PeerNetworkDescriptor};
use opendut_types::peer::executor::ExecutorDescriptors;
use opendut_types::util::net::{NetworkInterfaceConfiguration, NetworkInterfaceDescriptor, NetworkInterfaceName};

Expand All @@ -114,11 +121,12 @@ mod test {
id: PeerId::random(),
name: PeerName::try_from("MyPeer").unwrap(),
location: Some(PeerLocation::try_from("SiFi").unwrap()),
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: Default::default(),
executors: ExecutorDescriptors {
Expand Down
17 changes: 8 additions & 9 deletions opendut-edgar/src/service/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,10 @@ pub async fn create(self_id: PeerId, settings: LoadedConfig) -> anyhow::Result<(

let network_interface_management_enabled = settings.config.get::<bool>("network.interface.management.enabled")?;

let bridge_name = crate::common::default_bridge_name();

let remote_address = vpn::retrieve_remote_host(&settings).await?;

let setup_cluster_info = SetupClusterInfo {
self_id,
bridge_name,
network_interface_management_enabled,
network_interface_manager,
can_manager,
Expand Down Expand Up @@ -159,7 +156,7 @@ async fn handle_stream_message(
tx_outbound.send(message).await
.inspect_err(|cause| debug!("Failed to send ping to CARL: {cause}"));
}
Message::ApplyPeerConfiguration(message) => { apply_peer_configuration(message, context, setup_cluster_info).await }
Message::ApplyPeerConfiguration(message) => { apply_peer_configuration(message, context, setup_cluster_info).await? }
}
} else {
ignore(message)
Expand All @@ -169,7 +166,7 @@ async fn handle_stream_message(
}

#[tracing::instrument(skip(message, context, setup_cluster_info), level="trace")]
async fn apply_peer_configuration(message: ApplyPeerConfiguration, context: Option<TracingContext>, setup_cluster_info: &SetupClusterInfo) {
async fn apply_peer_configuration(message: ApplyPeerConfiguration, context: Option<TracingContext>, setup_cluster_info: &SetupClusterInfo) -> anyhow::Result<()> {
match message.clone() {
ApplyPeerConfiguration { configuration: Some(configuration) } => {

Expand All @@ -182,15 +179,17 @@ async fn apply_peer_configuration(message: ApplyPeerConfiguration, context: Opti
Err(error) => error!("Illegal PeerConfiguration: {error}"),
Ok(configuration) => {
setup_executors(configuration.executors);
let _ = setup_cluster(
setup_cluster(
configuration.cluster_assignment,
setup_cluster_info,
).await;
configuration.network.bridge_name,
).await?
}
};
}
_ => ignore(message),
}
Ok(())
}

#[tracing::instrument(skip(executors))]
Expand Down Expand Up @@ -250,7 +249,6 @@ fn setup_executors(executors: ExecutorDescriptors) { //TODO make idempotent

struct SetupClusterInfo {
self_id: PeerId,
bridge_name: NetworkInterfaceName,
network_interface_management_enabled: bool,
network_interface_manager: NetworkInterfaceManagerRef,
can_manager: CanManagerRef,
Expand All @@ -259,6 +257,7 @@ struct SetupClusterInfo {
async fn setup_cluster(
cluster_assignment: Option<ClusterAssignment>,
info: &SetupClusterInfo,
bridge_name: NetworkInterfaceName,
) -> anyhow::Result<()> { //TODO make idempotent

match cluster_assignment {
Expand All @@ -270,7 +269,7 @@ async fn setup_cluster(
cluster_assignment::network_interfaces_setup(
cluster_assignment,
info.self_id,
&info.bridge_name,
&bridge_name,
Arc::clone(&info.network_interface_manager),
Arc::clone(&info.can_manager)
).await
Expand Down
Loading

0 comments on commit e1e3dba

Please sign in to comment.