-
Notifications
You must be signed in to change notification settings - Fork 102
/
audit_trail.go
152 lines (126 loc) · 3.98 KB
/
audit_trail.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package tfe
import (
"context"
"encoding/json"
"io"
"net/http"
"time"
"github.com/google/go-querystring/query"
retryablehttp "github.com/hashicorp/go-retryablehttp"
)
// Compile-time proof of interface implementation
var _ AuditTrails = (*auditTrails)(nil)
// AuditTrails describes all the audit event related methods that the HCP Terraform
// API supports.
// **Note:** These methods require the client to be configured with an organization token for
// an organization in the Business tier. Furthermore, these methods are only available in HCP Terraform.
//
// HCP Terraform API Docs: https://developer.hashicorp.com/terraform/cloud-docs/api-docs/audit-trails
type AuditTrails interface {
// Read all the audit events in an organization.
List(ctx context.Context, options *AuditTrailListOptions) (*AuditTrailList, error)
}
// auditTrails implements AuditTrails
type auditTrails struct {
client *Client
}
// AuditTrailRequest represents the request details of the audit event.
type AuditTrailRequest struct {
ID string `json:"id"`
}
// AuditTrailAuth represents the details of the actor that invoked the audit event.
type AuditTrailAuth struct {
AccessorID string `json:"accessor_id"`
Description string `json:"description"`
Type string `json:"type"`
ImpersonatorID *string `json:"impersonator_id"`
OrganizationID string `json:"organization_id"`
}
// AuditTrailResource represents the details of the API resource in the audit event.
type AuditTrailResource struct {
ID string `json:"id"`
Type string `json:"type"`
Action string `json:"action"`
Meta map[string]interface{} `json:"meta"`
}
type AuditTrailPagination struct {
CurrentPage int `json:"current_page"`
PreviousPage int `json:"prev_page"`
NextPage int `json:"next_page"`
TotalPages int `json:"total_pages"`
TotalCount int `json:"total_count"`
}
// AuditTrail represents an event in the HCP Terraform audit log.
type AuditTrail struct {
ID string `json:"id"`
Version string `json:"version"`
Type string `json:"type"`
Timestamp time.Time `json:"timestamp"`
Auth AuditTrailAuth `json:"auth"`
Request AuditTrailRequest `json:"request"`
Resource AuditTrailResource `json:"resource"`
}
// AuditTrailList represents a list of audit trails.
type AuditTrailList struct {
*AuditTrailPagination `json:"pagination"`
Items []*AuditTrail `json:"data"`
}
// AuditTrailListOptions represents the options for listing audit trails.
type AuditTrailListOptions struct {
// Optional: Returns only audit trails created after this date
Since time.Time `url:"since,omitempty"`
*ListOptions
}
// List all the audit events in an organization.
func (s *auditTrails) List(ctx context.Context, options *AuditTrailListOptions) (*AuditTrailList, error) {
u, err := s.client.baseURL.Parse("/api/v2/organization/audit-trail")
if err != nil {
return nil, err
}
headers := make(http.Header)
headers.Set("User-Agent", _userAgent)
headers.Set("Authorization", "Bearer "+s.client.token)
headers.Set("Content-Type", "application/json")
if options != nil {
q, err := query.Values(options)
if err != nil {
return nil, err
}
u.RawQuery = encodeQueryParams(q)
}
req, err := retryablehttp.NewRequest("GET", u.String(), nil)
if err != nil {
return nil, err
}
// Attach the headers to the request
for k, v := range headers {
req.Header[k] = v
}
if err := s.client.limiter.Wait(ctx); err != nil {
return nil, err
}
resp, err := s.client.http.Do(req.WithContext(ctx))
if err != nil {
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
return nil, err
}
}
defer resp.Body.Close()
if err := checkResponseCode(resp); err != nil {
return nil, err
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
atl := &AuditTrailList{}
if err := json.Unmarshal(body, atl); err != nil {
return nil, err
}
return atl, nil
}