Skip to content
This repository has been archived by the owner on Sep 15, 2023. It is now read-only.

Commit

Permalink
sync: delete all existing rules before upload
Browse files Browse the repository at this point in the history
  • Loading branch information
gstoehld committed Feb 23, 2022
1 parent 3ac318b commit 582c528
Showing 1 changed file with 74 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,29 @@
import ch.admin.bag.covidcertificate.backend.verifier.model.sync.SigningPayload;
import ch.admin.bag.covidcertificate.backend.verifier.sync.syncer.model.RulesSyncResult;
import ch.admin.bag.covidcertificate.backend.verifier.sync.utils.CmsUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.HttpStatusCodeException;
import org.springframework.web.client.RestTemplate;

public class DgcRulesClient {
private static final Logger logger = LoggerFactory.getLogger(DgcRulesClient.class);
private static final String RULE_UPLOAD_PATH = "/rules";
private static final String RULE_DELETE_PATH = "/rules/delete";
private static final String DOWNLOAD_PATH = "/rules/%s";
private final String dgcBaseUrl;
private final RestTemplate dgcRT;
Expand All @@ -42,23 +48,85 @@ public DgcRulesClient(String dgcBaseUrl, RestTemplate dgcRT, SigningClient signi
}

/**
* downloads rules for all countries
* downloads rules for Switzerland
*
* @return rules per country
* @return rules for Switezrland
*/
public Map<String, String> download() { // TODO
public Map<String, JsonNode> download() {
logger.info("[DgcRulesClient] Downloading rules");
Map<String, JsonNode> rules = new HashMap<>();
ResponseEntity<String> response = this.dgcRT.exchange(RequestEntity.get(dgcBaseUrl+String.format(DOWNLOAD_PATH, "CH")).headers(CmsUtil.createCmsTextUploadHeaders()).build(),
String.class);
try {
new ObjectMapper().readTree(response.getBody()).fields().forEachRemaining( field -> {
rules.put(field.getKey(), field.getValue());
});
} catch (JsonProcessingException e) {
logger.error("[DgcRulesClient] Failed to deserialize downloaded rules", e);
}

logger.info("[DgcRulesClient] downloaded {} rules", rules.size());
return rules;
}

/**
* deletes all rules for Switzerland
*
* @return successfully deleted rule IDs
*/
public RulesSyncResult deleteAll(){
logger.info("Deleting all Swiss rules");
Map<String, JsonNode> rules = download();
Set<String> ruleIds = rules.keySet();
List<String> deletedRuleIds = new ArrayList<>();
List<String> failedRuleIds = new ArrayList<>();
ruleIds.forEach(ruleId -> {
try {
// sign payload
String cms = null;
try {
logger.info("signing rule ID {}", ruleId);
var base64encoded = Base64.getEncoder().encodeToString(ruleId.getBytes(
StandardCharsets.UTF_8));
var payloadObject = new SigningPayload(base64encoded);
cms = signingClient.sign(payloadObject);
} catch (Exception e) {
logger.error("Signing rule ID {} failed", ruleId, e);
return;
}
// upload to gateway
logger.info("Deleting rule {}", ruleId);
try {
this.dgcRT.exchange(
RequestEntity.post(dgcBaseUrl + RULE_DELETE_PATH)
.headers(CmsUtil.createCmsTextUploadHeaders())
.body(cms),
String.class);
logger.info("All versions of rule {} deleted", ruleId);
deletedRuleIds.add(ruleId);
} catch (HttpStatusCodeException e) {
failedRuleIds.add(ruleId);
logger.error("[FAILED CMS] {}", cms);
logger.error("Deletion of rule {} failed", ruleId, e);
}
} catch (Exception ex) {
failedRuleIds.add(ruleId);
logger.error("Failed to delete rule {}", ruleId, ex);
}

logger.info("[DgcRulesClient] downloaded rules for: ");
return null;
});
logger.info("Finished uploading Swiss rules");
return new RulesSyncResult(deletedRuleIds, failedRuleIds);
}


/**
* downloads rules for all countries
* uploads Swiss rules
*
* @return successfully uploaded rule ids
*/
public RulesSyncResult upload(JsonNode rules) {
deleteAll();
logger.info("Uploading Swiss rules");
List<String> uploadedRuleIds = new ArrayList<>();
List<String> failedRuleIds = new ArrayList<>();
Expand Down

0 comments on commit 582c528

Please sign in to comment.