mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-11-16 23:04:24 +08:00
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:
@@ -10,12 +10,15 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
|
||||
"github.com/Rain-kl/Wavelet/internal/apps/agent/localfile"
|
||||
)
|
||||
|
||||
const (
|
||||
stateDirPerm = 0o750
|
||||
stateFilePerm = 0o600
|
||||
nodeIDRandomBytes = 8
|
||||
stateDirPerm = 0o750
|
||||
stateFilePerm = 0o600
|
||||
nodeIDRandomBytes = 8
|
||||
registrationKeyBytes = 32
|
||||
)
|
||||
|
||||
// PagesDeployment records a Pages project tracked by the agent and the last
|
||||
@@ -31,6 +34,7 @@ type PagesDeployment struct {
|
||||
// Snapshot represents the state of the agent at a given point in time.
|
||||
type Snapshot struct {
|
||||
NodeID string `json:"node_id"`
|
||||
RegistrationKey string `json:"registration_key,omitempty"`
|
||||
CurrentVersion string `json:"current_version"`
|
||||
CurrentChecksum string `json:"current_checksum"`
|
||||
PagesDeployments []PagesDeployment `json:"pages_deployments"`
|
||||
@@ -47,15 +51,42 @@ type Snapshot struct {
|
||||
AccessLogOffset int64 `json:"access_log_offset"`
|
||||
}
|
||||
|
||||
// EnsureRegistrationKey persists the private retry credential before enrollment can reach the server.
|
||||
func (s *Store) EnsureRegistrationKey() (string, error) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
snapshot, err := s.loadUnlocked()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if snapshot.RegistrationKey != "" {
|
||||
// A previous rename may have succeeded even though its directory sync failed.
|
||||
if err = s.syncDirectory(filepath.Dir(s.path)); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return snapshot.RegistrationKey, nil
|
||||
}
|
||||
key := make([]byte, registrationKeyBytes)
|
||||
if _, err = rand.Read(key); err != nil {
|
||||
return "", err
|
||||
}
|
||||
snapshot.RegistrationKey = hex.EncodeToString(key)
|
||||
if err = s.saveUnlocked(snapshot); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return snapshot.RegistrationKey, nil
|
||||
}
|
||||
|
||||
// Store manages the storage and retrieval of the agent state snapshot.
|
||||
type Store struct {
|
||||
path string
|
||||
mu sync.Mutex
|
||||
path string
|
||||
mu sync.Mutex
|
||||
syncDirectory func(string) error
|
||||
}
|
||||
|
||||
// NewStore creates a new Store instance at the given path.
|
||||
func NewStore(path string) *Store {
|
||||
return &Store{path: filepath.Clean(path)}
|
||||
return &Store{path: filepath.Clean(path), syncDirectory: localfile.SyncDirectory}
|
||||
}
|
||||
|
||||
// Load loads the snapshot from the store.
|
||||
@@ -91,6 +122,29 @@ func (s *Store) EnsureNodeID() (string, error) {
|
||||
func (s *Store) Save(snapshot *Snapshot) error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
current, err := s.loadUnlocked()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
updated := *snapshot
|
||||
|
||||
// Other workers can hold a snapshot loaded before enrollment first persisted its key.
|
||||
if current.RegistrationKey != "" {
|
||||
updated.RegistrationKey = current.RegistrationKey
|
||||
updated.NodeID = current.NodeID
|
||||
}
|
||||
return s.saveUnlocked(&updated)
|
||||
}
|
||||
|
||||
// SetRegisteredNodeID commits the server-assigned identity without overwriting concurrent runtime state.
|
||||
func (s *Store) SetRegisteredNodeID(nodeID string) error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
snapshot, err := s.loadUnlocked()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
snapshot.NodeID = nodeID
|
||||
return s.saveUnlocked(snapshot)
|
||||
}
|
||||
|
||||
@@ -120,7 +174,7 @@ func (s *Store) saveUnlocked(snapshot *Snapshot) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return os.WriteFile(s.path, data, stateFilePerm)
|
||||
return localfile.WriteAtomic(s.path, data, stateFilePerm)
|
||||
}
|
||||
|
||||
func newNodeID() (string, error) {
|
||||
|
||||
Reference in New Issue
Block a user