Skip to content

Commit ba24c03

Browse files
authored
support protobuf versioning (#292)
* update protos * use versioned protos * update dependencies * update proto submodule * update protos directory structure * update proto submodule * handle updated protobuf * adjust protobuf naming * update proto submodule
1 parent 8cdc6ed commit ba24c03

File tree

10 files changed

+74
-51
lines changed

10 files changed

+74
-51
lines changed

Cargo.lock

Lines changed: 1 addition & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ defguard_certs = { git = "https://github.com/DefGuard/defguard.git", rev = "0195
1212
defguard_version = { git = "https://github.com/DefGuard/defguard.git", rev = "01957186101fc105803d56f1190efbdb5102df2f" }
1313
defguard_wireguard_rs = "0.9"
1414
env_logger = "0.11"
15-
gethostname = "1.0"
1615
ipnetwork = "0.21"
1716
libc = { version = "0.2", default-features = false }
1817
log = "0.4"
1918
prost = "0.14"
19+
prost-types = "0.14"
2020
serde = { version = "1.0", features = ["derive"] }
2121
syslog = "7.0"
2222
thiserror = "2.0"

build.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
1111
// compiling protos using path on build time
1212
.compile_protos(
1313
&[
14-
"proto/wireguard/gateway.proto",
15-
"proto/enterprise/firewall/firewall.proto",
14+
"proto/v2/gateway.proto",
15+
"proto/enterprise/v2/firewall/firewall.proto",
1616
],
17-
&["proto/wireguard", "proto/enterprise/firewall"],
17+
&["proto"],
1818
)?;
1919
println!("cargo:rerun-if-changed=proto");
2020
Ok(())

flake.lock

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/enterprise/firewall/mod.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use std::{
1414
use ipnetwork::{IpNetwork, Ipv4Network, Ipv6Network};
1515
use iprange::{IpAddrRange, IpAddrRangeError};
1616
use thiserror::Error;
17+
use tracing::error;
1718

1819
use crate::proto;
1920

@@ -161,7 +162,7 @@ impl Protocol {
161162
proto::enterprise::firewall::Protocol::Udp => Ok(Self::Udp),
162163
proto::enterprise::firewall::Protocol::Icmp => Ok(Self::Icmp),
163164
// TODO: IcmpV6
164-
proto::enterprise::firewall::Protocol::Invalid => {
165+
proto::enterprise::firewall::Protocol::Unspecified => {
165166
Err(FirewallError::UnsupportedProtocol(proto as u8))
166167
}
167168
}
@@ -196,10 +197,14 @@ impl From<bool> for Policy {
196197

197198
impl Policy {
198199
#[must_use]
199-
pub const fn from_proto(verdict: proto::enterprise::firewall::FirewallPolicy) -> Self {
200+
pub fn from_proto(verdict: proto::enterprise::firewall::FirewallPolicy) -> Self {
200201
match verdict {
201202
proto::enterprise::firewall::FirewallPolicy::Allow => Self::Allow,
202203
proto::enterprise::firewall::FirewallPolicy::Deny => Self::Deny,
204+
proto::enterprise::firewall::FirewallPolicy::Unspecified => {
205+
error!("Received invalid gRPC FirewallPolicy. Falling back to Deny.");
206+
Self::Deny
207+
}
203208
}
204209
}
205210
}

src/gateway.rs

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use defguard_version::{
1313
ComponentInfo, DefguardComponent, Version, get_tracing_variables, server::DefguardVersionLayer,
1414
};
1515
use defguard_wireguard_rs::{WireguardInterfaceApi, net::IpAddrMask};
16-
use gethostname::gethostname;
1716
use tokio::{
1817
sync::{mpsc, oneshot},
1918
time::interval,
@@ -35,9 +34,12 @@ use crate::{
3534
},
3635
error::GatewayError,
3736
execute_command, mask,
38-
proto::gateway::{
39-
Configuration, ConfigurationRequest, CoreRequest, CoreResponse, LogEntry, Peer, Update,
40-
core_request, core_response, gateway_server, update,
37+
proto::{
38+
common::LogEntry,
39+
gateway::{
40+
Configuration, CoreRequest, CoreResponse, Peer, Update, core_request, core_response,
41+
gateway_server, update,
42+
},
4143
},
4244
setup::run_setup,
4345
version::is_core_version_supported,
@@ -110,7 +112,7 @@ pub async fn run_gateway_loop(
110112
#[derive(Clone, PartialEq)]
111113
struct InterfaceConfiguration {
112114
name: String,
113-
prvkey: String,
115+
private_key: String,
114116
addresses: Vec<IpAddrMask>,
115117
port: u16,
116118
mtu: u32,
@@ -127,7 +129,7 @@ impl From<Configuration> for InterfaceConfiguration {
127129
.collect();
128130
Self {
129131
name: config.name,
130-
prvkey: config.prvkey,
132+
private_key: config.private_key,
131133
addresses,
132134
port: config.port as u16,
133135
mtu: config.mtu,
@@ -384,7 +386,7 @@ impl Gateway {
384386
);
385387
trace!(
386388
"Received configuration: {:?}",
387-
mask!(new_configuration, prvkey)
389+
mask!(new_configuration, private_key)
388390
);
389391

390392
// check if new configuration is different than current one
@@ -405,7 +407,7 @@ impl Gateway {
405407
);
406408
trace!(
407409
"Reconfigured WireGuard interface. Configuration: {:?}",
408-
mask!(new_configuration, prvkey)
410+
mask!(new_configuration, private_key)
409411
);
410412
// store new configuration and peers
411413
self.interface_configuration = Some(new_interface_configuration);
@@ -685,20 +687,11 @@ impl gateway_server::Gateway for GatewayServer {
685687
}
686688

687689
let (tx, rx) = mpsc::unbounded_channel();
688-
let Ok(hostname) = gethostname().into_string() else {
689-
error!("Unable to get hostname");
690-
return Err(Status::internal("failed to get hostname"));
691-
};
692690

693691
// First, send configuration request.
694-
#[allow(deprecated)]
695-
let payload = ConfigurationRequest {
696-
name: None, // TODO: remove?
697-
hostname,
698-
};
699692
let req = CoreRequest {
700693
id: self.message_id.fetch_add(1, Ordering::Relaxed),
701-
payload: Some(core_request::Payload::ConfigRequest(payload)),
694+
payload: Some(core_request::Payload::ConfigRequest(())),
702695
};
703696

704697
match tx.send(Ok(req)) {
@@ -906,7 +899,7 @@ mod tests {
906899
async fn test_configuration_comparison() {
907900
let old_config = InterfaceConfiguration {
908901
name: "gateway".to_string(),
909-
prvkey: "FGqcPuaSlGWC2j50TBA4jHgiefPgQQcgTNLwzKUzBS8=".to_string(),
902+
private_key: "FGqcPuaSlGWC2j50TBA4jHgiefPgQQcgTNLwzKUzBS8=".to_string(),
910903
addresses: vec!["10.6.1.1/24".parse().unwrap()],
911904
port: 50051,
912905
mtu: 1420,
@@ -954,7 +947,7 @@ mod tests {
954947
// only interface config is different
955948
let new_config = InterfaceConfiguration {
956949
name: "gateway".to_string(),
957-
prvkey: "FGqcPuaSlGWC2j50TBA4jHgiefPgQQcgTNLwzKUzBS8=".to_string(),
950+
private_key: "FGqcPuaSlGWC2j50TBA4jHgiefPgQQcgTNLwzKUzBS8=".to_string(),
958951
addresses: vec!["10.6.1.2/24".parse().unwrap()],
959952
port: 50051,
960953
mtu: 1420,

src/lib.rs

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,40 @@ pub mod gateway;
44
pub mod server;
55
mod version;
66

7+
pub mod generated {
8+
pub mod defguard {
9+
pub mod common {
10+
pub mod v2 {
11+
tonic::include_proto!("defguard.common.v2");
12+
}
13+
}
14+
pub mod gateway {
15+
pub mod v2 {
16+
17+
tonic::include_proto!("defguard.gateway.v2");
18+
}
19+
}
20+
pub mod enterprise {
21+
pub mod firewall {
22+
pub mod v2 {
23+
24+
tonic::include_proto!("defguard.enterprise.firewall.v2");
25+
}
26+
}
27+
}
28+
}
29+
}
30+
731
pub mod proto {
32+
pub mod common {
33+
pub use crate::generated::defguard::common::v2::*;
34+
}
835
pub mod gateway {
9-
tonic::include_proto!("gateway");
36+
pub use crate::generated::defguard::gateway::v2::*;
1037
}
1138
pub mod enterprise {
1239
pub mod firewall {
13-
tonic::include_proto!("enterprise.firewall");
40+
pub use crate::generated::defguard::enterprise::firewall::v2::*;
1441
}
1542
}
1643
}
@@ -23,6 +50,7 @@ use std::{process::Command, str::FromStr, time::SystemTime};
2350
use config::Config;
2451
use defguard_wireguard_rs::{InterfaceConfiguration, net::IpAddrMask, peer::Peer};
2552
use error::GatewayError;
53+
use prost_types::Timestamp;
2654
use syslog::{BasicLogger, Facility, Formatter3164};
2755

2856
pub mod enterprise;
@@ -102,7 +130,7 @@ impl From<proto::gateway::Configuration> for InterfaceConfiguration {
102130
.collect();
103131
InterfaceConfiguration {
104132
name: config.name,
105-
prvkey: config.prvkey,
133+
prvkey: config.private_key,
106134
addresses,
107135
port: config.port as u16,
108136
peers,
@@ -150,9 +178,13 @@ impl From<&Peer> for proto::gateway::PeerStats {
150178
.endpoint
151179
.map_or(String::new(), |endpoint| endpoint.to_string()),
152180
allowed_ips: peer.allowed_ips.iter().map(ToString::to_string).collect(),
153-
latest_handshake: peer.last_handshake.map_or(0, |ts| {
181+
latest_handshake: peer.last_handshake.and_then(|ts| {
154182
ts.duration_since(SystemTime::UNIX_EPOCH)
155-
.map_or(0, |duration| duration.as_secs())
183+
.ok()
184+
.map(|d| Timestamp {
185+
seconds: d.as_secs() as i64,
186+
nanos: d.subsec_nanos() as i32,
187+
})
156188
}),
157189
download: peer.rx_bytes,
158190
upload: peer.tx_bytes,

src/logging.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use tokio::sync::mpsc::Sender;
33
use tracing::{Event, Subscriber};
44
use tracing_subscriber::{Layer, layer::SubscriberExt, util::SubscriberInitExt};
55

6-
use crate::proto::gateway::LogEntry;
6+
use crate::proto::common::LogEntry;
77

88
pub fn init_tracing(own_version: &Version, level: &str, logs_tx: Option<Sender<LogEntry>>) {
99
let subscriber = tracing_subscriber::registry();

src/setup.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ use crate::{
1616
config::Config,
1717
error::GatewayError,
1818
gateway::TlsConfig,
19-
proto::gateway::{CertificateInfo, DerPayload, LogEntry, gateway_setup_server},
19+
proto::{
20+
common::{CertificateInfo, DerPayload, LogEntry},
21+
gateway::gateway_setup_server,
22+
},
2023
};
2124

2225
const AUTH_HEADER: &str = "authorization";

0 commit comments

Comments
 (0)