-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate DataDog event enrichment depending on the payload type (#11)
* Pick up the timestamp value from X-Cirrus-Timestamp * Separate DataDog event enrichment depending on the payload type * CI: run "go test ./..." * Enrich build and task's initializer and audit_event's actor
- Loading branch information
Showing
14 changed files
with
422 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package payload | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"github.com/cirruslabs/cirrus-webhooks-server/internal/datadogsender" | ||
"go.uber.org/zap" | ||
"net/http" | ||
) | ||
|
||
type AuditEvent struct { | ||
Data *string `json:"data"` | ||
|
||
Actor struct { | ||
Username *string `json:"username"` | ||
} `json:"actor"` | ||
|
||
ActorLocationIP *string `json:"actorLocationIp"` | ||
|
||
common | ||
} | ||
|
||
func (auditEvent AuditEvent) Enrich(header http.Header, evt *datadogsender.Event, logger *zap.SugaredLogger) { | ||
auditEvent.common.Enrich(header, evt, logger) | ||
|
||
if data := auditEvent.Data; data != nil { | ||
var auditEventData auditEventData | ||
|
||
if err := json.Unmarshal([]byte(*data), &auditEventData); err != nil { | ||
logger.Warnf("failed to unmarshal audit event's data: %v", err) | ||
|
||
return | ||
} else { | ||
auditEventData.Enrich(header, evt, logger) | ||
} | ||
} | ||
|
||
actorUsername := "api" | ||
if value := auditEvent.Actor.Username; value != nil { | ||
actorUsername = *value | ||
} | ||
evt.Tags = append(evt.Tags, fmt.Sprintf("actor_username:%s", actorUsername)) | ||
|
||
if value := auditEvent.ActorLocationIP; value != nil { | ||
evt.Tags = append(evt.Tags, fmt.Sprintf("actor_location_ip:%s", *value)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package payload_test | ||
|
||
import ( | ||
"encoding/json" | ||
"github.com/cirruslabs/cirrus-webhooks-server/internal/command/datadog/payload" | ||
"github.com/cirruslabs/cirrus-webhooks-server/internal/datadogsender" | ||
"github.com/stretchr/testify/require" | ||
"go.uber.org/zap" | ||
"net/http" | ||
"os" | ||
"path/filepath" | ||
"strconv" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestEnrichAuditEvent(t *testing.T) { | ||
body, err := os.ReadFile(filepath.Join("testdata", "audit_event.json")) | ||
require.NoError(t, err) | ||
|
||
evt := &datadogsender.Event{} | ||
|
||
payload := payload.AuditEvent{} | ||
require.NoError(t, json.Unmarshal(body, &payload)) | ||
payload.Enrich(http.Header{ | ||
"X-Cirrus-Timestamp": []string{strconv.FormatInt(time.Now().UnixMilli(), 10)}, | ||
}, evt, zap.S()) | ||
require.WithinDuration(t, time.Now(), evt.Timestamp, time.Second) | ||
require.Equal(t, []string{ | ||
"action:created", | ||
"type:graphql.mutation", | ||
"data.mutationName:GenerateNewScopedAccessToken", | ||
"actor_username:edigaryev", | ||
"actor_location_ip:1.2.3.4", | ||
}, evt.Tags) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package payload | ||
|
||
import ( | ||
"fmt" | ||
"github.com/cirruslabs/cirrus-webhooks-server/internal/datadogsender" | ||
"go.uber.org/zap" | ||
"net/http" | ||
) | ||
|
||
type auditEventData struct { | ||
MutationName *string `json:"mutationName"` | ||
BuildID *string `json:"buildId"` | ||
TaskID *string `json:"taskId"` | ||
} | ||
|
||
func (auditEventData auditEventData) Enrich(header http.Header, evt *datadogsender.Event, logger *zap.SugaredLogger) { | ||
if mutationName := auditEventData.MutationName; mutationName != nil { | ||
evt.Tags = append(evt.Tags, fmt.Sprintf("data.mutationName:%s", *mutationName)) | ||
} | ||
|
||
if buildID := auditEventData.BuildID; buildID != nil { | ||
evt.Tags = append(evt.Tags, fmt.Sprintf("data.buildId:%s", *buildID)) | ||
} | ||
|
||
if taskID := auditEventData.TaskID; taskID != nil { | ||
evt.Tags = append(evt.Tags, fmt.Sprintf("data.taskId:%s", *taskID)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package payload | ||
|
||
import ( | ||
"fmt" | ||
"github.com/cirruslabs/cirrus-webhooks-server/internal/datadogsender" | ||
"go.uber.org/zap" | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
type BuildOrTask struct { | ||
Build struct { | ||
ID *int64 `json:"id"` | ||
Status *string `json:"status"` | ||
Branch *string `json:"branch"` | ||
PullRequest *int64 `json:"pullRequest"` | ||
User struct { | ||
Username *string `json:"username"` | ||
} `json:"user"` | ||
} | ||
Task struct { | ||
ID *int64 `json:"id"` | ||
Name *string `json:"name"` | ||
Status *string `json:"status"` | ||
InstanceType *string `json:"instanceType"` | ||
UniqueLabels []string `json:"uniqueLabels"` | ||
} | ||
|
||
common | ||
} | ||
|
||
func (buildOrTask BuildOrTask) Enrich(header http.Header, evt *datadogsender.Event, logger *zap.SugaredLogger) { | ||
buildOrTask.common.Enrich(header, evt, logger) | ||
|
||
if value := buildOrTask.Build.ID; value != nil { | ||
evt.Tags = append(evt.Tags, fmt.Sprintf("build_id:%d", *value)) | ||
} | ||
if value := buildOrTask.Build.Status; value != nil { | ||
evt.Tags = append(evt.Tags, fmt.Sprintf("build_status:%s", *value)) | ||
} | ||
if value := buildOrTask.Build.Branch; value != nil { | ||
evt.Tags = append(evt.Tags, fmt.Sprintf("build_branch:%s", *value)) | ||
} | ||
if value := buildOrTask.Build.PullRequest; value != nil { | ||
evt.Tags = append(evt.Tags, fmt.Sprintf("build_pull_request:%d", *value)) | ||
} | ||
|
||
initializerUsername := "api" | ||
if value := buildOrTask.Build.User.Username; value != nil { | ||
initializerUsername = *value | ||
} | ||
evt.Tags = append(evt.Tags, fmt.Sprintf("initializer_username:%s", initializerUsername)) | ||
|
||
if value := buildOrTask.Task.ID; value != nil { | ||
evt.Tags = append(evt.Tags, fmt.Sprintf("task_id:%d", *value)) | ||
} | ||
if value := buildOrTask.Task.Name; value != nil { | ||
evt.Tags = append(evt.Tags, fmt.Sprintf("task_name:%s", *value)) | ||
} | ||
if value := buildOrTask.Task.Status; value != nil { | ||
evt.Tags = append(evt.Tags, fmt.Sprintf("task_status:%s", *value)) | ||
} | ||
if value := buildOrTask.Task.InstanceType; value != nil { | ||
evt.Tags = append(evt.Tags, fmt.Sprintf("task_instance_type:%s", *value)) | ||
} | ||
if value := buildOrTask.Task.UniqueLabels; len(value) > 0 { | ||
evt.Tags = append(evt.Tags, fmt.Sprintf("task_unique_labels:%s", strings.Join(value, ","))) | ||
} | ||
} |
Oops, something went wrong.