Guilherme Delgado
07/18/2023, 1:58 PMfatJar
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:
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?Guilherme Delgado
07/18/2023, 1:59 PMinstall(Sessions) {
cookie<Session>(SESSION_COOKIE) {
cookie.extensions["SameSite"] = "lax"
transform(SessionTransportTransformerMessageAuthentication(hex(getSecret(COOKIE_HASH))))
}
}
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
}
Aleksei Tirman [JB]
07/18/2023, 2:50 PMGuilherme Delgado
07/18/2023, 3:18 PMCOOKIE_HASH=123...
I’ve a “AppConfig” file with some constants:
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 remoteGuilherme Delgado
07/18/2023, 3:18 PMAleksei Tirman [JB]
07/18/2023, 5:01 PM"./local.properties"
does exist relatively to the working directory?Guilherme Delgado
07/18/2023, 5:42 PMGuilherme Delgado
07/18/2023, 5:42 PMGuilherme Delgado
07/18/2023, 10:33 PMprintln("File(path).exists()? ${File(path).exists()}")
will print
File(path).exists()? falseHow can i add this secrets in a safe way to a `fatJar`/`Dist` ? Or if there’s a better option i’m all ears 🙂
Guilherme Delgado
07/18/2023, 11:48 PMfun <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()
}
}