Skip to content

Commit

Permalink
GROUNDWORK-3850 prevent sending improper payload
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavlo Sumkin committed Dec 20, 2024
1 parent 04473ef commit 3dd80d8
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 18 deletions.
56 changes: 40 additions & 16 deletions services/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/binary"
"encoding/json"
"expvar"
"fmt"
"net/http"
"os"
"os/signal"
Expand Down Expand Up @@ -541,6 +542,16 @@ func (service *AgentService) stopNats() error {
}

func (service *AgentService) startTransport() error {
if service.Connector.AgentID == traceOnDemandAgentID ||
service.Connector.AppType == traceOnDemandAppType ||
len(service.Connector.AgentID) == 0 ||
len(service.Connector.AppType) == 0 {

err := fmt.Errorf("connector is not configured: AppType/AgentID: %v/%v",
service.Connector.AppType, service.Connector.AgentID)
log.Err(err).Msg("could not start")
return err
}
/* Process clients */
gwClients := make([]clients.GWClient, 0, len(config.GetConfig().GWConnections))
for i := range config.GetConfig().GWConnections {
Expand Down Expand Up @@ -580,28 +591,41 @@ func (service *AgentService) stopTransport() error {

// mixTracerContext adds `context` field if absent
func (service *AgentService) mixTracerContext(payloadJSON []byte) ([]byte, bool) {
if !bytes.Contains(payloadJSON, []byte(`"context":`)) || !bytes.Contains(payloadJSON, []byte(`"traceToken":`)) {
tc, todoTracerCtx := service.MakeTracerContext(), false
ctxJSON, err := json.Marshal(tc)
if err != nil {
log.Err(err).Msg("could not mixTracerContext")
return payloadJSON, false
}
if tc.AgentID == traceOnDemandAgentID ||
tc.AppType == traceOnDemandAppType {
todoTracerCtx = true
}
if bytes.Contains(payloadJSON, []byte(`"context":`)) &&
bytes.Contains(payloadJSON, []byte(`"traceToken":`)) {
return payloadJSON, false
}

l := bytes.LastIndexByte(payloadJSON, byte('}'))
return bytes.Join([][]byte{
payloadJSON[:l], []byte(`,"context":`), ctxJSON, []byte(`}`),
}, []byte(``)), todoTracerCtx
tc, todoTracerCtx := service.MakeTracerContext(), false
ctxJSON, err := json.Marshal(tc)
if err != nil {
log.Err(err).Msg("could not mixTracerContext")
return payloadJSON, false
}
return payloadJSON, false
if tc.AgentID == traceOnDemandAgentID ||
tc.AppType == traceOnDemandAppType {
todoTracerCtx = true
}

l := bytes.LastIndexByte(payloadJSON, byte('}'))
return bytes.Join([][]byte{
payloadJSON[:l], []byte(`,"context":`), ctxJSON, []byte(`}`),
}, []byte(``)), todoTracerCtx
}

// fixTracerContext replaces placeholders
func (service *AgentService) fixTracerContext(payloadJSON []byte) []byte {
if service.Connector.AgentID == traceOnDemandAgentID ||
service.Connector.AppType == traceOnDemandAppType ||
len(service.Connector.AgentID) == 0 ||
len(service.Connector.AppType) == 0 {

err := fmt.Errorf("connector is not configured: AppType/AgentID: %v/%v",
service.Connector.AppType, service.Connector.AgentID)
log.Err(err).Msg("could not fixTracerContext")
return payloadJSON
}

return bytes.ReplaceAll(
bytes.ReplaceAll(
payloadJSON,
Expand Down
5 changes: 3 additions & 2 deletions services/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,16 @@ func TestAgentService(t *testing.T) {
})

t.Run("NATS", func(t *testing.T) {
t.Setenv("TCG_CONNECTOR_NATSSTOREMAXBYTES", "333_222_111_000")
GetAgentService().Connector.NatsStoreMaxBytes = 333_222_111_000
assert.NoError(t, GetAgentService().StartNats())
assert.NoError(t, GetAgentService().StopNats())
assert.NoError(t, GetAgentService().StartNats())
assert.NoError(t, GetAgentService().StopNats())
})

t.Run("Transport", func(t *testing.T) {
t.Setenv("TCG_CONNECTOR_NATSSTOREMAXBYTES", "333_222_111_000")
GetAgentService().Connector.AgentID = "TESTAGENTID"
GetAgentService().Connector.AppType = "TESTAPPTYPE"
assert.NoError(t, GetAgentService().StartNats())
assert.NoError(t, GetAgentService().StartTransport())
assert.NoError(t, GetAgentService().StopTransport())
Expand Down

0 comments on commit 3dd80d8

Please sign in to comment.