From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) by lore.proxmox.com (Postfix) with ESMTPS id 581271FF13F for ; Thu, 07 May 2026 14:43:14 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id E00E419BE8; Thu, 7 May 2026 14:41:08 +0200 (CEST) From: Stefan Hanreich To: pve-devel@lists.proxmox.com Subject: [PATCH proxmox-ve-rs v4 10/31] ve-config: wireguard: add private keys section config Date: Thu, 7 May 2026 14:39:45 +0200 Message-ID: <20260507124008.417223-11-s.hanreich@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260507124008.417223-1-s.hanreich@proxmox.com> References: <20260507124008.417223-1-s.hanreich@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1778157509250 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.641 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: 5QSDS2NAAFBS4QLKRFQGHPVBHJLCBEWB X-Message-ID-Hash: 5QSDS2NAAFBS4QLKRFQGHPVBHJLCBEWB X-MailFrom: s.hanreich@proxmox.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox VE development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: This section configuration file acts as the key storage for all nodes in all wireguard fabrics. This is possible, because interface names are required to be unique for a node across all fabrics (since they will be created with the respective name). There is also a helper struct, that can be used for further parsing the section config format into a more structured version. This struct can be used for performing CRUD operations, and will be exposed to perl via pve-rs. Signed-off-by: Stefan Hanreich --- .../section_config/protocol/wireguard.rs | 224 ++++++++++++++++++ 1 file changed, 224 insertions(+) diff --git a/proxmox-ve-config/src/sdn/fabric/section_config/protocol/wireguard.rs b/proxmox-ve-config/src/sdn/fabric/section_config/protocol/wireguard.rs index 8ad24c4..e885ef8 100644 --- a/proxmox-ve-config/src/sdn/fabric/section_config/protocol/wireguard.rs +++ b/proxmox-ve-config/src/sdn/fabric/section_config/protocol/wireguard.rs @@ -499,3 +499,227 @@ pub struct WireGuardInterfaceCreateProperties { #[serde(skip_serializing_if = "Option::is_none")] pub(crate) ip6_ll: Option, } + +pub mod private_keys { + use std::collections::btree_map::Entry; + use std::collections::{BTreeMap, HashMap, HashSet}; + + use anyhow::Error; + use serde::{Deserialize, Serialize}; + + use proxmox_schema::{api, ApiStringFormat, PropertyString}; + use proxmox_section_config::typed::SectionConfigData; + use proxmox_wireguard::{PrivateKey, PublicKey}; + + use crate::sdn::fabric::section_config::{ + node::{Node, NodeId, NODE_ID_FORMAT}, + protocol::wireguard::{WireGuardInterfaceName, WireGuardNode}, + }; + use crate::sdn::fabric::FabricConfig; + + #[api()] + #[derive(Clone, Debug, Serialize, Deserialize, Hash)] + /// A private key for a wireguard interface + pub struct InterfacePrivateKey { + name: WireGuardInterfaceName, + key: PrivateKey, + } + + impl InterfacePrivateKey { + pub fn new(name: WireGuardInterfaceName, key: PrivateKey) -> Self { + Self { name, key } + } + } + + #[api( + properties: { + private_keys: { + type: Array, + description: "A list of private keys for this node.", + items: { + type: String, + description: "A private key for a wireguard interface.", + format: &ApiStringFormat::PropertyString(&InterfacePrivateKey::API_SCHEMA), + } + } + } + )] + #[derive(Clone, Debug, Serialize, Deserialize, Hash)] + /// The private keys for a node in a wireguard fabric. + pub struct NodePrivateKeysSection { + private_keys: Vec>, + } + + impl FromIterator for NodePrivateKeysSection { + fn from_iter>(iter: T) -> Self { + Self { + private_keys: iter.into_iter().map(PropertyString::new).collect(), + } + } + } + + #[api( + "id-property": "id", + "id-schema": { + type: String, + description: "Route Map Section ID", + format: &NODE_ID_FORMAT, + }, + "type-key": "type", + )] + #[derive(Clone, Debug, Serialize, Deserialize, Hash)] + /// The private key config for wireguard. + #[serde(tag = "type", rename_all = "kebab-case")] + pub enum FabricPrivateKeysSectionConfig { + /// Private keys for a node. + Node(NodePrivateKeysSection), + } + + impl From for FabricPrivateKeysSectionConfig { + fn from(value: NodePrivateKeysSection) -> Self { + Self::Node(value) + } + } + + #[derive(Clone, Debug, Serialize, Deserialize, Hash)] + pub struct WireGuardPrivateKeys( + pub(crate) BTreeMap>, + ); + + impl WireGuardPrivateKeys { + /// Creates a Private key for the given (node, interface) if it doesn't exist - then + /// returns the public key of the stored private key. + pub fn upsert( + &mut self, + node: NodeId, + interface: WireGuardInterfaceName, + ) -> Result { + Ok(match self.0.entry(node).or_default().entry(interface) { + Entry::Vacant(vacant_entry) => { + let private_key = PrivateKey::generate()?; + let public_key = private_key.public_key(); + + vacant_entry.insert(private_key); + public_key + } + Entry::Occupied(occupied_entry) => occupied_entry.get().public_key(), + }) + } + + /// Removes a private key. + pub fn remove( + &mut self, + node: &NodeId, + interface: &WireGuardInterfaceName, + ) -> Option { + self.0.get_mut(node)?.remove(interface) + } + + /// Return a private key. + pub fn get( + &self, + node: &NodeId, + interface: &WireGuardInterfaceName, + ) -> Option<&PrivateKey> { + self.0.get(node)?.get(interface) + } + + /// Removes all entries in the private key configuration that do not exist in the given [`FabricConfig`]. + pub fn cleanup(&mut self, fabric_config: &FabricConfig) -> Result<(), Error> { + let mut private_keys_nodes = HashSet::new(); + let mut private_keys_interfaces = HashSet::new(); + + let mut fabric_config_nodes = HashSet::new(); + let mut fabric_config_interfaces = HashSet::new(); + + for (node_id, node) in fabric_config.all_nodes() { + let Node::WireGuard(node) = node else { + continue; + }; + + let WireGuardNode::Internal(node) = node.properties() else { + continue; + }; + + fabric_config_nodes.insert(node_id.clone()); + + fabric_config_interfaces.extend( + node.interfaces() + .map(|interface| (node_id.clone(), interface.name().clone())), + ); + } + + for (node_id, interfaces) in &self.0 { + private_keys_nodes.insert(node_id.clone()); + + private_keys_interfaces.extend( + interfaces + .keys() + .map(|interface_name| (node_id.clone(), interface_name.clone())), + ); + } + + for node_id in private_keys_nodes.difference(&fabric_config_nodes) { + self.0.remove(node_id); + } + + for (node_id, interface_id) in + private_keys_interfaces.difference(&fabric_config_interfaces) + { + self.remove(node_id, interface_id); + } + + Ok(()) + } + } + + impl From for SectionConfigData { + fn from(value: WireGuardPrivateKeys) -> Self { + let mut data = HashMap::new(); + + for (node_id, interfaces) in value.0.into_iter() { + data.insert( + node_id.to_string(), + NodePrivateKeysSection::from_iter( + interfaces + .into_iter() + .map(|(name, key)| InterfacePrivateKey::new(name, key)), + ) + .into(), + ); + } + + Self::from(data) + } + } + + impl TryFrom> for WireGuardPrivateKeys { + type Error = anyhow::Error; + + fn try_from( + value: SectionConfigData, + ) -> Result { + let mut data = BTreeMap::new(); + + for (section_id, FabricPrivateKeysSectionConfig::Node(node)) in value { + let node_id = NodeId::from_string(section_id)?; + + let interfaces: &mut BTreeMap = + data.entry(node_id.clone()).or_default(); + + for interface in node.private_keys { + let interface = interface.into_inner(); + + if interfaces + .insert(interface.name.clone(), interface.key) + .is_some() + { + anyhow::bail!("duplicate interface {} for node {node_id}", interface.name); + } + } + } + + Ok(Self(data)) + } + } +} -- 2.47.3