Skip to content

Commit d70a3dd

Browse files
committed
Merge branch 'RM-3765_cli_exclusive_group_fix' into 'master'
RM-3765: fix CLI exclusive arguments group usage See merge request cdoc2/cdoc2-java-ref-impl!53
2 parents 593ad5b + d04b682 commit d70a3dd

File tree

5 files changed

+133
-126
lines changed

5 files changed

+133
-126
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package ee.cyber.cdoc2.cli;
2+
3+
import picocli.CommandLine;
4+
5+
import java.io.File;
6+
7+
import ee.cyber.cdoc2.cli.util.CliConstants;
8+
import ee.cyber.cdoc2.cli.util.LabeledPasswordParam;
9+
import ee.cyber.cdoc2.cli.util.LabeledPasswordParamConverter;
10+
import ee.cyber.cdoc2.cli.util.LabeledSecretConverter;
11+
import ee.cyber.cdoc2.crypto.keymaterial.LabeledSecret;
12+
13+
14+
/**
15+
* Optional group of mutually exclusive arguments, only one of the arguments in the group can
16+
* appear on the command line
17+
*/
18+
public class DecryptionKeyExclusiveArgument {
19+
20+
@CommandLine.Option(names = {"-k", "--key"},
21+
paramLabel = "PEM", description = "EC private key PEM used to decrypt")
22+
private File privKeyFile;
23+
24+
@CommandLine.Option(names = {"-p12"},
25+
paramLabel = ".p12", description = "Load private key from .p12 file (FILE.p12:password)")
26+
private String p12;
27+
28+
@CommandLine.Option(names = {"-s", "--secret"}, paramLabel = "<label>:<secret>",
29+
converter = LabeledSecretConverter.class,
30+
description = CliConstants.SECRET_DESCRIPTION)
31+
private LabeledSecret secret;
32+
33+
@CommandLine.Option(names = {"-pw", "--password"}, arity = "0..1",
34+
converter = LabeledPasswordParamConverter.class,
35+
paramLabel = "<label>:<password>", description = CliConstants.PASSWORD_DESCRIPTION)
36+
// if empty --pw was provided labeledPasswordParam.isEmpty() is true
37+
// if option was not provided then labeledPasswordParam is null
38+
private LabeledPasswordParam labeledPasswordParam;
39+
40+
public File getPrivKeyFile() {
41+
return this.privKeyFile;
42+
}
43+
44+
public String getP12() {
45+
return this.p12;
46+
}
47+
48+
public LabeledSecret getSecret() {
49+
return this.secret;
50+
}
51+
52+
public LabeledPasswordParam getLabeledPasswordParam() {
53+
return this.labeledPasswordParam;
54+
}
55+
56+
}

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

Lines changed: 14 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package ee.cyber.cdoc2.cli.commands;
22

3-
import ee.cyber.cdoc2.cli.util.LabeledPasswordParamConverter;
4-
import ee.cyber.cdoc2.cli.util.LabeledPasswordParam;
5-
import ee.cyber.cdoc2.cli.util.LabeledSecretConverter;
3+
import ee.cyber.cdoc2.cli.DecryptionKeyExclusiveArgument;
64
import ee.cyber.cdoc2.crypto.keymaterial.DecryptionKeyMaterial;
7-
import ee.cyber.cdoc2.crypto.keymaterial.LabeledSecret;
85
import picocli.CommandLine;
96
import picocli.CommandLine.Command;
107
import picocli.CommandLine.Option;
@@ -16,13 +13,14 @@
1613

1714
import java.util.concurrent.Callable;
1815

19-
import ee.cyber.cdoc2.cli.util.CliConstants;
2016
import ee.cyber.cdoc2.CDocDecrypter;
2117
import ee.cyber.cdoc2.client.KeyCapsuleClientFactory;
2218

