Skip to content

Commit

Permalink
chore: cleanup error messaging & logging in appgate server
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanchriswhite committed Nov 14, 2023
1 parent e13211e commit cf5910f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 34 deletions.
1 change: 1 addition & 0 deletions pkg/appgateserver/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ var (
ErrAppGateMissingSigningInformation = sdkerrors.Register(codespace, 5, "missing app client signing information")
ErrAppGateMissingListeningEndpoint = sdkerrors.Register(codespace, 6, "missing app client listening endpoint")
ErrAppGateEmptyRelayResponse = sdkerrors.Register(codespace, 7, "empty relay response")
ErrAppGateHandleRelay = sdkerrors.Register(codespace, 8, "internal error handling relay request")
)
49 changes: 15 additions & 34 deletions pkg/appgateserver/jsonrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ func (app *appGateServer) handleJSONRPCRelay(
// Read the request body bytes.
payloadBz, err := io.ReadAll(request.Body)
if err != nil {
log.Println("ERROR: Failed reading relay request body")
return err
return ErrAppGateHandleRelay.Wrapf("reading relay request body", err)

Check failure on line 28 in pkg/appgateserver/jsonrpc.go

View workflow job for this annotation

GitHub Actions / build

(*cosmossdk.io/errors.Error).Wrapf call has arguments but no formatting directives
}
log.Printf("DEBUG: relay request body: %s", string(payloadBz))

Expand All @@ -41,16 +40,14 @@ func (app *appGateServer) handleJSONRPCRelay(

session, err := app.getCurrentSession(ctx, appAddress, serviceId)
if err != nil {
log.Println("ERROR: Failed getting current session")
return err
return ErrAppGateHandleRelay.Wrapf("getting current session: %s", err)
}
log.Printf("DEBUG: Current session ID: %s", session.SessionId)

// Get a supplier URL and address for the given service and session.
supplierUrl, supplierAddress, err := app.getRelayerUrl(ctx, serviceId, sharedtypes.RPCType_JSON_RPC, session)
if err != nil {
log.Println("ERROR: Failed getting relayer URL")
return err
return ErrAppGateHandleRelay.Wrapf("getting supplier URL: %s", err)
}

// Create the relay request.
Expand All @@ -65,40 +62,32 @@ func (app *appGateServer) handleJSONRPCRelay(
// Get the application's signer.
signer, err := app.getRingSingerForAppAddress(ctx, appAddress)
if err != nil {
log.Println("ERROR: Failed getting signer")
return err
return ErrAppGateHandleRelay.Wrapf("getting signer", err)

Check failure on line 65 in pkg/appgateserver/jsonrpc.go

View workflow job for this annotation

GitHub Actions / build

(*cosmossdk.io/errors.Error).Wrapf call has arguments but no formatting directives
}

// Hash and sign the request's signable bytes.
log.Printf("DEBUG: Signing relay request...")
signableBz, err := relayRequest.GetSignableBytes()
if err != nil {
log.Println("ERROR: Failed getting signable bytes")
return err
return ErrAppGateHandleRelay.Wrapf("getting signable bytes", err)

Check failure on line 71 in pkg/appgateserver/jsonrpc.go

View workflow job for this annotation

GitHub Actions / build

(*cosmossdk.io/errors.Error).Wrapf call has arguments but no formatting directives
}

hash := crypto.Sha256(signableBz)
signature, err := signer.Sign(hash)
if err != nil {
log.Println("ERROR: Failed signing relay request")
return err
return ErrAppGateHandleRelay.Wrapf("signing relay", err)

Check failure on line 77 in pkg/appgateserver/jsonrpc.go

View workflow job for this annotation

GitHub Actions / build

(*cosmossdk.io/errors.Error).Wrapf call has arguments but no formatting directives
}
relayRequest.Meta.Signature = signature

// log.Printf("DEBUG: relayRequest: %+v", relayRequest)

// Marshal the relay request to bytes and create a reader to be used as an HTTP request body.
relayRequestBz, err := cdc.Marshal(relayRequest)
if err != nil {
log.Println("ERROR: Failed marshaling relay request")
return err
return ErrAppGateHandleRelay.Wrapf("marshalling relay request", err)

Check failure on line 84 in pkg/appgateserver/jsonrpc.go

View workflow job for this annotation

GitHub Actions / build

(*cosmossdk.io/errors.Error).Wrapf call has arguments but no formatting directives

Check warning on line 84 in pkg/appgateserver/jsonrpc.go

View workflow job for this annotation

GitHub Actions / misspell

[misspell] pkg/appgateserver/jsonrpc.go#L84

"marshalling" is a misspelling of "marshaling"
Raw output
./pkg/appgateserver/jsonrpc.go:84:38: "marshalling" is a misspelling of "marshaling"
}
relayRequestReader := io.NopCloser(bytes.NewReader(relayRequestBz))
var relayReq types.RelayRequest
if err := relayReq.Unmarshal(relayRequestBz); err != nil {
return err
return ErrAppGateHandleRelay.Wrapf("unmarshalling relay response", err)

Check failure on line 89 in pkg/appgateserver/jsonrpc.go

View workflow job for this annotation

GitHub Actions / build

(*cosmossdk.io/errors.Error).Wrapf call has arguments but no formatting directives
}
// log.Printf("DEBUG: Signed relay request: %+v", relayReq)

// Create the HTTP request to send the request to the relayer.
relayHTTPRequest := &http.Request{
Expand All @@ -108,29 +97,23 @@ func (app *appGateServer) handleJSONRPCRelay(
Body: relayRequestReader,
}

// log.Printf("DEBUG: relayHTTPRequest: %+v", relayHTTPRequest)

// Perform the HTTP request to the relayer.
log.Printf("DEBUG: Sending signed relay request to %s", supplierUrl)
relayHTTPResponse, err := http.DefaultClient.Do(relayHTTPRequest)
if err != nil {
log.Println("ERROR: Failed sending relay request to relayer")
return err
return ErrAppGateHandleRelay.Wrapf("sending relay request", err)

Check failure on line 104 in pkg/appgateserver/jsonrpc.go

View workflow job for this annotation

GitHub Actions / build

(*cosmossdk.io/errors.Error).Wrapf call has arguments but no formatting directives
}

// Read the response body bytes.
log.Printf("DEBUG: Reading relay response body...")
relayResponseBz, err := io.ReadAll(relayHTTPResponse.Body)
if err != nil {
log.Println("ERROR: Failed reading relay response body")
return err
return ErrAppGateHandleRelay.Wrapf("reading relay response body", err)

Check failure on line 110 in pkg/appgateserver/jsonrpc.go

View workflow job for this annotation

GitHub Actions / build

(*cosmossdk.io/errors.Error).Wrapf call has arguments but no formatting directives
}

// Unmarshal the response bytes into a RelayResponse.
relayResponse := &types.RelayResponse{}
if err := relayResponse.Unmarshal(relayResponseBz); err != nil {
log.Println("ERROR: Failed unmarshaling relay response")
return err
return ErrAppGateHandleRelay.Wrapf("unmarshalling relay response", err)

Check failure on line 116 in pkg/appgateserver/jsonrpc.go

View workflow job for this annotation

GitHub Actions / build

(*cosmossdk.io/errors.Error).Wrapf call has arguments but no formatting directives
}

// Verify the response signature. We use the supplier address that we got from
Expand All @@ -140,22 +123,20 @@ func (app *appGateServer) handleJSONRPCRelay(
// TODO_IMPROVE: Add more logging & telemetry so we can get visibility and signal into
// failed responses.
if err := app.verifyResponse(ctx, supplierAddress, relayResponse); err != nil {
log.Println("ERROR: Failed verifying relay response signature")
return err
// TODO_DISCUSS: should this be its own error type and asserted against in tests?
return ErrAppGateHandleRelay.Wrapf("verifying relay response signature", err)

Check failure on line 127 in pkg/appgateserver/jsonrpc.go

View workflow job for this annotation

GitHub Actions / build

(*cosmossdk.io/errors.Error).Wrapf call has arguments but no formatting directives
}

// Marshal the response payload to bytes to be sent back to the application.
relayResponsePayloadBz, err := cdc.MarshalJSON(relayResponse.GetJsonRpcPayload())
if err != nil {
log.Println("ERROR: Failed unmarshaling relay response")
return err
return ErrAppGateHandleRelay.Wrapf("unmarshallig relay response", err)
}

// Reply with the RelayResponse payload.
log.Printf("DEBUG: Writing relay response payload: %s", string(relayResponsePayloadBz))
if _, err := writer.Write(relayResponsePayloadBz); err != nil {
log.Println("ERROR: Failed writing relay response payload to writer")
return err
return ErrAppGateHandleRelay.Wrapf("writing relay response payload", err)
}

return nil
Expand Down

0 comments on commit cf5910f

Please sign in to comment.