feat: add headless agent management to CLI and both desktop clients
This commit is contained in:
1 parent
e91fa0235c
commit
6916dd54fb
50 files changed
+4837
-148
No files matched your search
+16
-1
@@ -49,13 +49,15 @@ type broker struct {
|
||||
leases map[string]*lease // indexed by device, one owner per device
|
||||
sessions map[string]*session
|
||||
agentToken, clientToken string
|
||||
adminToken string
|
||||
managers map[string]*manager
|
||||
ctx context.Context
|
||||
cluster *cluster
|
||||
}
|
||||
|
||||
func newBroker(ctx context.Context, agentToken, clientToken string) *broker {
|
||||
return &broker{agents: make(map[string]*agent), leases: make(map[string]*lease),
|
||||
sessions: make(map[string]*session), agentToken: agentToken, clientToken: clientToken, ctx: ctx}
|
||||
sessions: make(map[string]*session), managers: make(map[string]*manager), agentToken: agentToken, clientToken: clientToken, ctx: ctx}
|
||||
}
|
||||
func newID() string {
|
||||
var data [24]byte
|
||||
@@ -353,6 +355,9 @@ func bridgeWebSockets(ctx context.Context, left, right *websocket.Conn) {
|
||||
}
|
||||
func (b *broker) handler() http.Handler {
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("GET /v3/agents/register", b.auth(b.agentToken, b.managerRegister))
|
||||
mux.HandleFunc("GET /v3/agents", b.managementAuth(b.managersEndpoint))
|
||||
mux.HandleFunc("/v3/rpc", b.managementAuth(b.managerRPCEndpoint))
|
||||
mux.HandleFunc("GET /healthz", func(w http.ResponseWriter, r *http.Request) { io.WriteString(w, "ok\n") })
|
||||
mux.HandleFunc("GET /v1/agent", b.auth(b.agentToken, b.agentEndpoint))
|
||||
mux.HandleFunc("GET /v1/data", b.auth(b.agentToken, b.dataEndpoint))
|
||||
@@ -381,6 +386,10 @@ func main() {
|
||||
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||
defer stop()
|
||||
b := newBroker(ctx, agentToken, clientToken)
|
||||
b.adminToken = os.Getenv("ADS_ADMIN_TOKEN")
|
||||
if b.adminToken != "" && (len(b.adminToken) < 32 || b.adminToken == agentToken || b.adminToken == clientToken) {
|
||||
log.Fatal("ADS_ADMIN_TOKEN must be distinct and at least 32 characters")
|
||||
}
|
||||
if *clusterFile != "" {
|
||||
configured, err := loadCluster(*clusterFile, os.Getenv("ADS_RELAY_SECRET"), *insecureNodes)
|
||||
if err != nil {
|
||||
@@ -393,6 +402,12 @@ func main() {
|
||||
go func() {
|
||||
<-ctx.Done()
|
||||
b.mu.Lock()
|
||||
for _, m := range b.managers {
|
||||
m.cancel()
|
||||
if m.ws != nil {
|
||||
m.ws.CloseNow()
|
||||
}
|
||||
}
|
||||
for _, a := range b.agents {
|
||||
if a.ws != nil {
|
||||
a.ws.CloseNow()
|
||||
|
||||
@@ -0,0 +1,284 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"github.com/coder/websocket"
|
||||
"io"
|
||||
"net/http"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type managedDevice struct {
|
||||
ID string `json:"id"`
|
||||
Target *string `json:"target,omitempty"`
|
||||
ADBSerial *string `json:"adb_serial,omitempty"`
|
||||
ADBServer *string `json:"adb_server,omitempty"`
|
||||
DevicePort uint16 `json:"device_port"`
|
||||
Enabled bool `json:"enabled"`
|
||||
Connected bool `json:"connected"`
|
||||
LastError *string `json:"last_error"`
|
||||
}
|
||||
type managedStatus struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Hostname string `json:"hostname"`
|
||||
OS string `json:"os"`
|
||||
Arch string `json:"arch"`
|
||||
Version string `json:"version"`
|
||||
PID uint32 `json:"pid"`
|
||||
Uptime uint64 `json:"uptime_seconds"`
|
||||
Supervised bool `json:"supervised"`
|
||||
Revision uint64 `json:"revision"`
|
||||
Instance string `json:"instance"`
|
||||
Devices []managedDevice `json:"devices"`
|
||||
}
|
||||
type manager struct {
|
||||
id string
|
||||
ws *websocket.Conn
|
||||
status managedStatus
|
||||
lastSeen int64
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
pending map[string]chan json.RawMessage
|
||||
}
|
||||
|
||||
func (b *broker) managementAuth(handler http.HandlerFunc) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if len(b.adminToken) < 32 {
|
||||
http.Error(w, "management admin token is not configured", 503)
|
||||
return
|
||||
}
|
||||
b.auth(b.adminToken, handler)(w, r)
|
||||
}
|
||||
}
|
||||
func (b *broker) redactManagement(data []byte) []byte {
|
||||
text := string(data)
|
||||
for _, secret := range []string{b.agentToken, b.clientToken, b.adminToken} {
|
||||
if secret != "" {
|
||||
text = strings.ReplaceAll(text, secret, "[REDACTED]")
|
||||
}
|
||||
}
|
||||
return []byte(text)
|
||||
}
|
||||
func (b *broker) managerRegister(w http.ResponseWriter, r *http.Request) {
|
||||
id := r.URL.Query().Get("agent")
|
||||
if !validID(id) {
|
||||
http.Error(w, "invalid agent ID", 400)
|
||||
return
|
||||
}
|
||||
b.mu.Lock()
|
||||
if old := b.managers[id]; old != nil && old.ctx.Err() == nil {
|
||||
b.mu.Unlock()
|
||||
http.Error(w, "agent already registered", 409)
|
||||
return
|
||||
}
|
||||
if b.managers[id] == nil && len(b.managers) >= 128 {
|
||||
b.mu.Unlock()
|
||||
http.Error(w, "agent registry full", 429)
|
||||
return
|
||||
}
|
||||
ctx, cancel := context.WithCancel(b.ctx)
|
||||
m := &manager{id: id, ctx: ctx, cancel: cancel, pending: map[string]chan json.RawMessage{}, status: managedStatus{ID: id, Devices: []managedDevice{}}, lastSeen: time.Now().Unix()}
|
||||
b.managers[id] = m
|
||||
b.mu.Unlock()
|
||||
defer func() { cancel(); b.mu.Lock(); m.ws = nil; b.mu.Unlock() }()
|
||||
ws, err := accept(w, r)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer ws.CloseNow()
|
||||
b.mu.Lock()
|
||||
m.ws = ws
|
||||
b.mu.Unlock()
|
||||
if writeJSON(ctx, ws, map[string]string{"type": "registered", "agent": id}) != nil {
|
||||
return
|
||||
}
|
||||
go func() {
|
||||
ticker := time.NewTicker(20 * time.Second)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-ticker.C:
|
||||
deadline, stop := context.WithTimeout(ctx, 10*time.Second)
|
||||
err := ws.Ping(deadline)
|
||||
stop()
|
||||
if err != nil {
|
||||
cancel()
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
for {
|
||||
typ, data, err := ws.Read(ctx)
|
||||
if err != nil || typ != websocket.MessageText {
|
||||
return
|
||||
}
|
||||
var message struct {
|
||||
Type string `json:"type"`
|
||||
RequestID string `json:"request_id"`
|
||||
Status managedStatus `json:"status"`
|
||||
Response json.RawMessage `json:"response"`
|
||||
}
|
||||
if json.Unmarshal(b.redactManagement(data), &message) != nil {
|
||||
return
|
||||
}
|
||||
switch message.Type {
|
||||
case "status":
|
||||
s := message.Status
|
||||
if s.ID != id || len(s.Name) > 128 || len(s.Hostname) > 256 || len(s.OS) > 64 || len(s.Arch) > 64 || len(s.Version) > 64 || len(s.Instance) > 128 || len(s.Devices) > 128 {
|
||||
return
|
||||
}
|
||||
for _, d := range s.Devices {
|
||||
if !validID(d.ID) || (d.Target != nil && len(*d.Target) > 256) || (d.ADBSerial != nil && len(*d.ADBSerial) > 256) || (d.LastError != nil && len(*d.LastError) > 2048) {
|
||||
return
|
||||
}
|
||||
}
|
||||
if s.Devices == nil {
|
||||
s.Devices = []managedDevice{}
|
||||
}
|
||||
b.mu.Lock()
|
||||
m.status = s
|
||||
m.lastSeen = time.Now().Unix()
|
||||
b.mu.Unlock()
|
||||
case "result":
|
||||
if len(message.Response) == 0 {
|
||||
return
|
||||
}
|
||||
var check struct {
|
||||
OK bool `json:"ok"`
|
||||
}
|
||||
if json.Unmarshal(message.Response, &check) != nil {
|
||||
return
|
||||
}
|
||||
b.mu.Lock()
|
||||
ch := m.pending[message.RequestID]
|
||||
b.mu.Unlock()
|
||||
if ch != nil {
|
||||
select {
|
||||
case ch <- message.Response:
|
||||
default:
|
||||
}
|
||||
}
|
||||
default:
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
func isWebSocket(r *http.Request) bool {
|
||||
return strings.EqualFold(r.Header.Get("Upgrade"), "websocket")
|
||||
}
|
||||
func (b *broker) managersEndpoint(w http.ResponseWriter, r *http.Request) {
|
||||
type record struct {
|
||||
ID string `json:"id"`
|
||||
Online bool `json:"online"`
|
||||
LastSeen int64 `json:"last_seen"`
|
||||
Status managedStatus `json:"status"`
|
||||
}
|
||||
b.mu.Lock()
|
||||
records := []record{}
|
||||
for _, m := range b.managers {
|
||||
records = append(records, record{m.id, m.ws != nil, m.lastSeen, m.status})
|
||||
}
|
||||
b.mu.Unlock()
|
||||
sort.Slice(records, func(i, j int) bool { return records[i].ID < records[j].ID })
|
||||
if isWebSocket(r) {
|
||||
ws, err := accept(w, r)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer ws.CloseNow()
|
||||
writeJSON(r.Context(), ws, records)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(records)
|
||||
}
|
||||
func validManagementRequest(raw []byte) bool {
|
||||
var request struct {
|
||||
Op string `json:"op"`
|
||||
}
|
||||
if json.Unmarshal(raw, &request) != nil {
|
||||
return false
|
||||
}
|
||||
switch request.Op {
|
||||
case "status", "devices_list", "device_add", "device_remove", "device_enable", "config_get", "config_set", "logs", "restart":
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
func (b *broker) managerRPC(ctx context.Context, id string, request json.RawMessage) json.RawMessage {
|
||||
failure := func(reason string) json.RawMessage {
|
||||
data, _ := json.Marshal(map[string]any{"ok": false, "error": reason})
|
||||
return data
|
||||
}
|
||||
if !validID(id) || !validManagementRequest(request) {
|
||||
return failure("invalid management request")
|
||||
}
|
||||
b.mu.Lock()
|
||||
m := b.managers[id]
|
||||
if m == nil || m.ws == nil {
|
||||
b.mu.Unlock()
|
||||
return failure("agent is offline")
|
||||
}
|
||||
if len(m.pending) >= 16 {
|
||||
b.mu.Unlock()
|
||||
return failure("agent management request limit reached")
|
||||
}
|
||||
requestID := newID()
|
||||
response := make(chan json.RawMessage, 1)
|
||||
m.pending[requestID] = response
|
||||
ws := m.ws
|
||||
b.mu.Unlock()
|
||||
defer func() { b.mu.Lock(); delete(m.pending, requestID); b.mu.Unlock() }()
|
||||
deadline, cancel := context.WithTimeout(ctx, 15*time.Second)
|
||||
defer cancel()
|
||||
if writeJSON(deadline, ws, map[string]any{"type": "request", "request_id": requestID, "request": request}) != nil {
|
||||
return failure("cannot reach agent")
|
||||
}
|
||||
select {
|
||||
case data := <-response:
|
||||
return data
|
||||
case <-m.ctx.Done():
|
||||
return failure("agent disconnected")
|
||||
case <-deadline.Done():
|
||||
return failure("agent management request timed out")
|
||||
}
|
||||
}
|
||||
func (b *broker) managerRPCEndpoint(w http.ResponseWriter, r *http.Request) {
|
||||
var request []byte
|
||||
if isWebSocket(r) {
|
||||
ws, err := accept(w, r)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer ws.CloseNow()
|
||||
deadline, cancel := context.WithTimeout(r.Context(), 5*time.Second)
|
||||
defer cancel()
|
||||
typ, data, err := ws.Read(deadline)
|
||||
if err != nil || typ != websocket.MessageText || len(data) > 64*1024 {
|
||||
return
|
||||
}
|
||||
response := b.managerRPC(r.Context(), r.URL.Query().Get("agent"), data)
|
||||
writeJSON(r.Context(), ws, json.RawMessage(response))
|
||||
return
|
||||
}
|
||||
if r.Method != "POST" {
|
||||
http.Error(w, "POST required", 405)
|
||||
return
|
||||
}
|
||||
var err error
|
||||
request, err = io.ReadAll(http.MaxBytesReader(w, r.Body, 64*1024))
|
||||
if err != nil {
|
||||
http.Error(w, "request too large", 413)
|
||||
return
|
||||
}
|
||||
response := b.managerRPC(r.Context(), r.URL.Query().Get("agent"), request)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write(response)
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"github.com/coder/websocket"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
const testAdminToken = "management-admin-token-for-tests-1234567890"
|
||||
|
||||
func TestManagementRoleAndRequestCorrelation(t *testing.T) {
|
||||
b, base := testServer(t)
|
||||
b.adminToken = testAdminToken
|
||||
for _, role := range []string{testAgentToken, testClientToken} {
|
||||
if got := status(t, base, "/v3/agents", role); got != 401 {
|
||||
t.Fatal(got)
|
||||
}
|
||||
}
|
||||
if got := status(t, base, "/v3/agents/register?agent=host", testAdminToken); got != 401 {
|
||||
t.Fatal(got)
|
||||
}
|
||||
manager := dialTest(t, base, "/v3/agents/register?agent=host", testAgentToken)
|
||||
if readControl(t, manager)["type"] != "registered" {
|
||||
t.Fatal("not registered")
|
||||
}
|
||||
if got := status(t, base, "/v3/agents/register?agent=host", testAgentToken); got != 409 {
|
||||
t.Fatal(got)
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
manager.Write(ctx, websocket.MessageText, []byte(`{"type":"status","status":{"id":"host","name":"test","devices":[]}}`))
|
||||
admin := dialTest(t, base, "/v3/rpc?agent=host", testAdminToken)
|
||||
admin.Write(ctx, websocket.MessageText, []byte(`{"op":"status"}`))
|
||||
_, data, err := manager.Read(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var request struct {
|
||||
Type string `json:"type"`
|
||||
RequestID string `json:"request_id"`
|
||||
Request struct {
|
||||
Op string `json:"op"`
|
||||
} `json:"request"`
|
||||
}
|
||||
json.Unmarshal(data, &request)
|
||||
if request.Type != "request" || request.RequestID == "" || request.Request.Op != "status" {
|
||||
t.Fatal(string(data))
|
||||
}
|
||||
response, _ := json.Marshal(map[string]any{"type": "result", "request_id": request.RequestID, "response": map[string]any{"ok": true, "data": map[string]string{"secret": testAgentToken}}})
|
||||
manager.Write(ctx, websocket.MessageText, response)
|
||||
_, result, err := admin.Read(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if strings.Contains(string(result), testAgentToken) || !strings.Contains(string(result), "REDACTED") {
|
||||
t.Fatal("secret not redacted")
|
||||
}
|
||||
manager.CloseNow()
|
||||
deadline := time.Now().Add(time.Second)
|
||||
for time.Now().Before(deadline) {
|
||||
b.mu.Lock()
|
||||
offline := b.managers["host"].ws == nil
|
||||
b.mu.Unlock()
|
||||
if offline {
|
||||
return
|
||||
}
|
||||
time.Sleep(time.Millisecond * 5)
|
||||
}
|
||||
t.Fatal("agent stayed online after disconnect")
|
||||
}
|
||||
func TestManagementDisabledAndOfflineLimits(t *testing.T) {
|
||||
b, base := testServer(t)
|
||||
if got := status(t, base, "/v3/agents", testClientToken); got != 503 {
|
||||
t.Fatal(got)
|
||||
}
|
||||
b.adminToken = testAdminToken
|
||||
for _, body := range []string{`{"op":"shell"}`, `{"op":"restart"}`} {
|
||||
request, _ := http.NewRequest("POST", base+"/v3/rpc?agent=offline", strings.NewReader(body))
|
||||
request.Header.Set("Authorization", "Bearer "+testAdminToken)
|
||||
response, err := http.DefaultClient.Do(request)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var result struct {
|
||||
OK bool `json:"ok"`
|
||||
Error string `json:"error"`
|
||||
}
|
||||
json.NewDecoder(response.Body).Decode(&result)
|
||||
response.Body.Close()
|
||||
if result.OK || result.Error == "" {
|
||||
t.Fatal("offline or unsupported request accepted")
|
||||
}
|
||||
}
|
||||
if got := status(t, base, "/v3/agents/register?agent=bad%20id", testAgentToken); got != 400 {
|
||||
t.Fatal(got)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user