Skip to content

Commit e3a8ecc

Browse files
committed
Merge branch 'RM-4309_lib_api_changes' into 'SID'
RM-4309: Services interface instead of ExternalService See merge request cdoc2/cdoc2-java-ref-impl!91
2 parents 26d7766 + 3a315ef commit e3a8ecc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1024
-340
lines changed

.gitlab-ci.yml

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
variables:
2+
DOCKER_TLS_CERTDIR: ""
3+
DOCKER_HOST: "tcp://docker:2375"
4+
15
stages:
26
- test
37
- coverage
@@ -6,6 +10,11 @@ stages:
610
test:
711
stage: test
812
image: maven:3.8.8-eclipse-temurin-17
13+
# services:
14+
# - name: docker:25.0.3-dind
15+
# alias: docker
16+
# tags:
17+
# - dind
918
script:
1019
- mvn clean verify -s $MAVEN_SETTINGS -Dmaven.repo.local=$CI_PROJECT_DIR/.m2/repository
1120
- shopt -s globstar
@@ -14,6 +23,10 @@ test:
1423
- source install_bats.sh
1524
- source variables.sh
1625
- $BATS_HOME/bats-core/bin/bats --gather-test-outputs-in target/reports -x --report-formatter junit --output target/ cdoc2_tests.bats
26+
# following will fail because docker-compose-plugin is not available from Jammy repository
27+
# - apt-get update && apt-get install -y docker.io docker-compose-plugin
28+
# - source ../config/shares-server/export-env.sh ../config/shares-server/.env.cyber
29+
# - bash run-shares-server-bats-tests.sh
1730
- 'for file in target/reports/*; do echo "## $file ##" >> target/bats-test.log; cat "$file" >> target/bats-test.log; done'
1831
coverage: /Total \d+\.\d+ %/
1932
artifacts:
@@ -36,6 +49,58 @@ test:
3649
paths:
3750
- .m2/repository
3851

52+
test_with_servers:
53+
# this will fail as docker image is based on alpine linux and flatc installation fails
54+
# use build from previous stage?
55+
stage: test
56+
image: docker:25.0.3
57+
services:
58+
- name: docker:25.0.3-dind
59+
alias: docker
60+
tags:
61+
- dind
62+
before_script:
63+
# Install Java
64+
- apk add --no-cache openjdk17
65+
# Install Maven
66+
- apk add --no-cache maven
67+
# Verify installations
68+
- java -version
69+
- mvn -version
70+
- docker compose version
71+
script:
72+
- mvn clean verify -s $MAVEN_SETTINGS -Dmaven.repo.local=$CI_PROJECT_DIR/.m2/repository
73+
- shopt -s globstar
74+
- awk -F"," '{ instructions += $4 + $5; covered += $5 } END { print covered, "/", instructions, " instructions covered"; print "Total", 100*covered/instructions, "% covered" }' /builds/**/target/site/jacoco/jacoco.csv
75+
- cd test/bats
76+
- source install_bats.sh
77+
- source variables.sh
78+
- source ../config/shares-server/export-env.sh ../config/shares-server/.env.cyber
79+
- BATS_OPTS="--gather-test-outputs-in target/reports -x --report-formatter junit --output target/" bash run-shares-server-bats-tests.sh
80+
- 'for file in target/reports/*; do echo "## $file ##" >> target/bats-test.log; cat "$file" >> target/bats-test.log; done'
81+
coverage: /Total \d+\.\d+ %/
82+
artifacts:
83+
when: always
84+
reports:
85+
junit:
86+
- "**/target/surefire-reports/*.xml"
87+
- "**/target/report.xml"
88+
paths:
89+
- "**/target/site/jacoco"
90+
- "**/target/bats-test.log"
91+
rules:
92+
# disabled for now
93+
# - if: $CI_PIPELINE_SOURCE == "merge_request_event"
94+
# when: on_success
95+
# - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
96+
# when: on_success
97+
- when: never
98+
cache:
99+
key: "$CI_COMMIT_REF_NAME"
100+
paths:
101+
- .m2/repository
102+
103+
39104
coverage:
40105
stage: coverage
41106
image: mcr.microsoft.com/dotnet/sdk:8.0

cdoc2-cli/src/main/java/ee/cyber/cdoc2/cli/commands/CDocDecryptCmd.java

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package ee.cyber.cdoc2.cli.commands;
22

