Skip to content

Commit

Permalink
Webhook processing: 202 response with empty body (#198)
Browse files Browse the repository at this point in the history
* Refactor webhook accept

* Import HttpStatus
  • Loading branch information
gcatanese authored Mar 8, 2024
1 parent 22a0811 commit e7add55
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
Expand Down Expand Up @@ -42,7 +43,7 @@ public WebhookController(ApplicationProperty applicationProperty) {

/**
* Process incoming Webhook notification: get NotificationRequestItem, validate HMAC signature,
* consume the event asynchronously, send response ["accepted"]
* consume the event asynchronously, send response status 202
*
* @param json Payload of the webhook event
* @return
Expand Down Expand Up @@ -71,24 +72,25 @@ public ResponseEntity<String> webhooks(@RequestBody String json) throws Exceptio
consumeEvent(item);

} else {
// invalid HMAC signature: do not send [accepted] response
// invalid HMAC signature
log.warn("Could not validate HMAC signature for incoming webhook message: {}", item);
throw new RuntimeException("Invalid HMAC signature");
}
} catch (SignatureException e) {
// Unexpected error during HMAC validation: do not send [accepted] response
// Unexpected error during HMAC validation
log.error("Error while validating HMAC Key", e);
throw new SignatureException(e);
}

} else {
// Unexpected event with no payload: do not send [accepted] response
// Unexpected event with no payload
log.warn("Empty NotificationItem");
throw new Exception("empty");
throw new Exception("empty payload");
}

// Acknowledge event has been consumed
return ResponseEntity.ok().body("[accepted]");
return ResponseEntity.status(HttpStatus.ACCEPTED).build();

}

// process payload asynchronously
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
Expand Down Expand Up @@ -39,7 +40,7 @@ public WebhookResource(ApplicationProperty applicationProperty) {

/**
* Process incoming Webhook notification: get NotificationRequestItem, validate HMAC signature,
* consume the event asynchronously, send response ["accepted"]
* consume the event asynchronously, send response status 202
*
* @param json Payload of the webhook event
* @return
Expand Down Expand Up @@ -70,24 +71,24 @@ public ResponseEntity<String> webhooks(@RequestBody String json) throws Exceptio
consumeEvent(item);

} else {
// invalid HMAC signature: do not send [accepted] response
// invalid HMAC signature
log.warn("Could not validate HMAC signature for incoming webhook message: {}", item);
throw new RuntimeException("Invalid HMAC signature");
}
} catch (SignatureException e) {
// Unexpected error during HMAC validation: do not send [accepted] response
// Unexpected error during HMAC validation
log.error("Error while validating HMAC Key", e);
throw new SignatureException(e);
}

} else {
// Unexpected event with no payload: do not send [accepted] response
// Unexpected event with no payload
log.warn("Empty NotificationItem");
throw new Exception("empty");
throw new Exception("empty payload");
}

// Acknowledge event has been consumed
return ResponseEntity.ok().body("[accepted]");
return ResponseEntity.status(HttpStatus.ACCEPTED).build();
}

// process payload asynchronously
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
Expand Down Expand Up @@ -39,7 +40,7 @@ public WebhookResource(ApplicationProperty applicationProperty) {

/**
* Process the incoming Webhook event: get NotificationRequestItem, validate HMAC signature,
* consume the event asynchronously, send response ["accepted"]
* consume the event asynchronously, send response status 202
*
* @param json Payload of the webhook event
* @return
Expand Down Expand Up @@ -69,24 +70,24 @@ public ResponseEntity<String> webhooks(@RequestBody String json) throws Exceptio
consumeEvent(item);

} else {
// invalid HMAC signature: do not send [accepted] response
// invalid HMAC signature
log.warn("Could not validate HMAC signature for incoming webhook message: {}", item);
throw new RuntimeException("Invalid HMAC signature");
}
} catch (SignatureException e) {
// Unexpected error during HMAC validation: do not send [accepted] response
// Unexpected error during HMAC validation
log.error("Error while validating HMAC Key", e);
throw new SignatureException(e);
}

} else {
// Unexpected event with no payload: do not send [accepted] response
// Unexpected event with no payload
log.warn("Empty NotificationItem");
throw new Exception("empty");
throw new Exception("Event with empty payload");
}

// Acknowledge event has been consumed
return ResponseEntity.ok().body("[accepted]");
return ResponseEntity.status(HttpStatus.ACCEPTED).build();
}

// process payload asynchronously
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
Expand Down Expand Up @@ -39,7 +40,7 @@ public WebhookResource(ApplicationProperty applicationProperty) {

/**
* Process the incoming Webhook event: get NotificationRequestItem, validate HMAC signature,
* consume the event asynchronously, send response ["accepted"]
* consume the event asynchronously, send response status 202
*
* @param json Payload of the webhook event
* @return
Expand All @@ -59,7 +60,7 @@ public ResponseEntity<String> webhooks(@RequestBody String json) throws Exceptio

try {
if (!getHmacValidator().validateHMAC(item, this.applicationProperty.getHmacKey())) {
// invalid HMAC signature: do not send [accepted] response
// invalid HMAC signature
log.warn("Could not validate HMAC signature for incoming webhook message: {}", item);
throw new RuntimeException("Invalid HMAC signature");
}
Expand Down Expand Up @@ -105,19 +106,19 @@ public ResponseEntity<String> webhooks(@RequestBody String json) throws Exceptio
}

} catch (SignatureException e) {
// Unexpected error during HMAC validation: do not send [accepted] response
// Unexpected error during HMAC validation
log.error("Error while validating HMAC Key", e);
throw new SignatureException(e);
}

} else {
// Unexpected event with no payload: do not send [accepted] response
// Unexpected event with no payload
log.warn("Empty NotificationItem");
throw new Exception("empty");
throw new Exception("empty payload");
}

// Acknowledge event has been consumed
return ResponseEntity.ok().body("[accepted]");
return ResponseEntity.status(HttpStatus.ACCEPTED).build();
}

// process payload asynchronously
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
Expand Down Expand Up @@ -66,12 +67,12 @@ public ResponseEntity<String> webhooks(@RequestBody String json) throws IOExcept
consumeEvent(item);

} else {
// Unexpected event with no payload: do not send [accepted] response
// Unexpected event with no payload
log.warn("Empty NotificationItem");
}

// Acknowledge event has been consumed
return ResponseEntity.ok().body("[accepted]");
return ResponseEntity.status(HttpStatus.ACCEPTED).build();
}

// process payload asynchronously
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
Expand Down Expand Up @@ -39,7 +40,7 @@ public WebhookResource(ApplicationProperty applicationProperty) {

/**
* Process incoming Webhook notification: get NotificationRequestItem, validate HMAC signature,
* consume the event asynchronously, send response ["accepted"]
* consume the event asynchronously, send response status 202
*
* @param json Payload of the webhook event
* @return
Expand Down Expand Up @@ -70,24 +71,24 @@ public ResponseEntity<String> webhooks(@RequestBody String json) throws Exceptio
consumeEvent(item);

} else {
// invalid HMAC signature: do not send [accepted] response
// invalid HMAC signature
log.warn("Could not validate HMAC signature for incoming webhook message: {}", item);
throw new RuntimeException("Invalid HMAC signature");
}
} catch (SignatureException e) {
// Unexpected error during HMAC validation: do not send [accepted] response
// Unexpected error during HMAC validation
log.error("Error while validating HMAC Key", e);
throw new SignatureException(e);
}

} else {
// Unexpected event with no payload: do not send [accepted] response
// Unexpected event with no payload
log.warn("Empty NotificationItem");
throw new Exception("empty");
throw new Exception("empty payload");
}

// Acknowledge event has been consumed
return ResponseEntity.ok().body("[accepted]");
return ResponseEntity.status(HttpStatus.ACCEPTED).build();
}

// process payload asynchronously
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

Expand Down Expand Up @@ -34,7 +35,7 @@ public WebhookController(ApplicationProperty applicationProperty) {

/**
* Process the incoming Webhook event: get NotificationRequestItem, validate HMAC signature,
* consume the event asynchronously, send response ["accepted"]
* consume the event asynchronously, send response status 202
*
* @param json Payload of the webhook event
* @return
Expand All @@ -54,7 +55,7 @@ public ResponseEntity<String> webhooks(@RequestBody String json) throws IOExcept

try {
if (!getHmacValidator().validateHMAC(item, this.applicationProperty.getHmacKey())) {
// invalid HMAC signature: do not send [accepted] response
// invalid HMAC signature
log.warn("Could not validate HMAC signature for incoming webhook message: {}", item);
throw new RuntimeException("Invalid HMAC signature");
}
Expand Down Expand Up @@ -85,15 +86,15 @@ public ResponseEntity<String> webhooks(@RequestBody String json) throws IOExcept
}

} catch (SignatureException e) {
// Unexpected error during HMAC validation: do not send [accepted] response
// Unexpected error during HMAC validation
log.error("Error while validating HMAC Key", e);
throw new RuntimeException(e.getMessage());
}

}

// Acknowledge event has been consumed
return ResponseEntity.ok().body("[accepted]");
return ResponseEntity.status(HttpStatus.ACCEPTED).build();
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
Expand Down Expand Up @@ -40,7 +41,7 @@ public WebhookController(ApplicationProperty applicationProperty) {

/** Process incoming Webhook event: get NotificationRequestItem, validate HMAC signature,
* Process the incoming Webhook: get NotificationRequestItem, validate HMAC signature,
* consume the event asynchronously, send response ["accepted"]
* consume the event asynchronously, send response status 202
*
* @param json Payload of the webhook event
* @return
Expand Down Expand Up @@ -70,24 +71,24 @@ public ResponseEntity<String> webhooks(@RequestBody String json) throws Exceptio
consumeEvent(item);

} else {
// invalid HMAC signature: do not send [accepted] response
// invalid HMAC signature
log.warn("Could not validate HMAC signature for incoming webhook message: {}", item);
throw new RuntimeException("Invalid HMAC signature");
}
} catch (SignatureException e) {
// Unexpected error during HMAC validation: do not send [accepted] response
// Unexpected error during HMAC validation
log.error("Error while validating HMAC Key", e);
throw new SignatureException(e);
}

} else {
// Unexpected event with no payload: do not send [accepted] response
// Unexpected event with no payload
log.warn("Empty NotificationItem");
throw new Exception("empty");
}

// Acknowledge event has been consumed
return ResponseEntity.ok().body("[accepted]");
return ResponseEntity.status(HttpStatus.ACCEPTED).build();
}

// process payload asynchronously
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

Expand Down Expand Up @@ -37,7 +38,7 @@ public WebhookResource(ApplicationProperty applicationProperty) {

/**
* Process the incoming Webhook event: get NotificationRequestItem, validate HMAC signature,
* consume the event asynchronously, send response ["accepted"]
* consume the event asynchronously, send response status 202
*
* @param json Payload of the webhook event
* @return
Expand All @@ -57,7 +58,7 @@ public ResponseEntity<String> webhooks(@RequestBody String json) throws IOExcept

try {
if (!getHmacValidator().validateHMAC(item, this.applicationProperty.getHmacKey())) {
// invalid HMAC signature: do not send [accepted] response
// invalid HMAC signature
log.warn("Could not validate HMAC signature for incoming webhook message: {}", item);
throw new RuntimeException("Invalid HMAC signature");
}
Expand Down Expand Up @@ -86,15 +87,15 @@ public ResponseEntity<String> webhooks(@RequestBody String json) throws IOExcept
}

} catch (SignatureException e) {
// Unexpected error during HMAC validation: do not send [accepted] response
// Unexpected error during HMAC validation
log.error("Error while validating HMAC Key", e);
throw new RuntimeException(e.getMessage());
}

}

// Acknowledge event has been consumed
return ResponseEntity.ok().body("[accepted]");
return ResponseEntity.status(HttpStatus.ACCEPTED).build();
}


Expand Down

0 comments on commit e7add55

Please sign in to comment.