diff --git a/.env.example b/.env.example index 03a5c8a..a78e43e 100644 --- a/.env.example +++ b/.env.example @@ -1,18 +1,22 @@ -# Copy this file to `.env` and adjust the values for your environment. -# `.env` is gitignored and must never be committed. - -LOGIN_DB_HOST=localhost -LOGIN_DB_PORT=20011 -LOGIN_DB_NAME=openmmo_login_db -LOGIN_DB_USER=openmmo_login_user -LOGIN_DB_PASSWORD=changeMe! -LOGIN_DB_SEED_DEV=true - -GAME_DB_HOST=localhost -GAME_DB_PORT=20021 -GAME_DB_NAME=openmmo_game_db -GAME_DB_USER=openmmo_game_user -GAME_DB_PASSWORD=changeMe! -GAME_DB_SEED_DEV=true - -OPENMMO_SESSION_SECRET= \ No newline at end of file +# Copy this file to `.env` and adjust the values for your environment. +# `.env` is gitignored and must never be committed. + +LOGIN_DB_HOST=localhost +LOGIN_DB_PORT=20011 +LOGIN_DB_NAME=openmmo_login_db +LOGIN_DB_USER=openmmo_login_user +LOGIN_DB_PASSWORD=changeMe! +LOGIN_DB_SEED_DEV=true + +GAME_DB_HOST=localhost +GAME_DB_PORT=20021 +GAME_DB_NAME=openmmo_game_db +GAME_DB_USER=openmmo_game_user +GAME_DB_PASSWORD=changeMe! +GAME_DB_SEED_DEV=true + +OPENMMO_SESSION_SECRET= + +# Private key for both servers. Leave empty for development, set one in production. +OPENMMO_GAME_PRIVATE_KEY= +OPENMMO_GAME_PRIVATE_KEY_FILE= \ No newline at end of file diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5071207..4f2a9d6 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -13,6 +13,28 @@ concurrency: cancel-in-progress: true jobs: + pr-title: + name: Conventional PR title + if: github.event_name == 'pull_request' + runs-on: ubuntu-latest + steps: + - name: Check the title against the Conventional Commits spec + env: + PR_TITLE: ${{ github.event.pull_request.title }} + run: | + types='build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test' + if [[ "$PR_TITLE" =~ ^($types)(\([a-z0-9._/-]+\))?!?:\ .+ ]]; then + echo "OK: $PR_TITLE" + exit 0 + fi + echo "::error::PR title is not a Conventional Commit: $PR_TITLE" + echo "Expected '(): ', for example:" + echo " feat(game): start a wild battle when a player steps in grass" + echo " fix(codegen): merge the duplicate branches in the text parser" + echo "Valid types: ${types//|/, }" + echo "Append '!' after the type or scope to mark a breaking change." + exit 1 + ci: name: Spotless and check runs-on: ubuntu-latest @@ -37,9 +59,10 @@ jobs: - name: Apply Spotless and push formatting changes if: >- - github.event_name == 'pull_request' && - github.event.pull_request.head.repo.full_name == github.repository && - github.event.pull_request.user.login != 'renovate[bot]' + github.event_name == 'pull_request' && + github.event.pull_request.head.repo.full_name == github.repository && + github.event.pull_request.user.login != 'renovate[bot]' && + !startsWith(github.head_ref, 'release-please--') run: | ./gradlew spotlessApply if git diff --quiet; then diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..048f2a5 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,76 @@ +name: Release + +on: + push: + branches: [ "master" ] + +permissions: + contents: write + pull-requests: write + issues: write + +concurrency: + group: release-${{ github.ref }} + cancel-in-progress: false + +jobs: + release-please: + name: Prepare release + runs-on: ubuntu-latest + outputs: + released: ${{ steps.release.outputs.releases_created }} + tag: ${{ steps.release.outputs.tag_name }} + steps: + - name: Run release-please + id: release + uses: googleapis/release-please-action@v4 + with: + # A PAT is needed for CI to run on the release pull request. + token: ${{ secrets.RELEASE_PLEASE_TOKEN || secrets.GITHUB_TOKEN }} + config-file: release-please-config.json + manifest-file: .release-please-manifest.json + + publish: + name: Publish server distributions + needs: release-please + if: needs.release-please.outputs.released == 'true' + runs-on: ubuntu-latest + steps: + - name: Checkout the release tag + uses: actions/checkout@v7 + with: + ref: ${{ needs.release-please.outputs.tag }} + submodules: recursive + + - name: Set up JDK 25 + uses: actions/setup-java@v5 + with: + distribution: temurin + java-version: "25" + + - name: Set up Gradle + uses: gradle/actions/setup-gradle@v6 + + # No ROMs on the runner, so dialog ids are placeholders. + - name: Build distributions + run: ./gradlew :server.login:assembleDist :server.game:assembleDist + + - name: Check no keys are bundled + run: | + found=$(tar -tf server.login/build/distributions/*.tar \ + && tar -tf server.game/build/distributions/*.tar) + if grep -q '\.pem$' <<<"$found"; then + echo "::error::a distribution bundles a key" + grep '\.pem$' <<<"$found" + exit 1 + fi + echo "No keys in the distributions." + + - name: Attach distributions to the release + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + gh release upload "${{ needs.release-please.outputs.tag }}" \ + server.login/build/distributions/* \ + server.game/build/distributions/* \ + --clobber diff --git a/.release-please-manifest.json b/.release-please-manifest.json new file mode 100644 index 0000000..466df71 --- /dev/null +++ b/.release-please-manifest.json @@ -0,0 +1,3 @@ +{ + ".": "0.1.0" +} diff --git a/README.md b/README.md index 0d37671..350723a 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,7 @@ - [Description](#description) - [Building](#building) - [Configuration](#configuration) +- [Releases](#releases) - [Documentation](#wiki) - [License](LICENSE) - [Disclaimer](#disclaimer) @@ -49,6 +50,31 @@ For local-only tweaks to the container setup, create a Docker Compose merges it automatically on `docker compose up`. For deployment,supply a proper `.env` and run `docker compose -f docker-compose.yml up -d` to skip any override. +### Server key + +Both servers share one private key. A local build generates it, so development +needs no setup. + +Released archives ship no keys. Generate a pair with +`./gradlew :keys:generateGame` and pass the private key to both servers through +`OPENMMO_GAME_PRIVATE_KEY` (the PEM) or `OPENMMO_GAME_PRIVATE_KEY_FILE` (a path +to it). Clients need a patched build carrying the matching public key. + +## Releases + +[release-please](https://github.com/googleapis/release-please) cuts releases from +the commit history. Pull requests are squash merged, so their titles become the +commit messages it reads and must follow +[Conventional Commits](https://www.conventionalcommits.org/). CI rejects titles +that do not. + +`feat` bumps the minor version, `fix` the patch version. Below `1.0.0` a +breaking change bumps the minor version instead of jumping to `1.0.0`. + +Every push to `master` opens or updates a release pull request. Merging it tags +the release and attaches the server archives. The version lives in +`gradle.properties` and applies to every module, do not edit it by hand. + ## Wiki The documentation wiki can be found [here](https://openmmo.readthedocs.io/en/latest/). diff --git a/buildSrc/src/main/kotlin/keys.gradle.kts b/buildSrc/src/main/kotlin/keys.gradle.kts index 24bd375..8907ef8 100644 --- a/buildSrc/src/main/kotlin/keys.gradle.kts +++ b/buildSrc/src/main/kotlin/keys.gradle.kts @@ -82,6 +82,9 @@ tasks.register("copyKeys") { dependsOn("copyKeysChat", "copyKeysGame", "copyKeysFeed") } +// Generated keys are for local runs. Published archives ship none. +tasks.withType().configureEach { exclude("*.pem") } + val keyCopyTasks = listOf( "copyPublicKeyChat", diff --git a/common/src/main/kotlin/de/fiereu/openmmo/common/io/PemKeyLoader.kt b/common/src/main/kotlin/de/fiereu/openmmo/common/io/PemKeyLoader.kt index 9783ec6..674eede 100644 --- a/common/src/main/kotlin/de/fiereu/openmmo/common/io/PemKeyLoader.kt +++ b/common/src/main/kotlin/de/fiereu/openmmo/common/io/PemKeyLoader.kt @@ -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'.") + } diff --git a/common/src/test/kotlin/de/fiereu/openmmo/common/io/PemSourceTest.kt b/common/src/test/kotlin/de/fiereu/openmmo/common/io/PemSourceTest.kt new file mode 100644 index 0000000..e6da95f --- /dev/null +++ b/common/src/test/kotlin/de/fiereu/openmmo/common/io/PemSourceTest.kt @@ -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 { + 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 { pemStream(null, null, "missing.pem") } + error.message!!.contains("environment variable") shouldBe true + } + }) diff --git a/gradle.properties b/gradle.properties index 15ba851..03000b7 100644 --- a/gradle.properties +++ b/gradle.properties @@ -5,6 +5,11 @@ org.gradle.configuration-cache=true org.gradle.jvmargs=-Xmx3g -XX:MaxMetaspaceSize=1g kotlin.daemon.jvmargs=-Xmx2g -XX:MaxMetaspaceSize=1g +group=de.fiereu.openmmo +# x-release-please-start-version +version=0.1.0 +# x-release-please-end + # May be overridden by the user pokemmo.executable=C:/Program Files/PokeMMO/PokeMMO.exe pokemmo.workingDir=C:/Program Files/PokeMMO \ No newline at end of file diff --git a/release-please-config.json b/release-please-config.json new file mode 100644 index 0000000..8301388 --- /dev/null +++ b/release-please-config.json @@ -0,0 +1,31 @@ +{ + "$schema": "https://raw.githubusercontent.com/googleapis/release-please/main/schemas/config.json", + "packages": { + ".": { + "release-type": "simple", + "package-name": "openmmo", + "changelog-path": "CHANGELOG.md", + "include-component-in-tag": false, + "bump-minor-pre-major": true, + "extra-files": [ + { + "type": "generic", + "path": "gradle.properties" + } + ] + } + }, + "changelog-sections": [ + { "type": "feat", "section": "Features" }, + { "type": "fix", "section": "Bug Fixes" }, + { "type": "perf", "section": "Performance Improvements" }, + { "type": "refactor", "section": "Code Refactoring" }, + { "type": "docs", "section": "Documentation" }, + { "type": "build", "section": "Build System", "hidden": true }, + { "type": "ci", "section": "Continuous Integration", "hidden": true }, + { "type": "chore", "section": "Miscellaneous Chores", "hidden": true }, + { "type": "style", "section": "Styles", "hidden": true }, + { "type": "test", "section": "Tests", "hidden": true }, + { "type": "revert", "section": "Reverts" } + ] +} diff --git a/server.game/build.gradle.kts b/server.game/build.gradle.kts index 077648b..85adcc9 100644 --- a/server.game/build.gradle.kts +++ b/server.game/build.gradle.kts @@ -8,10 +8,6 @@ plugins { alias(libs.plugins.ksp) } -group = "de.fiereu.openmmo" - -version = "0.1.0" - application { mainClass.set("de.fiereu.openmmo.server.game.MainKt") } jooqDb { packageName = "de.fiereu.openmmo.db.game" } diff --git a/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/config/ConfigLoader.kt b/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/config/ConfigLoader.kt index a39c495..961350f 100644 --- a/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/config/ConfigLoader.kt +++ b/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/config/ConfigLoader.kt @@ -1,7 +1,11 @@ package de.fiereu.openmmo.server.game.config +import com.typesafe.config.Config import com.typesafe.config.ConfigFactory +private fun Config.stringOrNull(path: String): String? = + if (hasPath(path)) getString(path) else null + object ConfigLoader { fun load(): GameServerConfig { val config = ConfigFactory.load() @@ -12,6 +16,8 @@ object ConfigLoader { port = config.getInt("server.port"), checksumSize = config.getInt("server.checksumSize"), rootKeyResource = config.getString("server.rootKeyResource"), + rootKey = config.stringOrNull("server.rootKey"), + rootKeyFile = config.stringOrNull("server.rootKeyFile"), sessionSecret = secret.toByteArray(Charsets.UTF_8), db = DbConfig( diff --git a/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/config/GameServerConfig.kt b/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/config/GameServerConfig.kt index f2e44d8..a3894e7 100644 --- a/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/config/GameServerConfig.kt +++ b/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/config/GameServerConfig.kt @@ -7,6 +7,8 @@ data class GameServerConfig( val rootKeyResource: String, val sessionSecret: ByteArray, val db: DbConfig = DbConfig(), + val rootKey: String? = null, + val rootKeyFile: String? = null, ) { override fun equals(other: Any?): Boolean = other is GameServerConfig && @@ -14,6 +16,8 @@ data class GameServerConfig( port == other.port && checksumSize == other.checksumSize && rootKeyResource == other.rootKeyResource && + rootKey == other.rootKey && + rootKeyFile == other.rootKeyFile && sessionSecret.contentEquals(other.sessionSecret) && db == other.db @@ -22,6 +26,8 @@ data class GameServerConfig( h = h * 31 + port h = h * 31 + checksumSize h = h * 31 + rootKeyResource.hashCode() + h = h * 31 + rootKey.hashCode() + h = h * 31 + rootKeyFile.hashCode() h = h * 31 + sessionSecret.contentHashCode() h = h * 31 + db.hashCode() return h diff --git a/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/di/GameServerModule.kt b/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/di/GameServerModule.kt index 8847a75..cd7cc6a 100644 --- a/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/di/GameServerModule.kt +++ b/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/di/GameServerModule.kt @@ -6,7 +6,7 @@ import dagger.Module import dagger.Provides import de.fiereu.openmmo.common.auth.SessionTokenVerifier import de.fiereu.openmmo.common.io.PemKeyLoader -import de.fiereu.openmmo.common.io.resource +import de.fiereu.openmmo.common.io.pemStream import de.fiereu.openmmo.server.game.config.GameServerConfig import de.fiereu.openmmo.server.game.script.ScriptRegistry import de.fiereu.openmmo.server.game.storage.CharacterRepository @@ -34,7 +34,8 @@ object GameServerModule { @Provides @Singleton fun rootKey(config: GameServerConfig): ECPrivateKey = - PemKeyLoader.loadEcPrivate(resource(config.rootKeyResource)) + PemKeyLoader.loadEcPrivate( + pemStream(config.rootKey, config.rootKeyFile, config.rootKeyResource)) @Provides @Singleton diff --git a/server.game/src/main/resources/application.conf b/server.game/src/main/resources/application.conf index d5c743f..129d055 100644 --- a/server.game/src/main/resources/application.conf +++ b/server.game/src/main/resources/application.conf @@ -3,6 +3,8 @@ server { port = 7777 checksumSize = 2 rootKeyResource = "game.private.pem" + rootKey = ${?OPENMMO_GAME_PRIVATE_KEY} + rootKeyFile = ${?OPENMMO_GAME_PRIVATE_KEY_FILE} sessionSecret = "dev-only-secret-do-not-use-in-production" sessionSecret = ${?OPENMMO_SESSION_SECRET} } diff --git a/server.login/build.gradle.kts b/server.login/build.gradle.kts index 54dbfba..52dcb84 100644 --- a/server.login/build.gradle.kts +++ b/server.login/build.gradle.kts @@ -8,10 +8,6 @@ plugins { alias(libs.plugins.ksp) } -group = "de.fiereu.openmmo" - -version = "0.1.0" - application { mainClass.set("de.fiereu.openmmo.server.login.MainKt") } jooqDb { packageName = "de.fiereu.openmmo.db.login" } diff --git a/server.login/src/main/kotlin/de/fiereu/openmmo/server/login/config/ConfigLoader.kt b/server.login/src/main/kotlin/de/fiereu/openmmo/server/login/config/ConfigLoader.kt index 48a0722..9087bf9 100644 --- a/server.login/src/main/kotlin/de/fiereu/openmmo/server/login/config/ConfigLoader.kt +++ b/server.login/src/main/kotlin/de/fiereu/openmmo/server/login/config/ConfigLoader.kt @@ -1,7 +1,11 @@ package de.fiereu.openmmo.server.login.config +import com.typesafe.config.Config import com.typesafe.config.ConfigFactory +private fun Config.stringOrNull(path: String): String? = + if (hasPath(path)) getString(path) else null + object ConfigLoader { fun load(): LoginServerConfig { val config = ConfigFactory.load() @@ -12,6 +16,8 @@ object ConfigLoader { port = config.getInt("server.port"), checksumSize = config.getInt("server.checksumSize"), rootKeyResource = config.getString("server.rootKeyResource"), + rootKey = config.stringOrNull("server.rootKey"), + rootKeyFile = config.stringOrNull("server.rootKeyFile"), sessionSecret = secret.toByteArray(Charsets.UTF_8), db = DbConfig( diff --git a/server.login/src/main/kotlin/de/fiereu/openmmo/server/login/config/LoginServerConfig.kt b/server.login/src/main/kotlin/de/fiereu/openmmo/server/login/config/LoginServerConfig.kt index bf04a23..7a25fb1 100644 --- a/server.login/src/main/kotlin/de/fiereu/openmmo/server/login/config/LoginServerConfig.kt +++ b/server.login/src/main/kotlin/de/fiereu/openmmo/server/login/config/LoginServerConfig.kt @@ -7,6 +7,8 @@ data class LoginServerConfig( val rootKeyResource: String, val sessionSecret: ByteArray, val db: DbConfig = DbConfig(), + val rootKey: String? = null, + val rootKeyFile: String? = null, ) { override fun equals(other: Any?): Boolean = other is LoginServerConfig && @@ -14,6 +16,8 @@ data class LoginServerConfig( port == other.port && checksumSize == other.checksumSize && rootKeyResource == other.rootKeyResource && + rootKey == other.rootKey && + rootKeyFile == other.rootKeyFile && sessionSecret.contentEquals(other.sessionSecret) && db == other.db @@ -22,6 +26,8 @@ data class LoginServerConfig( h = h * 31 + port h = h * 31 + checksumSize h = h * 31 + rootKeyResource.hashCode() + h = h * 31 + rootKey.hashCode() + h = h * 31 + rootKeyFile.hashCode() h = h * 31 + sessionSecret.contentHashCode() h = h * 31 + db.hashCode() return h diff --git a/server.login/src/main/kotlin/de/fiereu/openmmo/server/login/di/LoginServerModule.kt b/server.login/src/main/kotlin/de/fiereu/openmmo/server/login/di/LoginServerModule.kt index 9fda9f8..f4ec777 100644 --- a/server.login/src/main/kotlin/de/fiereu/openmmo/server/login/di/LoginServerModule.kt +++ b/server.login/src/main/kotlin/de/fiereu/openmmo/server/login/di/LoginServerModule.kt @@ -7,7 +7,7 @@ import dagger.Module import dagger.Provides import de.fiereu.openmmo.common.auth.SessionTokenIssuer import de.fiereu.openmmo.common.io.PemKeyLoader -import de.fiereu.openmmo.common.io.resource +import de.fiereu.openmmo.common.io.pemStream import de.fiereu.openmmo.server.login.auth.JooqUserStore import de.fiereu.openmmo.server.login.auth.UserService import de.fiereu.openmmo.server.login.config.LoginServerConfig @@ -35,7 +35,8 @@ abstract class LoginServerModule { @Provides @Singleton fun rootKey(config: LoginServerConfig): ECPrivateKey = - PemKeyLoader.loadEcPrivate(resource(config.rootKeyResource)) + PemKeyLoader.loadEcPrivate( + pemStream(config.rootKey, config.rootKeyFile, config.rootKeyResource)) @Provides @Singleton diff --git a/server.login/src/main/resources/application.conf b/server.login/src/main/resources/application.conf index ff2c553..01b05f7 100644 --- a/server.login/src/main/resources/application.conf +++ b/server.login/src/main/resources/application.conf @@ -3,6 +3,8 @@ server { port = 2106 checksumSize = 16 rootKeyResource = "game.private.pem" + rootKey = ${?OPENMMO_GAME_PRIVATE_KEY} + rootKeyFile = ${?OPENMMO_GAME_PRIVATE_KEY_FILE} sessionSecret = "dev-only-secret-do-not-use-in-production" sessionSecret = ${?OPENMMO_SESSION_SECRET} }