Open-source status page, incident response and uptime monitoring
TypeScript alertingincident-managementmcpmcp-servermonitoringmonitoring-as-codeobservabilityon-callopen-sourcesrestatus-pagestatuspagesynthetic-monitoringterraformtinybirdtursouptimeuptime-checkeruptime-monitor
private location: bug ingest server (#2360)
Thibault Le Ouay05bb1f7bbd64
diff --git a/apps/private-location/internal/server/ingest_dns.go b/apps/private-location/internal/server/ingest_dns.goindex 053156102..9021b5252 100644--- a/apps/private-location/internal/server/ingest_dns.go+++ b/apps/private-location/internal/server/ingest_dns.go@@ -42,7 +42,7 @@ func (h *privateLocationHandler) IngestDNS(ctx context.Context, req *connect.Reqreturn nil, NewValidationError(err)}- ic, err := h.getIngestContext(ctx, token, req.Msg.Id)+ ic, err := h.getIngestContext(ctx, token, req.Msg.MonitorId)if err != nil {return nil, connect.NewError(connect.CodeInternal, err)}@@ -58,12 +58,10 @@ func (h *privateLocationHandler) IngestDNS(ctx context.Context, req *connect.Req}records := make(map[string][]string)- for _, record := range req.Msg.Records {+ for recordType, record := range req.Msg.Records {r := []string{}- for _, value := range record.GetRecord() {- r = append(r, value)- }- records[record.String()] = r+ r = append(r, record.GetRecord()...)+ records[recordType] = r}recordsJSON, err := json.Marshal(records)diff --git a/apps/private-location/internal/server/ingest_dns_test.go b/apps/private-location/internal/server/ingest_dns_test.goindex 098e9646f..a1700ee87 100644--- a/apps/private-location/internal/server/ingest_dns_test.go+++ b/apps/private-location/internal/server/ingest_dns_test.go@@ -2,6 +2,8 @@ package server_testimport ("context"+ "encoding/json"+ "io""net/http""testing"@@ -9,6 +11,7 @@ import ("github.com/openstatushq/openstatus/apps/private-location/internal/server""github.com/openstatushq/openstatus/apps/private-location/internal/tinybird"private_locationv1 "github.com/openstatushq/openstatus/apps/private-location/proto/private_location/v1"+ "github.com/stretchr/testify/require")func TestIngestDNS_Unauthenticated(t *testing.T) {@@ -54,6 +57,7 @@ func TestIngestDNS_ValidationError_InvalidTimestamp(t *testing.T) {req := connect.NewRequest(&private_locationv1.IngestDNSRequest{Id: "dns-123",+ MonitorId: "5",Timestamp: 0, // Invalid - must be positive})req.Header().Set("openstatus-token", "my-secret-key")@@ -75,6 +79,7 @@ func TestIngestDNS_ValidationError_NegativeLatency(t *testing.T) {req := connect.NewRequest(&private_locationv1.IngestDNSRequest{Id: "dns-123",+ MonitorId: "5",Latency: -100,Timestamp: 1234567890,})@@ -97,6 +102,7 @@ func TestIngestDNS_DBError(t *testing.T) {req := connect.NewRequest(&private_locationv1.IngestDNSRequest{Id: "nonexistent-monitor",+ MonitorId: "nonexistent-monitor",Timestamp: 1234567890,})req.Header().Set("openstatus-token", "invalid-token")@@ -117,7 +123,8 @@ func TestIngestDNS_MonitorNotExist(t *testing.T) {h := server.NewPrivateLocationServer(testDB(), tinybird.NewClient(http.DefaultClient, ""))req := connect.NewRequest(&private_locationv1.IngestDNSRequest{- Id: "nonexistent-monitor",+ Id: "dns-123",+ MonitorId: "nonexistent-monitor",Timestamp: 1234567890,})req.Header().Set("openstatus-token", "my-secret-key")@@ -138,7 +145,8 @@ func TestIngestDNS_MonitorExist(t *testing.T) {h := server.NewPrivateLocationServer(testDB(), getTBClient(context.Background()))req := connect.NewRequest(&private_locationv1.IngestDNSRequest{- Id: "5",+ Id: "dns-result-1",+ MonitorId: "5",Timestamp: 1234567890,Latency: 50,CronTimestamp: 1234567800,@@ -161,7 +169,8 @@ func TestIngestDNS_WithRecords(t *testing.T) {h := server.NewPrivateLocationServer(testDB(), getTBClient(context.Background()))req := connect.NewRequest(&private_locationv1.IngestDNSRequest{- Id: "5",+ Id: "dns-result-2",+ MonitorId: "5",Timestamp: 1234567890,Latency: 50,CronTimestamp: 1234567800,@@ -187,11 +196,56 @@ func TestIngestDNS_WithRecords(t *testing.T) {}}+// TestIngestDNS_RecordsKeyedByType guards against keying the stored records by+// the proto message's String() output instead of the DNS record type.+func TestIngestDNS_RecordsKeyedByType(t *testing.T) {+ var capturedBody []byte+ interceptor := &interceptorHTTPClient{+ f: func(req *http.Request) (*http.Response, error) {+ if req.Body != nil {+ capturedBody, _ = io.ReadAll(req.Body)+ }+ return &http.Response{StatusCode: http.StatusAccepted}, nil+ },+ }+ h := server.NewPrivateLocationServer(testDB(), tinybird.NewClient(interceptor.GetHTTPClient(), "apiKey"))++ req := connect.NewRequest(&private_locationv1.IngestDNSRequest{+ Id: "dns-result-key",+ MonitorId: "5",+ Timestamp: 1234567890,+ Latency: 50,+ CronTimestamp: 1234567800,+ Uri: "dns://example.com",+ Records: map[string]*private_locationv1.Records{+ "A": {Record: []string{"192.168.1.1"}},+ "AAAA": {Record: []string{"::1"}},+ },+ })+ req.Header().Set("openstatus-token", "my-secret-key")++ _, err := h.IngestDNS(context.Background(), req)+ require.NoError(t, err)++ // The event's `records` field is itself a JSON-encoded string.+ var event struct {+ Records string `json:"records"`+ }+ require.NoError(t, json.Unmarshal(capturedBody, &event))++ var records map[string][]string+ require.NoError(t, json.Unmarshal([]byte(event.Records), &records))++ require.Equal(t, []string{"192.168.1.1"}, records["A"])+ require.Equal(t, []string{"::1"}, records["AAAA"])+}+func TestIngestDNS_WithError(t *testing.T) {h := server.NewPrivateLocationServer(testDB(), getTBClient(context.Background()))req := connect.NewRequest(&private_locationv1.IngestDNSRequest{- Id: "5",+ Id: "dns-result-3",+ MonitorId: "5",Timestamp: 1234567890,Latency: 0,CronTimestamp: 1234567800,diff --git a/apps/private-location/internal/server/ingest_tcp.go b/apps/private-location/internal/server/ingest_tcp.goindex 32e67f767..8545f92a5 100644--- a/apps/private-location/internal/server/ingest_tcp.go+++ b/apps/private-location/internal/server/ingest_tcp.go@@ -38,7 +38,7 @@ func (h *privateLocationHandler) IngestTCP(ctx context.Context, req *connect.Reqreturn nil, NewValidationError(err)}- ic, err := h.getIngestContext(ctx, token, req.Msg.Id)+ ic, err := h.getIngestContext(ctx, token, req.Msg.MonitorId)if err != nil {return nil, connect.NewError(connect.CodeInternal, err)}diff --git a/apps/private-location/internal/server/ingest_tcp_test.go b/apps/private-location/internal/server/ingest_tcp_test.goindex dc86f0b81..884526744 100644--- a/apps/private-location/internal/server/ingest_tcp_test.go+++ b/apps/private-location/internal/server/ingest_tcp_test.go@@ -35,6 +35,7 @@ func TestIngestTCP_DBError(t *testing.T) {req := connect.NewRequest(&private_locationv1.IngestTCPRequest{})req.Header().Set("openstatus-token", "token123")req.Msg.Id = "monitor1"+ req.Msg.MonitorId = "monitor1"req.Msg.Timestamp = 1234567890resp, err := h.IngestTCP(context.Background(), req)if err == nil {@@ -74,6 +75,7 @@ func TestIngestTCP_ValidationError_InvalidTimestamp(t *testing.T) {req := connect.NewRequest(&private_locationv1.IngestTCPRequest{Id: "tcp-123",+ MonitorId: "5",Timestamp: 0,})req.Header().Set("openstatus-token", "my-secret-key")@@ -95,6 +97,7 @@ func TestIngestTCP_ValidationError_NegativeLatency(t *testing.T) {req := connect.NewRequest(&private_locationv1.IngestTCPRequest{Id: "tcp-123",+ MonitorId: "5",Latency: -100,Timestamp: 1234567890,})@@ -116,7 +119,8 @@ func TestIngestTCP_MonitorNotExist(t *testing.T) {h := server.NewPrivateLocationServer(testDB(), tinybird.NewClient(http.DefaultClient, ""))req := connect.NewRequest(&private_locationv1.IngestTCPRequest{- Id: "nonexistent-monitor",+ Id: "tcp-123",+ MonitorId: "nonexistent-monitor",Timestamp: 1234567890,})req.Header().Set("openstatus-token", "my-secret-key")@@ -137,7 +141,8 @@ func TestIngestTCP_MonitorExist(t *testing.T) {h := server.NewPrivateLocationServer(testDB(), getTBClient(context.Background()))req := connect.NewRequest(&private_locationv1.IngestTCPRequest{- Id: "5",+ Id: "tcp-result-1",+ MonitorId: "5",Timestamp: 1234567890,Latency: 50,CronTimestamp: 1234567800,@@ -159,7 +164,8 @@ func TestIngestTCP_WithError(t *testing.T) {h := server.NewPrivateLocationServer(testDB(), getTBClient(context.Background()))req := connect.NewRequest(&private_locationv1.IngestTCPRequest{- Id: "5",+ Id: "tcp-result-2",+ MonitorId: "5",Timestamp: 1234567890,Latency: 0,CronTimestamp: 1234567800,diff --git a/apps/private-location/internal/server/validation.go b/apps/private-location/internal/server/validation.goindex 6f375a78c..43e42b6f4 100644--- a/apps/private-location/internal/server/validation.go+++ b/apps/private-location/internal/server/validation.go@@ -32,8 +32,8 @@ func ValidateIngestHTTPRequest(req *private_locationv1.IngestHTTPRequest) error// ValidateIngestTCPRequest validates a TCP ingest requestfunc ValidateIngestTCPRequest(req *private_locationv1.IngestTCPRequest) error {- if req.Id == "" {- return ErrEmptyID+ if req.MonitorId == "" {+ return ErrEmptyMonitorID}if req.Latency < 0 {return ErrInvalidLatency@@ -46,8 +46,8 @@ func ValidateIngestTCPRequest(req *private_locationv1.IngestTCPRequest) error {// ValidateIngestDNSRequest validates a DNS ingest requestfunc ValidateIngestDNSRequest(req *private_locationv1.IngestDNSRequest) error {- if req.Id == "" {- return ErrEmptyID+ if req.MonitorId == "" {+ return ErrEmptyMonitorID}if req.Latency < 0 {return ErrInvalidLatencydiff --git a/apps/private-location/internal/server/validation_test.go b/apps/private-location/internal/server/validation_test.goindex 7414a8666..d83f80ab6 100644--- a/apps/private-location/internal/server/validation_test.go+++ b/apps/private-location/internal/server/validation_test.go@@ -89,7 +89,7 @@ func TestValidateIngestTCPRequest(t *testing.T) {{name: "valid request",req: &private_locationv1.IngestTCPRequest{- Id: "tcp-123",+ MonitorId: "monitor-123",Latency: 100,Timestamp: 1234567890,},@@ -98,25 +98,25 @@ func TestValidateIngestTCPRequest(t *testing.T) {{name: "valid request with zero latency",req: &private_locationv1.IngestTCPRequest{- Id: "tcp-123",+ MonitorId: "monitor-123",Latency: 0,Timestamp: 1234567890,},wantErr: nil,},{- name: "empty id",+ name: "empty monitor_id",req: &private_locationv1.IngestTCPRequest{- Id: "",+ MonitorId: "",Latency: 100,Timestamp: 1234567890,},- wantErr: server.ErrEmptyID,+ wantErr: server.ErrEmptyMonitorID,},{name: "negative latency",req: &private_locationv1.IngestTCPRequest{- Id: "tcp-123",+ MonitorId: "monitor-123",Latency: -1,Timestamp: 1234567890,},@@ -125,7 +125,7 @@ func TestValidateIngestTCPRequest(t *testing.T) {{name: "zero timestamp",req: &private_locationv1.IngestTCPRequest{- Id: "tcp-123",+ MonitorId: "monitor-123",Latency: 100,Timestamp: 0,},@@ -134,7 +134,7 @@ func TestValidateIngestTCPRequest(t *testing.T) {{name: "negative timestamp",req: &private_locationv1.IngestTCPRequest{- Id: "tcp-123",+ MonitorId: "monitor-123",Latency: 100,Timestamp: -1,},@@ -161,7 +161,7 @@ func TestValidateIngestDNSRequest(t *testing.T) {{name: "valid request",req: &private_locationv1.IngestDNSRequest{- Id: "dns-123",+ MonitorId: "monitor-123",Latency: 100,Timestamp: 1234567890,},@@ -170,25 +170,25 @@ func TestValidateIngestDNSRequest(t *testing.T) {{name: "valid request with zero latency",req: &private_locationv1.IngestDNSRequest{- Id: "dns-123",+ MonitorId: "monitor-123",Latency: 0,Timestamp: 1234567890,},wantErr: nil,},{- name: "empty id",+ name: "empty monitor_id",req: &private_locationv1.IngestDNSRequest{- Id: "",+ MonitorId: "",Latency: 100,Timestamp: 1234567890,},- wantErr: server.ErrEmptyID,+ wantErr: server.ErrEmptyMonitorID,},{name: "negative latency",req: &private_locationv1.IngestDNSRequest{- Id: "dns-123",+ MonitorId: "monitor-123",Latency: -1,Timestamp: 1234567890,},@@ -197,7 +197,7 @@ func TestValidateIngestDNSRequest(t *testing.T) {{name: "zero timestamp",req: &private_locationv1.IngestDNSRequest{- Id: "dns-123",+ MonitorId: "monitor-123",Latency: 100,Timestamp: 0,},@@ -206,7 +206,7 @@ func TestValidateIngestDNSRequest(t *testing.T) {{name: "negative timestamp",req: &private_locationv1.IngestDNSRequest{- Id: "dns-123",+ MonitorId: "monitor-123",Latency: 100,Timestamp: -1,},