-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose guage metric for SSL cert timetamps
Signed-off-by: “Nithin <[email protected]>
- Loading branch information
“Nithin
committed
Mar 13, 2024
1 parent
fb95900
commit ffc3866
Showing
4 changed files
with
142 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 0 additions & 45 deletions
45
src/main/java/hlf/java/rest/client/config/SSLAuthFilesCreationHelper.java
This file was deleted.
Oops, something went wrong.
82 changes: 82 additions & 0 deletions
82
src/main/java/hlf/java/rest/client/config/SSLAuthFilesHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package hlf.java.rest.client.config; | ||
|
||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.security.KeyStore; | ||
import java.security.KeyStoreException; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.security.cert.Certificate; | ||
import java.security.cert.CertificateException; | ||
import java.security.cert.X509Certificate; | ||
import java.sql.Timestamp; | ||
import java.util.Base64; | ||
import java.util.Enumeration; | ||
import lombok.experimental.UtilityClass; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
@UtilityClass | ||
public class SSLAuthFilesHelper { | ||
|
||
void createSSLAuthFiles(KafkaProperties.SSLProperties kafkaSSLProperties) { | ||
|
||
log.info("Creating Kafka ssl keystore file"); | ||
createSSLFileFromBase64( | ||
kafkaSSLProperties.getSslKeystoreBase64(), kafkaSSLProperties.getSslKeystoreLocation()); | ||
|
||
log.info("Keystore file created at {}", kafkaSSLProperties.getSslKeystoreLocation()); | ||
|
||
log.info("Creating Kafka ssl truststore file"); | ||
createSSLFileFromBase64( | ||
kafkaSSLProperties.getSslTruststoreBase64(), kafkaSSLProperties.getSslTruststoreLocation()); | ||
|
||
log.info("TrustStore file created at {}", kafkaSSLProperties.getSslTruststoreLocation()); | ||
} | ||
|
||
private static void createSSLFileFromBase64( | ||
String sslKeystoreBase64, String sslKeystoreLocation) { | ||
log.info("Creating file at: {}", sslKeystoreLocation); | ||
try { | ||
final Path path = Paths.get(sslKeystoreLocation); | ||
final Path parentPath = path.getParent(); | ||
if (!Files.isDirectory(parentPath)) { | ||
log.info("Creating directory: {}", parentPath); | ||
Files.createDirectory(parentPath); | ||
} | ||
Files.write(path, Base64.getDecoder().decode(sslKeystoreBase64)); | ||
} catch (IOException e) { | ||
log.error("Failed to create the ssl auth file at location: {}", sslKeystoreLocation, e); | ||
} | ||
} | ||
|
||
Timestamp getExpiryTimestampForKeyStore(String keyStorePath, String keyStorePassword) | ||
throws CertificateException, KeyStoreException, IOException, NoSuchAlgorithmException { | ||
|
||
KeyStore keyStore = loadKeyStore(keyStorePath, keyStorePassword); | ||
|
||
Enumeration<String> aliases = keyStore.aliases(); | ||
while (aliases.hasMoreElements()) { | ||
String alias = aliases.nextElement(); | ||
Certificate cert = keyStore.getCertificate(alias); | ||
if (cert instanceof X509Certificate) { | ||
X509Certificate x509Cert = (X509Certificate) cert; | ||
return new Timestamp(x509Cert.getNotAfter().getTime()); | ||
} | ||
} | ||
|
||
throw new CertificateException( | ||
"Couldn't extract an instance of X509Certificate for fetching expiry details"); | ||
} | ||
|
||
private static KeyStore loadKeyStore(String path, String password) | ||
throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException { | ||
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); | ||
try (FileInputStream fis = new FileInputStream(path)) { | ||
keyStore.load(fis, password.toCharArray()); | ||
} | ||
return keyStore; | ||
} | ||
} |