-
Notifications
You must be signed in to change notification settings - Fork 140
Extracted public key data in getPublicKey API [MOSIP-22993] #1284
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: release-1.3.x
Are you sure you want to change the base?
Changes from all commits
8e28bc9
cbde736
c14c2f9
6c7a7d6
8579d20
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| package io.mosip.kernel.syncdata.service.impl; | ||
|
|
||
| import com.fasterxml.jackson.core.type.TypeReference; | ||
| import com.fasterxml.jackson.databind.JsonNode; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import io.mosip.kernel.clientcrypto.dto.TpmCryptoRequestDto; | ||
| import io.mosip.kernel.clientcrypto.dto.TpmCryptoResponseDto; | ||
|
|
@@ -9,6 +9,7 @@ | |
| import io.mosip.kernel.core.exception.ServiceError; | ||
| import io.mosip.kernel.core.http.ResponseWrapper; | ||
| import io.mosip.kernel.core.util.CryptoUtil; | ||
| import io.mosip.kernel.core.util.DateUtils2; | ||
| import io.mosip.kernel.core.util.HMACUtils2; | ||
| import io.mosip.kernel.syncdata.constant.MasterDataErrorCode; | ||
| import io.mosip.kernel.syncdata.constant.SyncConfigDetailsErrorCode; | ||
|
|
@@ -39,9 +40,13 @@ | |
| import org.springframework.web.client.RestTemplate; | ||
| import org.springframework.web.util.UriComponentsBuilder; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.ByteArrayInputStream; | ||
| import java.io.StringReader; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.security.PublicKey; | ||
| import java.security.cert.CertificateFactory; | ||
| import java.security.cert.X509Certificate; | ||
| import java.time.OffsetDateTime; | ||
| import java.util.*; | ||
| import java.util.Map.Entry; | ||
|
|
||
|
|
@@ -62,6 +67,8 @@ public class SyncConfigDetailsServiceImpl implements SyncConfigDetailsService { | |
|
|
||
| private static final Logger LOGGER = LoggerFactory.getLogger(SyncConfigDetailsServiceImpl.class); | ||
| private static final String SLASH = "/"; | ||
| private static final String BEGIN_KEY = "-----BEGIN PUBLIC KEY-----"; | ||
| private static final String END_KEY = "-----END PUBLIC KEY-----"; | ||
|
|
||
| @Autowired | ||
| private RestTemplate restTemplate; | ||
|
|
@@ -236,14 +243,31 @@ public PublicKeyResponse<String> getPublicKey(String applicationId, String timeS | |
| } | ||
|
|
||
| try { | ||
| publicKeyResponseMapped = objectMapper.readValue(publicKeyResponseEntity.getBody(), | ||
| new TypeReference<ResponseWrapper<PublicKeyResponse<String>>>() { | ||
| }); | ||
|
|
||
| publicKeyResponseMapped.getResponse().setProfile(environment.getActiveProfiles()[0]); | ||
| LOGGER.debug("Public key fetched successfully for applicationId: {}", applicationId); | ||
| return publicKeyResponseMapped.getResponse(); | ||
| } catch (IOException | NullPointerException e) { | ||
| JsonNode root = objectMapper.readTree(publicKeyResponseEntity.getBody()); | ||
| String certificatePem = root.path("response").path("certificate").asText(); | ||
| String issuedAt = root.path("response").path("issuedAt").asText(); | ||
| String expiryAt = root.path("response").path("expiryAt").asText(); | ||
|
|
||
| CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509"); | ||
| ByteArrayInputStream certStream = new ByteArrayInputStream(certificatePem.getBytes(StandardCharsets.UTF_8)); | ||
| X509Certificate certificate = (X509Certificate) certificateFactory.generateCertificate(certStream); | ||
| PublicKey publicKey = certificate.getPublicKey(); | ||
|
|
||
| String publicKeyPEM = convertToPEM(publicKey); | ||
|
|
||
| OffsetDateTime issued = OffsetDateTime.parse(issuedAt); | ||
| OffsetDateTime expiry = OffsetDateTime.parse(expiryAt); | ||
|
|
||
| PublicKeyResponse<String> response = new PublicKeyResponse<>(); | ||
| response.setPublicKey(publicKeyPEM); | ||
| response.setLastSyncTime(DateUtils2.getUTCCurrentDateTimeString()); | ||
| response.setIssuedAt(issued.toLocalDateTime()); | ||
| response.setExpiryAt(expiry.toLocalDateTime()); | ||
| response.setProfile(environment.getActiveProfiles()[0]); | ||
|
|
||
| return response; | ||
|
|
||
| } catch (Exception e) { | ||
|
Comment on lines
245
to
+270
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chainHandle missing response fields and active profiles more defensively The new parsing flow is directionally good, but a few robustness points are worth tightening:
Please verify these changes against existing registration‑client flows (especially ones relying on I need to verify the specific concerns in this review comment by examining the actual code. Let me gather detailed information from the repository. Length of output: 7165 🏁 Script executed: #!/bin/bash
# Search for getActiveProfiles usage in the codebase to understand safety patterns
rg -nC5 "getActiveProfiles" --type=javaLength of output: 5302 🏁 Script executed: #!/bin/bash
# Look for the convertToPEM method and BEGIN_KEY/END_KEY constants
fd -e java "SyncConfigDetailsServiceImpl" --exec cat {} \; | grep -A 20 "convertToPEM\|BEGIN_KEY"Length of output: 2077 🏁 Script executed: #!/bin/bash
# Search for calls to getPublicKey to understand downstream consumers
rg -nP '\bgetPublicKey\s*\(' --type=java -C3Length of output: 43012 Let me verify one more critical aspect—whether the behavioral change for Length of output: 2924 🏁 Script executed: #!/bin/bash
# Inspect full getPublicKey method to see what was there before (context lines)
fd -e java "SyncConfigDetailsServiceImpl" --exec cat {} \; | grep -B50 "try {" | grep -A50 "getPublicKey"Length of output: 6990 🏁 Script executed: #!/bin/bash
# Check the test setup to confirm if profile was being set from response before
cat admin/kernel-syncdata-service/src/test/java/io/mosip/kernel/syncdata/test/service/SyncConfigDetailsServiceImplTest.java | grep -A 30 "public void getPublicKey"Length of output: 4388 Add defensive null-check for Your implementation correctly extracts certificate details and derives the public key, but two issues need attention:
🤖 Prompt for AI Agents |
||
| LOGGER.error("Failed to parse public key response: {}", e.getMessage()); | ||
| throw new SyncDataServiceException(SyncConfigDetailsErrorCode.SYNC_IO_EXCEPTION.getErrorCode(), | ||
| SyncConfigDetailsErrorCode.SYNC_IO_EXCEPTION.getErrorMessage(), e); | ||
|
|
@@ -345,4 +369,17 @@ private String getEncryptedData(String data, Machine machine) { | |
| SyncConfigDetailsErrorCode.SYNC_SERIALIZATION_ERROR.getErrorMessage()); | ||
| } | ||
| } | ||
|
|
||
| private String convertToPEM(PublicKey publicKey) { | ||
| String encoded = CryptoUtil.encodeBase64String(publicKey.getEncoded()); | ||
| StringBuilder pemBuilder = new StringBuilder(); | ||
| pemBuilder.append(BEGIN_KEY).append("\n"); | ||
| for (int i = 0; i < encoded.length(); i += 64) { | ||
| int endIndex = Math.min(i + 64, encoded.length()); | ||
| pemBuilder.append(encoded, i, endIndex).append("\n"); | ||
| } | ||
| pemBuilder.append(END_KEY); | ||
| return pemBuilder.toString(); | ||
| } | ||
|
|
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.