Skip to content

Commit 02dac3d

Browse files
authored
MOSIP-30573: keymanagerservice test case and db setup (#431)
Signed-off-by: nagendra0721 <nagendra0718@gmail.com>
1 parent 21c83e3 commit 02dac3d

10 files changed

Lines changed: 2190 additions & 13 deletions

File tree

kernel/kernel-keymanager-service/src/test/java/io/mosip/kernel/crypto/jce/test/CryptoCoreTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ public void testAESSymmetricDecryptSaltInvalidKeyLength() throws java.security.N
211211
cryptoCore.symmetricDecrypt(setSymmetricUp(15, "AES"), encryptedData, keyBytes, MOCKAAD.getBytes());
212212
}
213213

214-
@Test(expected = InvalidKeyException.class)
214+
@Test(expected = InvalidDataException.class)
215215
public void testRSAPKS1AsymmetricPrivateDecryptInvalidDataIllegalBlockSize() {
216216
cryptoCore.asymmetricDecrypt(rsaPair.getPrivate(), new byte[121]);
217217
}

kernel/kernel-keymanager-service/src/test/java/io/mosip/kernel/cryptomanager/test/integration/CryptographicServiceIntegrationExceptionTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ public void testIllegalArgumentException() throws Exception {
194194
result.getResponse().getContentAsString(),
195195
new TypeReference<ResponseWrapper<CryptomanagerResponseDto>>() {
196196
});
197-
assertThat(responseWrapper.getErrors().get(0).getErrorCode(), is("KER-CRY-012"));
197+
assertThat(responseWrapper.getErrors().get(0).getErrorCode(), is("KER-CRY-003"));
198198
}
199199

200200
}

kernel/kernel-keymanager-service/src/test/java/io/mosip/kernel/keymanagerservice/test/controller/KeymanagerControllerTest.java

