Skip to content

Commit d26802f

Browse files
committed
agent_handler: Move the /agent scope configuration
Move the /agent scope configuration from main to agent_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 46ab7c9 commit d26802f

File tree

3 files changed

+78
-47
lines changed

3 files changed

+78
-47
lines changed

keylime-agent/src/agent_handler.rs

+75-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
use crate::common::JsonWrapper;
55
use crate::{tpm, Error as KeylimeError, QuoteData};
6-
use actix_web::{web, HttpRequest, HttpResponse, Responder};
6+
use actix_web::{http, web, HttpRequest, HttpResponse, Responder};
77
use base64::{engine::general_purpose, Engine as _};
88
use log::*;
99
use serde::{Deserialize, Serialize};
@@ -19,7 +19,7 @@ pub(crate) struct AgentInfo {
1919

2020
// This is an Info request which gets some information about this keylime agent
2121
// It should return a AgentInfo object as JSON
22-
pub async fn info(
22+
async fn info(
2323
req: HttpRequest,
2424
data: web::Data<QuoteData>,
2525
) -> impl Responder {
@@ -38,12 +38,52 @@ pub async fn info(
3838
HttpResponse::Ok().json(response)
3939
}
4040

41+
/// Configure the endpoints for the /agent scope
42+
async fn agent_default(req: HttpRequest) -> impl Responder {
43+
let error;
44+
let response;
45+
let message;
46+
47+
match req.head().method {
48+
http::Method::GET => {
49+
error = 400;
50+
message = "URI not supported, only /info is supported for GET in /agent interface";
51+
response = HttpResponse::BadRequest()
52+
.json(JsonWrapper::error(error, message));
53+
}
54+
_ => {
55+
error = 405;
56+
message = "Method is not supported in /agent interface";
57+
response = HttpResponse::MethodNotAllowed()
58+
.insert_header(http::header::Allow(vec![http::Method::GET]))
59+
.json(JsonWrapper::error(error, message));
60+
}
61+
};
62+
63+
warn!(
64+
"{} returning {} response. {}",
65+
req.head().method,
66+
error,
67+
message
68+
);
69+
70+
response
71+
}
72+
73+
/// Configure the endpoints for the /agents scope
74+
pub(crate) fn configure_agent_endpoints(cfg: &mut web::ServiceConfig) {
75+
_ = cfg
76+
.service(web::resource("/info").route(web::get().to(info)))
77+
.default_service(web::to(agent_default));
78+
}
79+
4180
#[cfg(test)]
4281
#[cfg(feature = "testing")]
4382
mod tests {
4483
use super::*;
4584
use crate::common::API_VERSION;
4685
use actix_web::{test, web, App};
86+
use serde_json::{json, Value};
4787

4888
#[actix_rt::test]
4989
async fn test_agent_info() {
@@ -73,4 +113,37 @@ mod tests {
73113
assert_eq!(result.results.tpm_enc_alg.as_str(), "rsa");
74114
assert_eq!(result.results.tpm_sign_alg.as_str(), "rsassa");
75115
}
116+
117+
#[actix_rt::test]
118+
async fn test_agents_default() {
119+
let mut app = test::init_service(
120+
App::new().service(web::resource("/").to(agent_default)),
121+
)
122+
.await;
123+
124+
let req = test::TestRequest::get().uri("/").to_request();
125+
126+
let resp = test::call_service(&app, req).await;
127+
assert!(resp.status().is_client_error());
128+
129+
let result: JsonWrapper<Value> = test::read_body_json(resp).await;
130+
131+
assert_eq!(result.results, json!({}));
132+
assert_eq!(result.code, 400);
133+
134+
let req = test::TestRequest::delete().uri("/").to_request();
135+
136+
let resp = test::call_service(&app, req).await;
137+
assert!(resp.status().is_client_error());
138+
139+
let headers = resp.headers();
140+
141+
assert!(headers.contains_key("allow"));
142+
assert_eq!(headers.get("allow").unwrap().to_str().unwrap(), "GET"); //#[allow_ci]
143+
144+
let result: JsonWrapper<Value> = test::read_body_json(resp).await;
145+
146+
assert_eq!(result.results, json!({}));
147+
assert_eq!(result.code, 405);
148+
}
76149
}

keylime-agent/src/errors_handler.rs

-36
Original file line numberDiff line numberDiff line change
@@ -96,37 +96,6 @@ pub(crate) async fn api_default(req: HttpRequest) -> impl Responder {
9696
response
9797
}
9898

99-
pub(crate) async fn agent_default(req: HttpRequest) -> impl Responder {
100-
let error;
101-
let response;
102-
let message;
103-
104-
match req.head().method {
105-
http::Method::GET => {
106-
error = 400;
107-
message = "URI not supported, only /info is supported for GET in /agent interface";
108-
response = HttpResponse::BadRequest()
109-
.json(JsonWrapper::error(error, message));
110-
}
111-
_ => {
112-
error = 405;
113-
message = "Method is not supported in /agent interface";
114-
response = HttpResponse::MethodNotAllowed()
115-
.insert_header(http::header::Allow(vec![http::Method::GET]))
116-
.json(JsonWrapper::error(error, message));
117-
}
118-
};
119-
120-
warn!(
121-
"{} returning {} response. {}",
122-
req.head().method,
123-
error,
124-
message
125-
);
126-
127-
response
128-
}
129-
13099
pub(crate) async fn version_not_supported(
131100
req: HttpRequest,
132101
version: web::Path<APIVersion>,
@@ -255,11 +224,6 @@ mod tests {
255224
test_default(web::resource("/").to(api_default), "GET, POST").await
256225
}
257226

258-
#[actix_rt::test]
259-
async fn test_agent_default() {
260-
test_default(web::resource("/").to(agent_default), "GET").await
261-
}
262-
263227
#[derive(Serialize, Deserialize)]
264228
struct DummyQuery {
265229
param: String,

keylime-agent/src/main.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -897,15 +897,9 @@ async fn main() -> Result<()> {
897897
)
898898
.service(
899899
web::scope(&format!("/{API_VERSION}"))
900-
.service(
901-
web::scope("/agent")
902-
.service(web::resource("/info").route(
903-
web::get().to(agent_handler::info),
904-
))
905-
.default_service(web::to(
906-
errors_handler::agent_default,
907-
)),
908-
)
900+
.service(web::scope("/agent").configure(
901+
agent_handler::configure_agent_endpoints,
902+
))
909903
.service(web::scope("/keys").configure(
910904
keys_handler::configure_keys_endpoints,
911905
))

0 commit comments

Comments
 (0)