I’m trying to publish a `fatJar` or a `Dist` from ...
# ktor
g
I’m trying to publish a
fatJar
or a
Dist
from my ktor app and I’m having troubles with local keys. If I run the project via IDE or in a remote server with ENV_Keys, everything works, but when I try to publish it to have a localhost “runnable binary” i get:
Copy code
Exception in thread "main" java.lang.IllegalArgumentException: Empty key
        at java.base/javax.crypto.spec.SecretKeySpec.<init>(SecretKeySpec.java:110)
        at io.ktor.sessions.SessionTransportTransformerMessageAuthentication.<init>(SessionTransportTransformerMessageAuthentication.kt:25)
        at io.ktor.sessions.SessionTransportTransformerMessageAuthentication.<init>(SessionTransportTransformerMessageAuthentication.kt:24)
        at main.ApplicationKt$installFeatures$3.invoke(Application.kt:119)
        at main.ApplicationKt$installFeatures$3.invoke(Application.kt:115)
        at io.ktor.sessions.Sessions$Feature.install(Sessions.kt:56)
        at io.ktor.sessions.Sessions$Feature.install(Sessions.kt:53)
        at io.ktor.application.ApplicationFeatureKt.install(ApplicationFeature.kt:68)
        at main.ApplicationKt.installFeatures(Application.kt:115)
        at main.ApplicationKt.module(Application.kt:70)
        at main.ApplicationKt.module$default(Application.kt:54)
more in thread. How can I solve this?
Copy code
install(Sessions) {
    cookie<Session>(SESSION_COOKIE) {
        cookie.extensions["SameSite"] = "lax"
        transform(SessionTransportTransformerMessageAuthentication(hex(getSecret(COOKIE_HASH))))
    }
}
Copy code
fun getSecret(envKey: String): String {
    val path = "./local.properties"
    val key = if (File(path).exists()) {
        Properties().apply { load(FileInputStream(path)) }.getProperty(envKey, "")
    } else System.getenv(envKey) ?: ""
    println(key) <<<<<< it prints correctly when running via IDE.
    return key
}
a
How do you pass the env variables?
g
locally they’re in the local.properties file:
Copy code
COOKIE_HASH=123...
I’ve a “AppConfig” file with some constants:
Copy code
const val COOKIE_HASH = "COOKIE_HASH"
and the method above will try to fetch them from local.properties if running local or system.getEnv if remote
if I run via IDE this works fine
a
And the file
"./local.properties"
does exist relatively to the working directory?
g
That question makes me think that possible when creating a distribution this file is ignored 🤔 or not copied...
I'll have to check that, nice catch.
Copy code
println("File(path).exists()? ${File(path).exists()}")
will print
File(path).exists()? false
How can i add this secrets in a safe way to a `fatJar`/`Dist` ? Or if there’s a better option i’m all ears 🙂
made it work by changing the method to:
Copy code
fun <T : Any> T.getSecret(envKey: String): String {
    val inputStream: InputStream = javaClass.classLoader.getResourceAsStream("local.properties") as InputStream
    return try {
        Properties().apply { load(inputStream) }.getProperty(envKey, "")
    } catch (e: IOException) {
        System.getenv(envKey)
    } finally {
        inputStream.close()
    }
}