Lines changed: 773 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
package io.mosip.kernel.keymanagerservice.test.helper;
2+
3+
import io.mosip.kernel.core.util.DateUtils;
4+
import io.mosip.kernel.keymanagerservice.entity.KeyAlias;
5+
import io.mosip.kernel.keymanagerservice.entity.KeyPolicy;
6+
import io.mosip.kernel.keymanagerservice.entity.KeyStore;
7+
import io.mosip.kernel.keymanagerservice.exception.InvalidApplicationIdException;
8+
import io.mosip.kernel.keymanagerservice.exception.KeymanagerServiceException;
9+
import io.mosip.kernel.keymanagerservice.helper.KeymanagerDBHelper;
10+
import io.mosip.kernel.keymanagerservice.test.KeymanagerTestBootApplication;
11+
import org.junit.Assert;
12+
import org.junit.Before;
13+
import org.junit.Test;
14+
import org.junit.runner.RunWith;
15+
import org.springframework.beans.factory.annotation.Autowired;
16+
import org.springframework.boot.test.context.SpringBootTest;
17+
import org.springframework.test.context.junit4.SpringRunner;
18+
19+
import java.time.LocalDateTime;
20+
import java.util.List;
21+
import java.util.Map;
22+
import java.util.Optional;
23+
import java.util.UUID;
24+
25+
import static org.junit.jupiter.api.Assertions.assertThrows;
26+
27+
@SpringBootTest(classes = { KeymanagerTestBootApplication.class })
28+
@RunWith(SpringRunner.class)
29+
public class KeymanagerDBHelperTest {
30+
31+
@Autowired
32+
private KeymanagerDBHelper dbHelper;
33+
34+
private LocalDateTime timestamp;
35+
private String testAlias;
36+
private String testAppId = "TEST_APP";
37+
private String testRefId = "TEST_REF";
38+
39+
@Before
40+
public void setUp() {
41+
timestamp = DateUtils.getUTCCurrentDateTime();
42+
testAlias = UUID.randomUUID().toString();
43+
}
44+
45+
@Test
46+
public void testStoreKeyInDBStore() {
47+
String masterAlias = testAlias;
48+
String certificateData = "-----BEGIN CERTIFICATE-----\nMIICertificateData\n-----END CERTIFICATE-----";
49+
String encryptedPrivateKey = "encryptedPrivateKeyData";
50+
51+
dbHelper.storeKeyInDBStore(testAlias, masterAlias, certificateData, encryptedPrivateKey);
52+
53+
Optional<KeyStore> result = dbHelper.getKeyStoreFromDB(testAlias);
54+
Assert.assertTrue(result.isPresent());
55+
Assert.assertEquals(testAlias, result.get().getAlias());
56+
Assert.assertEquals(masterAlias, result.get().getMasterAlias());
57+
}
58+
59+
@Test
60+
public void testGetKeyAliases() {
61+
String certThumbprint = "DED24BC711E7F77273591F9B1A9567199575607425D9182DA53826AAAD4F950E";
62+
String uniqueIdentifier = "C15AF8613AF647262E38E6200305EF0D2AAA7DAA";
63+
64+
dbHelper.storeKeyInAlias(testAppId, timestamp.minusDays(1), testRefId, testAlias,
65+
timestamp.plusYears(1), certThumbprint, uniqueIdentifier);
66+
67+
Map<String, List<KeyAlias>> result = dbHelper.getKeyAliases(testAppId, testRefId, timestamp);
68+
69+
Assert.assertNotNull(result);
70+
Assert.assertTrue(result.containsKey("keyAlias"));
71+
Assert.assertTrue(result.containsKey("currentKeyAlias"));
72+
Assert.assertFalse(result.get("keyAlias").isEmpty());
73+
}
74+
75+
@Test
76+
public void testGetExpiryPolicy() {
77+
LocalDateTime result = dbHelper.getExpiryPolicy("TEST", timestamp, List.of());
78+
Assert.assertEquals(timestamp.plusDays(1095), result);
79+
}
80+
81+
@Test
82+
public void testGetKeyStoreFromDB() {
83+
String masterAlias = testAlias;
84+
String certificateData = "-----BEGIN CERTIFICATE-----\nTestCertData\n-----END CERTIFICATE-----";
85+
String encryptedPrivateKey = "testEncryptedKey";
86+
87+
dbHelper.storeKeyInDBStore(testAlias, masterAlias, certificateData, encryptedPrivateKey);
88+
89+
Optional<KeyStore> result = dbHelper.getKeyStoreFromDB(testAlias);
90+
91+
Assert.assertTrue(result.isPresent());
92+
Assert.assertEquals(testAlias, result.get().getAlias());
93+
Assert.assertEquals(certificateData, result.get().getCertificateData());
94+
}
95+
96+
@Test
97+
public void testGetKeyStoreFromDBNotFound() {
98+
Optional<KeyStore> result = dbHelper.getKeyStoreFromDB("NON_EXISTENT_ALIAS");
99+
Assert.assertFalse(result.isPresent());
100+
}
101+
102+
@Test
103+
public void testGetKeyPolicy() {
104+
Optional<KeyPolicy> result = dbHelper.getKeyPolicy("TEST");
105+
Assert.assertTrue(result.isPresent());
106+
}
107+
108+
@Test
109+
public void testGetExpiryPolicyInvalidApplicationId() {
110+
InvalidApplicationIdException exception = assertThrows(InvalidApplicationIdException.class, () -> {
111+
dbHelper.getExpiryPolicy("INVALID_APP_ID", timestamp, List.of());
112+
});
113+
Assert.assertEquals("KER-KMS-002", exception.getErrorCode());
114+
Assert.assertEquals("KER-KMS-002 --> ApplicationId not found in Key Policy. Key/CSR generation not allowed.", exception.getMessage());
115+
}
116+
117+
@Test
118+
public void testGetKeyPolicyFromCache() {
119+
Optional<KeyPolicy> result = dbHelper.getKeyPolicyFromCache("TEST");
120+
Assert.assertNotNull(result);
121+
}
122+
123+
@Test
124+
public void testGetKeyPolicyInvalidApplicationId() {
125+
InvalidApplicationIdException exception = assertThrows(InvalidApplicationIdException.class, () -> {
126+
dbHelper.getKeyPolicy("INVALID_APP_ID");
127+
});
128+
Assert.assertEquals("KER-KMS-002", exception.getErrorCode());
129+
Assert.assertEquals("KER-KMS-002 --> ApplicationId not found in Key Policy. Key/CSR generation not allowed.", exception.getMessage());
130+
}
131+
132+
@Test
133+
public void testGetKeyAliasWithThumbprint() {
134+
String certThumbprint = "DED24BC711E7F77273591F9B1A9567199575607425D9182DA53826AAAD4F950E";
135+
String uniqueIdentifier = "C15AF8613AF647262E38E6200305EF0D2AAA7DA8";
136+
String appIdRefIdKey = testAppId + "-" + testRefId;
137+
138+
// Store key in alias first
139+
dbHelper.storeKeyInAlias(testAppId, timestamp, testRefId, testAlias,
140+
timestamp.plusYears(1), certThumbprint, uniqueIdentifier);
141+
142+
// Store corresponding key in DB store
143+
String certificateData = "-----BEGIN CERTIFICATE-----\nThumbprintTestCert\n-----END CERTIFICATE-----";
144+
dbHelper.storeKeyInDBStore(testAlias, testAlias, certificateData, "encryptedKey");
145+
146+
KeyStore result = dbHelper.getKeyAlias(certThumbprint, appIdRefIdKey, testAppId, testRefId);
147+
148+
Assert.assertNotNull(result);
149+
Assert.assertEquals(testAlias, result.getAlias());
150+
}
151+
152+
@Test
153+
public void testGetKeyAliasThumbprintNotFound() {
154+
String nonExistentThumbprint = "NON2EXISTENTF77273591F9B1A9567199575607425D9182DA53826AAAD4F950E";
155+
String appIdRefIdKey = testAppId + "-" + testRefId;
156+
157+
KeymanagerServiceException exception = assertThrows(KeymanagerServiceException.class, () -> {
158+
dbHelper.getKeyAlias(nonExistentThumbprint, appIdRefIdKey, testAppId, testRefId);
159+
});
160+
Assert.assertEquals("KER-KMS-025", exception.getErrorCode());
161+
Assert.assertEquals("KER-KMS-025 --> Key Not found for the thumbprint prepended in encrypted data.", exception.getMessage());
162+
}
163+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package io.mosip.kernel.keymanagerservice.test.helper;
2+
3+
import io.mosip.kernel.keymanagerservice.constant.KeymanagerErrorConstant;
4+
import io.mosip.kernel.keymanagerservice.dto.CSRGenerateRequestDto;
5+
import io.mosip.kernel.keymanagerservice.dto.KeyPairGenerateRequestDto;
6+
import io.mosip.kernel.keymanagerservice.entity.KeyAlias;
7+
import io.mosip.kernel.keymanagerservice.entity.KeyStore;
8+
import io.mosip.kernel.keymanagerservice.exception.KeymanagerServiceException;
9+
import io.mosip.kernel.keymanagerservice.helper.PrivateKeyDecryptorHelper;
10+
import io.mosip.kernel.keymanagerservice.repository.KeyAliasRepository;
11+
import io.mosip.kernel.keymanagerservice.service.KeymanagerService;
12+
import io.mosip.kernel.keymanagerservice.test.KeymanagerTestBootApplication;
13+
import org.junit.After;
14+
import org.junit.Assert;
15+
import org.junit.Before;
16+
import org.junit.Test;
17+
import org.junit.runner.RunWith;
18+
import org.springframework.beans.factory.annotation.Autowired;
19+
import org.springframework.boot.test.context.SpringBootTest;
20+
import org.springframework.test.context.junit4.SpringRunner;
21+
22+
import java.util.List;
23+
24+
import static org.junit.jupiter.api.Assertions.assertThrows;
25+
26+
@SpringBootTest(classes = { KeymanagerTestBootApplication.class })
27+
@RunWith(SpringRunner.class)
28+
public class PrivateKeyDecryptorHelperTest {
29+
30+
@Autowired
31+
private PrivateKeyDecryptorHelper decryptorHelper;
32+
33+
@Autowired
34+
private KeymanagerService keymanagerService;
35+
36+
@Autowired
37+
private KeyAliasRepository keyAliasRepository;
38+
39+
@Before
40+
public void setUp() {
41+
KeyPairGenerateRequestDto keyPairGenRequestDto = new KeyPairGenerateRequestDto();
42+
keyPairGenRequestDto.setApplicationId("ROOT");
43+
keyPairGenRequestDto.setReferenceId("");
44+
keymanagerService.generateMasterKey("CSR", keyPairGenRequestDto);
45+
46+
keyPairGenRequestDto.setApplicationId("TEST");
47+
keyPairGenRequestDto.setReferenceId("");
48+
keymanagerService.generateMasterKey("CSR", keyPairGenRequestDto);
49+
}
50+
51+
@After
52+
public void tearDown() {
53+
keyAliasRepository.deleteAll();
54+
}
55+
56+
@Test
57+
public void testGetDBKeyStoreData() {
58+
CSRGenerateRequestDto csrGenerateRequestDto = new CSRGenerateRequestDto();
59+
csrGenerateRequestDto.setApplicationId("TEST");
60+
csrGenerateRequestDto.setReferenceId("test");
61+
keymanagerService.generateCSR(csrGenerateRequestDto);
62+
63+
List<KeyAlias> certDetails = keyAliasRepository.findByApplicationIdAndReferenceId("TEST", "test");
64+
String certThumbprint = certDetails.get(0).getCertThumbprint();
65+
KeyStore result = decryptorHelper.getDBKeyStoreData(certThumbprint, "TEST", "test");
66+
67+
Assert.assertNotNull(result);
68+
Assert.assertNotNull(result.getAlias());
69+
}
70+
71+
@Test
72+
public void testGetKeyObjectsOtherDomainKey() {
73+
KeyStore otherDomainKeyStore = new KeyStore();
74+
String alias = "TEST_ALIAS";
75+
otherDomainKeyStore.setAlias(alias);
76+
otherDomainKeyStore.setMasterAlias(alias);
77+
otherDomainKeyStore.setPrivateKey("somePrivateKey");
78+
otherDomainKeyStore.setCertificateData("someCertData");
79+
80+
KeymanagerServiceException exception = assertThrows(KeymanagerServiceException.class, () -> {
81+
decryptorHelper.getKeyObjects(otherDomainKeyStore, false);
82+
});
83+
Assert.assertEquals(KeymanagerErrorConstant.DECRYPTION_NOT_ALLOWED.getErrorCode(),
84+
exception.getErrorCode());
85+
}
86+
87+
@Test
88+
public void testGetKeyObjectsWithNAPrivateKey() {
89+
KeyStore naKeyStore = new KeyStore();
90+
naKeyStore.setAlias("TEST_ALIAS");
91+
naKeyStore.setMasterAlias("MASTER_ALIAS");
92+
naKeyStore.setPrivateKey("NA");
93+
naKeyStore.setCertificateData("someCertData");
94+
95+
KeymanagerServiceException exception = assertThrows(KeymanagerServiceException.class, () -> {
96+
decryptorHelper.getKeyObjects(naKeyStore, false);
97+
});
98+
Assert.assertEquals(KeymanagerErrorConstant.DECRYPTION_NOT_ALLOWED.getErrorCode(),
99+
exception.getErrorCode());
100+
}
101+
}

0 commit comments

Comments
 (0)