Skip to content

Commit

Permalink
Added signature for business rules
Browse files Browse the repository at this point in the history
  • Loading branch information
slaurenz committed Jul 28, 2021
1 parent e6119f7 commit 3df4466
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;
Expand Down Expand Up @@ -59,4 +57,7 @@ public class BusinessRuleEntity {
@Lob
@Column(name = "raw_data", nullable = false)
String rawData;

@Column(name = "signature", length = 256)
private String signature;
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@
package eu.europa.ec.dgc.businessrule.repository;

import eu.europa.ec.dgc.businessrule.entity.BusinessRuleEntity;
import eu.europa.ec.dgc.businessrule.entity.ValueSetEntity;
import eu.europa.ec.dgc.businessrule.restapi.dto.BusinessRuleListItemDto;
import eu.europa.ec.dgc.businessrule.restapi.dto.ValueSetListItemDto;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public ResponseEntity<List<BusinessRuleListItemDto>> getRules(
if (rulesList.isPresent()) {
ResponseEntity.BodyBuilder respBuilder = ResponseEntity.ok();
String signature = rulesList.get().getSignature();
if (signature != null & signature.length() > 0) {
if (signature != null && signature.length() > 0) {
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.set(X_SIGNATURE_HEADER, signature);
respBuilder.headers(responseHeaders);
Expand Down Expand Up @@ -232,6 +232,8 @@ public ResponseEntity<String> getRuleByCountryAndHash(
@Valid @PathVariable("country") String country,
@Valid @PathVariable("hash") String hash
) {
ResponseEntity<String> responseEntity;

validateCountryParameter(country);
if (hash == null || hash.isBlank()) {
throw new DgcaBusinessRulesResponseException(HttpStatus.BAD_REQUEST, "0x005", "Possible reasons: "
Expand All @@ -244,7 +246,16 @@ public ResponseEntity<String> getRuleByCountryAndHash(
throw new DgcaBusinessRulesResponseException(HttpStatus.NOT_FOUND, "0x006", "Possible reasons: "
+ "The provided hash or country may not be correct.", "country: " + country + ", hash: " + hash,"");
}
return ResponseEntity.ok(rule.getRawData());

if (rule.getSignature() != null) {
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.set(BusinessRuleController.X_SIGNATURE_HEADER, rule.getSignature());
responseEntity = ResponseEntity.ok().headers(responseHeaders).body(rule.getRawData());
} else {
responseEntity = ResponseEntity.ok(rule.getRawData());
}

return responseEntity;
}

private void validateCountryParameter(String country) throws DgcaBusinessRulesResponseException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public class BusinessRuleService {

private final BusinessRuleRepository businessRuleRepository;
private final ListSigningService listSigningService;
private final Optional<SigningService> signingService;
private final SignedListRepository signedListRepository;

private final BusinessRulesUtils businessRulesUtils;
Expand Down Expand Up @@ -127,6 +128,10 @@ public void saveBusinessRule(BusinessRuleItem rule) {
bre.setVersion(rule.getVersion());
bre.setRawData(rule.getRawData());

if (signingService.isPresent()) {
bre.setSignature(signingService.get().computeSignature(bre.getHash()));
}

businessRuleRepository.save(bre);
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/db/changelog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.3.xsd">
<include file="db/changelog/init_tables.xml"/>
<include file="db/changelog/add_list_table.xml"/>

<include file="db/changelog/add_rule_signature_column.xml"/>
</databaseChangeLog>
15 changes: 15 additions & 0 deletions src/main/resources/db/changelog/add_rule_signature_column.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.3.xsd"
objectQuotingStrategy="QUOTE_ONLY_RESERVED_WORDS">
<changeSet id="add_rule_signature_column" author="slaurenz">
<addColumn tableName="business_rules">
<column name="signature" type="VARCHAR(256)">
<constraints nullable="true" />
</column>
</addColumn>
</changeSet>
</databaseChangeLog>

0 comments on commit 3df4466

Please sign in to comment.