feat: release pipeline and env-supplied server key (#82)
* build: move group and version to gradle.properties * ci: add release-please versioning and release pipeline * feat(server): load the root key from the environment * ci: publish key-free server archives on release
This commit is contained in:
1 parent
8bd065fcb4
commit
dfeabdc581
20 files changed
+291
-33
No files matched your search
@@ -1,5 +1,6 @@
|
||||
package de.fiereu.openmmo.common.io
|
||||
|
||||
import java.io.File
|
||||
import java.io.InputStream
|
||||
import java.security.KeyFactory
|
||||
import java.security.interfaces.ECPrivateKey
|
||||
@@ -36,3 +37,15 @@ object PemKeyLoader {
|
||||
fun resource(name: String): InputStream =
|
||||
PemKeyLoader::class.java.classLoader.getResourceAsStream(name)
|
||||
?: error("classpath resource not found: $name")
|
||||
|
||||
/** Opens a PEM key. Inline wins over a file path, which wins over the classpath. */
|
||||
fun pemStream(pem: String?, path: String?, resourceName: String): InputStream =
|
||||
when {
|
||||
!pem.isNullOrBlank() -> pem.byteInputStream(Charsets.US_ASCII)
|
||||
!path.isNullOrBlank() -> File(path).inputStream()
|
||||
else ->
|
||||
PemKeyLoader::class.java.classLoader.getResourceAsStream(resourceName)
|
||||
?: error(
|
||||
"no private key. Set the key or key file environment variable, " +
|
||||
"or run a local build that generates '$resourceName'.")
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package de.fiereu.openmmo.common.io
|
||||
|
||||
import io.kotest.assertions.throwables.shouldThrow
|
||||
import io.kotest.core.spec.style.FunSpec
|
||||
import io.kotest.matchers.shouldBe
|
||||
import java.security.KeyPairGenerator
|
||||
import java.security.spec.ECGenParameterSpec
|
||||
import java.util.Base64
|
||||
import kotlin.io.path.createTempFile
|
||||
import kotlin.io.path.writeText
|
||||
|
||||
private fun newKeyPem(): Pair<String, ByteArray> {
|
||||
val generator = KeyPairGenerator.getInstance("EC")
|
||||
generator.initialize(ECGenParameterSpec("secp256r1"))
|
||||
val key = generator.generateKeyPair().private
|
||||
val body = Base64.getMimeEncoder(64, "\n".toByteArray()).encodeToString(key.encoded)
|
||||
return "-----BEGIN EC PRIVATE KEY-----\n$body\n-----END EC PRIVATE KEY-----\n" to key.encoded
|
||||
}
|
||||
|
||||
private fun pemFile(pem: String) = createTempFile(suffix = ".pem").also { it.writeText(pem) }
|
||||
|
||||
class PemSourceTest :
|
||||
FunSpec({
|
||||
test("reads the key from inline content") {
|
||||
val (pem, encoded) = newKeyPem()
|
||||
PemKeyLoader.loadEcPrivate(pemStream(pem, null, "missing.pem")).encoded shouldBe encoded
|
||||
}
|
||||
|
||||
test("reads the key from a file") {
|
||||
val (pem, encoded) = newKeyPem()
|
||||
PemKeyLoader.loadEcPrivate(pemStream(null, pemFile(pem).toString(), "missing.pem"))
|
||||
.encoded shouldBe encoded
|
||||
}
|
||||
|
||||
test("inline content wins over a file") {
|
||||
val (pem, encoded) = newKeyPem()
|
||||
val other = pemFile(newKeyPem().first)
|
||||
PemKeyLoader.loadEcPrivate(pemStream(pem, other.toString(), "missing.pem")).encoded shouldBe
|
||||
encoded
|
||||
}
|
||||
|
||||
test("treats blank values as unset") {
|
||||
val (pem, encoded) = newKeyPem()
|
||||
PemKeyLoader.loadEcPrivate(pemStream(" ", pemFile(pem).toString(), "missing.pem"))
|
||||
.encoded shouldBe encoded
|
||||
}
|
||||
|
||||
test("fails with a helpful message when nothing supplies a key") {
|
||||
val error = shouldThrow<IllegalStateException> { pemStream(null, null, "missing.pem") }
|
||||
error.message!!.contains("environment variable") shouldBe true
|
||||
}
|
||||
})
|
||||
Reference in new issue
Block a user