Skip to content

Commit

Permalink
improve logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Marketen committed Mar 28, 2024
1 parent e023e0b commit 59256c0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ services:
MONGO_DB_URI: "mongodb://${MONGO_INITDB_ROOT_USERNAME}:${MONGO_INITDB_ROOT_PASSWORD}@mongo:27017"
MONGO_DB_NAME: "${MONGO_DB_NAME}"
API_PORT: "${API_PORT}"
LOG_LEVEL: "${LOG_LEVEL}"
depends_on:
- mongo

Expand Down
15 changes: 13 additions & 2 deletions listener/src/api/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"net/http"

"github.com/dappnode/validator-monitoring/listener/src/logger"
"github.com/dappnode/validator-monitoring/listener/src/types"
"go.mongodb.org/mongo-driver/bson"
)
Expand All @@ -14,9 +15,11 @@ func (s *httpApi) handleRoot(w http.ResponseWriter, r *http.Request) {
}

func (s *httpApi) handleSignature(w http.ResponseWriter, r *http.Request) {
logger.Debug("Received new POST '/signature' request")
var req types.SignatureRequest
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
logger.Error("Failed to decode request body" + err.Error())
s.respondError(w, http.StatusBadRequest, "Invalid request")
return
}
Expand All @@ -25,35 +28,42 @@ func (s *httpApi) handleSignature(w http.ResponseWriter, r *http.Request) {
// we expect network and label to be strings, signature to be a 0x prefixed hex string of 194 characters
// and payload to be a 0x prefixed hex string.
if req.Network == "" || req.Label == "" || req.Signature == "" || req.Payload == "" {
logger.Debug("Invalid request, missing fields. Request had the following fields: " + req.Network + " " + req.Label + " " + req.Signature + " " + req.Payload)
s.respondError(w, http.StatusBadRequest, "Invalid request, missing fields")
return
}

// validate the signature
if !validateSignature(req.Signature) {
logger.Debug("Invalid signature. It should be a 0x prefixed hex string of 194 characters. Request had the following signature: " + req.Signature)
s.respondError(w, http.StatusBadRequest, "Invalid signature")
return
}

// Decode BASE64 payload
decodedBytes, err := base64.StdEncoding.DecodeString(req.Payload)
if err != nil {
logger.Error("Failed to decode BASE64 payload" + err.Error())
s.respondError(w, http.StatusBadRequest, "Invalid BASE64 payload")
return
}
var decodedPayload types.DecodedPayload
err = json.Unmarshal(decodedBytes, &decodedPayload)
if err != nil {
logger.Error("Failed to decode JSON payload" + err.Error())
s.respondError(w, http.StatusBadRequest, "Invalid payload format")
return
}

// Validate the decoded payload (maybe we should be more strict here)
if decodedPayload.Platform != "dappnode" || decodedPayload.Timestamp == "" || decodedPayload.Pubkey == "" {
logger.Debug("Invalid payload content. Request had the following payload: " + decodedPayload.Platform + " " + decodedPayload.Timestamp + " " + decodedPayload.Pubkey)
s.respondError(w, http.StatusBadRequest, "Payload content is invalid")
return
}

logger.Debug("Request's payload decoded successfully. Inserting decoded message into MongoDB")

// Insert into MongoDB
_, err = s.dbCollection.InsertOne(r.Context(), bson.M{
"platform": decodedPayload.Platform,
Expand All @@ -64,11 +74,12 @@ func (s *httpApi) handleSignature(w http.ResponseWriter, r *http.Request) {
"label": req.Label,
})
if err != nil {
logger.Error("Failed to insert message into MongoDB" + err.Error())
s.respondError(w, http.StatusInternalServerError, "Failed to insert payload into MongoDB")
return
}

s.respondOK(w, "Payload validated and inserted into MongoDB")
logger.Info("A new message with pubkey " + decodedPayload.Pubkey + " was decoded and inserted into MongoDB successfully")
s.respondOK(w, "Message validated and inserted into MongoDB")

}

Expand Down

0 comments on commit 59256c0

Please sign in to comment.