Skip to content

Commit 4b1e148

Browse files
committed
Merge branch 'refactor-device-service-singleton' into feature/network-settings
Signed-off-by: Jan Zachmann <50990105+JanZachmann@users.noreply.github.com>
2 parents b8a3dfe + 58fa57d commit 4b1e148

File tree

10 files changed

+324
-277
lines changed

10 files changed

+324
-277
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ actix-multipart = { version = "0.7", default-features = false, features = [
1818
"derive"
1919
] }
2020
actix-server = { version = "2.6", default-features = false }
21-
actix-session = { version = "0.10", features = ["cookie-session"] }
21+
actix-session = { version = "0.11", features = ["cookie-session"] }
2222
actix-web = { version = "4.11", default-features = false, features = [
2323
"macros",
2424
"rustls-0_23",
@@ -53,7 +53,7 @@ serde_json = { version = "1.0", default-features = false, features = [
5353
"raw_value",
5454
] }
5555
serde_repr = { version = "0.1", default-features = false }
56-
serde_valid = { version = "1.0", default-features = false }
56+
serde_valid = { version = "2.0", default-features = false }
5757
tokio = { version = "1.45", default-features = false, features = [
5858
"macros",
5959
"net",

src/api.rs

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use argon2::{
1616
};
1717
use log::{debug, error};
1818
use serde::Deserialize;
19-
use serde_valid::Validate;
2019
use std::{
2120
fs::{self, File},
2221
io::Write,
@@ -284,22 +283,10 @@ where
284283
) -> impl Responder {
285284
debug!("set_network_config() called");
286285

287-
if let Err(e) = network_config.validate() {
288-
error!("set_network_config() failed: {e:#}");
289-
return HttpResponse::BadRequest().body(format!("{e:#}"));
290-
}
291-
292-
if let Err(e) =
293-
NetworkConfigService::apply_network_config(&api.service_client, &network_config).await
294-
{
295-
error!("set_network_config() failed: {e:#}");
296-
if let Err(err) = NetworkConfigService::rollback_network_config(&network_config) {
297-
error!("Failed to restore network config: {err:#}");
298-
}
299-
return HttpResponse::InternalServerError().body(format!("{e:#}"));
300-
}
301-
302-
HttpResponse::Ok().finish()
286+
Self::handle_service_result(
287+
NetworkConfigService::set_network_config(&api.service_client, &network_config).await,
288+
"set_network_config",
289+
)
303290
}
304291

305292
async fn validate_token_and_claims(&self, token: &str) -> Result<()> {

src/auth/token.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl TokenManager {
4646
self.inner
4747
.key
4848
.authenticate(claims)
49-
.map_err(|e| anyhow::anyhow!("failed to create token: {}", e))
49+
.map_err(|e| anyhow::anyhow!("failed to create token: {e:#}"))
5050
}
5151

5252
/// Verify a token and check if it's valid

src/certificate.rs

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
#![cfg_attr(feature = "mock", allow(dead_code, unused_imports))]
22

3-
use crate::{
4-
common::handle_http_response, http_client, omnect_device_service_client::DeviceServiceClient,
5-
};
3+
use crate::{common::handle_http_response, http_client};
64
use anyhow::{Context, Result};
75
use log::info;
86
use serde::{Deserialize, Serialize};
97
use std::{fs::File, io::Write};
108

11-
#[derive(Serialize)]
12-
struct CreateCertPayload {
9+
// Public payload for passing to certificate creation
10+
#[derive(Debug, Serialize)]
11+
pub struct CreateCertPayload {
1312
#[serde(rename = "commonName")]
14-
common_name: String,
13+
pub common_name: String,
1514
}
1615

1716
#[derive(Debug, Deserialize)]
@@ -40,20 +39,13 @@ pub fn key_path() -> String {
4039
}
4140

4241
#[cfg(feature = "mock")]
43-
pub async fn create_module_certificate<T>(_service_client: &T) -> Result<()>
44-
where
45-
T: DeviceServiceClient,
46-
{
42+
pub async fn create_module_certificate(_payload: CreateCertPayload) -> Result<()> {
4743
Ok(())
4844
}
4945

5046
#[cfg(not(feature = "mock"))]
51-
pub async fn create_module_certificate<T>(service_client: &T) -> Result<()>
52-
where
53-
T: DeviceServiceClient,
54-
{
47+
pub async fn create_module_certificate(payload: CreateCertPayload) -> Result<()> {
5548
info!("create module certificate");
56-
5749
let id = std::env::var("IOTEDGE_MODULEID")
5850
.context("failed to read IOTEDGE_MODULEID environment variable")?;
5951
let gen_id = std::env::var("IOTEDGE_MODULEGENERATIONID")
@@ -63,17 +55,13 @@ where
6355
let workload_uri = std::env::var("IOTEDGE_WORKLOADURI")
6456
.context("failed to read IOTEDGE_WORKLOADURI environment variable")?;
6557

66-
let payload = CreateCertPayload {
67-
common_name: service_client.ip_address().await?,
68-
};
69-
70-
let path = format!("/modules/{id}/genid/{gen_id}/certificate/server?api-version={api_version}");
58+
let path = format!("modules/{id}/genid/{gen_id}/certificate/server?api-version={api_version}");
7159

7260
// Create a client for the IoT Edge workload socket
7361
let client = http_client::unix_socket_client(&workload_uri)?;
7462

75-
let url = format!("http://localhost{}", path);
76-
info!("POST {url} (IoT Edge workload API)");
63+
let url = format!("http://localhost/{path}");
64+
info!("POST {url} (IoT Edge workload API) with payload: {payload:?}");
7765

7866
let res = client
7967
.post(&url)

src/common.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,23 @@ pub fn centrifugo_config() -> Arc<CentrifugoConfig> {
3333
.clone()
3434
}
3535

36+
pub fn centrifugo_publish_endpoint() -> crate::omnect_device_service_client::PublishEndpoint {
37+
let cfg = centrifugo_config();
38+
crate::omnect_device_service_client::PublishEndpoint {
39+
url: format!("https://localhost:{}/api/publish", cfg.port),
40+
headers: vec![
41+
crate::omnect_device_service_client::HeaderKeyValue {
42+
name: String::from("Content-Type"),
43+
value: String::from("application/json"),
44+
},
45+
crate::omnect_device_service_client::HeaderKeyValue {
46+
name: String::from("X-API-Key"),
47+
value: cfg.api_key.clone(),
48+
},
49+
],
50+
}
51+
}
52+
3653
macro_rules! config_path {
3754
() => {
3855
std::path::Path::new(
@@ -109,10 +126,7 @@ pub async fn handle_http_response(res: Response, context_msg: &str) -> Result<St
109126

110127
ensure!(
111128
status.is_success(),
112-
"{} failed with status {} and body: {}",
113-
context_msg,
114-
status,
115-
body
129+
"{context_msg} failed with status {status} and body: {body}"
116130
);
117131

118132
Ok(body)

0 commit comments

Comments
 (0)