2319
import static ee.cyber.cdoc2.cli.util.CDocDecryptionHelper.getDecrypterWithFilesExtraction;
2420
import static ee.cyber.cdoc2.cli.util.CDocDecryptionHelper.getDecryptionKeyMaterial;
2521
import static ee.cyber.cdoc2.cli.util.CDocDecryptionHelper.getKeyCapsulesClientFactory;
22+
import static ee.cyber.cdoc2.cli.util.CDocDecryptionHelper.getSmartCardDecryptionKeyMaterial;
23+
2624

2725
//S106 Standard outputs should not be used directly to log anything
2826
//CLI needs to interact with standard outputs
@@ -36,30 +34,8 @@ public class CDocDecryptCmd implements Callable<Void> {
3634
paramLabel = "CDOC", description = "the CDOC2 file")
3735
private File cdocFile;
3836

39-
@CommandLine.ArgGroup(exclusive = true, multiplicity = "0..1")
40-
Exclusive exclusive;
41-
42-
static class Exclusive {
43-
@Option(names = {"-k", "--key"},
44-
paramLabel = "PEM", description = "Private key PEM to use for decrypting")
45-
private File privKeyFile;
46-
47-
@Option(names = {"-p12"},
48-
paramLabel = ".p12", description = "Load private key from .p12 file (FILE.p12:password)")
49-
private String p12;
50-
51-
@Option(names = {"-s", "--secret"}, paramLabel = "<label>:<secret>",
52-
converter = LabeledSecretConverter.class,
53-
description = CliConstants.SECRET_DESCRIPTION)
54-
private LabeledSecret secret;
55-
56-
@Option(names = {"-pw", "--password"}, arity = "0..1",
57-
converter = LabeledPasswordParamConverter.class,
58-
paramLabel = "<label>:<password>", description = CliConstants.PASSWORD_DESCRIPTION)
59-
// if empty --pw was provided labeledPasswordParam.isEmpty() is true
60-
// if option was not provided then labeledPasswordParam is null
61-
private LabeledPasswordParam labeledPasswordParam;
62-
}
37+
@CommandLine.ArgGroup
38+
DecryptionKeyExclusiveArgument exclusive;
6339

6440
@Option (names = {"--slot"},
6541
description = "Smart card key slot to use for decrypting. Default: 0")
@@ -103,15 +79,15 @@ public Void call() throws Exception {
10379
keyCapsulesClientFactory = getKeyCapsulesClientFactory(this.keyServerPropertiesFile);
10480
}
10581

106-
DecryptionKeyMaterial decryptionKeyMaterial = getDecryptionKeyMaterial(
107-
this.cdocFile,
108-
this.exclusive.labeledPasswordParam,
109-
this.exclusive.secret,
110-
this.exclusive.p12,
111-
this.exclusive.privKeyFile,
112-
this.slot,
113-
this.keyAlias
114-
);
82+
DecryptionKeyMaterial decryptionKeyMaterial = (null == this.exclusive)
83+
? getSmartCardDecryptionKeyMaterial(this.slot, this.keyAlias)
84+
: getDecryptionKeyMaterial(
85+
this.cdocFile,
86+
this.exclusive.getLabeledPasswordParam(),
87+
this.exclusive.getSecret(),
88+
this.exclusive.getP12(),
89+
this.exclusive.getPrivKeyFile()
90+
);
11591

