Unverified Commit 694e3cb8 authored by Minseong Choi's avatar Minseong Choi 💬
Browse files

feat(rcon): provision per-server RCON so the console, player list and permissions work

A server created through the panel never had RCON. CreateServer built a
MinecraftServerSpec without a Rcon block at all, so the field took its zero value
and every downstream consumer read Enabled=false. Nothing failed loudly: the
operator skips the probe when RCON is off and marks the server Ready on pod
readiness alone, so the panel showed "运行中" for a server the control plane could
not talk to. Everything that rides the write channel (spec §8 写=RCON) was dead —
the online-player list returned nothing because Status.Players is only ever
sampled by the probe, and console writes answered 503 ErrConsoleUnavailable
because internal/api/console.go refuses when Enabled is false.

The whole RCON machinery already existed — builders gate the service port,
container port, preStop save-and-stop hook and the RCON_* env on Spec.Rcon,
the reconciler probes and reports, console.go dials, the NetworkPolicy opens
25575 to {api, operator}. The only thing missing was that nobody ever turned it
on or created a password. This wires the three layers that were absent.

Provisioning lives in the operator, not in felis-api. felis-api holds secrets:get
and not create, and giving it create solely to mint a password it immediately
stops caring about (console.go re-reads the Secret at command time) would widen
the API's powers for nothing. The operator already reads every Secret in the
namespace, so adding create there grants no read it did not have. It also makes
provisioning declarative: a Secret deleted by hand comes back on the next pass, a
controller reference garbage-collects it with the server so no delete path has to
remember it, and a server that predates RCON only needs spec.rcon filled in for
the password to appear. The name comes from naming.RconSecretName so felis-api,
`felis setup` and the operator cannot drift apart on it.

RCON is enabled per system service rather than by default, because enabling it on
a backend that serves no RCON listener is destructive rather than merely useless:
the operator gates readiness on the probe, so such a server never leaves Starting
and is eventually marked Failed. The login limbo is exactly that backend
(LOOHP/Limbo has no RCON) and it is the front door, so it stays off; the lobby
runs Paper and is administered through the panel like any other server, so it is
on.

Paper only reads RCON settings from server.properties, so the operator's injected
RCON_PASSWORD did nothing on its own — felis-lobby's entrypoint now writes the
three keys on every boot. Rewriting them each time makes the copy in the world
volume derived state rather than the source of truth, so an owner who edits them
through the panel's file editor cannot lock the control plane out of their own
server. Without a password it sets enable-rcon=false and warns rather than
refusing to start: unlike the forwarding secret, a missing RCON password degrades
the server rather than making it unsafe.

That password landing in server.properties is a §286 exposure (RCON 密码绝不下发
前端), since server.properties is readable through the file editor. It is redacted
on read rather than the file being denied outright the way config/paper-global.yml
is: the forwarding secret is cluster-wide material that merely happens to sit in
the volume, whereas server.properties is the single most-edited config an owner
has, and hiding one line should not cost them MOTD, difficulty and view-distance.
The write path is deliberately left alone — the boot-time rewrite restores the
real value, which is what makes redacting rather than denying safe here.

Also guards idle auto-stop on Rcon.Enabled. Status.Players is only meaningful
when the probe ran; with RCON off it keeps its zero value, which that branch would
have read as "empty" and used to stop a server full of people. AutoStopEnabled is
not currently settable through any path, so this is a latent footgun rather than a
live bug, but it is one line and the alternative is discovering it in production.

Checks: the operator provisions a missing Secret with a 32-hex-char password and a
controller reference, and does not rotate an existing one; idle auto-stop stays
inert without RCON; the editor redacts rcon.password from the world root's
server.properties while leaving the rest of the file (and a plugin's own nested
copy) intact; login has RCON off and lobby has it on with the shared secret name;
CreateServer sets the block. That last one departs from K8sCluster being
integration-tested against a live cluster: this defect was a struct literal
missing a field, it shipped, and a fake client is enough to pin a struct literal.

