Repository: DefGuard/defguard (language: rust)

Issue:
Generated config file is invalid when the AllowedIPs field is left blank
If the location doesn't have any allowedips set (which is optional), its generated config file (downloadable during the enrollment or from the dashboard) will be impossible to parse by the `wg-quick` command or the wireguard GUI client.

```
[#] ip link add lokacja type wireguard
[#] wg setconf lokacja /dev/fd/63
Line unrecognized: `AllowedIPs='
Configuration parsing error
[#] ip link delete dev lokacja
```

Currently the `AllowedIPs` line looks like this in such scenario:
```
AllowedIPs =
```

Instead, if there are no allowedips specified, it should be empty altogether. The issue doesn't occur when importing the config via the defguard client menu.



Discussion / hints:

Fix patch:
diff --git a/src/db/models/device.rs b/src/db/models/device.rs
index f21751e8e..62583415e 100644
--- a/src/db/models/device.rs
+++ b/src/db/models/device.rs
@@ -366,12 +366,21 @@ impl Device {
             }
             None => String::new(),
         };
-        let allowed_ips = network
-            .allowed_ips
-            .iter()
-            .map(IpNetwork::to_string)
-            .collect::<Vec<String>>()
-            .join(",");
+
+        let allowed_ips = if network.allowed_ips.is_empty() {
+            String::new()
+        } else {
+            format!(
+                "AllowedIPs = {}\n",
+                network
+                    .allowed_ips
+                    .iter()
+                    .map(IpNetwork::to_string)
+                    .collect::<Vec<String>>()
+                    .join(",")
+            )
+        };
+
         format!(
             "[Interface]\n\
             PrivateKey = YOUR_PRIVATE_KEY\n\
@@ -380,7 +389,7 @@ impl Device {
             \n\
             [Peer]\n\
             PublicKey = {}\n\
-            AllowedIPs = {allowed_ips}\n\
+            {allowed_ips}\
             Endpoint = {}:{}\n\
             PersistentKeepalive = 300",
             wireguard_network_device.wireguard_ip, network.pubkey, network.endpoint, network.port,


Test patch: