|
| 1 | +pub use attested_tls_proxy::attestation::AttestationGenerator; |
| 2 | +use std::net::SocketAddr; |
| 3 | + |
| 4 | +use anyhow::anyhow; |
| 5 | +use attested_tls_proxy::attestation::{AttestationExchangeMessage, AttestationVerifier}; |
| 6 | +use axum::{ |
| 7 | + extract::{Path, State}, |
| 8 | + http::StatusCode, |
| 9 | + response::{IntoResponse, Response}, |
| 10 | +}; |
| 11 | +use parity_scale_codec::{Decode, Encode}; |
| 12 | +use tokio::net::TcpListener; |
| 13 | + |
| 14 | +#[derive(Clone)] |
| 15 | +struct SharedState { |
| 16 | + attestation_generator: AttestationGenerator, |
| 17 | +} |
| 18 | + |
| 19 | +/// An HTTP server which produces test attestations |
| 20 | +pub async fn dummy_attestation_server( |
| 21 | + listener: TcpListener, |
| 22 | + attestation_generator: AttestationGenerator, |
| 23 | +) -> anyhow::Result<()> { |
| 24 | + let app = axum::Router::new() |
| 25 | + .route("/attest/{input_data}", axum::routing::get(get_attest)) |
| 26 | + .with_state(SharedState { |
| 27 | + attestation_generator, |
| 28 | + }); |
| 29 | + |
| 30 | + axum::serve(listener, app).await?; |
| 31 | + |
| 32 | + Ok(()) |
| 33 | +} |
| 34 | + |
| 35 | +/// Handler for the GET `/attest/{input_data}` route |
| 36 | +/// Input data should be 64 bytes hex |
| 37 | +async fn get_attest( |
| 38 | + State(shared_state): State<SharedState>, |
| 39 | + Path(input_data): Path<String>, |
| 40 | +) -> Result<(StatusCode, Vec<u8>), ServerError> { |
| 41 | + let input_data: [u8; 64] = hex::decode(input_data)? |
| 42 | + .try_into() |
| 43 | + .map_err(|_| anyhow!("Input data must be 64 bytes"))?; |
| 44 | + |
| 45 | + let attestation = shared_state |
| 46 | + .attestation_generator |
| 47 | + .generate_attestation(input_data) |
| 48 | + .await? |
| 49 | + .encode(); |
| 50 | + |
| 51 | + Ok((StatusCode::OK, attestation)) |
| 52 | +} |
| 53 | + |
| 54 | +/// A client helper which makes a request to `/attest` |
| 55 | +pub async fn dummy_attestation_client( |
| 56 | + server_addr: SocketAddr, |
| 57 | + attestation_verifier: AttestationVerifier, |
| 58 | +) -> anyhow::Result<AttestationExchangeMessage> { |
| 59 | + let input_data = [0; 64]; |
| 60 | + let response = reqwest::get(format!( |
| 61 | + "http://{server_addr}/attest/{}", |
| 62 | + hex::encode(input_data) |
| 63 | + )) |
| 64 | + .await? |
| 65 | + .bytes() |
| 66 | + .await?; |
| 67 | + |
| 68 | + let remote_attestation_message = AttestationExchangeMessage::decode(&mut &response[..])?; |
| 69 | + let remote_attestation_type = remote_attestation_message.attestation_type; |
| 70 | + |
| 71 | + println!("Remote attestation type: {remote_attestation_type}"); |
| 72 | + |
| 73 | + attestation_verifier |
| 74 | + .verify_attestation(remote_attestation_message.clone(), input_data) |
| 75 | + .await?; |
| 76 | + |
| 77 | + Ok(remote_attestation_message) |
| 78 | +} |
| 79 | + |
| 80 | +struct ServerError(pub anyhow::Error); |
| 81 | + |
| 82 | +impl<E> From<E> for ServerError |
| 83 | +where |
| 84 | + E: Into<anyhow::Error>, |
| 85 | +{ |
| 86 | + fn from(err: E) -> Self { |
| 87 | + ServerError(err.into()) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +impl IntoResponse for ServerError { |
| 92 | + fn into_response(self) -> Response { |
| 93 | + eprintln!("{:?}", self.0); |
| 94 | + (StatusCode::INTERNAL_SERVER_ERROR, format!("{:?}", self.0)).into_response() |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +#[cfg(test)] |
| 99 | +mod tests { |
| 100 | + use super::*; |
| 101 | + |
| 102 | + #[tokio::test] |
| 103 | + async fn test_dummy_server() { |
| 104 | + let attestation_generator = AttestationGenerator::with_no_attestation(); |
| 105 | + |
| 106 | + let listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); |
| 107 | + let server_addr = listener.local_addr().unwrap(); |
| 108 | + |
| 109 | + tokio::spawn(async move { |
| 110 | + dummy_attestation_server(listener, attestation_generator) |
| 111 | + .await |
| 112 | + .unwrap(); |
| 113 | + }); |
| 114 | + dummy_attestation_client(server_addr, AttestationVerifier::expect_none()) |
| 115 | + .await |
| 116 | + .unwrap(); |
| 117 | + } |
| 118 | +} |
0 commit comments