Skip to content

Commit

Permalink
feature: git upload
Browse files Browse the repository at this point in the history
  • Loading branch information
ascheibal committed May 6, 2024
1 parent 28679a6 commit 680a008
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 1 deletion.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,11 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.eclipse.jgit</groupId>
<artifactId>org.eclipse.jgit</artifactId>
<version>6.9.0.202403050737-r</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,25 @@ public static class DidConfig {
private Map<String, String> virtualCountries = new HashMap<>();

private LocalFileConfig localFile = new LocalFileConfig();
private GitConfig git = new GitConfig();

private DgcGatewayConnectorConfigProperties.KeyStoreWithAlias localKeyStore =
new DgcGatewayConnectorConfigProperties.KeyStoreWithAlias();

@Getter
@Setter
public static class LocalFileConfig {
private String fileName;
private String directory;
}

@Getter
@Setter
public static class GitConfig {
private String path;
private String pat;
private String url;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,23 @@
import foundation.identity.jsonld.JsonLDObject;
import info.weboftrust.ldsignatures.jsonld.LDSecurityKeywords;
import info.weboftrust.ldsignatures.signer.JsonWebSignature2020LdSigner;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.PublicKey;
import java.security.cert.CertificateEncodingException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.security.interfaces.ECPublicKey;
import java.security.interfaces.RSAPublicKey;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Date;
Expand All @@ -53,6 +59,9 @@
import lombok.extern.slf4j.Slf4j;
import net.javacrumbs.shedlock.spring.annotation.SchedulerLock;
import org.bouncycastle.cert.X509CertificateHolder;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -102,6 +111,22 @@ public class DidTrustListService {
public void job() {

String trustList;

deleteDirectoryAndContents(configProperties.getDid().getGit().getPath());
try {
Git.cloneRepository()
.setURI(configProperties.getDid().getGit().getUrl())
.setDirectory(new File(configProperties.getDid().getGit().getPath()))
.setCredentialsProvider(
new UsernamePasswordCredentialsProvider(
"anonymous", configProperties.getDid().getGit().getPat()))
.call();
} catch (Exception e) {
log.error("Failed to clone repository {}: {}",
configProperties.getDid().getGit().getUrl(), e.getMessage());
}


try {
trustList = generateTrustList(null);
} catch (Exception e) {
Expand Down Expand Up @@ -139,6 +164,18 @@ public void job() {
}

log.info("Finished DID Export Process");

try {
Git git = Git.open(new File(configProperties.getDid().getGit().getPath()));
git.add().addFilepattern(".").call();
git.commit().setMessage("Added DID files on " + Instant.now()).call();
git.push().setCredentialsProvider(new UsernamePasswordCredentialsProvider(
"anonymous", configProperties.getDid().getGit().getPat())).call();
git.close();
} catch (GitAPIException | IOException e) {
log.error("Error during Git commit & push: {}",e.getMessage());
}

}

private String getCountryAsLowerCaseAlpha3(String country) {
Expand Down Expand Up @@ -300,4 +337,25 @@ private Optional<X509Certificate> searchCsca(X509Certificate dsc, String country
.equals(dsc.getIssuerX500Principal()))
.findFirst();
}

private void deleteDirectoryAndContents(String directoryPath) {
Path dir = Paths.get(directoryPath);
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
for (Path path : stream) {
if (Files.isDirectory(path)) {
deleteDirectoryAndContents(path.toString());
} else {
Files.delete(path);
}
}
} catch (IOException e) {
log.error("Error deleting file {} {}",dir,e.getMessage());
}
try {
Files.delete(dir);
} catch (IOException e) {
log.error("Error deleting directory {} {}",dir,e.getMessage());
}
}

}
4 changes: 4 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ dgc:
localFile:
directory: ./did
file-name: did.json
git:
path: <local-working-directory>
url: <git-repository-to-be-cloned>
pat: <personal-access-token>
didSigningProvider: dummy
ld-proof-verification-method: did:web:dummy.net
ld-proof-nonce: n0nc3
Expand Down

0 comments on commit 680a008

Please sign in to comment.