33
import ee.cyber.cdoc2.cli.DecryptionKeyExclusiveArgument;
4-
import ee.cyber.cdoc2.client.ExternalService;
54
import ee.cyber.cdoc2.crypto.keymaterial.DecryptionKeyMaterial;
5+
import ee.cyber.cdoc2.services.Cdoc2Services;
66
import picocli.CommandLine;
77
import picocli.CommandLine.Command;
88
import picocli.CommandLine.Option;
@@ -16,11 +16,10 @@
1616

1717
import ee.cyber.cdoc2.CDocDecrypter;
1818

19-
import static ee.cyber.cdoc2.cli.util.CDocCommonHelper.getKeyCapsulesClientFactory;
20-
import static ee.cyber.cdoc2.cli.util.CDocDecryptionHelper.addKeySharesIfAny;
2119
import static ee.cyber.cdoc2.cli.util.CDocDecryptionHelper.getDecrypterWithFilesExtraction;
2220
import static ee.cyber.cdoc2.cli.util.CDocDecryptionHelper.getDecryptionKeyMaterial;
2321
import static ee.cyber.cdoc2.cli.util.CDocDecryptionHelper.getSmartCardDecryptionKeyMaterial;
22+
import static ee.cyber.cdoc2.config.Cdoc2ConfigurationProperties.KEY_CAPSULES_PROPERTIES;
2423

2524

2625
//S106 Standard outputs should not be used directly to log anything
@@ -50,12 +49,13 @@ public class CDocDecryptCmd implements Callable<Void> {
5049
description = "output destination | Default: current-directory")
5150
private File outputPath = new File(".");
5251

53-
@Option(names = {"--server"}, paramLabel = "FILE.properties"
54-
// commented out until public key server is in live
55-
//, arity = "0..1"
56-
//,defaultValue = DEFAULT_SERVER_PROPERTIES
57-
)
5852
private String keyServerPropertiesFile;
53+
@Option(names = {"--server"}, paramLabel = "FILE.properties")
54+
private void setKeyServerPropertiesFile(String server) {
55+
keyServerPropertiesFile = server;
56+
System.setProperty(KEY_CAPSULES_PROPERTIES, keyServerPropertiesFile);
57+
}
58+
5959

6060
@CommandLine.Parameters(description = "one or more files to decrypt", paramLabel = "fileToExtract")
6161
private String[] filesToExtract = new String[0];
@@ -75,11 +75,6 @@ public Void call() throws Exception {
7575
throw new InvalidPathException(this.cdocFile.getAbsolutePath(), "Input CDOC file does not exist");
7676
}
7777

78-
ExternalService keyCapsulesClientFactory = null;
79-
if (this.keyServerPropertiesFile != null) {
80-
keyCapsulesClientFactory = getKeyCapsulesClientFactory(this.keyServerPropertiesFile);
81-
}
82-
8378
DecryptionKeyMaterial decryptionKeyMaterial = (null == this.exclusive)
8479
? getSmartCardDecryptionKeyMaterial(this.slot, this.keyAlias)
8580
: getDecryptionKeyMaterial(this.cdocFile, this.exclusive);
@@ -89,11 +84,9 @@ public Void call() throws Exception {
8984
this.filesToExtract,
9085
this.outputPath,
9186
decryptionKeyMaterial,
92-
keyCapsulesClientFactory
87+
Cdoc2Services.initFromSystemProperties()
9388
);
9489

95-
addKeySharesIfAny(cDocDecrypter, this.exclusive);
96-
9790
System.out.println("Decrypting " + this.cdocFile + " to " + this.outputPath.getAbsolutePath());
9891
List<String> extractedFileNames = cDocDecrypter.decrypt();
9992
extractedFileNames.forEach(System.out::println);

cdoc2-cli/src/main/java/ee/cyber/cdoc2/cli/commands/CDocListCmd.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import ee.cyber.cdoc2.cli.DecryptionKeyExclusiveArgument;
44
import ee.cyber.cdoc2.CDocDecrypter;
5-
import ee.cyber.cdoc2.client.ExternalService;
65
import ee.cyber.cdoc2.crypto.keymaterial.DecryptionKeyMaterial;
76
import java.io.File;
87
import java.nio.file.InvalidPathException;
@@ -12,16 +11,17 @@
1211
import java.util.List;
1312
import java.util.Map;
1413
import java.util.concurrent.Callable;
14+
15+
import ee.cyber.cdoc2.services.Cdoc2Services;
1516
import org.apache.commons.compress.archivers.ArchiveEntry;
1617

