-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathclient.go
213 lines (177 loc) · 5.57 KB
/
client.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package retraced
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
)
const (
apiVersion = 2
defaultEndpoint = "http://localhost:3000/auditlog"
)
// Client represents a client that can send events into the retraced service.
type Client struct {
projectID string
token string
// Endpoint is the retraced api base url, default is `https://api.retraced.io`
Endpoint string
// Component is an identifier for a specific component of a vendor app platform
Component string
// Version is an identifier for the specific version of this component, usually a git SHA
Version string
// ViewLogAction is the action logged when a Viewer Token is used, default is 'audit.log.view'
ViewLogAction string
//
HttpClient *http.Client
}
// NewClient creates a new retraced api client that can be used to send events
func NewClient(endpoint string, projectID string, apiToken string) (*Client, error) {
ep := defaultEndpoint
if endpoint != "" {
ep = endpoint
}
return &Client{
projectID: projectID,
token: apiToken,
Endpoint: ep,
HttpClient: http.DefaultClient,
}, nil
}
// NewClientWithVersion Same as NewClient, but includes params for specifying the
// Component and Version of the Retraced client application
func NewClientWithVersion(endpoint string, projectID string, apiToken string, component string, version string) (*Client, error) {
ep := defaultEndpoint
if endpoint != "" {
ep = endpoint
}
return &Client{
projectID: projectID,
token: apiToken,
Endpoint: ep,
Component: component,
Version: version,
HttpClient: http.DefaultClient,
}, nil
}
// NewEventRecord is returned from the Retraced API when an event is created
type NewEventRecord struct {
ID string `json:"id"`
Hash string `json:"hash"`
}
// ReportEvent is the method to call to send a new event.
func (c *Client) ReportEvent(event *Event) (*NewEventRecord, error) {
event.apiVersion = apiVersion
if event.Version == "" {
event.Version = c.Version
}
if event.Component == "" {
event.Component = c.Component
}
encoded, err := json.Marshal(event)
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", fmt.Sprintf("%s/publisher/v1/project/%s/event", c.Endpoint, c.projectID), bytes.NewBuffer(encoded))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Token token=%s", c.token))
resp, err := c.HttpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusCreated {
return nil, fmt.Errorf("unexpected response from retraced api endpoint %s: %d", req.URL.String(), resp.StatusCode)
}
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var reqResp NewEventRecord
if err := json.Unmarshal(bodyBytes, &reqResp); err != nil {
return nil, err
}
if err := event.VerifyHash(&reqResp); err != nil {
return nil, err
}
return &reqResp, nil
}
// GetViewerToken will return a one-time use token that can be used to view a group's audit log.
func (c *Client) GetViewerToken(groupID string, isAdmin bool, actorID string, targetID string) (*ViewerToken, error) {
params := url.Values{}
params.Add("group_id", groupID)
params.Add("is_admin", strconv.FormatBool(isAdmin))
params.Add("actor_id", actorID)
params.Add("view_log_action", c.ViewLogAction)
if targetID != "" {
params.Add("target_id", targetID)
}
u, err := url.Parse(fmt.Sprintf("%s/publisher/v1/project/%s/viewertoken", c.Endpoint, c.projectID))
if err != nil {
return nil, err
}
u.RawQuery = params.Encode()
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Token token=%s", c.token))
resp, err := c.HttpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusCreated && resp.StatusCode != http.StatusOK { // There's a pending PR in the retraced API to match this.
return nil, fmt.Errorf("unexpected response from retraced api endpoint %s: %d", req.URL.String(), resp.StatusCode)
}
contents, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
viewerToken := ViewerToken{}
if err := json.Unmarshal(contents, &viewerToken); err != nil {
return nil, err
}
return &viewerToken, nil
}
// DeleteViewerSessions will delete all viewer sessions for the given actor in the given group.
func (c *Client) DeleteViewerSessions(groupID string, actorID string) error {
url := fmt.Sprintf("%s/v1/project/%s/group/%s/actor/%s/viewersessions", c.Endpoint, c.projectID, groupID, actorID)
req, err := http.NewRequest("DELETE", url, nil)
if err != nil {
return err
}
req.Header.Set("Authorization", fmt.Sprintf("Token token=%s", c.token))
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("Unexpected response from retraced api: %d", resp.StatusCode)
}
return nil
}
// Query searches for events using the Publisher API's GraphQL endpoint.
func (c *Client) Query(sq *StructuredQuery, mask *EventNodeMask, pageSize int) (EventsPager, error) {
url := fmt.Sprintf("%s/publisher/v1/project/%s/graphql", c.Endpoint, c.projectID)
ec := &EventsConnection{
url: url,
authorization: fmt.Sprintf("Token token=%s", c.token),
structuredQuery: sq,
mask: mask,
pageSize: pageSize,
httpClient: c.HttpClient,
}
err := ec.call()
if err != nil {
return nil, err
}
return ec, nil
}