11692
CDocDecrypter cDocDecrypter = getDecrypterWithFilesExtraction(
11793
this.cdocFile,

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

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

3-
import ee.cyber.cdoc2.cli.util.LabeledPasswordParam;
4-
import ee.cyber.cdoc2.cli.util.LabeledPasswordParamConverter;
5-
import ee.cyber.cdoc2.cli.util.LabeledSecretConverter;
6-
import ee.cyber.cdoc2.cli.util.CliConstants;
3+
import ee.cyber.cdoc2.cli.DecryptionKeyExclusiveArgument;
74
import ee.cyber.cdoc2.CDocDecrypter;
85
import ee.cyber.cdoc2.client.KeyCapsuleClientFactory;
96
import ee.cyber.cdoc2.client.KeyCapsuleClientImpl;
107
import ee.cyber.cdoc2.crypto.keymaterial.DecryptionKeyMaterial;
11-
import ee.cyber.cdoc2.crypto.keymaterial.LabeledSecret;
128
import ee.cyber.cdoc2.util.Resources;
139
import java.io.File;
1410
import java.nio.file.InvalidPathException;
@@ -20,10 +16,14 @@
2016
import java.util.Properties;
2117
import java.util.concurrent.Callable;
2218
import org.apache.commons.compress.archivers.ArchiveEntry;
19+
20+
import picocli.CommandLine;
2321
import picocli.CommandLine.Command;
2422
import picocli.CommandLine.Option;
2523

2624
import static ee.cyber.cdoc2.cli.util.CDocDecryptionHelper.getDecryptionKeyMaterial;
25+
import static ee.cyber.cdoc2.cli.util.CDocDecryptionHelper.getSmartCardDecryptionKeyMaterial;
26+
2727

2828
//S106 Standard outputs should not be used directly to log anything
2929
//CLI needs to interact with standard outputs
@@ -34,25 +34,8 @@ public class CDocListCmd implements Callable<Void> {
3434
paramLabel = "CDOC", description = "the CDOC2 file")
3535
private File cdocFile;
3636

37-
@Option(names = {"-k", "--key"},
38-
paramLabel = "PEM", description = "EC private key PEM used to decrypt")
39-
private File privKeyFile;
40-
41-
@Option(names = {"-p12"},
42-
paramLabel = ".p12", description = "Load private key from .p12 file (FILE.p12:password)")
43-
private String p12;
44-
45-
@Option(names = {"-s", "--secret"},
46-
paramLabel = "<label>:<secret>",
47-
converter = LabeledSecretConverter.class,
48-
description = CliConstants.SECRET_DESCRIPTION)
49-
private LabeledSecret secret;
50-
51-
@Option(names = {"-pass", "--password"}, arity = "0..1",
52-
paramLabel = "<label>:<password>",
53-
converter = LabeledPasswordParamConverter.class,
54-
description = CliConstants.PASSWORD_DESCRIPTION)
55-
private LabeledPasswordParam labeledPasswordParam;
37+
@CommandLine.ArgGroup
38+
DecryptionKeyExclusiveArgument exclusive;
5639

5740
@Option (names = {"--slot"},
5841
description = "Key from smartcard slot used for decrypting. Default 0")
@@ -93,14 +76,14 @@ public Void call() throws Exception {
9376
keyCapsulesClient = KeyCapsuleClientImpl.createFactory(p);
9477
}
9578

96-
DecryptionKeyMaterial decryptionKeyMaterial = getDecryptionKeyMaterial(
79+
DecryptionKeyMaterial decryptionKeyMaterial = (null == this.exclusive)
80+
? getSmartCardDecryptionKeyMaterial(this.slot, this.keyAlias)
81+
: getDecryptionKeyMaterial(
9782
this.cdocFile,
98-
this.labeledPasswordParam,
99-
this.secret,
100-
this.p12,
101-
this.privKeyFile,
102-
this.slot,
103-
this.keyAlias
83+
this.exclusive.getLabeledPasswordParam(),
84+
this.exclusive.getSecret(),
85+
this.exclusive.getP12(),
86+
this.exclusive.getPrivKeyFile()
10487
);
10588

10689
CDocDecrypter cDocDecrypter = new CDocDecrypter()

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

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package ee.cyber.cdoc2.cli.commands;
22

3+
import ee.cyber.cdoc2.cli.DecryptionKeyExclusiveArgument;
34
import ee.cyber.cdoc2.cli.util.InteractiveCommunicationUtil;
45
import ee.cyber.cdoc2.cli.util.LabeledPasswordParamConverter;
56
import ee.cyber.cdoc2.cli.util.LabeledPasswordParam;
@@ -26,6 +27,7 @@
2627

2728
import static ee.cyber.cdoc2.cli.util.CDocDecryptionHelper.getDecryptionKeyMaterial;
2829
import static ee.cyber.cdoc2.cli.util.CDocDecryptionHelper.getKeyCapsulesClientFactory;
30+
import static ee.cyber.cdoc2.cli.util.CDocDecryptionHelper.getSmartCardDecryptionKeyMaterial;
2931

3032

3133
//S106 Standard outputs should not be used directly to log anything
@@ -40,30 +42,9 @@ public class CDocReEncryptCmd implements Callable<Void> {
4042
@CommandLine.Option(names = {"-f", "--file" }, required = true,
4143
paramLabel = "CDOC", description = "the CDOC2 file")
4244
private File cdocFile;
43-
@CommandLine.ArgGroup(exclusive = true, multiplicity = "0..1")
44-
Exclusive exclusive;
45-
46-
static class Exclusive {
47-
@CommandLine.Option(names = {"-k", "--key"},
48-
paramLabel = "PEM", description = "Private key PEM to use for decrypting")
49-
private File privKeyFile;
50-
51-
@CommandLine.Option(names = {"-p12"},
52-
paramLabel = ".p12", description = "Load private key from .p12 file (FILE.p12:password)")
53-
private String p12;
54-
55-
@CommandLine.Option(names = {"-s", "--secret"}, paramLabel = "<label>:<secret>",
56-
converter = LabeledSecretConverter.class,
57-
description = CliConstants.SECRET_DESCRIPTION
58-
+ ". Used to decrypt existing CDOC container.")
59-
private LabeledSecret secret;
60-
61-
@CommandLine.Option(names = {"-pw", "--password"}, arity = "0..1",
62-
converter = LabeledPasswordParamConverter.class,
63-
paramLabel = "<label>:<password>",
64-
description = CliConstants.PASSWORD_DESCRIPTION + ". Used to decrypt existing CDOC container.")
65-
private LabeledPasswordParam password;
66-
}
45+
46+
@CommandLine.ArgGroup
47+
DecryptionKeyExclusiveArgument exclusive;
6748

6849
@CommandLine.Option(names = {"-encpw", "--encpassword"}, arity = "0..1",
6950
converter = LabeledPasswordParamConverter.class,
@@ -76,7 +57,6 @@ static class Exclusive {
7657
description = CliConstants.SECRET_DESCRIPTION + ". Used for re-encryption part.")
7758
private LabeledSecret reEncryptSecret;
7859

79-
8060
@CommandLine.Option(names = {"--slot"},
8161
description = "Smart card key slot to use for decrypting. Default: 0")
8262
private Integer slot = 0;
@@ -111,14 +91,14 @@ public Void call() throws Exception {
11191
throw new InvalidPathException(this.cdocFile.getAbsolutePath(), "Input CDOC file does not exist");
11292
}
11393

114-
DecryptionKeyMaterial decryptionKeyMaterial = getDecryptionKeyMaterial(
94+
DecryptionKeyMaterial decryptionKeyMaterial = (null == this.exclusive)
95+
? getSmartCardDecryptionKeyMaterial(this.slot, this.keyAlias)
96+
: getDecryptionKeyMaterial(
11597
this.cdocFile,
116-
this.exclusive.password,
117-
this.exclusive.secret,
118-
this.exclusive.p12,
119-
this.exclusive.privKeyFile,
120-
this.slot,
121-
this.keyAlias
98+
this.exclusive.getLabeledPasswordParam(),
99+
this.exclusive.getSecret(),
100+
this.exclusive.getP12(),
101+
this.exclusive.getPrivKeyFile()
122102
);
123103

124104
KeyCapsuleClientFactory keyCapsulesClientFactory = null;

0 commit comments

Comments
 (0)