1718
import picocli.CommandLine;
1819
import picocli.CommandLine.Command;
1920
import picocli.CommandLine.Option;
2021

21-
import static ee.cyber.cdoc2.cli.util.CDocCommonHelper.getKeyCapsulesClientFactory;
22-
import static ee.cyber.cdoc2.cli.util.CDocDecryptionHelper.addKeySharesIfAny;
2322
import static ee.cyber.cdoc2.cli.util.CDocDecryptionHelper.getDecryptionKeyMaterial;
2423
import static ee.cyber.cdoc2.cli.util.CDocDecryptionHelper.getSmartCardDecryptionKeyMaterial;
24+
import static ee.cyber.cdoc2.config.Cdoc2ConfigurationProperties.KEY_CAPSULES_PROPERTIES;
2525

2626

2727
//S106 Standard outputs should not be used directly to log anything
@@ -44,8 +44,12 @@ public class CDocListCmd implements Callable<Void> {
4444
description = "Alias of the keystore entry to use for decrypting")
4545
private String keyAlias;
4646

47-
@Option(names = {"--server"}, paramLabel = "FILE.properties")
4847
private String keyServerPropertiesFile;
48+
@Option(names = {"--server"}, paramLabel = "FILE.properties")
49+
private void setKeyServerPropertiesFile(String server) {
50+
keyServerPropertiesFile = server;
51+
System.setProperty(KEY_CAPSULES_PROPERTIES, keyServerPropertiesFile);
52+
}
4953

5054
// allow -Dkey for setting System properties
5155
@Option(names = "-D", mapFallbackValue = "", description = "Set Java System property")
@@ -65,22 +69,15 @@ public Void call() throws Exception {
6569
throw new InvalidPathException(this.cdocFile.getAbsolutePath(), "Input CDOC file does not exist");
6670
}
6771

68-
ExternalService keyCapsulesClientFactory = null;
69-
if (keyServerPropertiesFile != null) {
70-
keyCapsulesClientFactory = getKeyCapsulesClientFactory(this.keyServerPropertiesFile);
71-
}
72-
7372
DecryptionKeyMaterial decryptionKeyMaterial = (null == this.exclusive)
7473
? getSmartCardDecryptionKeyMaterial(this.slot, this.keyAlias)
7574
: getDecryptionKeyMaterial(this.cdocFile, this.exclusive);
7675

7776
CDocDecrypter cDocDecrypter = new CDocDecrypter()
7877
.withCDoc(cdocFile)
79-
.withKeyServers(keyCapsulesClientFactory)
78+
.withServices(Cdoc2Services.initFromSystemProperties())
8079
.withRecipient(decryptionKeyMaterial);
8180

