-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathevent.go
174 lines (137 loc) · 4.7 KB
/
event.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package retraced
import (
"bytes"
"crypto/sha256"
"encoding/hex"
"fmt"
"sort"
"strings"
"time"
)
// Event represents a single audited event.
// Required fields: Action, Group
type Event struct {
// Action is a short, readable word to describe the action
Action string `json:"action"`
// Group is the team that will be able to see this event in the audit log
Group *Group `json:"group,omitempty"`
// Created is a timestamp representing when the event took place
Created time.Time `json:"created"`
// CRUD is a list of the most basic verbs that describe the type of action
CRUD string `json:"crud"`
// Target represents the item that had an action performed on it
Target *Target `json:"target,omitempty"`
// Description is a string with the full details of the event
Description string `json:"description,omitempty"`
// SourceIP is the client ip address where the event was performed
SourceIP string `json:"source_ip,omitempty"`
// Actor represents the entity that performed the action
Actor *Actor `json:"actor,omitempty"`
// Fields are any additional data to store with the event
Fields Fields `json:"fields,omitempty"`
// IsFailure is an optional flag that, when set, indicates that this audited event is a failed use of privileges
IsFailure bool `json:"is_failure,omitempty"`
// IsAnonymous is an optional flag that, when set, indicates that this is an anonymous event
IsAnonymous bool `json:"is_anonymous,omitempty"`
// Component is an identifier for a specific component of a vendor app platform
// Component can be set on each Event, or on an instance of retraced.Client
Component string `json:"component,omitempty"`
// Version is an identifier for the specific version of this component, usually a git SHA
// Version can be set on each Event, or on an instance of retraced.Client
Version string `json:"version,omitempty"`
// apiVersion is set here to allow updates to this model without breaking the API server
apiVersion int
ExternalID string `json:"external_id,omitempty"`
Metadata Fields `json:"metadata,omitempty"`
}
// VerifyHash computes a hash of the sent event, and verifies
// that it matches the hash we got back from Retraced API
func (event *Event) VerifyHash(newEvent *NewEventRecord) error {
// Basic sanity check
if event.Action == "" {
return fmt.Errorf("missing required field for hash verification: Action")
}
hashTarget := event.BuildHashTarget(newEvent)
hashBytes := sha256.Sum256(hashTarget)
result := hex.EncodeToString(hashBytes[:])
if result != newEvent.Hash {
return fmt.Errorf("hash mismatch: local[%s] != remote[%s]", result, newEvent.Hash)
}
return nil
}
// BuildHashTarget builds a string that will be used to
// compute a hash of the event
func (event *Event) BuildHashTarget(newEvent *NewEventRecord) []byte {
concat := &bytes.Buffer{}
fmt.Fprintf(concat, "%s:", encodePassOne(newEvent.ID))
fmt.Fprintf(concat, "%s:", encodePassOne(event.Action))
targetId := ""
if event.Target != nil {
targetId = event.Target.ID
}
fmt.Fprintf(concat, "%s:", encodePassOne(targetId))
actorId := ""
if event.Actor != nil {
actorId = event.Actor.ID
}
fmt.Fprintf(concat, "%s:", encodePassOne(actorId))
groupId := ""
if event.Group != nil {
groupId = event.Group.ID
}
fmt.Fprintf(concat, "%s:", encodePassOne(groupId))
fmt.Fprintf(concat, "%s:", encodePassOne(event.SourceIP))
if event.IsFailure {
fmt.Fprint(concat, "1:")
} else {
fmt.Fprint(concat, "0:")
}
if event.IsAnonymous {
fmt.Fprint(concat, "1:")
} else {
fmt.Fprint(concat, "0:")
}
if len(event.Fields) == 0 {
fmt.Fprintf(concat, ":")
} else {
allKeys := []string{}
for k := range event.Fields {
allKeys = append(allKeys, k)
}
sort.Strings(allKeys)
for i := 0; i < len(allKeys); i++ {
k := allKeys[i]
v := event.Fields[k]
encodedKey := encodePassTwo(encodePassOne(k))
encodedValue := encodePassTwo(encodePassOne(v))
fmt.Fprintf(concat, "%s=%s;", encodedKey, encodedValue)
}
}
if event.ExternalID != "" {
fmt.Fprintf(concat, ":%s", encodePassOne(event.ExternalID))
}
if len(event.Metadata) > 0 {
fmt.Fprintf(concat, ":")
allKeys := []string{}
for k := range event.Metadata {
allKeys = append(allKeys, k)
}
sort.Strings(allKeys)
for i := 0; i < len(allKeys); i++ {
k := allKeys[i]
v := event.Metadata[k]
encodedKey := encodePassTwo(encodePassOne(k))
encodedValue := encodePassTwo(encodePassOne(v))
fmt.Fprintf(concat, "%s=%s;", encodedKey, encodedValue)
}
}
return concat.Bytes()
}
func encodePassOne(in string) string {
s := strings.Replace(in, "%", "%25", -1)
return strings.Replace(s, ":", "%3A", -1)
}
func encodePassTwo(in string) string {
s := strings.Replace(in, "=", "%3D", -1)
return strings.Replace(s, ";", "%3B", -1)
}