Fix generated Run component identity

This commit is contained in:
npc0-hue
2026-08-29 12:38:34 +08:00
parent ac14f80306
commit 34e240dc56
8 changed files with 187 additions and 16 deletions
+45 -8
View File
@@ -69,18 +69,19 @@ func Load() Config {
}
workspaceRoot := envOrDefault("RUN_WORKSPACE_ROOT", filepath.Join(".", ".run-workspace"))
identity := loadPackagedIdentity()
return Config{
Mode: mode,
PlatformURL: platformURL,
RunEndpointID: envOrDefault("RUN_ENDPOINT_ID", stringOrDefault(BuildRunEndpointID, DefaultEndpointID)),
RunEndpointID: identity.runEndpointID,
DisplayName: envOrDefault("RUN_DISPLAY_NAME", stringOrDefault(BuildDisplayName, DefaultDisplayName)),
Version: envOrDefault("RUN_VERSION", BuildVersion),
RegistrationToken: envOrDefault("RUN_REGISTRATION_TOKEN", stringOrDefault(BuildRegistrationToken, "local-registration")),
ServerInstanceID: envOrDefault("RUN_SERVER_INSTANCE_ID", BuildServerInstanceID),
PluginID: envOrDefault("RUN_PLUGIN_ID", BuildPluginID),
ComponentKind: envOrDefault("RUN_COMPONENT_KIND", BuildComponentKind),
ComponentKey: envOrDefault("RUN_COMPONENT_KEY", BuildComponentKey),
KeyGeneration: intEnvOrDefault("RUN_KEY_GENERATION", intStringOrDefault(BuildKeyGeneration, 0)),
Version: identity.version,
RegistrationToken: identity.registrationToken,
ServerInstanceID: identity.serverInstanceID,
PluginID: identity.pluginID,
ComponentKind: identity.componentKind,
ComponentKey: identity.componentKey,
KeyGeneration: identity.keyGeneration,
WorkspaceSeed: envOrDefault("RUN_WORKSPACE_SEED", BuildWorkspaceSeed),
WorkspaceRoot: workspaceRoot,
BuildSourceRoot: envOrDefault("RUN_BUILD_SOURCE_ROOT", "."),
@@ -98,6 +99,42 @@ func Load() Config {
}
}
type packagedIdentity struct {
runEndpointID string
version string
registrationToken string
serverInstanceID string
pluginID string
componentKind string
componentKey string
keyGeneration int
}
func loadPackagedIdentity() packagedIdentity {
if BuildRegistrationToken != "" {
return packagedIdentity{
runEndpointID: stringOrDefault(BuildRunEndpointID, DefaultEndpointID),
version: stringOrDefault(BuildVersion, DefaultVersion),
registrationToken: BuildRegistrationToken,
serverInstanceID: BuildServerInstanceID,
pluginID: BuildPluginID,
componentKind: BuildComponentKind,
componentKey: BuildComponentKey,
keyGeneration: intStringOrDefault(BuildKeyGeneration, 0),
}
}
return packagedIdentity{
runEndpointID: envOrDefault("RUN_ENDPOINT_ID", stringOrDefault(BuildRunEndpointID, DefaultEndpointID)),
version: envOrDefault("RUN_VERSION", BuildVersion),
registrationToken: envOrDefault("RUN_REGISTRATION_TOKEN", "local-registration"),
serverInstanceID: envOrDefault("RUN_SERVER_INSTANCE_ID", BuildServerInstanceID),
pluginID: envOrDefault("RUN_PLUGIN_ID", BuildPluginID),
componentKind: envOrDefault("RUN_COMPONENT_KIND", BuildComponentKind),
componentKey: envOrDefault("RUN_COMPONENT_KEY", BuildComponentKey),
keyGeneration: intEnvOrDefault("RUN_KEY_GENERATION", intStringOrDefault(BuildKeyGeneration, 0)),
}
}
func stringOrDefault(value string, fallback string) string {
if value == "" {
return fallback
+10 -3
View File
@@ -53,7 +53,7 @@ func TestLoadUsesEnvironment(t *testing.T) {
}
}
func TestLoadUsesBuildDefaultsWithEnvironmentOverride(t *testing.T) {
func TestLoadUsesPackagedIdentityOverEnvironment(t *testing.T) {
oldMode, oldPlatformURL, oldRunEndpointID, oldDisplayName := BuildMode, BuildPlatformURL, BuildRunEndpointID, BuildDisplayName
oldRegistrationToken, oldServerInstanceID, oldPluginID := BuildRegistrationToken, BuildServerInstanceID, BuildPluginID
oldComponentKind, oldComponentKey, oldKeyGeneration, oldVersion, oldWorkspaceSeed := BuildComponentKind, BuildComponentKey, BuildKeyGeneration, BuildVersion, BuildWorkspaceSeed
@@ -85,9 +85,16 @@ func TestLoadUsesBuildDefaultsWithEnvironmentOverride(t *testing.T) {
}
t.Setenv("RUN_PLATFORM_URL", "http://127.0.0.1:18080")
t.Setenv("RUN_ENDPOINT_ID", "stale-run")
t.Setenv("RUN_REGISTRATION_TOKEN", "stale-token")
t.Setenv("RUN_SERVER_INSTANCE_ID", "stale-server")
t.Setenv("RUN_PLUGIN_ID", "stale-plugin")
t.Setenv("RUN_COMPONENT_KIND", "client-manager")
t.Setenv("RUN_COMPONENT_KEY", "stale-profile")
t.Setenv("RUN_KEY_GENERATION", "7")
t.Setenv("RUN_VERSION", "stale-version")
cfg = Load()
if cfg.PlatformURL != "http://127.0.0.1:18080" || cfg.KeyGeneration != 7 {
t.Fatalf("expected environment override, got %+v", cfg)
if cfg.PlatformURL != "http://127.0.0.1:18080" || cfg.RunEndpointID != "run-server-1" || cfg.RegistrationToken != "compiled-run-key" || cfg.ServerInstanceID != "server-1" || cfg.PluginID != "game.scum" || cfg.ComponentKind != "run" || cfg.ComponentKey != "" || cfg.KeyGeneration != 5 || cfg.Version != "run-dist-1" {
t.Fatalf("expected packaged identity with platform URL override, got %+v", cfg)
}
}
+9 -2
View File
@@ -138,7 +138,7 @@ func ApplyPackageConfig(base Config, pkg PackageConfig) Config {
base.ServerInstanceID = pkg.ServerInstanceID
base.PluginID = pkg.PluginID
base.ComponentKind = pkg.Kind
base.ComponentKey = pkg.ProfileKey
base.ComponentKey = packageComponentKey(pkg)
base.KeyGeneration = pkg.KeyGeneration
base.SecretRef = pkg.SecretRef
if pkg.RunEndpointID != "" {
@@ -184,7 +184,7 @@ func AuthenticatePackageGeneration(pkg PackageConfig, auth ComponentAuthResult)
if err := ValidatePackageConfig(pkg); err != nil {
return err
}
if auth.ServerInstanceID != pkg.ServerInstanceID || auth.Kind != pkg.Kind || auth.ProfileKey != pkg.ProfileKey {
if auth.ServerInstanceID != pkg.ServerInstanceID || auth.Kind != pkg.Kind || auth.ProfileKey != packageComponentKey(pkg) {
return fmt.Errorf("component authentication scope does not match package")
}
if !auth.Allowed {
@@ -196,6 +196,13 @@ func AuthenticatePackageGeneration(pkg PackageConfig, auth ComponentAuthResult)
return nil
}
func packageComponentKey(pkg PackageConfig) string {
if pkg.Kind == PackageComponentRun {
return ""
}
return strings.TrimSpace(pkg.ProfileKey)
}
func fingerprint(value string) string {
sum := sha256.Sum256([]byte(value))
return hex.EncodeToString(sum[:])[:12]
+3 -1
View File
@@ -14,6 +14,7 @@ func TestLoadPackageConfigAppliesServerScopedIdentity(t *testing.T) {
ServerInstanceID: "server-1",
PluginID: "game.minecraft",
RunEndpointID: "run-server-1",
ProfileKey: "run-local",
TargetOS: "linux",
TargetArch: "amd64",
SecretRef: "secret://runtime-keys/server-1/run/current",
@@ -26,7 +27,7 @@ func TestLoadPackageConfigAppliesServerScopedIdentity(t *testing.T) {
t.Fatalf("load package config: %v", err)
}
cfg := ApplyPackageConfig(Config{RunEndpointID: DefaultEndpointID, DisplayName: DefaultDisplayName}, pkg)
if cfg.RegistrationToken != "opaque-runtime-key" || cfg.RunEndpointID != "run-server-1" || cfg.ServerInstanceID != "server-1" || cfg.KeyGeneration != 3 {
if cfg.RegistrationToken != "opaque-runtime-key" || cfg.RunEndpointID != "run-server-1" || cfg.ServerInstanceID != "server-1" || cfg.ComponentKey != "" || cfg.KeyGeneration != 3 {
t.Fatalf("expected package identity to be applied, got %+v", cfg)
}
diagnostics := pkg.RedactedDiagnostics()
@@ -70,6 +71,7 @@ func TestAuthenticatePackageGenerationRejectsStalePackages(t *testing.T) {
Kind: PackageComponentRun,
ServerInstanceID: "server-1",
PluginID: "game.minecraft",
ProfileKey: "run-local",
TargetOS: "linux",
TargetArch: "amd64",
SecretRef: "secret://runtime-keys/server-1/run/current",