Existing servers are NOT migrated by this change — CreateServer only covers new
ones and ensureSystemServers is create-if-absent, so a `felis setup` re-run will
not touch an existing lobby. A deployed install additionally needs the
felis-lobby image rebuilt and re-imported for the entrypoint change, and its pods
recreated, before the RCON keys reach server.properties.
parent b3989fa4
Loading
Loading
Loading
Loading
+33 −3
Changes for cmd/felis/systemservers.go: 33 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -48,6 +48,13 @@ type systemServerSpec struct {
	storage        string // world PVC size
	fallbackServer string // "" = none (refuse when down); never the lobby
	healthHTTPPort int32  // > 0 → gate readiness on an HTTP health endpoint
	// rcon opts a system service into the RCON write channel. It is per-service and
	// NOT a default, because enabling it on a backend that runs no RCON listener is
	// actively destructive rather than merely useless: the operator gates readiness
	// on the probe, so the server would never leave Starting and would eventually be
	// marked Failed. The login limbo is exactly that case (LOOHP/Limbo has no RCON),
	// and it is the front door — taking it down locks everyone out.
	rcon bool
	// env are extra plain (non-secret) environment variables baked into the pod.
	// System-service configuration derived from the deployment (the internal API
	// URL, root domain, lobby name) rides here; secrets never do — the service
@@ -55,6 +62,24 @@ type systemServerSpec struct {
	env []v1alpha1.EnvVar
}

// systemRcon renders the RCON block for a system service. The secret name comes
// from naming.RconSecretName — the same convention felis-api writes for user
// servers and the operator provisions against — so a system service is not a
// second, parallel way of doing this. Port is left 0 so the operator's default is
// the only place the number lives.
func systemRcon(in systemServerSpec) v1alpha1.RconSpec {
	if !in.rcon {
		return v1alpha1.RconSpec{Enabled: false}
	}
	return v1alpha1.RconSpec{
		Enabled: true,
		SecretRef: v1alpha1.SecretKeyRef{
			Name: naming.RconSecretName(in.name),
			Key:  naming.RconSecretKey,
		},
	}
}

// felisLimboHealthPort is the port the felis-limbo readiness plugin serves its
// HTTP health endpoint on. The login system service gates pod readiness on it so
// "the limbo has finished starting" — not merely "the game socket is bound" —
@@ -82,8 +107,10 @@ const (
//   - permits reserved names (login/lobby) via ValidateSystemServerName,
//   - sets DesiredState=Running (the service is up the moment it exists),
//   - sets ReaperExempt=true and AutostartPolicy=public,
//   - leaves RCON disabled (LOOHP/Limbo has none; readiness is gated on pod
//     TCP/HTTP health, not an RCON probe — see the operator reconciler).
//   - enables RCON only where the image actually serves it (in.rcon): the lobby
//     is Paper and needs the write channel like any user server, while the login
//     limbo has no RCON listener at all and gates readiness on pod TCP/HTTP
//     health instead — see the operator reconciler.
func buildSystemServer(in systemServerSpec, namespace string) (*v1alpha1.MinecraftServer, error) {
	if err := naming.ValidateSystemServerName(in.name); err != nil {
		return nil, fmt.Errorf("invalid name: %w", err)
@@ -141,7 +168,7 @@ func buildSystemServer(in systemServerSpec, namespace string) (*v1alpha1.Minecra
			// forwarding), backends run offline-mode; the proxy is the one place
			// online-mode is true (spec §8, §11).
			OnlineMode: false,
			Rcon:       v1alpha1.RconSpec{Enabled: false},
			Rcon:       systemRcon(in),
			Storage:    v1alpha1.StorageSpec{Size: storageQ.String()},
			Resources:  corev1.ResourceRequirements{Limits: limits, Requests: requests},
			Startup:    v1alpha1.StartupSpec{HealthHTTPPort: in.healthHTTPPort},
@@ -192,6 +219,9 @@ func lobbySystemServer(image, namespace string) (*v1alpha1.MinecraftServer, erro
		memory:         "1Gi",
		storage:        "2Gi",
		fallbackServer: naming.SystemLoginServer,
		// Paper serves RCON, and the lobby is administered through the panel like any
		// other server — online players, console, permissions all ride this channel.
		rcon: true,
	}, namespace)
}

+37 −0
Changes for cmd/felis/systemservers_test.go: 37 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -439,3 +439,40 @@ func TestEnsureSystemServersSkipsUnsetImage(t *testing.T) {
		t.Errorf("lobby: skipped = %q, want %q", byName[naming.SystemLobbyServer].skipped, "image not configured")
	}
}

// TestSystemServerRconPolicy pins which system service gets the RCON write
// channel. This is not a preference: the operator gates readiness on the RCON
// probe, so enabling it on a backend that serves no RCON listener would hold that
// server in Starting until it was marked Failed. The login limbo is exactly that
// backend (LOOHP/Limbo has no RCON) AND it is the front door, so getting this
// backwards locks every player out of the deployment.
func TestSystemServerRconPolicy(t *testing.T) {
	login, err := loginSystemServer("reg/limbo:1", "minecraft", "http://api:8081", "mc.example.net", "console.mc.example.net")
	if err != nil {
		t.Fatalf("loginSystemServer: %v", err)
	}
	if login.Spec.Rcon.Enabled {
		t.Fatal("the login limbo must not enable RCON: it serves no RCON listener, so the " +
			"operator's readiness probe would never succeed and the login gate would be marked Failed")
	}

	lobby, err := lobbySystemServer("reg/lobby:1", "minecraft")
	if err != nil {
		t.Fatalf("lobbySystemServer: %v", err)
	}
	if !lobby.Spec.Rcon.Enabled {
		t.Fatal("the lobby runs Paper and is administered through the panel; without RCON its " +
			"console, online-player list and permission changes are all unavailable")
	}
	if got, want := lobby.Spec.Rcon.SecretRef.Name, naming.RconSecretName("lobby"); got != want {
		t.Fatalf("lobby rcon secret = %q, want %q — the operator provisions against this name", got, want)
	}
	if got, want := lobby.Spec.Rcon.SecretRef.Key, naming.RconSecretKey; got != want {
		t.Fatalf("lobby rcon secret key = %q, want %q", got, want)
	}
	// Port stays unset so the operator's DefaultRconPort is the only place the
	// number is written down.
	if lobby.Spec.Rcon.Port != 0 {
		t.Fatalf("lobby rcon port = %d, want 0 (operator default)", lobby.Spec.Rcon.Port)
	}
}
+27 −0
Changes for deploy/lobby/entrypoint.sh: 27 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -58,6 +58,33 @@ set_prop() {
set_prop server-port "$PORT"
set_prop online-mode false

# RCON is the control plane's write channel (spec §8 写=RCON): the operator probes it
# for readiness and the player tally, and felis-api runs console/permission commands over
# it. Paper only reads these three keys from server.properties, so the operator's injected
# RCON_PASSWORD has to be written here to take effect — env alone does nothing.
#
# Rewritten on EVERY boot from the Secret, deliberately. That makes the value in the world
# volume derived state rather than the source of truth: an owner who edits (or clobbers)
# these lines through the panel's file editor cannot lock the control plane out of their
# own server, because the next restart restores the real password. The editor is also kept
# from reading the password back out — see internal/fileedit/exec.go (spec §286: RCON
# 密码绝不下发前端).
#
# No password, no RCON: an empty enable-rcon=true would let anything that reaches the port
# in unauthenticated. Unlike the forwarding secret this is not fatal — a server without the
# write channel still serves players — so it warns and starts rather than refusing.
if [ -n "${RCON_PASSWORD:-}" ]; then
  set_prop enable-rcon true
  set_prop rcon.port "${RCON_PORT:-25575}"
  set_prop rcon.password "$RCON_PASSWORD"
  echo "felis-lobby: rcon enabled on port ${RCON_PORT:-25575}"
else
  set_prop enable-rcon false
  echo "felis-lobby: WARNING — RCON_PASSWORD is empty, so the console, the online-player" >&2
  echo "  list and permission changes will be unavailable for this server. The operator" >&2
  echo "  injects it from the <server>-rcon Secret when spec.rcon.enabled is true." >&2
fi

# ponytail: rewritten whole, not merged. Paper loads this file and fills every key it does
# not find with the default, then writes the full tree back — so a proxies-only file is a
# complete, stable input, and the lobby's other globals are simply always the defaults.
+18 −0
Changes for internal/api/k8scluster.go: 18 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -96,6 +96,24 @@ func (k *K8sCluster) CreateServer(ctx context.Context, in CreateServerInput) err
			FallbackServer:  naming.SystemLoginServer,
			Storage:         v1alpha1.StorageSpec{Size: in.StorageSize},
			Resources:       in.Resources,
			// RCON is what makes a server manageable at all: the operator gates
			// phase=Running on the probe and samples the player tally from it (spec
			// §5), and every write — console commands, the LuckPerms grants behind the
			// permissions UI — travels over it (spec §8 写=RCON). Leaving it unset
			// produced a server that looked Running, reported nobody online, and
			// answered the console with 503; enabling it here is the fix for all
			// three. Port stays 0 so the operator applies its own default rather than
			// this package pinning a second copy of it. The password is not set (and
			// felis-api could not set it — it holds secrets:get, not create): the
			// operator mints it into this Secret on first reconcile, and felis-api
			// only ever reads it back at command time.
			Rcon: v1alpha1.RconSpec{
				Enabled: true,
				SecretRef: v1alpha1.SecretKeyRef{
					Name: naming.RconSecretName(in.Name),
					Key:  naming.RconSecretKey,
				},
			},
		},
	}
	if err := k.c.Create(ctx, ms); err != nil {
+65 −0
Changes for internal/api/k8scluster_test.go: 65 added lines, 0 removed lines.
Original line number Diff line number Diff line
package api

import (
	"context"
	"testing"

	"felis.lolicon.best/internal/apis/felis/v1alpha1"
	"felis.lolicon.best/internal/naming"
	"k8s.io/apimachinery/pkg/runtime"
	"k8s.io/apimachinery/pkg/types"
	"sigs.k8s.io/controller-runtime/pkg/client/fake"
)

// K8sCluster is documented as integration-tested against a live cluster rather
// than covered by the hermetic suite, and for most of it that is the right call —
// merge-patch semantics are not worth faking. This one test departs from it
// deliberately: "CreateServer forgot to set spec.rcon" is precisely the kind of
// defect a live-cluster test catches only if someone runs it, and it shipped. It
// produced servers that reported Running, listed nobody online, and answered the
// console with 503, and the cause was a struct literal missing a field. A fake
// client is enough to pin a struct literal.
func TestCreateServerEnablesRcon(t *testing.T) {
	scheme := runtime.NewScheme()
	if err := v1alpha1.AddToScheme(scheme); err != nil {
		t.Fatalf("scheme: %v", err)
	}
	c := fake.NewClientBuilder().WithScheme(scheme).Build()
	k := NewK8sCluster(c, "minecraft")

	if err := k.CreateServer(context.Background(), CreateServerInput{
		Name:        "survival",
		Subdomain:   "survival",
		DisplayName: "Survival",
		Image:       "reg/paper:1",
		JavaMemory:  "2G",
		StorageSize: "10Gi",
	}); err != nil {
		t.Fatalf("CreateServer: %v", err)
	}

	var ms v1alpha1.MinecraftServer
	if err := c.Get(context.Background(),
		types.NamespacedName{Namespace: "minecraft", Name: "survival"}, &ms); err != nil {
		t.Fatalf("get created server: %v", err)
	}

	if !ms.Spec.Rcon.Enabled {
		t.Fatal("spec.rcon.enabled is false: the operator will skip the readiness probe and " +
			"never sample a player count, and every console write will fail with 503")
	}
	// The name is the contract with internal/operator.ensureRconSecret, which
	// provisions the password against exactly this key. A mismatch leaves the pod
	// stuck on a secretKeyRef that nothing ever creates.
	if got, want := ms.Spec.Rcon.SecretRef.Name, naming.RconSecretName("survival"); got != want {
		t.Fatalf("rcon secretRef name = %q, want %q", got, want)
	}
	if got, want := ms.Spec.Rcon.SecretRef.Key, naming.RconSecretKey; got != want {
		t.Fatalf("rcon secretRef key = %q, want %q", got, want)
	}
	// felis-api holds secrets:get, not create — it must never try to mint the
	// password itself, and nothing here should carry one.
	if ms.Spec.Rcon.Port != 0 {
		t.Fatalf("rcon port = %d, want 0 so the operator default is the only copy", ms.Spec.Rcon.Port)
	}
}
Loading