Skip to content

Commit b56070c

Browse files
committed
feat(api): enhance runtime data handling with encoding support
Added support for encoding options in the runtime data parameter for the /aa/evidence and /aa/additional_evidence endpoints. The new implementation allows clients to specify 'hex', 'base64', or omit the encoding for raw UTF-8 strings. Updated the OpenAPI documentation and added a decoding function to handle the different formats.
1 parent 24c2114 commit b56070c

5 files changed

Lines changed: 60 additions & 10 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api-server-rest/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ async-trait.workspace = true
1111
base64.workspace = true
1212
clap = { workspace = true, features = ["derive"] }
1313
form_urlencoded = "1.2.2"
14+
hex.workspace = true
1415
hyper = { version = "0.14.27", features = ["server", "http1", "runtime"] }
1516
serde.workspace = true
1617
serde_json.workspace = true

api-server-rest/build.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ fn _token() {}
3131
get,
3232
path = "/aa/evidence",
3333
params(
34-
("runtime_data" = String, Query, description = "Runtime Data")
34+
("runtime_data" = String, Query, description = "Runtime Data"),
35+
("encoding" = Option<String>, Query, description = "Encoding of runtime_data: 'hex', 'base64', or omit for raw UTF-8 string")
3536
),
3637
responses(
3738
(status = 200, description = "success response",
@@ -50,12 +51,14 @@ fn _evidence() {}
5051
get,
5152
path = "/aa/additional_evidence",
5253
params(
53-
("runtime_data" = String, Query, description = "Runtime Data")
54+
("runtime_data" = String, Query, description = "Runtime Data"),
55+
("encoding" = Option<String>, Query, description = "Encoding of runtime_data: 'hex', 'base64', or omit for raw UTF-8 string")
5456
),
5557
responses(
5658
(status = 200, description = "success response",
5759
content_type = "application/octet-stream",
58-
body = String),
60+
body = String,
61+
example = json!({"svn":"1","report_data":"eHh4eA=="})),
5962
(status = 400, description = "bad request for invalid query param"),
6063
(status = 403, description = "forbid external access"),
6164
(status = 404, description = "resource not found"),

api-server-rest/openapi/api.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,15 @@
6262
"schema": {
6363
"type": "string"
6464
}
65+
},
66+
{
67+
"name": "encoding",
68+
"in": "query",
69+
"description": "Encoding of runtime_data: 'hex', 'base64', or omit for raw UTF-8 string",
70+
"required": false,
71+
"schema": {
72+
"type": "string"
73+
}
6574
}
6675
],
6776
"responses": {
@@ -71,6 +80,10 @@
7180
"application/octet-stream": {
7281
"schema": {
7382
"type": "string"
83+
},
84+
"example": {
85+
"report_data": "eHh4eA==",
86+
"svn": "1"
7487
}
7588
}
7689
}
@@ -103,6 +116,15 @@
103116
"schema": {
104117
"type": "string"
105118
}
119+
},
120+
{
121+
"name": "encoding",
122+
"in": "query",
123+
"description": "Encoding of runtime_data: 'hex', 'base64', or omit for raw UTF-8 string",
124+
"required": false,
125+
"schema": {
126+
"type": "string"
127+
}
106128
}
107129
],
108130
"responses": {

api-server-rest/src/router.rs

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,27 @@
44
//
55

66
use anyhow::*;
7+
use base64::Engine;
78
use hyper::body::HttpBody;
89
use hyper::{header, Body, Method, Request, Response, StatusCode};
910
use serde::Serialize;
1011
use std::collections::HashMap;
1112
use std::net::SocketAddr;
1213
use tracing::{debug, info};
1314

15+
fn decode_runtime_data(raw: &str, encoding: Option<&str>) -> Result<Vec<u8>> {
16+
match encoding {
17+
Some("hex") => {
18+
hex::decode(raw).map_err(|e| anyhow!("invalid hex in runtime_data: {e}"))
19+
}
20+
Some("base64") => base64::engine::general_purpose::STANDARD
21+
.decode(raw)
22+
.map_err(|e| anyhow!("invalid base64 in runtime_data: {e}")),
23+
Some(other) => bail!("unsupported encoding: {other} (expected hex, base64, or omit)"),
24+
None => Ok(raw.as_bytes().to_vec()),
25+
}
26+
}
27+
1428
use crate::client::{
1529
aa::{
1630
AAClient, AaelEvent, AA_AAEL_URL, AA_ADDITIONAL_EVIDENCE_URL, AA_EVIDENCE_URL, AA_ROOT,
@@ -171,10 +185,14 @@ impl Router {
171185
info!("Get evidence");
172186
match params.get("runtime_data") {
173187
Some(runtime_data) => {
174-
match client
175-
.get_evidence(&runtime_data.clone().into_bytes())
176-
.await
177-
{
188+
let data = match decode_runtime_data(
189+
runtime_data,
190+
params.get("encoding").map(|s| s.as_str()),
191+
) {
192+
std::result::Result::Ok(d) => d,
193+
Err(e) => return self.internal_error(e.to_string()),
194+
};
195+
match client.get_evidence(&data).await {
178196
std::result::Result::Ok(results) => {
179197
return self.octet_stream_response(results)
180198
}
@@ -188,10 +206,15 @@ impl Router {
188206
info!("Get additional evidence");
189207
match params.get("runtime_data") {
190208
Some(runtime_data) => {
209+
let data = match decode_runtime_data(
210+
runtime_data,
211+
params.get("encoding").map(|s| s.as_str()),
212+
) {
213+
std::result::Result::Ok(d) => d,
214+
Err(e) => return self.internal_error(e.to_string()),
215+
};
191216
match client
192-
.get_additional_evidence(
193-
&runtime_data.clone().into_bytes(),
194-
)
217+
.get_additional_evidence(&data)
195218
.await
196219
{
197220
std::result::Result::Ok(results) => {

0 commit comments

Comments
 (0)