Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 31 additions & 11 deletions cmd/sam-node/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,20 @@ func normalizeControlPlaneURL(url string) string {
return strings.TrimSuffix(url, "/")
}

// parseRouterAddrs keeps the stored router addresses that still parse.
func parseRouterAddrs(addrs []string) []multiaddr.Multiaddr {
out := make([]multiaddr.Multiaddr, 0, len(addrs))
for _, s := range addrs {
ma, err := multiaddr.NewMultiaddr(s)
if err != nil {
logger.Warnf("Ignoring stored router address %q: %v", s, err)
continue
}
out = append(out, ma)
}
return out
}

// interactiveJoin discovers a control plane's OIDC settings and completes an
// interactive browser/device-code login against it. Shared by "join" and
// "run --join"; targetControlPlane must already be a normalized URL. store is
Expand Down Expand Up @@ -332,13 +346,15 @@ func main() {
var controlPlanePubKey ed25519.PublicKey
var routerAddrs []multiaddr.Multiaddr

storedPubKey, syncedAddrs, bannedPeerIDs, err := node.SyncMeshConfig(context.Background(), store)
// What the last run left behind; the node itself pulls what the
// control plane knows now before it starts (SyncControlPlane).
storedPubKey, storedAddrs, err := store.LoadMeshConfig()
if err != nil {
logger.Warnf("Failed to sync mesh config: %v", err)
logger.Warnf("Failed to load stored mesh config: %v", err)
}
if len(storedPubKey) > 0 {
controlPlanePubKey = storedPubKey
routerAddrs = syncedAddrs
routerAddrs = parseRouterAddrs(storedAddrs)
}

if controlPlanePublicKeyFlag != "" {
Expand Down Expand Up @@ -457,7 +473,6 @@ func main() {
ControlPlanePubKey: controlPlanePubKey,
RouterAddrs: routerAddrs,
Store: store,
BannedPeerIDs: bannedPeerIDs,
MeshID: meshFlag,
DiscoveryInterval: discoveryIntervalFlag,
ListenAddrs: listenAddrs,
Expand All @@ -483,6 +498,9 @@ func main() {
if err != nil {
logger.Fatalf("Failed to initialize mesh node: %v", err)
}
if err := meshNode.SyncControlPlane(ctx); err != nil {
logger.Warnf("Control plane sync before start failed (using stored config): %v", err)
}
if err := meshNode.Start(ctx); err != nil {
logger.Fatalf("Failed to start mesh node: %v", err)
}
Expand Down Expand Up @@ -525,7 +543,6 @@ func main() {
PrivKey: priv,
RouterAddrs: initRouterAddrs,
Store: store,
BannedPeerIDs: bannedPeerIDs,
MeshID: meshFlag,
DiscoveryInterval: discoveryIntervalFlag,
ListenAddrs: listenAddrs,
Expand Down Expand Up @@ -583,20 +600,20 @@ func main() {
}
enrollCancel()

storedPubKey, newRouterAddrs, postEnrollBannedPeerIDs, err := node.SyncMeshConfig(context.Background(), store)
// Enrollment stored the control plane key and the router
// addresses it answered with; the node built from them pulls
// the rest before it starts.
controlPlanePubKey, storedAddrs, err = store.LoadMeshConfig()
if err != nil {
logger.Warnf("Failed to sync mesh config post-enrollment: %v", err)
logger.Fatalf("Failed to load mesh config after enrollment: %v", err)
}
controlPlanePubKey = storedPubKey
bannedPeerIDs = postEnrollBannedPeerIDs

logger.Debugf("listenAddrs: %v, allowLoopback: %v", listenAddrs, allowLoopbackFlag)
meshNode, err = node.NewSamNode(node.Options{
PrivKey: priv,
ControlPlanePubKey: controlPlanePubKey,
RouterAddrs: newRouterAddrs,
RouterAddrs: parseRouterAddrs(storedAddrs),
Store: store,
BannedPeerIDs: bannedPeerIDs,
MeshID: meshFlag,
DiscoveryInterval: discoveryIntervalFlag,
ListenAddrs: listenAddrs,
Expand All @@ -618,6 +635,9 @@ func main() {
if err != nil {
logger.Fatalf("Failed to initialize node after enrollment: %v", err)
}
if err := meshNode.SyncControlPlane(ctx); err != nil {
logger.Warnf("Control plane sync after enrollment failed (using enrollment config): %v", err)
}
if err := meshNode.Start(ctx); err != nil {
logger.Fatalf("Failed to start node after enrollment: %v", err)
}
Expand Down
164 changes: 164 additions & 0 deletions internal/controlplane/client/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package client is how a mesh component reads from its control plane. Node
// and router share it, so the body cap, the status handling and the signature
// check on /keys are a single code path. It depends on api/ only: importing it
// pulls in none of the control plane server.
package client

import (
"context"
"crypto/ed25519"
"encoding/base64"
"errors"
"fmt"
"io"
"net/http"
"strings"
"time"

"google.golang.org/protobuf/proto"

"github.com/google/sam/api"
)

// MaxBodyBytes caps every response body read from a control plane: a
// misbehaving or impersonated server must not be able to make a client
// buffer arbitrary amounts of memory. It is sized for the largest legitimate
// answer, the ban set in /info at roughly 55 bytes per peer ID, so about
// 150k banned peers fit; the policy is bounded by the control plane's own
// 1 MiB cap on POST /policies, and /keys is a few hundred bytes.
const MaxBodyBytes = 8 << 20

// ErrBodyTooLarge marks an answer over MaxBodyBytes. It is an error, never a
// prefix: a protobuf message cut at a field boundary still decodes, so a
// truncated ban set or router list would be read as a smaller, valid one.
var ErrBodyTooLarge = errors.New("control plane answer exceeds the body cap")

// ReadBody reads a control plane response body of at most MaxBodyBytes and
// reports ErrBodyTooLarge for anything larger.
func ReadBody(r io.Reader) ([]byte, error) {
body, err := io.ReadAll(io.LimitReader(r, MaxBodyBytes+1))
if err != nil {
return nil, fmt.Errorf("failed to read response body: %w", err)
}
if len(body) > MaxBodyBytes {
return nil, fmt.Errorf("%w (%d bytes)", ErrBodyTooLarge, MaxBodyBytes)
}
return body, nil
}

// transport applies api.ValidateControlPlaneTransport to every request,
// redirects included, so a plaintext hop is refused wherever the URL came
// from. allowInsecure is read per request: the node learns the operator's
// choice after its clients exist.
type transport struct {
allowInsecure func() bool
}

func (t transport) RoundTrip(req *http.Request) (*http.Response, error) {
if err := api.ValidateControlPlaneTransport(req.URL.String(), t.allowInsecure()); err != nil {
return nil, err
}
return http.DefaultTransport.RoundTrip(req)
}

// NewHTTPClient is the HTTP client for every request a mesh component makes
// to its control plane. A nil allowInsecure never allows plaintext.
func NewHTTPClient(timeout time.Duration, allowInsecure func() bool) *http.Client {
if allowInsecure == nil {
allowInsecure = func() bool { return false }
}
return &http.Client{Timeout: timeout, Transport: transport{allowInsecure: allowInsecure}}
}

// Client reads the pull side of the mesh protocol from one control plane.
type Client struct {
baseURL string
http *http.Client
}

// New normalizes baseURL, https:// when no scheme is given and no trailing
// slash, and speaks through httpClient, which the caller builds with
// NewHTTPClient so its own transport policy applies.
func New(baseURL string, httpClient *http.Client) *Client {
if !strings.HasPrefix(baseURL, "http://") && !strings.HasPrefix(baseURL, "https://") {
baseURL = "https://" + baseURL
}
return &Client{baseURL: strings.TrimSuffix(baseURL, "/"), http: httpClient}
}
Comment thread
aojea marked this conversation as resolved.

// FetchInfo is GET /info: the router addresses, the ban set and the OIDC
// details a node needs to enroll.
func (c *Client) FetchInfo(ctx context.Context) (*api.ControlPlaneInfoResponse, error) {
var info api.ControlPlaneInfoResponse
if err := c.get(ctx, "/info", nil, &info); err != nil {
return nil, err
}
return &info, nil
}

// FetchKeys is GET /keys: the control plane's currently valid signing keys.
// The set is accepted only if signed by a key in trusted
// (api.VerifyKeysResponse): whoever answers the URL must already be the
// control plane, not become it.
func (c *Client) FetchKeys(ctx context.Context, trusted []ed25519.PublicKey) ([]ed25519.PublicKey, error) {
var resp api.KeysResponse
if err := c.get(ctx, "/keys", nil, &resp); err != nil {
return nil, err
}
keys, err := api.VerifyKeysResponse(&resp, trusted, time.Now())
if err != nil {
return nil, fmt.Errorf("/keys response rejected: %w", err)
}
return keys, nil
}

// FetchPolicy is GET /policies, authenticated with the caller's biscuit: the
// roles and bindings a node compiles into its authorization rules.
func (c *Client) FetchPolicy(ctx context.Context, biscuit []byte) (*api.PolicyConfigGetResponse, error) {
var policy api.PolicyConfigGetResponse
if err := c.get(ctx, "/policies", biscuit, &policy); err != nil {
return nil, err
}
return &policy, nil
}

func (c *Client) get(ctx context.Context, path string, biscuit []byte, msg proto.Message) error {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.baseURL+path, nil)
if err != nil {
return fmt.Errorf("failed to create HTTP request: %w", err)
}
if len(biscuit) > 0 {
req.Header.Set("Authorization", "Bearer "+base64.StdEncoding.EncodeToString(biscuit))
}
resp, err := c.http.Do(req)
if err != nil {
return fmt.Errorf("HTTP request failed: %w", err)
}
defer func() { _ = resp.Body.Close() }()

body, err := ReadBody(resp.Body)
if err != nil {
return fmt.Errorf("%s: %w", path, err)
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("control plane returned status %s: %s", resp.Status, string(body))
}
if err := proto.Unmarshal(body, msg); err != nil {
return fmt.Errorf("failed to decode %s response: %w", path, err)
}
return nil
}
Loading
Loading