From 27e49e3bd07612a0f4b2fb966b3fbfbebe16b215 Mon Sep 17 00:00:00 2001 From: Shrey Bansod Date: Mon, 2 Sep 2024 17:07:13 +0530 Subject: [PATCH] Updating changes for DDCCGW-678 --- .../gateway/config/DgcConfigProperties.java | 12 ++ .../gateway/service/did/AzureDidUploader.java | 4 +- .../service/did/DidTrustListService.java | 7 +- .../gateway/service/did/DidUploadInvoker.java | 34 ++++++ .../gateway/service/did/GitDidUploader.java | 106 ++++++++++++++++++ src/main/resources/application.yml | 8 ++ 6 files changed, 166 insertions(+), 5 deletions(-) create mode 100644 src/main/java/eu/europa/ec/dgc/gateway/service/did/DidUploadInvoker.java create mode 100644 src/main/java/eu/europa/ec/dgc/gateway/service/did/GitDidUploader.java diff --git a/src/main/java/eu/europa/ec/dgc/gateway/config/DgcConfigProperties.java b/src/main/java/eu/europa/ec/dgc/gateway/config/DgcConfigProperties.java index 48bbb2f4..38106060 100644 --- a/src/main/java/eu/europa/ec/dgc/gateway/config/DgcConfigProperties.java +++ b/src/main/java/eu/europa/ec/dgc/gateway/config/DgcConfigProperties.java @@ -69,6 +69,7 @@ public static class DidConfig { private Boolean includeFederated = false; private AzureConfig azure; + private GitConfig git = new GitConfig(); private Map contextMapping = new HashMap<>(); @@ -179,4 +180,15 @@ public static class SignerInformation { public static class CountryCodeMap { private Map virtualCountries = new HashMap<>(); } + + @Getter + @Setter + public static class GitConfig { + private String prefix; + private String workdir; + private String pat; + private String url; + private String owner; + private String branch; + } } diff --git a/src/main/java/eu/europa/ec/dgc/gateway/service/did/AzureDidUploader.java b/src/main/java/eu/europa/ec/dgc/gateway/service/did/AzureDidUploader.java index 393a89ca..27923b02 100644 --- a/src/main/java/eu/europa/ec/dgc/gateway/service/did/AzureDidUploader.java +++ b/src/main/java/eu/europa/ec/dgc/gateway/service/did/AzureDidUploader.java @@ -34,11 +34,11 @@ import java.net.InetSocketAddress; import java.text.MessageFormat; import lombok.extern.slf4j.Slf4j; -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; import org.springframework.http.MediaType; import org.springframework.stereotype.Service; -@ConditionalOnProperty(name = "dgc.did.didUploadProvider", havingValue = "azure") +@ConditionalOnExpression("'${dgc.did.didUploadProvider}'.contains('azure')") @Service @Slf4j public class AzureDidUploader implements DidUploader { diff --git a/src/main/java/eu/europa/ec/dgc/gateway/service/did/DidTrustListService.java b/src/main/java/eu/europa/ec/dgc/gateway/service/did/DidTrustListService.java index b62f113a..e70cc786 100644 --- a/src/main/java/eu/europa/ec/dgc/gateway/service/did/DidTrustListService.java +++ b/src/main/java/eu/europa/ec/dgc/gateway/service/did/DidTrustListService.java @@ -87,7 +87,7 @@ public class DidTrustListService { private final ByteSigner byteSigner; - private final DidUploader didUploader; + private final DidUploadInvoker didUploadInvoker; private final ObjectMapper objectMapper; @@ -108,7 +108,7 @@ public void job() { } try { - didUploader.uploadDid(trustList.getBytes(StandardCharsets.UTF_8)); + didUploadInvoker.uploadDid(trustList.getBytes(StandardCharsets.UTF_8)); } catch (Exception e) { log.error("Failed to Upload DID-TrustList: {}", e.getMessage()); return; @@ -129,7 +129,8 @@ public void job() { } try { - didUploader.uploadDid(countryAsSubcontainer, countryTrustList.getBytes(StandardCharsets.UTF_8)); + didUploadInvoker + .uploadDid(countryAsSubcontainer, countryTrustList.getBytes(StandardCharsets.UTF_8)); } catch (Exception e) { log.error("Failed to Upload DID-TrustList for country {} : {}", country, e.getMessage()); } diff --git a/src/main/java/eu/europa/ec/dgc/gateway/service/did/DidUploadInvoker.java b/src/main/java/eu/europa/ec/dgc/gateway/service/did/DidUploadInvoker.java new file mode 100644 index 00000000..5663d935 --- /dev/null +++ b/src/main/java/eu/europa/ec/dgc/gateway/service/did/DidUploadInvoker.java @@ -0,0 +1,34 @@ +package eu.europa.ec.dgc.gateway.service.did; + +import java.util.List; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class DidUploadInvoker { + + @Autowired + List didUploaders; + + /** + * Method invokes the DID document upload of all the DidUploader implementations. + * @param content DID document in byte array form + */ + public void uploadDid(byte[] content) { + for (DidUploader didUploader : didUploaders) { + didUploader.uploadDid(content); + } + } + + /** + * Method invokes the DID document upload of all the DidUploader implementations. + * @param subDirectory is a sub folder + * @param content DID document in byte array form + */ + public void uploadDid(String subDirectory, byte[] content) { + for (DidUploader didUploader : didUploaders) { + didUploader.uploadDid(subDirectory, content); + } + } + +} diff --git a/src/main/java/eu/europa/ec/dgc/gateway/service/did/GitDidUploader.java b/src/main/java/eu/europa/ec/dgc/gateway/service/did/GitDidUploader.java new file mode 100644 index 00000000..a5cb7ed4 --- /dev/null +++ b/src/main/java/eu/europa/ec/dgc/gateway/service/did/GitDidUploader.java @@ -0,0 +1,106 @@ +package eu.europa.ec.dgc.gateway.service.did; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import eu.europa.ec.dgc.gateway.config.DgcConfigProperties; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.Base64; +import java.util.HashMap; +import java.util.Map; +import lombok.extern.slf4j.Slf4j; +import okhttp3.MediaType; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.RequestBody; +import okhttp3.Response; +import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; +import org.springframework.stereotype.Service; + +@ConditionalOnExpression("'${dgc.did.didUploadProvider}'.contains('git')") +@Service +@Slf4j +public class GitDidUploader implements DidUploader { + + private final DgcConfigProperties configProperties; + + public GitDidUploader(DgcConfigProperties configProperties) { + this.configProperties = configProperties; + } + + + private Request prepareRequest(String owner, String repo, String path, String content, String token) + throws JsonProcessingException { + String url = "https://api.github.com/repos/" + owner + "/" + repo + "/contents/" + path; + + // Encode file content to Base64 + String encodedContent = Base64.getEncoder().encodeToString(content.getBytes(StandardCharsets.UTF_8)); + + // Prepare JSON payload + Map jsonMap = new HashMap<>(); + jsonMap.put("message", "Automated commit message"); + jsonMap.put("content", encodedContent); + jsonMap.put("branch", configProperties.getDid().getGit().getBranch()); + + ObjectMapper objectMapper = new ObjectMapper(); + String jsonPayload = objectMapper.writeValueAsString(jsonMap); + + RequestBody body = RequestBody.create(jsonPayload, MediaType.parse("application/json")); + Request request = new Request.Builder() + .url(url) + .header("Authorization", "Bearer " + token) + .header("Accept", "application/vnd.github+json") + .header("X-GitHub-Api-Version", "2022-11-28") + .put(body) + .build(); + return request; + } + + private void uploadFileToGitHub(String owner, String repo, String path, String content, String token) + throws IOException { + OkHttpClient client = new OkHttpClient(); + + Request request = prepareRequest(owner, repo, path, content, token); + + Response response = client.newCall(request).execute(); + if (response.isSuccessful()) { + log.info("File uploaded successfully"); + } else { + log.error("Failed to upload file: " + response.message()); + } + } + + @Override + public void uploadDid(byte[] content) { + String fileContent = new String(content, StandardCharsets.UTF_8); + try { + uploadFileToGitHub(configProperties.getDid().getGit().getOwner(), + configProperties.getDid().getGit().getWorkdir(), + configProperties.getDid().getGit().getPrefix() + "/" + configProperties.getDid().getGit().getUrl(), + fileContent, + configProperties.getDid().getGit().getPat()); + } catch (IOException e) { + log.error("Error occured while uploading a file to Github"); + } + log.info("Upload successful"); + } + + @Override + public void uploadDid(String subContainer, byte[] content) { + + String fileContent = new String(content, StandardCharsets.UTF_8); + try { + uploadFileToGitHub(configProperties.getDid().getGit().getOwner(), + configProperties.getDid().getGit().getWorkdir(), + configProperties.getDid().getGit().getPrefix() + "/" + + subContainer + "/" + configProperties.getDid().getGit().getUrl(), + fileContent, + configProperties.getDid().getGit().getPat()); + } catch (IOException e) { + log.error("Error occured while uploading a file to Github"); + } + log.info("Upload successful"); + } +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index b659aa8d..df843d24 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -102,6 +102,14 @@ dgc: contextMapping: "[https://www.w3.org/ns/did/v1]": did_v1.json "[https://w3id.org/security/suites/jws-2020/v1]": jws-2020_v1.json + didUploadProvider: azure,git + git: + workdir: tng-cdn-dev + prefix: trustlist + url: did.json + pat: + owner: WorldHealthOrganization + branch: main countryCodeMap: virtualCountries: XA: XXA