application.CertInfo.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of specmatic-executable Show documentation
Show all versions of specmatic-executable Show documentation
Command-line standalone executable jar for Specmatic
package application
import io.ktor.network.tls.certificates.*
import io.specmatic.core.APPLICATION_NAME_LOWER_CASE
import io.specmatic.core.KeyData
import io.specmatic.core.utilities.exitWithMessage
import java.io.File
import java.security.KeyStore
data class CertInfo(val keyStoreFile: String = "", val keyStoreDir: String = "", val keyStorePassword: String = "forgotten", val keyStoreAlias: String = "${APPLICATION_NAME_LOWER_CASE}proxy", val keyPassword: String = "forgotten") {
fun getHttpsCert(): KeyData? {
return when {
keyStoreFile.isNotBlank() -> KeyData(keyStore = loadKeyStoreFromFile(keyStoreFile, keyStorePassword), keyStorePassword = keyStorePassword, keyAlias = keyStoreAlias, keyPassword = keyPassword)
keyStoreDir.isNotBlank() -> createKeyStore(keyStoreDir, keyStorePassword, keyStoreAlias, keyPassword)
else -> null
}
}
}
private fun createKeyStore(keyStoreDirPath: String, keyStorePassword: String, keyAlias: String, keyPassword: String): KeyData {
val keyStoreDir = File(keyStoreDirPath)
if (!keyStoreDir.exists())
keyStoreDir.mkdirs()
val filename = "$APPLICATION_NAME_LOWER_CASE.jks"
val keyStoreFile = keyStoreDir.resolve(filename)
if (keyStoreFile.exists()) {
val deleteStatus = keyStoreFile.delete()
if (!deleteStatus) {
exitWithMessage("Unable to delete existing keystore file at $keyStoreFile")
}
}
val keyStore = generateCertificate(keyStoreFile, jksPassword = keyStorePassword, keyAlias = keyAlias, keyPassword = keyPassword)
return KeyData(keyStore = keyStore, keyStorePassword = keyStorePassword, keyAlias = keyAlias, keyPassword = keyPassword)
}
private fun loadKeyStoreFromFile(keyStoreFile: String, keyStorePassword: String): KeyStore {
val certFilePath = File(keyStoreFile)
val keyStoreType = when (certFilePath.extension.lowercase()) {
"jks" -> "JKS"
"pfx" -> "PKCS12"
else -> exitWithMessage("The certificate file must be either in Java Key Store or PKCS12 format")
}
return KeyStore.getInstance(keyStoreType).apply {
this.load(certFilePath.inputStream(), keyStorePassword.toCharArray())
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy