Skip to content

Agent Mesh

refactor(node): reduce complexity of Enroll and add unit tests

Antonio Ojea3581892ca92e
diff --git a/.github/workflows/kind-mesh-e2e.yml b/.github/workflows/kind-mesh-e2e.yml
index 89ad4f56..496c52df 100644
--- a/.github/workflows/kind-mesh-e2e.yml
+++ b/.github/workflows/kind-mesh-e2e.yml
@@ -71,12 +71,6 @@ jobs:
run: |
mkdir -p _artifacts
kind export logs --name sam-kind ./_artifacts/logs
- kubectl --context kind-sam-kind -n sam-kind get pods -o wide > _artifacts/pods.txt 2>&1 || true
- kubectl --context kind-sam-kind -n sam-kind logs deploy/sam-control-plane --all-containers --tail=-1 > _artifacts/sam-control-plane.log 2>&1 || true
- kubectl --context kind-sam-kind -n sam-kind logs statefulset/sam-router --all-containers --tail=-1 > _artifacts/sam-router.log 2>&1 || true
- for d in node-a node-b node-c; do
- kubectl --context kind-sam-kind -n sam-kind logs "deploy/$d" --all-containers --tail=-1 > "_artifacts/$d.log" 2>&1 || true
- done
cp "$RUNNER_TEMP/local-node.log" _artifacts/ 2>/dev/null || true
- name: Upload logs
diff --git a/internal/node/enroll.go b/internal/node/enroll.go
index bdcfa313..c5b31ade 100644
--- a/internal/node/enroll.go
+++ b/internal/node/enroll.go
@@ -86,9 +86,7 @@ func (n *SamNode) Enroll(ctx context.Context, hubURL string, jwt string) error {
}
httpReq.Header.Set("Content-Type", "application/x-protobuf")
- client := &http.Client{
- Timeout: 30 * time.Second,
- }
+ client := &http.Client{Timeout: 30 * time.Second}
resp, err := client.Do(httpReq)
if err != nil {
return fmt.Errorf("HTTP request failed: %v", err)
@@ -99,64 +97,72 @@ func (n *SamNode) Enroll(ctx context.Context, hubURL string, jwt string) error {
}
}()
+ enrollResp, err := n.processEnrollResponse(resp)
+ if err != nil {
+ return err
+ }
+
+ return n.connectToHubAddresses(ctx, enrollResp.HubAddresses)
+}
+
+func (n *SamNode) processEnrollResponse(resp *http.Response) (*api.EnrollResponse, error) {
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
- return fmt.Errorf("enrollment failed with status %s: %s", resp.Status, string(body))
+ return nil, fmt.Errorf("enrollment failed with status %s: %s", resp.Status, string(body))
}
respData, err := io.ReadAll(resp.Body)
if err != nil {
- return fmt.Errorf("failed to read response body: %v", err)
+ return nil, fmt.Errorf("failed to read response body: %v", err)
}
var enrollResp api.EnrollResponse
if err := proto.Unmarshal(respData, &enrollResp); err != nil {
- return fmt.Errorf("failed to unmarshal response: %v", err)
+ return nil, fmt.Errorf("failed to unmarshal response: %v", err)
}
if enrollResp.ErrorMessage != "" {
- return fmt.Errorf("enrollment failed: %s", enrollResp.ErrorMessage)
+ return nil, fmt.Errorf("enrollment failed: %s", enrollResp.ErrorMessage)
}
-
if len(enrollResp.BiscuitToken) == 0 {
- return fmt.Errorf("received empty biscuit token")
+ return nil, fmt.Errorf("received empty biscuit token")
}
-
if len(enrollResp.HubPublicKey) != ed25519.PublicKeySize {
- return fmt.Errorf("received invalid hub public key size: %d bytes (expected %d)", len(enrollResp.HubPublicKey), ed25519.PublicKeySize)
+ return nil, fmt.Errorf("received invalid hub public key size: %d bytes (expected %d)", len(enrollResp.HubPublicKey), ed25519.PublicKeySize)
}
if n.config.RequiredRole != "" {
if err := identity.VerifyBiscuitRole(enrollResp.BiscuitToken, ed25519.PublicKey(enrollResp.HubPublicKey), n.config.RequiredRole); err != nil {
- return fmt.Errorf("enrolled biscuit token lacks required role %q: %w", n.config.RequiredRole, err)
+ return nil, fmt.Errorf("enrolled biscuit token lacks required role %q: %w", n.config.RequiredRole, err)
}
}
if err := n.Store.SaveIdentity(enrollResp.BiscuitToken); err != nil {
- return fmt.Errorf("failed to save identity: %v", err)
+ return nil, fmt.Errorf("failed to save identity: %v", err)
}
n.SetIdentityCache(enrollResp.BiscuitToken)
if err := n.Store.SaveIdentityExpiration(enrollResp.Expiration); err != nil {
- return fmt.Errorf("failed to save identity expiration: %v", err)
+ return nil, fmt.Errorf("failed to save identity expiration: %v", err)
}
-
if err := n.Store.SaveHubConfig(enrollResp.HubPublicKey, enrollResp.HubAddresses); err != nil {
- return fmt.Errorf("failed to save hub config: %v", err)
+ return nil, fmt.Errorf("failed to save hub config: %v", err)
}
n.keysMu.Lock()
n.trustedKeys = append(n.trustedKeys, TrustedKey{Key: ed25519.PublicKey(enrollResp.HubPublicKey), ReceivedAt: time.Now()})
n.keysMu.Unlock()
- // Connect and Auth to hub after enrollment to join the mesh
- if len(enrollResp.HubAddresses) == 0 {
+ return &enrollResp, nil
+}
+
+func (n *SamNode) connectToHubAddresses(ctx context.Context, addrs []string) error {
+ if len(addrs) == 0 {
return fmt.Errorf("failed to connect and authenticate after HTTP enrollment: control plane returned no hub addresses")
}
var lastAuthErr error
- var authed bool
- for _, addrStr := range enrollResp.HubAddresses {
+ for _, addrStr := range addrs {
addr, err := multiaddr.NewMultiaddr(addrStr)
if err != nil {
logger.Warnf("Failed to parse hub address from response: %v", err)
@@ -166,17 +172,12 @@ func (n *SamNode) Enroll(ctx context.Context, hubURL string, jwt string) error {
logger.Warnf("Failed to connect and auth with hub after enrollment: %v", err)
lastAuthErr = err
} else {
- authed = true
- break
+ logger.Info("Successfully enrolled via HTTP and stored identity and hub config.")
+ return nil
}
}
- if !authed {
- return fmt.Errorf("failed to connect and authenticate with any hub after HTTP enrollment (last error: %v)", lastAuthErr)
- }
-
- logger.Info("Successfully enrolled via HTTP and stored identity and hub config.")
- return nil
+ return fmt.Errorf("failed to connect and authenticate with any hub after HTTP enrollment (last error: %v)", lastAuthErr)
}
// EnrollBootstrap enrolls the node with the control plane using a pre-shared bootstrap token.
diff --git a/internal/node/enroll_test.go b/internal/node/enroll_test.go
index a247b2dc..f04bcbc6 100644
--- a/internal/node/enroll_test.go
+++ b/internal/node/enroll_test.go
@@ -17,6 +17,7 @@ package node
import (
"bytes"
"context"
+ "io"
"net/http"
"net/http/httptest"
"strings"
@@ -108,3 +109,87 @@ func TestEnroll_InvalidHubPublicKeySize(t *testing.T) {
t.Fatalf("Expected invalid public key size error, got: %v", err)
}
}
+
+func TestProcessEnrollResponse_Errors(t *testing.T) {
+ dir := t.TempDir()
+ store, err := NewStore(dir)
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer func() { _ = store.Close() }()
+
+ priv, _, _ := crypto.GenerateKeyPair(crypto.Ed25519, -1)
+ node, err := NewSamNode(Options{
+ PrivKey: priv,
+ Store: store,
+ ListenAddrs: []string{"/ip4/127.0.0.1/tcp/0"},
+ })
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ t.Run("Non200Status", func(t *testing.T) {
+ resp := &http.Response{
+ StatusCode: http.StatusBadRequest,
+ Status: "400 Bad Request",
+ Body: io.NopCloser(strings.NewReader("bad request details")),
+ }
+ _, err := node.processEnrollResponse(resp)
+ if err == nil || !strings.Contains(err.Error(), "enrollment failed with status 400") {
+ t.Fatalf("Expected status 400 error, got: %v", err)
+ }
+ })
+
+ t.Run("ErrorMessageInResponse", func(t *testing.T) {
+ enrollResp := &api.EnrollResponse{ErrorMessage: "unauthorized user"}
+ data, _ := proto.Marshal(enrollResp)
+ resp := &http.Response{
+ StatusCode: http.StatusOK,
+ Body: io.NopCloser(bytes.NewReader(data)),
+ }
+ _, err := node.processEnrollResponse(resp)
+ if err == nil || !strings.Contains(err.Error(), "enrollment failed: unauthorized user") {
+ t.Fatalf("Expected enrollment failed error message, got: %v", err)
+ }
+ })
+
+ t.Run("EmptyBiscuitToken", func(t *testing.T) {
+ enrollResp := &api.EnrollResponse{
+ BiscuitToken: nil,
+ HubPublicKey: make([]byte, 32),
+ }
+ data, _ := proto.Marshal(enrollResp)
+ resp := &http.Response{
+ StatusCode: http.StatusOK,
+ Body: io.NopCloser(bytes.NewReader(data)),
+ }
+ _, err := node.processEnrollResponse(resp)
+ if err == nil || !strings.Contains(err.Error(), "received empty biscuit token") {
+ t.Fatalf("Expected empty biscuit token error, got: %v", err)
+ }
+ })
+}
+
+func TestConnectToHubAddresses_EmptyAddrs(t *testing.T) {
+ dir := t.TempDir()
+ store, err := NewStore(dir)
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer func() { _ = store.Close() }()
+
+ priv, _, _ := crypto.GenerateKeyPair(crypto.Ed25519, -1)
+ node, err := NewSamNode(Options{
+ PrivKey: priv,
+ Store: store,
+ ListenAddrs: []string{"/ip4/127.0.0.1/tcp/0"},
+ })
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ err = node.connectToHubAddresses(context.Background(), nil)
+ if err == nil || !strings.Contains(err.Error(), "returned no hub addresses") {
+ t.Fatalf("Expected no hub addresses error, got: %v", err)
+ }
+}