-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathagentfirewall.go
More file actions
104 lines (93 loc) · 3.56 KB
/
Copy pathagentfirewall.go
File metadata and controls
104 lines (93 loc) · 3.56 KB
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
package codersdk
import (
"context"
"fmt"
"net/http"
"strconv"
"time"
"github.com/google/uuid"
)
// AgentFirewallSession represents a firewall session for a workspace agent.
type AgentFirewallSession struct {
ID uuid.UUID `json:"id" format:"uuid"`
WorkspaceID uuid.UUID `json:"workspace_id" format:"uuid"`
OwnerID uuid.UUID `json:"owner_id" format:"uuid"`
ConfinedProcess string `json:"confined_process"`
StartedAt time.Time `json:"started_at" format:"date-time"`
}
// AgentFirewallSessionByID returns an agent firewall session by its ID.
func (c *Client) AgentFirewallSessionByID(ctx context.Context, id uuid.UUID) (AgentFirewallSession, error) {
res, err := c.Request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/agent-firewall/sessions/%s", id), nil)
if err != nil {
return AgentFirewallSession{}, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return AgentFirewallSession{}, ReadBodyAsError(res)
}
var session AgentFirewallSession
return session, ReadBodyAsJSON(res, &session)
}
// AgentFirewallLog represents a single audit event from an agent firewall proxy.
type AgentFirewallLog struct {
ID uuid.UUID `json:"id" format:"uuid"`
SessionID uuid.UUID `json:"session_id" format:"uuid"`
SequenceNumber int32 `json:"sequence_number"`
Allowed bool `json:"allowed"`
CreatedAt time.Time `json:"created_at" format:"date-time"`
Proto string `json:"proto"`
Method string `json:"method"`
Detail string `json:"detail"`
MatchedRule *string `json:"matched_rule"`
CapturedAt *time.Time `json:"captured_at,omitempty" format:"date-time"`
}
// AgentFirewallSessionLogsResponse is the response for
// GET /api/v2/agent-firewall/sessions/{id}/logs.
type AgentFirewallSessionLogsResponse struct {
Results []AgentFirewallLog `json:"results"`
}
// AgentFirewallSessionLogsParams are query parameters for listing
// agent firewall session logs.
type AgentFirewallSessionLogsParams struct {
// SeqAfter is an inclusive lower bound on sequence_number.
// Only logs with sequence_number >= SeqAfter are returned.
SeqAfter *int64 `json:"seq_after,omitempty"`
// SeqBefore is an exclusive upper bound on sequence_number.
// Only logs with sequence_number < SeqBefore are returned.
SeqBefore *int64 `json:"seq_before,omitempty"`
// Limit caps the number of returned rows. Defaults to 100.
Limit *int32 `json:"limit,omitempty"`
}
func (p AgentFirewallSessionLogsParams) asRequestOption() RequestOption {
return func(r *http.Request) {
q := r.URL.Query()
if p.SeqAfter != nil {
q.Set("seq_after", strconv.FormatInt(*p.SeqAfter, 10))
}
if p.SeqBefore != nil {
q.Set("seq_before", strconv.FormatInt(*p.SeqBefore, 10))
}
if p.Limit != nil {
q.Set("limit", strconv.FormatInt(int64(*p.Limit), 10))
}
r.URL.RawQuery = q.Encode()
}
}
// AgentFirewallSessionLogs returns agent firewall audit logs for the
// given session, sorted by sequence number ascending.
func (c *Client) AgentFirewallSessionLogs(ctx context.Context, sessionID uuid.UUID, params AgentFirewallSessionLogsParams) (AgentFirewallSessionLogsResponse, error) {
res, err := c.Request(ctx, http.MethodGet,
fmt.Sprintf("/api/v2/agent-firewall/sessions/%s/logs", sessionID),
nil,
params.asRequestOption(),
)
if err != nil {
return AgentFirewallSessionLogsResponse{}, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return AgentFirewallSessionLogsResponse{}, ReadBodyAsError(res)
}
var resp AgentFirewallSessionLogsResponse
return resp, ReadBodyAsJSON(res, &resp)
}