Skip to content

Commit c2f8b09

Browse files
committed
keylime-agent: move JsonWrapper from common.rs to the library
Move the JsonWrapper structure from common.rs to the library, effectively making the common.rs empty. Remove the common.rs file. Signed-off-by: Anderson Toshiyuki Sasaki <[email protected]>
1 parent 3d616a8 commit c2f8b09

File tree

10 files changed

+107
-80
lines changed

10 files changed

+107
-80
lines changed

keylime-agent/src/agent_handler.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
// SPDX-License-Identifier: Apache-2.0
22
// Copyright 2023 Keylime Authors
33

4-
use crate::common::JsonWrapper;
54
use crate::{tpm, Error as KeylimeError, QuoteData};
65
use actix_web::{http, web, HttpRequest, HttpResponse, Responder};
76
use base64::{engine::general_purpose, Engine as _};
7+
use keylime::json_wrapper::JsonWrapper;
88
use log::*;
99
use serde::{Deserialize, Serialize};
1010

keylime-agent/src/api.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use crate::{
2-
agent_handler, common::JsonWrapper, config, errors_handler, keys_handler,
2+
agent_handler, config, errors_handler, keys_handler,
33
notifications_handler, quotes_handler, QuoteData,
44
};
55
use actix_web::{http, web, HttpRequest, HttpResponse, Responder, Scope};
66
use keylime::{
7-
config::SUPPORTED_API_VERSIONS, list_parser::parse_list,
8-
version::KeylimeVersion,
7+
config::SUPPORTED_API_VERSIONS, json_wrapper::JsonWrapper,
8+
list_parser::parse_list, version::KeylimeVersion,
99
};
1010
use log::*;
1111
use serde::{Deserialize, Serialize};

keylime-agent/src/common.rs

Lines changed: 0 additions & 63 deletions
This file was deleted.

keylime-agent/src/errors_handler.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
// SPDX-License-Identifier: Apache-2.0
22
// Copyright 2021 Keylime Authors
33

4-
use crate::{common::JsonWrapper, QuoteData};
4+
use crate::QuoteData;
55
use actix_web::{
66
body, dev,
77
error::{InternalError, JsonPayloadError, PathError, QueryPayloadError},
88
http,
99
middleware::{ErrorHandlerResponse, ErrorHandlers},
1010
web, Error, HttpRequest, HttpResponse, Responder, Result,
1111
};
12-
use keylime::version::Version;
12+
use keylime::{json_wrapper::JsonWrapper, version::Version};
1313
use log::*;
1414

