Skip to content

Conversation

@gusinacio
Copy link
Contributor

Signed-off-by: Gustavo Inacio [email protected]Signed-off-by: Gustavo Inacio [email protected]

@gusinacio gusinacio changed the title refactor: add inject attestation signer refactor: add attestation middleware Nov 22, 2024
@gusinacio gusinacio force-pushed the gustavo/tap-308-create-middleware-for-attestation branch 2 times, most recently from 7d8ebcb to 490fe8f Compare November 22, 2024 19:54
Base automatically changed from gustavo/tap-307-create-middleware-for-auth to main November 22, 2024 20:00
@gusinacio gusinacio force-pushed the gustavo/tap-308-create-middleware-for-attestation branch from 490fe8f to 4db0df2 Compare November 22, 2024 22:29
@coveralls
Copy link

coveralls commented Nov 22, 2024

Pull Request Test Coverage Report for Build 12013926275

Details

  • 178 of 223 (79.82%) changed or added relevant lines in 6 files are covered.
  • 2 unchanged lines in 2 files lost coverage.
  • Overall coverage increased (+1.1%) to 77.197%

Changes Missing Coverage Covered Lines Changed/Added Lines %
crates/service/src/error.rs 1 2 50.0%
crates/service/src/service.rs 0 1 0.0%
crates/service/src/middleware/attestation.rs 103 106 97.17%
crates/service/src/service/indexer_service.rs 0 10 0.0%
crates/service/src/routes/request_handler.rs 0 30 0.0%
Files with Coverage Reduction New Missed Lines %
crates/service/src/service.rs 1 0.0%
crates/service/src/tap/checks/deny_list_check.rs 1 98.15%
Totals Coverage Status
Change from base Build 11979296541: 1.1%
Covered Lines: 6554
Relevant Lines: 8490

💛 - Coveralls

@gusinacio gusinacio force-pushed the gustavo/tap-308-create-middleware-for-attestation branch from 91130fd to 36de277 Compare November 23, 2024 00:02
@gusinacio gusinacio marked this pull request as ready for review November 23, 2024 00:04
Copy link
Member

@anirudh2 anirudh2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks mostly good! Just fix the docstring!

attestation: Option<Attestation>,
}

/// Check if the query can be attestable generates attestation
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can be attestable generates attestation

This is worded strangely. I think you meant to say "is attestable?"

let app = Router::new().route("/", get(handle)).layer(middleware);

// with signer
let res = app
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You use this same code three times in a row in the 3 tests in this file, I suggest you do the change below. Should economize a few lines.

// add helper send_request function
async fn send_test_request(app: &Router, signer: Option<AttestationSigner>) -> Response<Body> {
        let mut request = Request::builder().uri("/").body(Body::empty()).unwrap();

        if let Some(s) = signer {
            request.extensions_mut().insert(s);
        }

        app.clone().oneshot(request).await.unwrap()
    }

//then substitute according in the tests:
    #[tokio::test]
    async fn test_create_attestation() {
        let (allocation, signer) = allocation_signer();
        let middleware = from_fn(attestation_middleware);

        let handle = move |_: Request<Body>| async move {
            let mut res = Response::new(RESPONSE.to_string());
            res.extensions_mut().insert(AttestationInput::Attestable {
                req: REQUEST.to_string(),
            });
            res
        };

        let app = Router::new().route("/", get(handle)).layer(middleware);

        // Use helper function to send request
        let res = send_test_request(&app, Some(signer.clone())).await;
        assert_eq!(res.status(), StatusCode::OK);

        let response = payload_from_response(res).await;
        assert_eq!(response.graphql_response, RESPONSE.to_string());

        let attestation = response.attestation.unwrap();
        assert!(signer
            .verify(&attestation, REQUEST, RESPONSE, &allocation.id)
            .is_ok());
    }

    #[tokio::test]
    async fn test_non_assignable() {
        let (_, signer) = allocation_signer();
        let handle = move |_: Request<Body>| async move { Response::new(RESPONSE.to_string()) };

        let middleware = from_fn(attestation_middleware);
        let app = Router::new().route("/", get(handle)).layer(middleware);

        // Use helper function to send request
        let res = send_test_request(&app, Some(signer.clone())).await;
        assert_eq!(res.status(), StatusCode::OK);

        let response = payload_from_response(res).await;
        assert_eq!(response.graphql_response, RESPONSE.to_string());
        assert!(response.attestation.is_none());
    }

    #[tokio::test]
    async fn test_no_signer() {
        let handle = move |_: Request<Body>| async move { Response::new(RESPONSE.to_string()) };

        let middleware = from_fn(attestation_middleware);
        let app = Router::new().route("/", get(handle)).layer(middleware);

        // Use helper function to send request without signer
        let res = send_test_request(&app, None).await;
        assert_eq!(res.status(), StatusCode::INTERNAL_SERVER_ERROR);
    }

Copy link
Member

@anirudh2 anirudh2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good from my side. Once Pedro is good, feel free to merge!

@gusinacio gusinacio merged commit 40e7612 into main Nov 25, 2024
10 checks passed
@gusinacio gusinacio deleted the gustavo/tap-308-create-middleware-for-attestation branch November 25, 2024 17:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants