Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions crates/defguard/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use defguard_core::{
limits::update_counts,
},
events::{ApiEvent, BidiStreamEvent, GrpcEvent, InternalEvent},
gateway_config,
grpc::{
WorkerState,
gateway::{client_state::ClientMap, map::GatewayMap},
Expand Down Expand Up @@ -79,6 +80,10 @@ async fn main() -> Result<(), anyhow::Error> {
let token = init_vpn_location(&pool, args).await?;
println!("{token}");
}
Command::GatewayConfig(args) => {
let config = gateway_config(&pool, args).await?;
println!("{config:#?}");
}
}

// return early
Expand Down
27 changes: 27 additions & 0 deletions crates/defguard_common/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ pub enum Command {
about = "Add a new VPN location and return a gateway token. Used for automated setup."
)]
InitVpnLocation(InitVpnLocationArgs),
#[command(about = "Output the gateway gRPC configuration payload for a VPN location by ID.")]
GatewayConfig(GatewayConfigArgs),
}

#[derive(Args, Debug, Clone)]
Expand All @@ -209,6 +211,12 @@ pub struct InitVpnLocationArgs {
pub id: Option<i64>,
}

#[derive(Args, Debug, Clone)]
pub struct GatewayConfigArgs {
#[arg(long)]
pub location_id: i64,
}

impl DefGuardConfig {
#[must_use]
pub fn new() -> Self {
Expand Down Expand Up @@ -318,6 +326,25 @@ mod tests {
DefGuardConfig::command().debug_assert();
}

#[test]
fn test_parse_gateway_config_command() {
let config = DefGuardConfig::parse_from([
"defguard",
"--secret-key",
"1234567890123456789012345678901234567890123456789012345678901234",
"gateway-config",
"--location-id",
"42",
]);

assert!(matches!(
config.cmd,
Some(Command::GatewayConfig(GatewayConfigArgs {
location_id: 42
}))
));
}

#[test]
fn test_generate_rp_id() {
unsafe {
Expand Down
2 changes: 1 addition & 1 deletion crates/defguard_core/src/grpc/gateway/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ impl GatewayServer {
}
}

fn gen_config(
pub(crate) fn gen_config(
network: &WireguardNetwork<Id>,
peers: Vec<Peer>,
maybe_firewall_config: Option<FirewallConfig>,
Expand Down
52 changes: 50 additions & 2 deletions crates/defguard_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use db::models::{device::DeviceType, wireguard::LocationMfaMode};
use defguard_common::{
VERSION,
auth::claims::{Claims, ClaimsType},
config::{DefGuardConfig, InitVpnLocationArgs, server_config},
config::{DefGuardConfig, GatewayConfigArgs, InitVpnLocationArgs, server_config},
db::init_db,
};
use defguard_mail::Mail;
Expand Down Expand Up @@ -146,7 +146,10 @@ use self::{
worker::{create_job, create_worker_token, job_status, list_workers, remove_worker},
},
};
use crate::{db::models::wireguard::ServiceLocationMode, version::IncompatibleComponents};
use crate::{
db::models::wireguard::ServiceLocationMode, grpc::gateway::gen_config,
version::IncompatibleComponents,
};

pub mod appstate;
pub mod auth;
Expand Down Expand Up @@ -930,6 +933,51 @@ pub async fn init_vpn_location(
Ok(token)
}

pub async fn gateway_config(
pool: &PgPool,
args: &GatewayConfigArgs,
) -> Result<defguard_proto::gateway::Configuration, anyhow::Error> {
let location_id = args.location_id;

let mut conn = pool.acquire().await?;

// fetch specified location
let location = match WireguardNetwork::find_by_id(&mut *conn, location_id).await {
Ok(Some(network)) => network,
Ok(None) => return Err(anyhow!("Location {location_id} not found")),
Err(err) => {
return Err(anyhow!(
"Failed to rerieve location {location_id} with error: {err}"
));
}
};

// get peers
let peers = location
.get_peers(&mut *conn)
.await
.map_err(|err| anyhow!("Failed to get peers for location {location} with error: {err}"))?;

// prepare firewall config
let maybe_firewall_config =
location
.try_get_firewall_config(&mut conn)
.await
.map_err(|err| {
anyhow!(
"Failed to prepare firewall config for location {location} with error: {err}"
)
})?;

// generate config
let mut config = gen_config(&location, peers, maybe_firewall_config);

// overwrite private key just in case
config.prvkey = "REDACTED".into();

Ok(config)
}

pub(crate) fn is_valid_phone_number(number: &str) -> bool {
PHONE_NUMBER_REGEX.is_match(number)
}
Expand Down
Loading