Skip to content

Commit 46ab7c9

Browse files
committed
notifications_handler: Move the /notifications scope configuration
Move the /notifications scope configuration from main to notifications_handler. This is a preparation to support multiple API versions. Also, restrict the visibility of methods that are not required outside the module to private. Signed-off-by: Anderson Toshiyuki Sasaki <[email protected]>
1 parent 9ec3d43 commit 46ab7c9

File tree

3 files changed

+87
-52
lines changed

3 files changed

+87
-52
lines changed

keylime-agent/src/errors_handler.rs

-39
Original file line numberDiff line numberDiff line change
@@ -127,39 +127,6 @@ pub(crate) async fn agent_default(req: HttpRequest) -> impl Responder {
127127
response
128128
}
129129

130-
pub(crate) async fn notifications_default(
131-
req: HttpRequest,
132-
) -> impl Responder {
133-
let error;
134-
let response;
135-
let message;
136-
137-
match req.head().method {
138-
http::Method::POST => {
139-
error = 400;
140-
message = "URI not supported, only /revocation is supported for POST in /notifications/ interface";
141-
response = HttpResponse::BadRequest()
142-
.json(JsonWrapper::error(error, message));
143-
}
144-
_ => {
145-
error = 405;
146-
message = "Method is not supported in /notifications/ interface";
147-
response = HttpResponse::MethodNotAllowed()
148-
.insert_header(http::header::Allow(vec![http::Method::POST]))
149-
.json(JsonWrapper::error(error, message));
150-
}
151-
};
152-
153-
warn!(
154-
"{} returning {} response. {}",
155-
req.head().method,
156-
error,
157-
message
158-
);
159-
160-
response
161-
}
162-
163130
pub(crate) async fn version_not_supported(
164131
req: HttpRequest,
165132
version: web::Path<APIVersion>,
@@ -288,12 +255,6 @@ mod tests {
288255
test_default(web::resource("/").to(api_default), "GET, POST").await
289256
}
290257

291-
#[actix_rt::test]
292-
async fn test_notifications_default() {
293-
test_default(web::resource("/").to(notifications_default), "POST")
294-
.await
295-
}
296-
297258
#[actix_rt::test]
298259
async fn test_agent_default() {
299260
test_default(web::resource("/").to(agent_default), "GET").await

keylime-agent/src/main.rs

+3-10
Original file line numberDiff line numberDiff line change
@@ -910,16 +910,9 @@ async fn main() -> Result<()> {
910910
keys_handler::configure_keys_endpoints,
911911
))
912912
.service(
913-
web::scope("/notifications")
914-
.service(web::resource("/revocation").route(
915-
web::post().to(
916-
notifications_handler::revocation,
917-
),
918-
))
919-
.default_service(web::to(
920-
errors_handler::notifications_default,
921-
)),
922-
)
913+
web::scope("/notifications").configure(
914+
notifications_handler::configure_notifications_endpoints,
915+
))
923916
.service(web::scope("/quotes").configure(
924917
quotes_handler::configure_quotes_endpoints,
925918
))

keylime-agent/src/notifications_handler.rs

+84-3
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ use crate::{
66
revocation::{Revocation, RevocationMessage},
77
Error, QuoteData, Result,
88
};
9-
use actix_web::{web, HttpRequest, HttpResponse, Responder};
9+
use actix_web::{http, web, HttpRequest, HttpResponse, Responder};
1010
use log::*;
1111
use serde::{Deserialize, Serialize};
1212
use std::path::{Path, PathBuf};
1313

1414
// This is Revocation request from the cloud verifier via REST API
15-
pub(crate) async fn revocation(
15+
async fn revocation(
1616
body: web::Json<Revocation>,
1717
req: HttpRequest,
1818
data: web::Data<QuoteData>,
@@ -35,16 +35,97 @@ pub(crate) async fn revocation(
3535
}
3636
}
3737

38+
async fn notifications_default(req: HttpRequest) -> impl Responder {
39+
let error;
40+
let response;
41+
let message;
42+
43+
match req.head().method {
44+
http::Method::POST => {
45+
error = 400;
46+
message = "URI not supported, only /revocation is supported for POST in /notifications/ interface";
47+
response = HttpResponse::BadRequest()
48+
.json(JsonWrapper::error(error, message));
49+
}
50+
_ => {
51+
error = 405;
52+
message = "Method is not supported in /notifications/ interface";
53+
response = HttpResponse::MethodNotAllowed()
54+
.insert_header(http::header::Allow(vec![http::Method::POST]))
55+
.json(JsonWrapper::error(error, message));
56+
}
57+
};
58+
59+
warn!(
60+
"{} returning {} response. {}",
61+
req.head().method,
62+
error,
63+
message
64+
);
65+
66+
response
67+
}
68+
69+
/// Configure the endpoints for the /notifications scope
70+
pub(crate) fn configure_notifications_endpoints(
71+
cfg: &mut web::ServiceConfig,
72+
) {
73+
_ = cfg
74+
.service(
75+
web::resource("/revocation").route(web::post().to(revocation)),
76+
)
77+
.default_service(web::to(notifications_default));
78+
}
79+
3880
#[cfg(test)]
3981
mod tests {
4082
use super::*;
4183
use crate::common::API_VERSION;
4284
use actix_rt::Arbiter;
4385
use actix_web::{test, web, App};
44-
use serde_json::json;
86+
use serde_json::{json, Value};
4587
use std::{fs, path::Path};
4688
use tokio::sync::mpsc;
4789

90+
#[actix_rt::test]
91+
async fn test_notifications_default() {
92+
let mut app = test::init_service(
93+
App::new().service(web::resource("/").to(notifications_default)),
94+
)
95+
.await;
96+
97+
let req = test::TestRequest::post()
98+
.uri("/")
99+
.data("some data")
100+
.to_request();
101+
102+
let resp = test::call_service(&app, req).await;
103+
assert!(resp.status().is_client_error());
104+
105+
let result: JsonWrapper<Value> = test::read_body_json(resp).await;
106+
107+
assert_eq!(result.results, json!({}));
108+
assert_eq!(result.code, 400);
109+
110+
let req = test::TestRequest::delete().uri("/").to_request();
111+
112+
let resp = test::call_service(&app, req).await;
113+
assert!(resp.status().is_client_error());
114+
115+
let headers = resp.headers();
116+
117+
assert!(headers.contains_key("allow"));
118+
assert_eq!(
119+
headers.get("allow").unwrap().to_str().unwrap(), //#[allow_ci]
120+
"POST"
121+
);
122+
123+
let result: JsonWrapper<Value> = test::read_body_json(resp).await;
124+
125+
assert_eq!(result.results, json!({}));
126+
assert_eq!(result.code, 405);
127+
}
128+
48129
#[cfg(feature = "testing")]
49130
#[actix_rt::test]
50131
async fn test_revocation() {

0 commit comments

Comments
 (0)