From e4debd9af1c291dda9e559131c3390ea7dbfbfcd Mon Sep 17 00:00:00 2001 From: Matheus Nogueira Date: Fri, 13 Oct 2023 14:54:57 -0300 Subject: [PATCH] fix: transmit http body as bytes instead of string (#3254) * fix: transmit http body as bytes instead of string * fix: update grpc body to []byte --- .../workflow_send_trigger_response_test.go | 2 +- agent/proto/orchestrator.pb.go | 22 ++++----- agent/proto/orchestrator.proto | 4 +- agent/proto/orchestrator_grpc.pb.go | 2 +- agent/workers/trigger.go | 4 +- agent/workers/trigger_test.go | 48 ++++++++++++++++++- 6 files changed, 64 insertions(+), 18 deletions(-) diff --git a/agent/client/workflow_send_trigger_response_test.go b/agent/client/workflow_send_trigger_response_test.go index 4060c0c251..0ed63a535e 100644 --- a/agent/client/workflow_send_trigger_response_test.go +++ b/agent/client/workflow_send_trigger_response_test.go @@ -29,7 +29,7 @@ func TestSendTriggerResult(t *testing.T) { StatusCode: 200, Status: "OK", Headers: []*proto.HttpHeader{}, - Body: `{"ok": true}`, + Body: []byte(`{"ok": true}`), }, }, } diff --git a/agent/proto/orchestrator.pb.go b/agent/proto/orchestrator.pb.go index fa531b0eb8..b3ceb32617 100644 --- a/agent/proto/orchestrator.pb.go +++ b/agent/proto/orchestrator.pb.go @@ -1,7 +1,7 @@ // Code generated by protoc-gen-go. DO NOT EDIT. // versions: // protoc-gen-go v1.31.0 -// protoc v4.24.2 +// protoc v4.23.4 // source: proto/orchestrator.proto package proto @@ -1220,7 +1220,7 @@ type HttpResponse struct { StatusCode int32 `protobuf:"varint,1,opt,name=statusCode,proto3" json:"statusCode,omitempty"` Status string `protobuf:"bytes,2,opt,name=status,proto3" json:"status,omitempty"` Headers []*HttpHeader `protobuf:"bytes,3,rep,name=headers,proto3" json:"headers,omitempty"` - Body string `protobuf:"bytes,4,opt,name=body,proto3" json:"body,omitempty"` + Body []byte `protobuf:"bytes,4,opt,name=body,proto3" json:"body,omitempty"` } func (x *HttpResponse) Reset() { @@ -1276,11 +1276,11 @@ func (x *HttpResponse) GetHeaders() []*HttpHeader { return nil } -func (x *HttpResponse) GetBody() string { +func (x *HttpResponse) GetBody() []byte { if x != nil { return x.Body } - return "" + return nil } type GrpcResponse struct { @@ -1290,7 +1290,7 @@ type GrpcResponse struct { StatusCode int32 `protobuf:"varint,1,opt,name=statusCode,proto3" json:"statusCode,omitempty"` Metadata []*GrpcHeader `protobuf:"bytes,2,rep,name=metadata,proto3" json:"metadata,omitempty"` - Body string `protobuf:"bytes,3,opt,name=body,proto3" json:"body,omitempty"` + Body []byte `protobuf:"bytes,3,opt,name=body,proto3" json:"body,omitempty"` } func (x *GrpcResponse) Reset() { @@ -1339,11 +1339,11 @@ func (x *GrpcResponse) GetMetadata() []*GrpcHeader { return nil } -func (x *GrpcResponse) GetBody() string { +func (x *GrpcResponse) GetBody() []byte { if x != nil { return x.Body } - return "" + return nil } type TraceIdResponse struct { @@ -1399,7 +1399,7 @@ type DataStoreConnectionTestRequest struct { unknownFields protoimpl.UnknownFields RequestID string `protobuf:"bytes,1,opt,name=requestID,proto3" json:"requestID,omitempty"` - Datastore *DataStore `protobuf:"bytes,5,opt,name=datastore,proto3" json:"datastore,omitempty"` + Datastore *DataStore `protobuf:"bytes,2,opt,name=datastore,proto3" json:"datastore,omitempty"` } func (x *DataStoreConnectionTestRequest) Reset() { @@ -3235,21 +3235,21 @@ var file_proto_orchestrator_proto_rawDesc = []byte{ 0x65, 0x72, 0x73, 0x18, 0x03, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x11, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x48, 0x74, 0x74, 0x70, 0x48, 0x65, 0x61, 0x64, 0x65, 0x72, 0x52, 0x07, 0x68, 0x65, 0x61, 0x64, 0x65, 0x72, 0x73, 0x12, 0x12, 0x0a, 0x04, 0x62, 0x6f, 0x64, 0x79, 0x18, 0x04, 0x20, - 0x01, 0x28, 0x09, 0x52, 0x04, 0x62, 0x6f, 0x64, 0x79, 0x22, 0x71, 0x0a, 0x0c, 0x47, 0x72, 0x70, + 0x01, 0x28, 0x0c, 0x52, 0x04, 0x62, 0x6f, 0x64, 0x79, 0x22, 0x71, 0x0a, 0x0c, 0x47, 0x72, 0x70, 0x63, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x1e, 0x0a, 0x0a, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x43, 0x6f, 0x64, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x0a, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x43, 0x6f, 0x64, 0x65, 0x12, 0x2d, 0x0a, 0x08, 0x6d, 0x65, 0x74, 0x61, 0x64, 0x61, 0x74, 0x61, 0x18, 0x02, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x11, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x47, 0x72, 0x70, 0x63, 0x48, 0x65, 0x61, 0x64, 0x65, 0x72, 0x52, 0x08, 0x6d, 0x65, 0x74, 0x61, 0x64, 0x61, 0x74, 0x61, 0x12, 0x12, 0x0a, 0x04, 0x62, 0x6f, 0x64, 0x79, - 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x62, 0x6f, 0x64, 0x79, 0x22, 0x21, 0x0a, 0x0f, + 0x18, 0x03, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x04, 0x62, 0x6f, 0x64, 0x79, 0x22, 0x21, 0x0a, 0x0f, 0x54, 0x72, 0x61, 0x63, 0x65, 0x49, 0x64, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x64, 0x22, 0x6e, 0x0a, 0x1e, 0x44, 0x61, 0x74, 0x61, 0x53, 0x74, 0x6f, 0x72, 0x65, 0x43, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x54, 0x65, 0x73, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x1c, 0x0a, 0x09, 0x72, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x49, 0x44, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x72, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x49, 0x44, 0x12, - 0x2e, 0x0a, 0x09, 0x64, 0x61, 0x74, 0x61, 0x73, 0x74, 0x6f, 0x72, 0x65, 0x18, 0x05, 0x20, 0x01, + 0x2e, 0x0a, 0x09, 0x64, 0x61, 0x74, 0x61, 0x73, 0x74, 0x6f, 0x72, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x10, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x44, 0x61, 0x74, 0x61, 0x53, 0x74, 0x6f, 0x72, 0x65, 0x52, 0x09, 0x64, 0x61, 0x74, 0x61, 0x73, 0x74, 0x6f, 0x72, 0x65, 0x22, 0xe8, 0x01, 0x0a, 0x1f, 0x44, 0x61, 0x74, 0x61, 0x53, 0x74, 0x6f, 0x72, 0x65, 0x43, 0x6f, 0x6e, diff --git a/agent/proto/orchestrator.proto b/agent/proto/orchestrator.proto index 7cf4a8851f..7c5422e8fd 100644 --- a/agent/proto/orchestrator.proto +++ b/agent/proto/orchestrator.proto @@ -163,13 +163,13 @@ message HttpResponse { int32 statusCode = 1; string status = 2; repeated HttpHeader headers = 3; - string body = 4; + bytes body = 4; } message GrpcResponse { int32 statusCode = 1; repeated GrpcHeader metadata = 2; - string body = 3; + bytes body = 3; } message TraceIdResponse { diff --git a/agent/proto/orchestrator_grpc.pb.go b/agent/proto/orchestrator_grpc.pb.go index b0adf7f67a..f03e71cfd6 100644 --- a/agent/proto/orchestrator_grpc.pb.go +++ b/agent/proto/orchestrator_grpc.pb.go @@ -1,7 +1,7 @@ // Code generated by protoc-gen-go-grpc. DO NOT EDIT. // versions: // - protoc-gen-go-grpc v1.3.0 -// - protoc v4.24.2 +// - protoc v4.23.4 // source: proto/orchestrator.proto package proto diff --git a/agent/workers/trigger.go b/agent/workers/trigger.go index 9ecac8af94..9f2f328ce1 100644 --- a/agent/workers/trigger.go +++ b/agent/workers/trigger.go @@ -254,7 +254,7 @@ func convertHttpResponseToProto(http *trigger.HTTPResponse) *proto.HttpResponse StatusCode: int32(http.StatusCode), Status: http.Status, Headers: headers, - Body: http.Body, + Body: []byte(http.Body), } } @@ -271,7 +271,7 @@ func convertGrpcResponseToProto(grpc *trigger.GRPCResponse) *proto.GrpcResponse return &proto.GrpcResponse{ StatusCode: int32(grpc.StatusCode), Metadata: headers, - Body: grpc.Body, + Body: []byte(grpc.Body), } } diff --git a/agent/workers/trigger_test.go b/agent/workers/trigger_test.go index 10cfcd6b92..308bad93c9 100644 --- a/agent/workers/trigger_test.go +++ b/agent/workers/trigger_test.go @@ -61,12 +61,58 @@ func TestTrigger(t *testing.T) { require.NotNil(t, response) assert.Equal(t, "http", response.TriggerResult.Type) assert.Equal(t, int32(http.StatusOK), response.TriggerResult.Http.StatusCode) - assert.JSONEq(t, `{"hello": "world"}`, response.TriggerResult.Http.Body) + assert.JSONEq(t, `{"hello": "world"}`, string(response.TriggerResult.Http.Body)) _, traceIdIsWatched := cache.Get(traceID) assert.True(t, traceIdIsWatched) } +func TestTriggerAgainstGoogle(t *testing.T) { + cache := collector.NewTraceCache() + controlPlane := mocks.NewGrpcServer() + + client, err := client.Connect(context.Background(), controlPlane.Addr()) + require.NoError(t, err) + + triggerWorker := workers.NewTriggerWorker(client, workers.WithTraceCache(cache)) + + client.OnTriggerRequest(func(ctx context.Context, tr *proto.TriggerRequest) error { + err := triggerWorker.Trigger(ctx, tr) + assert.NoError(t, err, "trigger failed") + return err + }) + + client.Start(context.Background()) + + traceID := "42a2c381da1a5b3a32bc4988bf2431b0" + + triggerRequest := &proto.TriggerRequest{ + TestID: "my test", + RunID: 1, + TraceID: traceID, + Trigger: &proto.Trigger{ + Type: "http", + Http: &proto.HttpRequest{ + Method: "GET", + Url: "https://google.com", + Headers: []*proto.HttpHeader{ + {Key: "Content-Type", Value: "application/json"}, + }, + }, + }, + } + + // make the control plane send a trigger request to the agent + controlPlane.SendTriggerRequest(triggerRequest) + time.Sleep(1 * time.Second) + + response := controlPlane.GetLastTriggerResponse() + + require.NotNil(t, response) + assert.Equal(t, "http", response.TriggerResult.Type) + assert.Equal(t, int32(http.StatusOK), response.TriggerResult.Http.StatusCode) +} + func createHelloWorldApi() *httptest.Server { return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Write([]byte(`{"hello": "world"}`))