Skip to content

Commit 8db349a

Browse files
committed
suggestions
1 parent 4740f83 commit 8db349a

File tree

3 files changed

+55
-14
lines changed

3 files changed

+55
-14
lines changed

src/grpc.rs

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,20 +64,29 @@ impl ProxyServer {
6464
}
6565

6666
pub(crate) fn set_tls_config(&self, cert_pem: String, key_pem: String) -> Result<(), ApiError> {
67-
let mut lock = self.config.lock().unwrap();
67+
let mut lock = self
68+
.config
69+
.lock()
70+
.expect("Failed to acquire lock on config mutex when updating TLS configuration");
6871
let config = lock.get_or_insert_with(Configuration::default);
6972
config.grpc_cert_pem = cert_pem;
7073
config.grpc_key_pem = key_pem;
7174
Ok(())
7275
}
7376

7477
pub(crate) fn configure(&self, config: Configuration) {
75-
let mut lock = self.config.lock().unwrap();
78+
let mut lock = self
79+
.config
80+
.lock()
81+
.expect("Failed to acquire lock on config mutex when applying proxy configuration");
7682
*lock = Some(config);
7783
}
7884

7985
pub(crate) fn get_configuration(&self) -> Option<Configuration> {
80-
let lock = self.config.lock().unwrap();
86+
let lock = self
87+
.config
88+
.lock()
89+
.expect("Failed to acquire lock on config mutex when retrieving proxy configuration");
8190
lock.clone()
8291
}
8392

@@ -130,7 +139,13 @@ impl ProxyServer {
130139
payload: core_request::Payload,
131140
device_info: DeviceInfo,
132141
) -> Result<oneshot::Receiver<core_response::Payload>, ApiError> {
133-
if let Some(client_tx) = self.clients.lock().unwrap().values().next() {
142+
if let Some(client_tx) = self
143+
.clients
144+
.lock()
145+
.expect("Failed to acquire lock on clients hashmap when sending message to core")
146+
.values()
147+
.next()
148+
{
134149
let id = self.current_id.fetch_add(1, Ordering::Relaxed);
135150
let res = CoreRequest {
136151
id,
@@ -142,7 +157,10 @@ impl ProxyServer {
142157
return Err(ApiError::Unexpected("Failed to send CoreRequest".into()));
143158
}
144159
let (tx, rx) = oneshot::channel();
145-
self.results.lock().unwrap().insert(id, tx);
160+
self.results
161+
.lock()
162+
.expect("Failed to acquire lock on results hashmap when sending CoreRequest")
163+
.insert(id, tx);
146164
self.connected.store(true, Ordering::Relaxed);
147165
Ok(rx)
148166
} else {
@@ -155,7 +173,10 @@ impl ProxyServer {
155173
}
156174

157175
pub(crate) fn setup_completed(&self) -> bool {
158-
let lock = self.config.lock().unwrap();
176+
let lock = self
177+
.config
178+
.lock()
179+
.expect("Failed to acquire lock on config mutex when checking setup status");
159180
lock.is_some()
160181
}
161182
}
@@ -197,7 +218,9 @@ impl proxy_server::Proxy for ProxyServer {
197218
};
198219
let maybe_info = ComponentInfo::from_metadata(request.metadata());
199220
let (version, info) = get_tracing_variables(&maybe_info);
200-
*self.core_version.lock().unwrap() = Some(version.clone());
221+
*self.core_version.lock().expect(
222+
"Failed to acquire lock on core_version mutex when storing version information",
223+
) = Some(version.clone());
201224

202225
let span = tracing::info_span!("core_bidi_stream", component = %DefguardComponent::Core,
203226
version = version.to_string(), info);
@@ -206,7 +229,12 @@ impl proxy_server::Proxy for ProxyServer {
206229
info!("Defguard Core gRPC client connected from: {address}");
207230

208231
let (tx, rx) = mpsc::unbounded_channel();
209-
self.clients.lock().unwrap().insert(address, tx);
232+
self.clients
233+
.lock()
234+
.expect(
235+
"Failed to acquire lock on clients hashmap when registering new core connection",
236+
)
237+
.insert(address, tx);
210238
self.connected.store(true, Ordering::Relaxed);
211239

212240
let clients = Arc::clone(&self.clients);
@@ -221,7 +249,7 @@ impl proxy_server::Proxy for ProxyServer {
221249
debug!("Received message from Defguard Core ID={}", response.id);
222250
connected.store(true, Ordering::Relaxed);
223251
if let Some(payload) = response.payload {
224-
let maybe_rx = results.lock().unwrap().remove(&response.id);
252+
let maybe_rx = results.lock().expect("Failed to acquire lock on results hashmap when processing response").remove(&response.id);
225253
if let Some(rx) = maybe_rx {
226254
if let Err(err) = rx.send(payload) {
227255
error!("Failed to send message to rx {:?}", err.type_id());
@@ -243,7 +271,7 @@ impl proxy_server::Proxy for ProxyServer {
243271
}
244272
info!("Defguard core client disconnected: {address}");
245273
connected.store(false, Ordering::Relaxed);
246-
clients.lock().unwrap().remove(&address);
274+
clients.lock().expect("Failed to acquire lock on clients hashmap when removing disconnected client").remove(&address);
247275
}
248276
.instrument(tracing::Span::current()),
249277
);

src/http.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,11 +211,17 @@ pub async fn run_server(config: Config) -> anyhow::Result<()> {
211211
read_to_string(cert_dir.join(GRPC_CERT_NAME)).ok(),
212212
read_to_string(cert_dir.join(GRPC_KEY_NAME)).ok(),
213213
) {
214-
info!("Using existing gRPC TLS certificates from {cert_dir:?}");
214+
info!(
215+
"Using existing gRPC TLS certificates from {}",
216+
cert_dir.display()
217+
);
215218
server_clone.set_tls_config(cert, key)?;
216219
} else if !server_clone.setup_completed() {
217220
// Only attempt setup if not already configured
218-
info!("No gRPC TLS certificates found at {cert_dir:?}, new certificates will be generated");
221+
info!(
222+
"No gRPC TLS certificates found at {}, new certificates will be generated",
223+
cert_dir.display()
224+
);
219225
let configuration = setup_server
220226
.await_setup(SocketAddr::new(
221227
config

src/setup.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,10 @@ impl proxy_setup_server::ProxySetup for ProxySetupServer {
149149
}
150150
};
151151

152-
self.key_pair.lock().unwrap().replace(key_pair);
152+
self.key_pair
153+
.lock()
154+
.expect("Failed to acquire lock on key pair during proxy setup when trying to store generated key pair")
155+
.replace(key_pair);
153156

154157
let csr_der = csr.to_der();
155158
let csr_request = DerPayload {
@@ -176,7 +179,11 @@ impl proxy_setup_server::ProxySetup for ProxySetupServer {
176179
};
177180

178181
let key_pair = {
179-
let key_pair = self.key_pair.lock().unwrap().take();
182+
let key_pair = self
183+
.key_pair
184+
.lock()
185+
.expect("Failed to acquire lock on key pair during proxy setup when trying to receive certificate")
186+
.take();
180187
if let Some(kp) = key_pair {
181188
kp
182189
} else {

0 commit comments

Comments
 (0)