fix(agent): make discovery enrollment compatible and idempotent (#53)

* fix(agent): make discovery enrollment compatible and idempotent

* fix(agent): confirm enrollment persistence and document token aliases
This commit is contained in:
ArthurKoba
2026-11-07 16:29:47 +03:00
committed by GitHub
parent 1ca03df0ef
commit 72d67c80f2
30 changed files with 1324 additions and 71 deletions
+43 -1
View File
@@ -4,7 +4,11 @@
// Package protocol defines the communication protocol between OpenFlare server, agent, and relay components.
package protocol
import "encoding/json"
import (
"encoding/json"
"errors"
"strings"
)
// APIResponse is a generic API response wrapper.
type APIResponse[T any] struct {
@@ -166,6 +170,12 @@ type NodeHealthEvent struct {
Metadata map[string]string `json:"metadata,omitempty"`
}
// RegisterNodeRequest carries enrollment data separately from heartbeat telemetry.
type RegisterNodeRequest struct {
NodePayload
RegistrationKey string `json:"registration_key,omitempty"`
}
// RegisterNodeResponse is the node registration response.
type RegisterNodeResponse struct {
NodeID string `json:"node_id"`
@@ -173,6 +183,38 @@ type RegisterNodeResponse struct {
Name string `json:"name"`
}
// MarshalJSON retains access_token for consumers of older server responses.
func (r RegisterNodeResponse) MarshalJSON() ([]byte, error) {
type response RegisterNodeResponse
return json.Marshal(struct {
response
LegacyAccessToken string `json:"access_token"`
}{response(r), r.AccessToken})
}
// UnmarshalJSON accepts either spelling used by released servers.
func (r *RegisterNodeResponse) UnmarshalJSON(data []byte) error {
type response RegisterNodeResponse
var decoded struct {
response
LegacyAccessToken string `json:"access_token"`
}
if err := json.Unmarshal(data, &decoded); err != nil {
return err
}
canonical := strings.TrimSpace(decoded.AccessToken)
legacy := strings.TrimSpace(decoded.LegacyAccessToken)
if canonical != "" && legacy != "" && canonical != legacy {
return errors.New("registration response contains conflicting agent tokens")
}
*r = RegisterNodeResponse(decoded.response)
r.AccessToken = canonical
if canonical == "" {
r.AccessToken = legacy
}
return nil
}
// ActiveConfigResponse is the active configuration response.
type ActiveConfigResponse struct {
Version string `json:"version"`
+54
View File
@@ -0,0 +1,54 @@
// Copyright 2026 Arctel.net
// SPDX-License-Identifier: Apache-2.0
package protocol
import (
"encoding/json"
"strings"
"testing"
)
func TestRegisterNodeResponseTokenAliases(t *testing.T) {
for _, tt := range []struct {
name string
body string
want string
wantErr bool
}{
{name: "canonical", body: `{"node_id":"node-1","agent_token":"issued"}`, want: "issued"},
{name: "legacy", body: `{"node_id":"node-1","access_token":"issued"}`, want: "issued"},
{name: "matching aliases", body: `{"agent_token":"issued","access_token":"issued"}`, want: "issued"},
{name: "conflicting aliases", body: `{"agent_token":"a","access_token":"b"}`, wantErr: true},
{name: "invalid JSON", body: `{`, wantErr: true},
} {
t.Run(tt.name, func(t *testing.T) {
var got RegisterNodeResponse
err := json.Unmarshal([]byte(tt.body), &got)
if (err != nil) != tt.wantErr {
t.Fatalf("Unmarshal(%s) error = %v, want error %t", tt.name, err, tt.wantErr)
}
if got.AccessToken != tt.want {
t.Errorf("Unmarshal(%s).AccessToken = %q, want %q", tt.name, got.AccessToken, tt.want)
}
})
}
}
func TestRegistrationRequestKeepsRetryCredentialOutOfTelemetry(t *testing.T) {
request := RegisterNodeRequest{NodePayload: NodePayload{NodeID: "node-1"}, RegistrationKey: "private"}
registration, err := json.Marshal(request)
if err != nil {
t.Fatal(err)
}
if !strings.Contains(string(registration), `"registration_key":"private"`) {
t.Errorf("Marshal(registration) = %s, want retry credential", registration)
}
telemetry, err := json.Marshal(request.NodePayload)
if err != nil {
t.Fatal(err)
}
if strings.Contains(string(telemetry), "registration_key") || strings.Contains(string(telemetry), "private") {
t.Errorf("Marshal(heartbeat) = %s, want no enrollment credential", telemetry)
}
}