82-
addKeySharesIfAny(cDocDecrypter, this.exclusive);
83-
8481
System.out.println("Listing contents of " + cdocFile);
8582
List<ArchiveEntry> files = cDocDecrypter.list();
8683
if (!verbose) {

cdoc2-cli/src/main/java/ee/cyber/cdoc2/cli/commands/CDocReEncryptCmd.java

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@
55
import ee.cyber.cdoc2.cli.util.LabeledPasswordParamConverter;
66
import ee.cyber.cdoc2.cli.util.LabeledPasswordParam;
77
import ee.cyber.cdoc2.cli.util.LabeledSecretConverter;
8-
import ee.cyber.cdoc2.client.ExternalService;
98
import ee.cyber.cdoc2.crypto.keymaterial.LabeledPassword;
109
import ee.cyber.cdoc2.crypto.keymaterial.LabeledSecret;
10+
import ee.cyber.cdoc2.services.Cdoc2Services;
1111
import picocli.CommandLine;
1212

1313
import java.io.File;
1414
import java.nio.file.InvalidPathException;
1515
import java.nio.file.Path;
16-
import java.security.GeneralSecurityException;
1716
import java.util.Map;
1817

1918
import java.util.concurrent.Callable;
@@ -26,11 +25,9 @@
2625
import ee.cyber.cdoc2.crypto.keymaterial.DecryptionKeyMaterial;
2726
import ee.cyber.cdoc2.crypto.keymaterial.EncryptionKeyMaterial;
2827

29-
import static ee.cyber.cdoc2.cli.util.CDocCommonHelper.getKeyCapsulesClientFactory;
30-
import static ee.cyber.cdoc2.cli.util.CDocCommonHelper.initKeyShareClientFactory;
3128
import static ee.cyber.cdoc2.cli.util.CDocDecryptionHelper.getDecryptionKeyMaterial;
3229
import static ee.cyber.cdoc2.cli.util.CDocDecryptionHelper.getSmartCardDecryptionKeyMaterial;
33-
30+
import static ee.cyber.cdoc2.config.Cdoc2ConfigurationProperties.KEY_CAPSULES_PROPERTIES;
3431

3532
//S106 Standard outputs should not be used directly to log anything
3633
//CLI needs to interact with standard outputs
@@ -71,12 +68,12 @@ public class CDocReEncryptCmd implements Callable<Void> {
7168
description = "output destination")
7269
private File outputPath;
7370

74-
@CommandLine.Option(names = {"--server"}, paramLabel = "FILE.properties"
75-
// commented out until public key server is in live
76-
//, arity = "0..1"
77-
//,defaultValue = DEFAULT_SERVER_PROPERTIES
78-
)
7971
private String keyServerPropertiesFile;
72+
@CommandLine.Option(names = {"--server"}, paramLabel = "FILE.properties")
73+
private void setKeyServerPropertiesFile(String server) {
74+
keyServerPropertiesFile = server;
75+
System.setProperty(KEY_CAPSULES_PROPERTIES, keyServerPropertiesFile);
76+
}
8077

8178
@CommandLine.Option(names = { "-h", "--help" }, usageHelp = true, description = "display a help message")
8279
private boolean helpRequested = false;
@@ -97,22 +94,14 @@ public Void call() throws Exception {
9794
? getSmartCardDecryptionKeyMaterial(this.slot, this.keyAlias)
9895
: getDecryptionKeyMaterial(this.cdocFile, this.exclusive);
9996

100-
ExternalService keyCapsulesClientFactory = null;
101-
102-
if (this.keyServerPropertiesFile != null) {
103-
keyCapsulesClientFactory = getKeyCapsulesClientFactory(this.keyServerPropertiesFile);
104-
}
105-
10697
File destCdocFile = getDestinationFile();
10798
CDocReEncrypter cDocReEncrypter = new CDocReEncrypter(
10899
cdocFile,
109100
decryptionKeyMaterial,
110101
destCdocFile,
111102
extractSymmetricKeyEncKeyMaterial(),
112-
keyCapsulesClientFactory
103+
Cdoc2Services.initFromSystemProperties()
113104
);
114-
// the order must be after assigning keyCapsulesClientFactory to override it
115-
addKeySharesIfAny(cDocReEncrypter);
116105

117106
cDocReEncrypter.reEncryptCDocContainer();
118107

@@ -150,12 +139,4 @@ private File getDestinationFile() {
150139
return outDir.toFile();
151140
}
152141

153-
private void addKeySharesIfAny(CDocReEncrypter cDocReEncrypter)
154-
throws GeneralSecurityException {
155-
156-
if (null != this.exclusive && (this.exclusive.isWithSid() || this.exclusive.isWithMid())) {
157-
cDocReEncrypter.addKeyShareClientFactory(initKeyShareClientFactory());
158-
}
159-
}
160-
161142
}

cdoc2-cli/src/main/java/ee/cyber/cdoc2/cli/util/CDocCommonHelper.java

Lines changed: 4 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,16 @@
44
import java.security.GeneralSecurityException;
55
import java.util.Properties;
66

7-
import ee.cyber.cdoc2.client.ExternalService;
8-
import ee.cyber.cdoc2.client.ExternalServiceImpl;
7+
import ee.cyber.cdoc2.client.KeyShareClientFactory;
8+
import ee.cyber.cdoc2.client.KeySharesClientHelper;
99
import ee.cyber.cdoc2.config.CDoc2ConfigurationProvider;
1010
import ee.cyber.cdoc2.config.Cdoc2Configuration;
11-
import ee.cyber.cdoc2.config.KeyCapsuleClientConfiguration;
12-
import ee.cyber.cdoc2.config.KeyCapsuleClientConfigurationImpl;
1311
import ee.cyber.cdoc2.config.KeySharesConfiguration;
1412
import ee.cyber.cdoc2.config.KeySharesConfigurationImpl;
15-
import ee.cyber.cdoc2.config.MobileIdClientConfigurationImpl;
16-
import ee.cyber.cdoc2.config.SmartIdClientConfigurationImpl;
1713
import ee.cyber.cdoc2.exceptions.ConfigurationLoadingException;
1814
import ee.cyber.cdoc2.util.Resources;
1915

2016
import static ee.cyber.cdoc2.config.Cdoc2ConfigurationProperties.KEY_SHARES_PROPERTIES;
21-
import static ee.cyber.cdoc2.config.Cdoc2ConfigurationProperties.MOBILE_ID_PROPERTIES;
22-
import static ee.cyber.cdoc2.config.Cdoc2ConfigurationProperties.SMART_ID_PROPERTIES;
2317
import static ee.cyber.cdoc2.config.PropertiesLoader.loadProperties;
2418

2519

@@ -36,30 +30,8 @@ public static Properties getServerProperties(String keyServerPropertiesFile) thr
3630
return p;
3731
}
3832

