|
| 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 | +} |
0 commit comments