1515
pub(crate) async fn app_default(

keylime-agent/src/keys_handler.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,20 @@
22
// Copyright 2021 Keylime Authors
33

44
use crate::{
5-
common::JsonWrapper,
65
config::KeylimeConfig,
76
payloads::{Payload, PayloadMessage},
87
Error, QuoteData, Result,
98
};
109
use actix_web::{http, web, HttpRequest, HttpResponse, Responder};
1110
use base64::{engine::general_purpose, Engine as _};
12-
use keylime::crypto::{
13-
self,
14-
auth_tag::AuthTag,
15-
encrypted_data::EncryptedData,
16-
symmkey::{KeySet, SymmKey},
11+
use keylime::{
12+
crypto::{
13+
self,
14+
auth_tag::AuthTag,
15+
encrypted_data::EncryptedData,
16+
symmkey::{KeySet, SymmKey},
17+
},
18+
json_wrapper::JsonWrapper,
1719
};
1820
use log::*;
1921
use serde::{Deserialize, Serialize};

keylime-agent/src/main.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333

3434
mod agent_handler;
3535
mod api;
36-
mod common;
3736
mod errors_handler;
3837
mod keys_handler;
3938
mod notifications_handler;
@@ -44,7 +43,6 @@ mod revocation;
4443
use actix_web::{dev::Service, http, middleware, rt, web, App, HttpServer};
4544
use base64::{engine::general_purpose, Engine as _};
4645
use clap::{Arg, Command as ClapApp};
47-
use common::*;
4846
use futures::{
4947
future::{ok, TryFutureExt},
5048
try_join,

keylime-agent/src/notifications_handler.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
// Copyright 2021 Keylime Authors
33

44
use crate::{
5-
common::JsonWrapper,
65
revocation::{Revocation, RevocationMessage},
76
Error, QuoteData, Result,
87
};
98
use actix_web::{http, web, HttpRequest, HttpResponse, Responder};
9+
use keylime::json_wrapper::JsonWrapper;
1010
use log::*;
1111
use serde::{Deserialize, Serialize};
1212
use std::path::{Path, PathBuf};

keylime-agent/src/quotes_handler.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
// SPDX-License-Identifier: Apache-2.0
22
// Copyright 2021 Keylime Authors
33

4-
use crate::common::JsonWrapper;
54
use crate::crypto;
65
use crate::serialization::serialize_maybe_base64;
76
use crate::{tpm, Error as KeylimeError, QuoteData};
87
use actix_web::{http, web, HttpRequest, HttpResponse, Responder};
98
use base64::{engine::general_purpose, Engine as _};
10-
use keylime::quote::{Integ, KeylimeQuote};
9+
use keylime::{
10+
json_wrapper::JsonWrapper,
11+
quote::{Integ, KeylimeQuote},
12+
};
1113
use log::*;
1214
use serde::{Deserialize, Serialize};
1315
use std::{

keylime/src/json_wrapper.rs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
use serde::{Deserialize, Serialize};
2+
use serde_json::{json, Value};
3+
use std::fmt::{self, Debug, Display};
4+
5+
#[derive(Serialize, Deserialize, Debug)]
6+
pub struct JsonWrapper<A> {
7+
pub code: u16,
8+
pub status: String,
9+
pub results: A,
10+
}
11+
12+
impl JsonWrapper<Value> {
13+
pub fn error(code: u16, status: impl ToString) -> JsonWrapper<Value> {
14+
JsonWrapper {
15+
code,
16+
status: status.to_string(),
17+
results: json!({}),
18+
}
19+
}
20+
}
21+
22+
impl<'de, A> JsonWrapper<A>
23+
where
24+
A: Deserialize<'de> + Serialize + Debug,
25+
{
26+
pub fn success(results: A) -> JsonWrapper<A> {
27+
JsonWrapper {
28+
code: 200,
29+
status: String::from("Success"),
30+
results,
31+
}
32+
}
33+
}
34+
35+
impl<'de, A> Display for JsonWrapper<A>
36+
where
37+
A: Deserialize<'de> + Serialize + Debug + Display,
38+
{
39+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40+
let out = json!(self).to_string();
41+
write!(f, "{}", out)
42+
}
43+
}
44+
45+
#[cfg(test)]
46+
mod test {
47+
use super::*;
48+
49+
#[derive(Serialize, Deserialize, Debug, PartialEq)]
50+
struct TestResult {
51+
result: String,
52+
}
53+
54+
impl Display for TestResult {
55+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56+
let out = json!(self).to_string();
57+
write!(f, "{}", out)
58+
}
59+
}
60+
61+
#[test]
62+
fn test_json_wrapper_success() {
63+
let expected = json!({
64+
"code": 200,
65+
"status": "Success",
66+
"results": TestResult{
67+
result: "Success".to_string(),
68+
},
69+
});
70+
let j = JsonWrapper::success(TestResult {
71+
result: "Success".to_string(),
72+
});
73+
assert_eq!(j.to_string(), expected.to_string());
74+
}
75+
76+
#[test]
77+
fn test_json_wrapper_error() {
78+
let expected = json!({
79+
"code": 400,
80+
"status": "Testing JsonWrapper error",
81+
"results": {},
82+
});
83+
let j =
84+
JsonWrapper::error(400, "Testing JsonWrapper error".to_string());
85+
assert_eq!(j.to_string(), expected.to_string());
86+
}
87+
}

keylime/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub mod hostname_parser;
1212
pub mod https_client;
1313
pub mod ima;
1414
pub mod ip_parser;
15+
pub mod json_wrapper;
1516
pub mod list_parser;
1617
pub mod permissions;
1718
pub mod quote;

0 commit comments

Comments
 (0)