39-
public static ExternalService initKeyShareClientFactory() throws GeneralSecurityException {
40-
ExternalServiceImpl clientFactory = new ExternalServiceImpl();
41-
return clientFactory.initKeyShareClientFactory(loadKeySharesConfiguration());
42-
}
43-
44-
public static ExternalService getKeyCapsulesClientFactory(
45-
String keyServerPropertiesFile
46-
) throws GeneralSecurityException, IOException, ConfigurationLoadingException {
47-
Properties p = CDocCommonHelper.getServerProperties(keyServerPropertiesFile);
48-
49-
return initKeyCapsuleClientFactory(p);
50-
}
51-
52-
private static ExternalService initKeyCapsuleClientFactory(Properties p)
53-
throws GeneralSecurityException {
54-
55-
ExternalServiceImpl clientFactory = new ExternalServiceImpl();
56-
return clientFactory.initKeyCapsuleClientFactory(initializeCapsuleConfiguration(p));
57-
}
58-
59-
private static KeyCapsuleClientConfiguration initializeCapsuleConfiguration(Properties p) {
60-
Cdoc2Configuration configuration = new KeyCapsuleClientConfigurationImpl(p);
61-
CDoc2ConfigurationProvider.initKeyCapsuleClientConfig(configuration);
62-
return configuration.keyCapsuleClientConfiguration();
33+
public static KeyShareClientFactory initKeyShareClientFactory() throws GeneralSecurityException {
34+
return KeySharesClientHelper.createFactory(loadKeySharesConfiguration());
6335
}
6436

6537
private static KeySharesConfiguration loadKeySharesConfiguration() {
@@ -77,33 +49,4 @@ private static KeySharesConfiguration loadKeySharesConfiguration() {
7749

7850
return configuration.keySharesConfiguration();
7951
}
80-
81-
static void loadSmartIdConfiguration() throws ConfigurationLoadingException {
82-
String propertiesFilePath = System.getProperty(
83-
SMART_ID_PROPERTIES,
84-
"config/smart-id/" + SMART_ID_PROPERTIES
85-
);
86-
if (null == propertiesFilePath) {
87-
throw new ConfigurationLoadingException("Smart ID configuration property is missing");
88-
}
89-
Properties properties = loadProperties(propertiesFilePath);
90-
Cdoc2Configuration configuration = new SmartIdClientConfigurationImpl(properties);
91-
92-
CDoc2ConfigurationProvider.initSmartIdClientConfig(configuration);
93-
}
94-
95-
static void loadMobileIdConfiguration() throws ConfigurationLoadingException {
96-
String propertiesFilePath = System.getProperty(
97-
MOBILE_ID_PROPERTIES,
98-
"config/mobile-id/" + MOBILE_ID_PROPERTIES
99-
);
100-
if (null == propertiesFilePath) {
101-
throw new ConfigurationLoadingException("Mobile ID configuration property is missing");
102-
}
103-
Properties properties = loadProperties(propertiesFilePath);
104-
Cdoc2Configuration configuration = new MobileIdClientConfigurationImpl(properties);
105-
106-
CDoc2ConfigurationProvider.initMobileIdClientConfig(configuration);
107-
}
108-
10952
}

0 commit comments

Comments
 (0)