Skip to content

Commit

Permalink
Updating changes for DDCCGW-678
Browse files Browse the repository at this point in the history
  • Loading branch information
shreybansod committed Sep 2, 2024
1 parent be69851 commit 27e49e3
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public static class DidConfig {
private Boolean includeFederated = false;

private AzureConfig azure;
private GitConfig git = new GitConfig();

private Map<String, String> contextMapping = new HashMap<>();

Expand Down Expand Up @@ -179,4 +180,15 @@ public static class SignerInformation {
public static class CountryCodeMap {
private Map<String, String> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public class DidTrustListService {

private final ByteSigner byteSigner;

private final DidUploader didUploader;
private final DidUploadInvoker didUploadInvoker;

private final ObjectMapper objectMapper;

Expand All @@ -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;
Expand All @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<DidUploader> 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);
}
}

}
106 changes: 106 additions & 0 deletions src/main/java/eu/europa/ec/dgc/gateway/service/did/GitDidUploader.java
Original file line number Diff line number Diff line change
@@ -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<String, String> 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");
}
}
8 changes: 8 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: <personal-access-token-for-git>
owner: WorldHealthOrganization
branch: main
countryCodeMap:
virtualCountries:
XA: XXA
Expand Down

0 comments on commit 27e49e3

Please sign in to comment.