From 0c7c96dd074d1bde1b1c8f3855503365a154768a Mon Sep 17 00:00:00 2001 From: Gabi Date: Tue, 7 May 2024 18:14:29 -0300 Subject: [PATCH] chore(connlib): pass to client new fields (#4900) Fixes #4885 --- rust/connlib/clients/shared/src/messages.rs | 33 +++++++++++- rust/connlib/shared/src/messages.rs | 11 +++- rust/connlib/shared/src/messages/client.rs | 33 +++++++++++- rust/connlib/shared/src/proptest.rs | 57 ++++++++++++++++++--- rust/connlib/tunnel/src/client.rs | 2 + rust/connlib/tunnel/src/dns.rs | 12 +++-- 6 files changed, 135 insertions(+), 13 deletions(-) diff --git a/rust/connlib/clients/shared/src/messages.rs b/rust/connlib/clients/shared/src/messages.rs index a170711e2..f9f1339d4 100644 --- a/rust/connlib/clients/shared/src/messages.rs +++ b/rust/connlib/clients/shared/src/messages.rs @@ -101,8 +101,9 @@ mod test { use super::*; use chrono::DateTime; use connlib_shared::messages::{ - client::ResourceDescriptionCidr, client::ResourceDescriptionDns, DnsServer, IpDnsServer, - Stun, Turn, + client::ResourceDescriptionCidr, + client::{GatewayGroup, ResourceDescriptionDns}, + DnsServer, IpDnsServer, Stun, Turn, }; use phoenix_channel::{OutboundRequestId, PhoenixMessage}; @@ -232,11 +233,21 @@ mod test { id: "73037362-715d-4a83-a749-f18eadd970e6".parse().unwrap(), address: "172.172.0.0/16".parse().unwrap(), name: "172.172.0.0/16".to_string(), + address_description: "cidr resource".to_string(), + gateway_groups: vec![GatewayGroup { + name: "test".to_string(), + id: "bf56f32d-7b2c-4f5d-a784-788977d014a4".parse().unwrap(), + }], }), ResourceDescription::Dns(ResourceDescriptionDns { id: "03000143-e25e-45c7-aafb-144990e57dcd".parse().unwrap(), address: "gitlab.mycorp.com".to_string(), name: "gitlab.mycorp.com".to_string(), + address_description: "dns resource".to_string(), + gateway_groups: vec![GatewayGroup { + name: "test".to_string(), + id: "bf56f32d-7b2c-4f5d-a784-788977d014a4".parse().unwrap(), + }], }), ], relays: vec![], @@ -256,6 +267,8 @@ mod test { "address": "172.172.0.0/16", "id": "73037362-715d-4a83-a749-f18eadd970e6", "name": "172.172.0.0/16", + "address_description": "cidr resource", + "gateway_groups": [{"name": "test", "id": "bf56f32d-7b2c-4f5d-a784-788977d014a4"}], "type": "cidr" }, { @@ -264,6 +277,8 @@ mod test { "ipv4": "100.126.44.50", "ipv6": "fd00:2021:1111::e:7758", "name": "gitlab.mycorp.com", + "address_description": "dns resource", + "gateway_groups": [{"name": "test", "id": "bf56f32d-7b2c-4f5d-a784-788977d014a4"}], "type": "dns" } ] @@ -291,11 +306,21 @@ mod test { id: "73037362-715d-4a83-a749-f18eadd970e6".parse().unwrap(), address: "172.172.0.0/16".parse().unwrap(), name: "172.172.0.0/16".to_string(), + address_description: "cidr resource".to_string(), + gateway_groups: vec![GatewayGroup { + name: "test".to_string(), + id: "bf56f32d-7b2c-4f5d-a784-788977d014a4".parse().unwrap(), + }], }), ResourceDescription::Dns(ResourceDescriptionDns { id: "03000143-e25e-45c7-aafb-144990e57dcd".parse().unwrap(), address: "gitlab.mycorp.com".to_string(), name: "gitlab.mycorp.com".to_string(), + address_description: "dns resource".to_string(), + gateway_groups: vec![GatewayGroup { + name: "test".to_string(), + id: "bf56f32d-7b2c-4f5d-a784-788977d014a4".parse().unwrap(), + }], }), ], relays: vec![], @@ -317,6 +342,8 @@ mod test { "id": "73037362-715d-4a83-a749-f18eadd970e6", "name": "172.172.0.0/16", "type": "cidr", + "address_description": "cidr resource", + "gateway_groups": [{"name": "test", "id": "bf56f32d-7b2c-4f5d-a784-788977d014a4"}], "not": "relevant" }, { @@ -326,6 +353,8 @@ mod test { "ipv6": "fd00:2021:1111::e:7758", "name": "gitlab.mycorp.com", "type": "dns", + "address_description": "dns resource", + "gateway_groups": [{"name": "test", "id": "bf56f32d-7b2c-4f5d-a784-788977d014a4"}], "not": "relevant" } ] diff --git a/rust/connlib/shared/src/messages.rs b/rust/connlib/shared/src/messages.rs index d1f8bd431..d687bedbf 100644 --- a/rust/connlib/shared/src/messages.rs +++ b/rust/connlib/shared/src/messages.rs @@ -313,13 +313,22 @@ mod tests { use itertools::Itertools; - use super::{client::ResourceDescription, client::ResourceDescriptionDns, ResourceId}; + use super::{ + client::ResourceDescription, + client::{GatewayGroup, ResourceDescriptionDns}, + ResourceId, + }; fn fake_resource(name: &str, uuid: &str) -> ResourceDescription { ResourceDescription::Dns(ResourceDescriptionDns { id: ResourceId::from_str(uuid).unwrap(), name: name.to_string(), address: "unused.example.com".to_string(), + address_description: "test description".to_string(), + gateway_groups: vec![GatewayGroup { + name: "test".to_string(), + id: "99ba0c1e-5189-4cfc-a4db-fd6cb1c937fd".parse().unwrap(), + }], }) } diff --git a/rust/connlib/shared/src/messages/client.rs b/rust/connlib/shared/src/messages/client.rs index 5b830727c..6025bc604 100644 --- a/rust/connlib/shared/src/messages/client.rs +++ b/rust/connlib/shared/src/messages/client.rs @@ -1,9 +1,10 @@ //! Client related messages that are needed within connlib -use std::borrow::Cow; +use std::{borrow::Cow, str::FromStr}; use ip_network::IpNetwork; use serde::{Deserialize, Serialize}; +use uuid::Uuid; use super::ResourceId; @@ -18,6 +19,9 @@ pub struct ResourceDescriptionDns { /// /// Used only for display. pub name: String, + + pub address_description: String, + pub gateway_groups: Vec, } /// Description of a resource that maps to a CIDR. @@ -31,6 +35,33 @@ pub struct ResourceDescriptionCidr { /// /// Used only for display. pub name: String, + + pub address_description: String, + pub gateway_groups: Vec, +} + +#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] +pub struct GatewayGroup { + pub name: String, + pub id: SiteId, +} + +#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] +pub struct SiteId(Uuid); + +impl FromStr for SiteId { + type Err = uuid::Error; + + fn from_str(s: &str) -> Result { + Ok(SiteId(Uuid::parse_str(s)?)) + } +} + +impl SiteId { + #[cfg(feature = "proptest")] + pub(crate) fn from_u128(v: u128) -> Self { + Self(Uuid::from_u128(v)) + } } impl ResourceDescription { diff --git a/rust/connlib/shared/src/proptest.rs b/rust/connlib/shared/src/proptest.rs index 27d20eb62..79953e146 100644 --- a/rust/connlib/shared/src/proptest.rs +++ b/rust/connlib/shared/src/proptest.rs @@ -1,22 +1,67 @@ use crate::messages::{ - client::ResourceDescriptionCidr, client::ResourceDescriptionDns, ClientId, ResourceId, + client::ResourceDescriptionCidr, + client::{GatewayGroup, ResourceDescriptionDns, SiteId}, + ClientId, ResourceId, }; use ip_network::{IpNetwork, Ipv4Network, Ipv6Network}; use proptest::{ arbitrary::{any, any_with}, - sample, + collection, sample, strategy::Strategy, }; use std::net::{Ipv4Addr, Ipv6Addr}; pub fn dns_resource() -> impl Strategy { - (resource_id(), resource_name(), dns_resource_address()) - .prop_map(|(id, name, address)| ResourceDescriptionDns { id, address, name }) + ( + resource_id(), + resource_name(), + dns_resource_address(), + gateway_groups(), + address_description(), + ) + .prop_map(|(id, name, address, gateway_groups, address_description)| { + ResourceDescriptionDns { + id, + address, + name, + gateway_groups, + address_description, + } + }) } pub fn cidr_resource(host_mask_bits: usize) -> impl Strategy { - (resource_id(), resource_name(), ip_network(host_mask_bits)) - .prop_map(|(id, name, address)| ResourceDescriptionCidr { id, address, name }) + ( + resource_id(), + resource_name(), + ip_network(host_mask_bits), + gateway_groups(), + address_description(), + ) + .prop_map(|(id, name, address, gateway_groups, address_description)| { + ResourceDescriptionCidr { + id, + address, + name, + gateway_groups, + address_description, + } + }) +} + +pub fn address_description() -> impl Strategy { + any_with::("[a-z]{4,10}".into()) +} + +pub fn gateway_groups() -> impl Strategy> { + collection::vec(gateway_group(), 1..=10) +} + +pub fn gateway_group() -> impl Strategy { + (any_with::("[a-z]{4,10}".into()), any::()).prop_map(|(name, id)| GatewayGroup { + name, + id: SiteId::from_u128(id), + }) } pub fn resource_id() -> impl Strategy + Clone { diff --git a/rust/connlib/tunnel/src/client.rs b/rust/connlib/tunnel/src/client.rs index 7b3663cfd..0621686d0 100644 --- a/rust/connlib/tunnel/src/client.rs +++ b/rust/connlib/tunnel/src/client.rs @@ -1489,6 +1489,8 @@ mod proptests { address, id: resource.id, name: resource.name, + address_description: resource.address_description, + gateway_groups: resource.gateway_groups, }; client_state.add_resources(&[ResourceDescription::Cidr(dns_as_cidr_resource.clone())]); diff --git a/rust/connlib/tunnel/src/dns.rs b/rust/connlib/tunnel/src/dns.rs index a4c4caaf6..9f212fbd1 100644 --- a/rust/connlib/tunnel/src/dns.rs +++ b/rust/connlib/tunnel/src/dns.rs @@ -506,7 +506,9 @@ mod test { r#"{ "id": "c4bb3d79-afa7-4660-8918-06c38fda3a4a", "address": "*.foo.com", - "name": "foo.com wildcard" + "name": "foo.com wildcard", + "address_description": "foo", + "gateway_groups": [{"id": "bf56f32d-7b2c-4f5d-a784-788977d014a4", "name": "test"}] }"#, ) .unwrap() @@ -517,7 +519,9 @@ mod test { r#"{ "id": "c4bb3d79-afa7-4660-8918-06c38fda3a4b", "address": "*.bar.com", - "name": "bar.com wildcard" + "name": "bar.com wildcard", + "address_description": "bar", + "gateway_groups": [{"id": "bf56f32d-7b2c-4f5d-a784-788977d014a4", "name": "test"}] }"#, ) .unwrap() @@ -528,7 +532,9 @@ mod test { r#"{ "id": "c4bb3d79-afa7-4660-8918-06c38fda3a4c", "address": "baz.com", - "name": "baz.com" + "name": "baz.com", + "address_description": "baz", + "gateway_groups": [{"id": "bf56f32d-7b2c-4f5d-a784-788977d014a4", "name": "test"}] }"#, ) .unwrap()