Skip to content
This repository was archived by the owner on Jun 5, 2025. It is now read-only.

Commit c1a3114

Browse files
Avoiding duplicated code between CloudKeyPairsEnums and TPPKeyPairsEnums
1 parent 75547b0 commit c1a3114

7 files changed

Lines changed: 135 additions & 172 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.venafi.vcert.sdk.features;
2+
3+
import com.venafi.vcert.sdk.certificate.EllipticCurve;
4+
5+
import java.util.List;
6+
import java.util.Map;
7+
import java.util.function.Function;
8+
import java.util.stream.Collectors;
9+
10+
public class SupportedECCKeys {
11+
12+
public static final SupportedECCKeys TPP = new SupportedECCKeys(List.of(EllipticCurve.EllipticCurveP256, EllipticCurve.EllipticCurveP384, EllipticCurve.EllipticCurveP521));
13+
14+
private Map<String, EllipticCurve> ellipticCurveMap;
15+
16+
public SupportedECCKeys(List<EllipticCurve> ellipticCurves) {
17+
ellipticCurveMap = ellipticCurves.stream().collect(Collectors.toMap(EllipticCurve::value, Function.identity()));
18+
}
19+
20+
public boolean containsEllipticCurves(String[] curves){
21+
22+
for (String curve : curves) {
23+
if(!containsEllipticCurve(curve))
24+
return false;
25+
}
26+
27+
return true;
28+
}
29+
30+
public boolean containsEllipticCurve(String value){
31+
return ellipticCurveMap.containsKey(value);
32+
}
33+
34+
public EllipticCurve getEllipticCurve(String value){
35+
return ellipticCurveMap.get(value);
36+
}
37+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.venafi.vcert.sdk.features;
2+
3+
import com.venafi.vcert.sdk.certificate.KeyType;
4+
5+
import java.util.List;
6+
import java.util.Map;
7+
import java.util.function.Function;
8+
import java.util.stream.Collectors;
9+
10+
public class SupportedKeyPairs {
11+
12+
public static final SupportedKeyPairs TPP = new SupportedKeyPairs(List.of(KeyType.RSA, KeyType.ECDSA));
13+
public static final SupportedKeyPairs VAAS = new SupportedKeyPairs(List.of(KeyType.RSA));
14+
15+
private Map<String, KeyType> keyTypeMap;
16+
17+
public SupportedKeyPairs(List<KeyType> keyTypes) {
18+
keyTypeMap = keyTypes.stream().collect(Collectors.toMap(KeyType::value, Function.identity()));
19+
}
20+
21+
public boolean containsKeyTypes(String[] types){
22+
23+
for (String type : types) {
24+
if(!containsKeyType(type))
25+
return false;
26+
}
27+
28+
return true;
29+
}
30+
31+
public boolean containsKeyType(String value){
32+
KeyType keyType = null;
33+
try {
34+
keyType = KeyType.from(value);
35+
} catch (IllegalArgumentException e){
36+
return false;
37+
}
38+
39+
return keyTypeMap.containsKey(keyType.value());
40+
}
41+
42+
public KeyType getKeyType(String value){
43+
return keyTypeMap.get(KeyType.from(value).value());
44+
}
45+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.venafi.vcert.sdk.features;
2+
3+
import com.venafi.vcert.sdk.certificate.KeySize;
4+
5+
import java.util.List;
6+
import java.util.Map;
7+
import java.util.function.Function;
8+
import java.util.stream.Collectors;
9+
10+
public class SupportedRSAKeySizes {
11+
12+
public static final SupportedRSAKeySizes TPP = new SupportedRSAKeySizes(List.of(KeySize.KS512, KeySize.KS1024, KeySize.KS2048, KeySize.KS3072, KeySize.KS4096));
13+
public static final SupportedRSAKeySizes VAAS = new SupportedRSAKeySizes(List.of(KeySize.KS1024, KeySize.KS2048, KeySize.KS4096));
14+
15+
private Map<Integer, KeySize> rsaKeySizeMap;
16+
17+
public SupportedRSAKeySizes(List<KeySize> keySizes) {
18+
rsaKeySizeMap = keySizes.stream().collect(Collectors.toMap(KeySize::value, Function.identity()));
19+
}
20+
21+
public boolean containsRsaKeySizes(Integer[] sizes){
22+
23+
for (int size : sizes) {
24+
if(!containsRsaKeySize(size))
25+
return false;
26+
}
27+
28+
return true;
29+
}
30+
31+
public boolean containsRsaKeySize(int value){
32+
return rsaKeySizeMap.containsKey(value);
33+
}
34+
35+
public KeySize getRsaKeySize(int value){
36+
return rsaKeySizeMap.get(value);
37+
}
38+
}

src/main/java/com/venafi/vcert/sdk/policy/converter/cloud/CloudKeyPairEnums.java

Lines changed: 0 additions & 67 deletions
This file was deleted.

src/main/java/com/venafi/vcert/sdk/policy/converter/cloud/CloudPolicySpecificationValidator.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.venafi.vcert.sdk.policy.converter.cloud;
22

33
import com.venafi.vcert.sdk.VCertException;
4+
import com.venafi.vcert.sdk.features.SupportedKeyPairs;
5+
import com.venafi.vcert.sdk.features.SupportedRSAKeySizes;
46
import com.venafi.vcert.sdk.policy.domain.*;
57
import com.venafi.vcert.sdk.policy.converter.IPolicySpecificationValidator;
68
import org.apache.commons.lang3.StringUtils;
@@ -91,13 +93,13 @@ private void validateKeyPair(KeyPair keyPair) throws VCertException {
9193
if(keyPair.keyTypes() != null) {
9294
int keyTypesLength = keyPair.keyTypes().length;
9395

94-
if (keyTypesLength > 0 && !CloudKeyPairEnums.containsKeyTypes(keyPair.keyTypes()))
96+
if (keyTypesLength > 0 && !SupportedKeyPairs.VAAS.containsKeyTypes(keyPair.keyTypes()))
9597
throw new VCertException(String.format(ATTRIBUTE_DOESNT_MATCH_WITH_ACCEPTED_VALUES_EXCEPTION_MESSAGE, PolicySpecificationConst.ATT_POLICY_KEYPAIR_KEY_TYPES));
9698
}
9799

98100
//validate key bit strength
99101
if(keyPair.rsaKeySizes() != null) {
100-
if (!CloudKeyPairEnums.containsRsaKeySizes(keyPair.rsaKeySizes()))
102+
if (!SupportedRSAKeySizes.VAAS.containsRsaKeySizes(keyPair.rsaKeySizes()))
101103
throw new VCertException(String.format(ATTRIBUTE_DOESNT_MATCH_WITH_ACCEPTED_VALUES_EXCEPTION_MESSAGE, PolicySpecificationConst.ATT_POLICY_KEYPAIR_RSA_KEY_SIZES));
102104
}
103105
}
@@ -165,7 +167,7 @@ private void validateDefaultKeyPair(DefaultsKeyPair defaultsKeyPair, KeyPair pol
165167

166168
String defaultKeyType = defaultsKeyPair.keyType();
167169
if ( defaultKeyType != null && !defaultKeyType.equals("")) {
168-
if(!CloudKeyPairEnums.containsKeyType( defaultKeyType ))
170+
if(!SupportedKeyPairs.VAAS.containsKeyType( defaultKeyType ))
169171
throw new VCertException(String.format(DEFAULT_ATTRIBUTE_DOESNT_MATCH_WITH_ACCEPTED_VALUES_EXCEPTION_MESSAGE, PolicySpecificationConst.ATT_DEFAULTS_KEYPAIR_KEY_TYPE));
170172

171173
if(policyKeyPair != null) {
@@ -177,7 +179,7 @@ private void validateDefaultKeyPair(DefaultsKeyPair defaultsKeyPair, KeyPair pol
177179

178180
Integer defaultRsaKeySize = defaultsKeyPair.rsaKeySize();
179181
if( defaultRsaKeySize != null ) {
180-
if( !CloudKeyPairEnums.containsRsaKeySize( defaultRsaKeySize ))
182+
if( !SupportedRSAKeySizes.VAAS.containsRsaKeySize( defaultRsaKeySize ))
181183
throw new VCertException(String.format(DEFAULT_ATTRIBUTE_DOESNT_MATCH_WITH_ACCEPTED_VALUES_EXCEPTION_MESSAGE, PolicySpecificationConst.ATT_DEFAULTS_KEYPAIR_RSA_KEY_SIZE));
182184

183185
if(policyKeyPair != null && !Arrays.stream(policyKeyPair.rsaKeySizes()).anyMatch(defaultRsaKeySize::equals))

src/main/java/com/venafi/vcert/sdk/policy/converter/tpp/TPPKeyPairEnums.java

Lines changed: 0 additions & 95 deletions
This file was deleted.

src/main/java/com/venafi/vcert/sdk/policy/converter/tpp/TPPPolicySpecificationValidator.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.venafi.vcert.sdk.policy.converter.tpp;
22

33
import com.venafi.vcert.sdk.VCertException;
4+
import com.venafi.vcert.sdk.features.SupportedKeyPairs;
5+
import com.venafi.vcert.sdk.features.SupportedRSAKeySizes;
6+
import com.venafi.vcert.sdk.features.SupportedECCKeys;
47
import com.venafi.vcert.sdk.policy.domain.*;
58
import com.venafi.vcert.sdk.policy.converter.IPolicySpecificationValidator;
69

@@ -68,7 +71,7 @@ private void validateKeyPair(KeyPair keyPair) throws VCertException {
6871
if (keyTypesLength > 1)
6972
throw new VCertException(String.format(ATTRIBUTE_HAS_MORE_THAN_ONE_VALUE_EXCEPTION_MESSAGE, PolicySpecificationConst.ATT_POLICY_KEYPAIR_KEY_TYPES));
7073

71-
if (keyTypesLength == 1 && !TPPKeyPairEnums.containsKeyTypes(keyPair.keyTypes()))
74+
if (keyTypesLength == 1 && !SupportedKeyPairs.TPP.containsKeyTypes(keyPair.keyTypes()))
7275
throw new VCertException(String.format(ATTRIBUTE_DOESNT_MATCH_WITH_ACCEPTED_VALUES_EXCEPTION_MESSAGE, PolicySpecificationConst.ATT_POLICY_KEYPAIR_KEY_TYPES));
7376
}
7477

@@ -78,7 +81,7 @@ private void validateKeyPair(KeyPair keyPair) throws VCertException {
7881
if (rsaKeySizesLength > 1)
7982
throw new VCertException(String.format(ATTRIBUTE_HAS_MORE_THAN_ONE_VALUE_EXCEPTION_MESSAGE, PolicySpecificationConst.ATT_POLICY_KEYPAIR_RSA_KEY_SIZES));
8083

81-
if (rsaKeySizesLength == 1 && !TPPKeyPairEnums.containsRsaKeySizes(keyPair.rsaKeySizes()))
84+
if (rsaKeySizesLength == 1 && !SupportedRSAKeySizes.TPP.containsRsaKeySizes(keyPair.rsaKeySizes()))
8285
throw new VCertException(String.format(ATTRIBUTE_DOESNT_MATCH_WITH_ACCEPTED_VALUES_EXCEPTION_MESSAGE, PolicySpecificationConst.ATT_POLICY_KEYPAIR_RSA_KEY_SIZES));
8386
}
8487

@@ -88,7 +91,7 @@ private void validateKeyPair(KeyPair keyPair) throws VCertException {
8891
if (ecLength > 1)
8992
throw new VCertException(String.format(ATTRIBUTE_HAS_MORE_THAN_ONE_VALUE_EXCEPTION_MESSAGE, PolicySpecificationConst.ATT_POLICY_KEYPAIR_ELLIPTIC_CURVES));
9093

91-
if (ecLength == 1 && !TPPKeyPairEnums.containsEllipticCurves(keyPair.ellipticCurves()))
94+
if (ecLength == 1 && !SupportedECCKeys.TPP.containsEllipticCurves(keyPair.ellipticCurves()))
9295
throw new VCertException(String.format(ATTRIBUTE_DOESNT_MATCH_WITH_ACCEPTED_VALUES_EXCEPTION_MESSAGE, PolicySpecificationConst.ATT_POLICY_KEYPAIR_ELLIPTIC_CURVES));
9396
}
9497
}
@@ -138,7 +141,7 @@ private void validateDefaultKeyPair(DefaultsKeyPair defaultsKeyPair, KeyPair pol
138141

139142
String defaultKeyType = defaultsKeyPair.keyType();
140143
if ( defaultKeyType != null && !defaultKeyType.equals("")) {
141-
if(!TPPKeyPairEnums.containsKeyType( defaultKeyType ))
144+
if(!SupportedKeyPairs.TPP.containsKeyType( defaultKeyType ))
142145
throw new VCertException(String.format(DEFAULT_ATTRIBUTE_DOESNT_MATCH_WITH_ACCEPTED_VALUES_EXCEPTION_MESSAGE, PolicySpecificationConst.ATT_DEFAULTS_KEYPAIR_KEY_TYPE));
143146

144147
if(policyKeyPair != null) {
@@ -150,7 +153,7 @@ private void validateDefaultKeyPair(DefaultsKeyPair defaultsKeyPair, KeyPair pol
150153

151154
Integer defaultRsaKeySize = defaultsKeyPair.rsaKeySize();
152155
if( defaultRsaKeySize != null ) {
153-
if( !TPPKeyPairEnums.containsRsaKeySize( defaultRsaKeySize ))
156+
if( !SupportedRSAKeySizes.TPP.containsRsaKeySize( defaultRsaKeySize ))
154157
throw new VCertException(String.format(DEFAULT_ATTRIBUTE_DOESNT_MATCH_WITH_ACCEPTED_VALUES_EXCEPTION_MESSAGE, PolicySpecificationConst.ATT_DEFAULTS_KEYPAIR_RSA_KEY_SIZE));
155158

156159
if(policyKeyPair != null) {
@@ -162,7 +165,7 @@ private void validateDefaultKeyPair(DefaultsKeyPair defaultsKeyPair, KeyPair pol
162165

163166
String defaultEC = defaultsKeyPair.ellipticCurve();
164167
if ( defaultEC != null && !defaultEC.equals("")){
165-
if ( !TPPKeyPairEnums.containsEllipticCurve( defaultEC ) )
168+
if ( !SupportedECCKeys.TPP.containsEllipticCurve( defaultEC ) )
166169
throw new VCertException(String.format(DEFAULT_ATTRIBUTE_DOESNT_MATCH_WITH_ACCEPTED_VALUES_EXCEPTION_MESSAGE, PolicySpecificationConst.ATT_DEFAULTS_KEYPAIR_ELLIPTIC_CURVE));
167170

168171
if(policyKeyPair != null) {

0 commit comments

Comments
 (0)