|
| 1 | +// SPDX-License-Identifier: AGPL-3.0-or-later |
| 2 | + |
| 3 | +package authorityhttp |
| 4 | + |
| 5 | +import ( |
| 6 | + "crypto/ed25519" |
| 7 | + "crypto/rand" |
| 8 | + "crypto/sha256" |
| 9 | + "encoding/base64" |
| 10 | + "encoding/hex" |
| 11 | + "fmt" |
| 12 | + "io" |
| 13 | + "net/http" |
| 14 | + "net/url" |
| 15 | + "strconv" |
| 16 | + "strings" |
| 17 | + "time" |
| 18 | +) |
| 19 | + |
| 20 | +const ( |
| 21 | + agentRequestDomain = "pilot-agent-request-v1" |
| 22 | + agentRequestAgentIDHeader = "X-Pilot-Agent-Id" |
| 23 | + agentRequestKeyIDHeader = "X-Pilot-Agent-Key-Id" |
| 24 | + agentRequestTimestampHeader = "X-Pilot-Agent-Timestamp" |
| 25 | + agentRequestNonceHeader = "X-Pilot-Agent-Nonce" |
| 26 | + agentRequestSignatureHeader = "X-Pilot-Agent-Signature" |
| 27 | +) |
| 28 | + |
| 29 | +var emptyAgentRequestBodyHash = sha256.Sum256(nil) |
| 30 | + |
| 31 | +// AgentRequestSigner proves which enrolled node is fetching account-specific |
| 32 | +// policy or control-plane state. It is intentionally limited to bodyless GETs: |
| 33 | +// node reports already carry their own signed, validated artifacts. |
| 34 | +type AgentRequestSigner struct { |
| 35 | + agentID string |
| 36 | + keyID string |
| 37 | + privateKey ed25519.PrivateKey |
| 38 | + now func() time.Time |
| 39 | + random io.Reader |
| 40 | +} |
| 41 | + |
| 42 | +// NewAgentRequestSigner binds one active agent intent key to node-plane reads. |
| 43 | +func NewAgentRequestSigner(agentID, keyID string, privateKey ed25519.PrivateKey) (*AgentRequestSigner, error) { |
| 44 | + if !clientIdentifier(agentID) || !clientIdentifier(keyID) || len(privateKey) != ed25519.PrivateKeySize { |
| 45 | + return nil, fmt.Errorf("authorityhttp: invalid agent request signing credentials") |
| 46 | + } |
| 47 | + return &AgentRequestSigner{ |
| 48 | + agentID: agentID, keyID: keyID, |
| 49 | + privateKey: append(ed25519.PrivateKey(nil), privateKey...), |
| 50 | + now: time.Now, |
| 51 | + random: rand.Reader, |
| 52 | + }, nil |
| 53 | +} |
| 54 | + |
| 55 | +// ConfigureAgentRequestSigning enables authenticated policy, mandate, and |
| 56 | +// fleet reads. Configure the client before concurrent use. |
| 57 | +func (client *Client) ConfigureAgentRequestSigning(agentID, keyID string, privateKey ed25519.PrivateKey) error { |
| 58 | + if client == nil { |
| 59 | + return fmt.Errorf("authorityhttp: client is not initialized") |
| 60 | + } |
| 61 | + signer, err := NewAgentRequestSigner(agentID, keyID, privateKey) |
| 62 | + if err != nil { |
| 63 | + return err |
| 64 | + } |
| 65 | + client.agentRequestSigner = signer |
| 66 | + return nil |
| 67 | +} |
| 68 | + |
| 69 | +// SignAgentRequest signs a bodyless GET for integrations that do not use |
| 70 | +// Client. tenantID and agentID must exactly match the request query. |
| 71 | +func SignAgentRequest(request *http.Request, tenantID, agentID, keyID string, privateKey ed25519.PrivateKey) error { |
| 72 | + signer, err := NewAgentRequestSigner(agentID, keyID, privateKey) |
| 73 | + if err != nil { |
| 74 | + return err |
| 75 | + } |
| 76 | + return signer.Sign(request, tenantID, agentID) |
| 77 | +} |
| 78 | + |
| 79 | +func (client *Client) signAgentRequest(request *http.Request, tenantID, agentID string) error { |
| 80 | + // Optional signing preserves compatibility with older/self-hosted |
| 81 | + // authorities. Hosted node-plane endpoints require the signature. |
| 82 | + if client == nil || client.agentRequestSigner == nil { |
| 83 | + return nil |
| 84 | + } |
| 85 | + return client.agentRequestSigner.Sign(request, tenantID, agentID) |
| 86 | +} |
| 87 | + |
| 88 | +// Sign binds the method, normalized path and query, tenant, node identity, |
| 89 | +// delegated key, short-lived timestamp, nonce, and empty-body digest. |
| 90 | +func (signer *AgentRequestSigner) Sign(request *http.Request, tenantID, agentID string) error { |
| 91 | + if signer == nil || request == nil || request.URL == nil || signer.now == nil || signer.random == nil || len(signer.privateKey) != ed25519.PrivateKeySize { |
| 92 | + return fmt.Errorf("authorityhttp: agent request signer is not initialized") |
| 93 | + } |
| 94 | + if request.Method != http.MethodGet || (request.Body != nil && request.Body != http.NoBody) { |
| 95 | + return fmt.Errorf("authorityhttp: agent request signing requires a bodyless GET") |
| 96 | + } |
| 97 | + if !clientIdentifier(tenantID) || agentID != signer.agentID || !clientIdentifier(agentID) { |
| 98 | + return fmt.Errorf("authorityhttp: agent request identity mismatch") |
| 99 | + } |
| 100 | + query, err := strictAgentRequestQuery(request.URL, tenantID, agentID) |
| 101 | + if err != nil { |
| 102 | + return err |
| 103 | + } |
| 104 | + nonceBytes := make([]byte, 16) |
| 105 | + if _, err := io.ReadFull(signer.random, nonceBytes); err != nil { |
| 106 | + return fmt.Errorf("authorityhttp: generate agent request nonce: %w", err) |
| 107 | + } |
| 108 | + timestamp := strconv.FormatInt(signer.now().UTC().Unix(), 10) |
| 109 | + nonce := hex.EncodeToString(nonceBytes) |
| 110 | + canonical, err := canonicalAgentRequest(request.Method, request.URL, query, tenantID, agentID, signer.keyID, timestamp, nonce) |
| 111 | + if err != nil { |
| 112 | + return err |
| 113 | + } |
| 114 | + signature := ed25519.Sign(signer.privateKey, canonical) |
| 115 | + request.Header.Set(agentRequestAgentIDHeader, agentID) |
| 116 | + request.Header.Set(agentRequestKeyIDHeader, signer.keyID) |
| 117 | + request.Header.Set(agentRequestTimestampHeader, timestamp) |
| 118 | + request.Header.Set(agentRequestNonceHeader, nonce) |
| 119 | + request.Header.Set(agentRequestSignatureHeader, base64.RawURLEncoding.EncodeToString(signature)) |
| 120 | + return nil |
| 121 | +} |
| 122 | + |
| 123 | +func strictAgentRequestQuery(target *url.URL, tenantID, agentID string) (string, error) { |
| 124 | + if target == nil { |
| 125 | + return "", fmt.Errorf("authorityhttp: agent request URL is required") |
| 126 | + } |
| 127 | + values, err := url.ParseQuery(target.RawQuery) |
| 128 | + if err != nil || len(values["tenant_id"]) != 1 || len(values["agent_id"]) != 1 || values.Get("tenant_id") != tenantID || values.Get("agent_id") != agentID { |
| 129 | + return "", fmt.Errorf("authorityhttp: agent request query identity mismatch") |
| 130 | + } |
| 131 | + return values.Encode(), nil |
| 132 | +} |
| 133 | + |
| 134 | +func canonicalAgentRequest(method string, target *url.URL, query, tenantID, agentID, keyID, timestamp, nonce string) ([]byte, error) { |
| 135 | + path := "" |
| 136 | + if target != nil { |
| 137 | + path = target.EscapedPath() |
| 138 | + } |
| 139 | + if method != http.MethodGet || path == "" || !strings.HasPrefix(path, "/") || !clientIdentifier(tenantID) || !clientIdentifier(agentID) || !clientIdentifier(keyID) || timestamp == "" || len(nonce) != 32 { |
| 140 | + return nil, fmt.Errorf("authorityhttp: invalid canonical agent request") |
| 141 | + } |
| 142 | + fields := []string{ |
| 143 | + agentRequestDomain, |
| 144 | + method, |
| 145 | + path, |
| 146 | + query, |
| 147 | + hex.EncodeToString(emptyAgentRequestBodyHash[:]), |
| 148 | + tenantID, |
| 149 | + agentID, |
| 150 | + keyID, |
| 151 | + timestamp, |
| 152 | + nonce, |
| 153 | + } |
| 154 | + return []byte(strings.Join(fields, "\n")), nil |
| 155 | +} |
0 commit comments