Add CarrierNode project attribution

This commit is contained in:
Gabriel20xx committed 2026-09-18 11:21:06 +02:00
1 parent 87cb39c04c
commit 2d8f138215
2 files changed
+29

No files matched your search

@@ -36,6 +36,12 @@ var forwardResponseHeaders = []string{"Content-Type", "Content-Length", "Content
// Client calls CarrierNode's HTTP API on behalf of one of the other 6 // Client calls CarrierNode's HTTP API on behalf of one of the other 6
// media-family services. // media-family services.
type Client struct { type Client struct {
// ProjectKey identifies the service making a request to CarrierNode. It is
// optional for backwards compatibility, but when set it lets CarrierNode
// attribute internal transfers to the originating project on its traffic
// dashboard instead of combining every service under one bucket.
ProjectKey string
// InternalServiceSecret is sent as the X-Internal-Auth header on every // InternalServiceSecret is sent as the X-Internal-Auth header on every
// request, so CarrierNode can distinguish trusted service-to-service // request, so CarrierNode can distinguish trusted service-to-service
// calls from public traffic. Empty means no header is sent. // calls from public traffic. Empty means no header is sent.
@@ -136,6 +142,9 @@ func (c *Client) newRequest(ctx context.Context, baseURL, method, path string, b
if secret != "" { if secret != "" {
request.Header.Set("X-Internal-Auth", secret) request.Header.Set("X-Internal-Auth", secret)
} }
if project := strings.TrimSpace(c.ProjectKey); project != "" {
request.Header.Set("X-Project-Key", project)
}
return request, nil return request, nil
} }
@@ -30,6 +30,26 @@ func TestListFilesMergesAcrossInstances(t *testing.T) {
} }
} }
func TestRequestsCarryProjectAttribution(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if got := r.Header.Get("X-Internal-Auth"); got != "secret" {
t.Fatalf("X-Internal-Auth = %q, want secret", got)
}
if got := r.Header.Get("X-Project-Key"); got != "ContentScanner" {
t.Fatalf("X-Project-Key = %q, want ContentScanner", got)
}
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"files":[]}`))
}))
defer server.Close()
client := New("secret", func(ctx context.Context) string { return server.URL })
client.ProjectKey = "ContentScanner"
if _, err := client.ListFiles(context.Background(), "", false, 0); err != nil {
t.Fatalf("ListFiles error: %v", err)
}
}
func TestListFilesSkipsUnreachableInstance(t *testing.T) { func TestListFilesSkipsUnreachableInstance(t *testing.T) {
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")