Closes six review findings against .4.6 (PR #775): - daemon.StateStoreGrantIssuer + staterpc.TaskGrants: containers get a per-task credential authorized only for their own Update/Transition/ InsertEvent, never the daemon's administrative token. Issued before Acquire, revoked on Release; fails closed if no issuer is wired. - StreamCaptures/StreamUndispatchedCaptures: new streaming RPCs so a batch of large capture bodies can't exceed gRPC's 4MiB unary cap. The original unary RPCs are kept, unmodified (deprecated) rather than reshaped, so buf breaking stays green for an in-flight rolling deploy. - Update rejects a nil Task with InvalidArgument instead of panicking in the store layer. - RequireHostGateway only forces on for embedded NATS, so an externally reachable NATS/State Store no longer requires a local Docker bridge. - Client-side token resolution now matches the server's (target_token, then STATE_STORE_TOKEN secret/env). - task dev now runs archie-state-store (dev:state-store) alongside archied/archie-gateway/vite; deployments/dev.toml points them at it.
108 lines
3.3 KiB
Go
108 lines
3.3 KiB
Go
package daemon
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"log/slog"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
|
|
dockercontainer "github.com/moby/moby/api/types/container"
|
|
"github.com/moby/moby/client"
|
|
|
|
"github.com/samcharles93/archie-core/internal/config"
|
|
archiecontainer "github.com/samcharles93/archie-core/internal/container"
|
|
)
|
|
|
|
// This pins the last handoff seam, not just its two halves: the runtime NATS
|
|
// endpoint frozen in Daemon becomes the environment on a container created on
|
|
// the same Docker network whose gateway embedded NATS binds.
|
|
func TestConnectedNATSEndpointReachesManagedContainerCreate(t *testing.T) {
|
|
const (
|
|
networkName = "workers"
|
|
gateway = "172.31.0.1"
|
|
containerID = "12345678901234567890123456789012"
|
|
token = "runtime-only-token"
|
|
)
|
|
var createRequest dockercontainer.CreateRequest
|
|
dockerAPI := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
switch {
|
|
case strings.HasSuffix(r.URL.Path, "/networks/"+networkName):
|
|
writeContainerDockerJSON(t, w, map[string]any{
|
|
"Name": networkName,
|
|
"Driver": "bridge",
|
|
"Scope": "local",
|
|
"IPAM": map[string]any{
|
|
"Config": []map[string]string{{
|
|
"Subnet": "172.31.0.0/16",
|
|
"Gateway": gateway,
|
|
}},
|
|
},
|
|
})
|
|
case strings.HasSuffix(r.URL.Path, "/containers/create"):
|
|
if err := json.NewDecoder(r.Body).Decode(&createRequest); err != nil {
|
|
t.Errorf("decode container create request: %v", err)
|
|
}
|
|
writeContainerDockerJSON(t, w, dockercontainer.CreateResponse{ID: containerID})
|
|
case strings.HasSuffix(r.URL.Path, "/containers/"+containerID+"/start"):
|
|
w.WriteHeader(http.StatusNoContent)
|
|
case strings.HasSuffix(r.URL.Path, "/containers/json"):
|
|
writeContainerDockerJSON(t, w, []any{})
|
|
default:
|
|
http.Error(w, fmt.Sprintf("unexpected Docker API path %s", r.URL.Path), http.StatusNotFound)
|
|
}
|
|
}))
|
|
t.Cleanup(dockerAPI.Close)
|
|
|
|
dockerClient, err := client.New(client.WithHost(dockerAPI.URL), client.WithAPIVersion("1.55"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { _ = dockerClient.Close() })
|
|
|
|
pool, err := archiecontainer.NewPool(t.Context(), archiecontainer.Config{
|
|
Image: "archie-agent:test",
|
|
Network: networkName,
|
|
DockerClient: dockerClient,
|
|
RequireHostGateway: true,
|
|
}, slog.New(slog.DiscardHandler))
|
|
if err != nil {
|
|
t.Fatalf("container.NewPool = %v", err)
|
|
}
|
|
t.Cleanup(func() { _ = pool.Close() })
|
|
|
|
d := &Daemon{
|
|
Cfg: config.NewHolder(config.Config{}),
|
|
ConnectedNATS: NATSEndpoint{
|
|
URL: "nats://" + gateway + ":4222",
|
|
Token: token,
|
|
},
|
|
}
|
|
if _, err := pool.Acquire(t.Context(), nil, d.containerEnv(nil, "")); err != nil {
|
|
t.Fatalf("Pool.Acquire = %v", err)
|
|
}
|
|
|
|
if createRequest.HostConfig == nil || string(createRequest.HostConfig.NetworkMode) != networkName {
|
|
t.Fatalf("container network = %v, want %q", createRequest.HostConfig, networkName)
|
|
}
|
|
for _, want := range []string{
|
|
"NATS_URL=nats://" + gateway + ":4222",
|
|
"NATS_TOKEN=" + token,
|
|
} {
|
|
if !slices.Contains(createRequest.Env, want) {
|
|
t.Errorf("container env = %q, want %q", createRequest.Env, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func writeContainerDockerJSON(t *testing.T, w http.ResponseWriter, value any) {
|
|
t.Helper()
|
|
w.Header().Set("Content-Type", "application/json")
|
|
if err := json.NewEncoder(w).Encode(value); err != nil {
|
|
t.Errorf("encode Docker response: %v", err)
|
|
}
|
|
}
|