Skip to content

Commit 64ab182

Browse files
committed
feat: export signed certificates to the same location as keystore if not already exported previously
1 parent 97c67f9 commit 64ab182

1 file changed

Lines changed: 31 additions & 2 deletions

File tree

src/main/kotlin/com/sakethh/linkora/Application.kt

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,17 @@ import kotlinx.serialization.encodeToString
2020
import kotlinx.serialization.json.Json
2121
import java.awt.Desktop
2222
import java.io.FileInputStream
23+
import java.io.FileOutputStream
24+
import java.io.FileWriter
2325
import java.net.Inet4Address
2426
import java.net.InetAddress
27+
import java.net.URI
2528
import java.nio.file.Files
2629
import java.nio.file.Paths
2730
import java.nio.file.StandardOpenOption
2831
import java.security.KeyStore
32+
import java.util.*
33+
import javax.security.auth.x500.X500Principal
2934
import kotlin.io.path.exists
3035
import kotlin.time.Duration.Companion.seconds
3136

@@ -37,6 +42,7 @@ fun main() {
3742
require(serverConfig.keyStorePassword != null && serverConfig.keyStorePassword.isNotBlank()) {
3843
"keyStorePassword value must be set in ServerConfig."
3944
}
45+
ServerConfiguration.exportSignedCertificates(serverKeyStore)
4046
embeddedServer(
4147
factory = Netty, configure = {
4248
sslConnector(builder = {
@@ -168,6 +174,26 @@ object ServerConfiguration {
168174
}
169175
}
170176

177+
fun exportSignedCertificates(keyStore: KeyStore) {
178+
val destinationPathForCer = jarDir.resolve("linkoraServerCert.cer")
179+
val destinationPathForPem = jarDir.resolve("linkoraServerCert.pem")
180+
val signedCertificate = keyStore.getCertificate(Constants.KEY_STORE_ALIAS)
181+
182+
if (!destinationPathForCer.exists()) {
183+
FileOutputStream(destinationPathForCer.toFile()).use {
184+
it.write(signedCertificate.encoded)
185+
}
186+
}
187+
188+
if (!destinationPathForPem.exists()) {
189+
FileWriter(destinationPathForPem.toFile()).use {
190+
it.write("-----BEGIN CERTIFICATE-----\n")
191+
it.write(Base64.getMimeEncoder().encodeToString(signedCertificate.encoded))
192+
it.write("\n-----END CERTIFICATE-----\n")
193+
}
194+
}
195+
}
196+
171197
fun createOrLoadServerKeystore(serverConfig: ServerConfig, forceCreate: Boolean = false): KeyStore {
172198
require(serverConfig.keyStorePassword != null && serverConfig.keyStorePassword.isNotBlank()) {
173199
"keyStorePassword value must be set in ServerConfig."
@@ -178,6 +204,8 @@ object ServerConfiguration {
178204
this.password = serverConfig.keyStorePassword
179205
this.domains = listOf(Inet4Address.getLocalHost().hostAddress)
180206
this.ipAddresses = listOf(Inet4Address.getLocalHost())
207+
this.daysValid = 365
208+
this.subject = X500Principal("CN=linkora-sync, OU=Linkora, O=Linkora, C=West Elizabeth")
181209
})
182210
}
183211
return jarDir.resolve("linkoraServerCert.jks").run {
@@ -221,10 +249,11 @@ fun Application.module() {
221249
maxFrameSize = Long.MAX_VALUE
222250
}
223251
configureEventsWebSocket()
224-
"http://" + serverConfig.hostAddress + ":" + serverConfig.httpPort + "/" + Route.Sync.SERVER_IS_CONFIGURED.name
252+
val serverConfiguredPage =
253+
"https://" + Inet4Address.getLocalHost().hostAddress + ":" + serverConfig.httpsPort + "/" + Route.Sync.SERVER_IS_CONFIGURED.name
225254
if (useSysEnvValues().not() && Desktop.isDesktopSupported() && Desktop.getDesktop()
226255
.isSupported(Desktop.Action.BROWSE)
227256
) {
228-
// Desktop.getDesktop().browse(URI(serverConfiguredPage))
257+
Desktop.getDesktop().browse(URI(serverConfiguredPage))
229258
}
230259
}

0 commit comments

Comments
 (0)