-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathsecret_scanning.go
271 lines (230 loc) · 11.2 KB
/
secret_scanning.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// Copyright 2022 The go-github AUTHORS. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package github
import (
"context"
"fmt"
)
// SecretScanningService handles communication with the secret scanning related
// methods of the GitHub API.
type SecretScanningService service
// SecretScanningAlert represents a GitHub secret scanning alert.
type SecretScanningAlert struct {
Number *int `json:"number,omitempty"`
CreatedAt *Timestamp `json:"created_at,omitempty"`
URL *string `json:"url,omitempty"`
HTMLURL *string `json:"html_url,omitempty"`
LocationsURL *string `json:"locations_url,omitempty"`
State *string `json:"state,omitempty"`
Resolution *string `json:"resolution,omitempty"`
ResolvedAt *Timestamp `json:"resolved_at,omitempty"`
ResolvedBy *User `json:"resolved_by,omitempty"`
SecretType *string `json:"secret_type,omitempty"`
SecretTypeDisplayName *string `json:"secret_type_display_name,omitempty"`
Secret *string `json:"secret,omitempty"`
Repository *Repository `json:"repository,omitempty"`
UpdatedAt *Timestamp `json:"updated_at,omitempty"`
PushProtectionBypassed *bool `json:"push_protection_bypassed,omitempty"`
PushProtectionBypassedBy *User `json:"push_protection_bypassed_by,omitempty"`
PushProtectionBypassedAt *Timestamp `json:"push_protection_bypassed_at,omitempty"`
ResolutionComment *string `json:"resolution_comment,omitempty"`
}
// SecretScanningAlertLocation represents the location for a secret scanning alert.
type SecretScanningAlertLocation struct {
Type *string `json:"type,omitempty"`
Details *SecretScanningAlertLocationDetails `json:"details,omitempty"`
}
// SecretScanningAlertLocationDetails represents the location details for a secret scanning alert.
type SecretScanningAlertLocationDetails struct {
Path *string `json:"path,omitempty"`
Startline *int `json:"start_line,omitempty"`
EndLine *int `json:"end_line,omitempty"`
StartColumn *int `json:"start_column,omitempty"`
EndColumn *int `json:"end_column,omitempty"`
BlobSHA *string `json:"blob_sha,omitempty"`
BlobURL *string `json:"blob_url,omitempty"`
CommitSHA *string `json:"commit_sha,omitempty"`
CommitURL *string `json:"commit_url,omitempty"`
PullRequestCommentURL *string `json:"pull_request_comment_url,omitempty"`
}
// SecretScanningAlertListOptions specifies optional parameters to the SecretScanningService.ListAlertsForEnterprise method.
type SecretScanningAlertListOptions struct {
// State of the secret scanning alerts to list. Set to open or resolved to only list secret scanning alerts in a specific state.
State string `url:"state,omitempty"`
// A comma-separated list of secret types to return. By default all secret types are returned.
SecretType string `url:"secret_type,omitempty"`
// A comma-separated list of resolutions. Only secret scanning alerts with one of these resolutions are listed.
// Valid resolutions are false_positive, wont_fix, revoked, pattern_edited, pattern_deleted or used_in_tests.
Resolution string `url:"resolution,omitempty"`
// A comma-separated list of validities that, when present, will return alerts that match the validities in this list.
// Valid options are active, inactive, and unknown.
Validity string `url:"validity,omitempty"`
// The direction to sort the results by. Possible values are: asc, desc. Default: desc.
Direction string `url:"direction,omitempty"`
// The property by which to sort the results. Possible values are: created, updated. Default: created.
Sort string `url:"sort,omitempty"`
ListCursorOptions
// List options can vary on the Enterprise type.
// On Enterprise Cloud, Secret Scan alerts support requesting by page number
// along with providing a cursor for an "after" param.
// See: https://docs.github.com/enterprise-cloud@latest/rest/secret-scanning#list-secret-scanning-alerts-for-an-organization
// Whereas on Enterprise Server, pagination is by index.
// See: https://docs.github.com/enterprise-server@3.6/rest/secret-scanning#list-secret-scanning-alerts-for-an-organization
ListOptions
}
// SecretScanningAlertUpdateOptions specifies optional parameters to the SecretScanningService.UpdateAlert method.
type SecretScanningAlertUpdateOptions struct {
// State is required and sets the state of the secret scanning alert.
// Can be either "open" or "resolved".
// You must provide resolution when you set the state to "resolved".
State string `json:"state"`
// Required when the state is "resolved" and represents the reason for resolving the alert.
// Can be one of: "false_positive", "wont_fix", "revoked", or "used_in_tests".
Resolution *string `json:"resolution,omitempty"`
// An optional comment when closing an alert.
ResolutionComment *string `json:"resolution_comment,omitempty"`
}
// ListAlertsForEnterprise lists secret scanning alerts for eligible repositories in an enterprise, from newest to oldest.
//
// To use this endpoint, you must be a member of the enterprise, and you must use an access token with the repo scope or
// security_events scope. Alerts are only returned for organizations in the enterprise for which you are an organization owner or a security manager.
//
// GitHub API docs: https://docs.github.com/rest/secret-scanning/secret-scanning#list-secret-scanning-alerts-for-an-enterprise
//
//meta:operation GET /enterprises/{enterprise}/secret-scanning/alerts
func (s *SecretScanningService) ListAlertsForEnterprise(ctx context.Context, enterprise string, opts *SecretScanningAlertListOptions) ([]*SecretScanningAlert, *Response, error) {
u := fmt.Sprintf("enterprises/%v/secret-scanning/alerts", enterprise)
u, err := addOptions(u, opts)
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
var alerts []*SecretScanningAlert
resp, err := s.client.Do(ctx, req, &alerts)
if err != nil {
return nil, resp, err
}
return alerts, resp, nil
}
// ListAlertsForOrg lists secret scanning alerts for eligible repositories in an organization, from newest to oldest.
//
// To use this endpoint, you must be an administrator for the repository or organization, and you must use an access token with
// the repo scope or security_events scope.
//
// GitHub API docs: https://docs.github.com/rest/secret-scanning/secret-scanning#list-secret-scanning-alerts-for-an-organization
//
//meta:operation GET /orgs/{org}/secret-scanning/alerts
func (s *SecretScanningService) ListAlertsForOrg(ctx context.Context, org string, opts *SecretScanningAlertListOptions) ([]*SecretScanningAlert, *Response, error) {
u := fmt.Sprintf("orgs/%v/secret-scanning/alerts", org)
u, err := addOptions(u, opts)
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
var alerts []*SecretScanningAlert
resp, err := s.client.Do(ctx, req, &alerts)
if err != nil {
return nil, resp, err
}
return alerts, resp, nil
}
// ListAlertsForRepo lists secret scanning alerts for a private repository, from newest to oldest.
//
// To use this endpoint, you must be an administrator for the repository or organization, and you must use an access token with
// the repo scope or security_events scope.
//
// GitHub API docs: https://docs.github.com/rest/secret-scanning/secret-scanning#list-secret-scanning-alerts-for-a-repository
//
//meta:operation GET /repos/{owner}/{repo}/secret-scanning/alerts
func (s *SecretScanningService) ListAlertsForRepo(ctx context.Context, owner, repo string, opts *SecretScanningAlertListOptions) ([]*SecretScanningAlert, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/secret-scanning/alerts", owner, repo)
u, err := addOptions(u, opts)
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
var alerts []*SecretScanningAlert
resp, err := s.client.Do(ctx, req, &alerts)
if err != nil {
return nil, resp, err
}
return alerts, resp, nil
}
// GetAlert gets a single secret scanning alert detected in a private repository.
//
// To use this endpoint, you must be an administrator for the repository or organization, and you must use an access token with
// the repo scope or security_events scope.
//
// GitHub API docs: https://docs.github.com/rest/secret-scanning/secret-scanning#get-a-secret-scanning-alert
//
//meta:operation GET /repos/{owner}/{repo}/secret-scanning/alerts/{alert_number}
func (s *SecretScanningService) GetAlert(ctx context.Context, owner, repo string, number int64) (*SecretScanningAlert, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/secret-scanning/alerts/%v", owner, repo, number)
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
var alert *SecretScanningAlert
resp, err := s.client.Do(ctx, req, &alert)
if err != nil {
return nil, resp, err
}
return alert, resp, nil
}
// UpdateAlert updates the status of a secret scanning alert in a private repository.
//
// To use this endpoint, you must be an administrator for the repository or organization, and you must use an access token with
// the repo scope or security_events scope.
//
// GitHub API docs: https://docs.github.com/rest/secret-scanning/secret-scanning#update-a-secret-scanning-alert
//
//meta:operation PATCH /repos/{owner}/{repo}/secret-scanning/alerts/{alert_number}
func (s *SecretScanningService) UpdateAlert(ctx context.Context, owner, repo string, number int64, opts *SecretScanningAlertUpdateOptions) (*SecretScanningAlert, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/secret-scanning/alerts/%v", owner, repo, number)
req, err := s.client.NewRequest("PATCH", u, opts)
if err != nil {
return nil, nil, err
}
var alert *SecretScanningAlert
resp, err := s.client.Do(ctx, req, &alert)
if err != nil {
return nil, resp, err
}
return alert, resp, nil
}
// ListLocationsForAlert lists all locations for a given secret scanning alert for a private repository.
//
// To use this endpoint, you must be an administrator for the repository or organization, and you must use an access token with
// the repo scope or security_events scope.
//
// GitHub API docs: https://docs.github.com/rest/secret-scanning/secret-scanning#list-locations-for-a-secret-scanning-alert
//
//meta:operation GET /repos/{owner}/{repo}/secret-scanning/alerts/{alert_number}/locations
func (s *SecretScanningService) ListLocationsForAlert(ctx context.Context, owner, repo string, number int64, opts *ListOptions) ([]*SecretScanningAlertLocation, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/secret-scanning/alerts/%v/locations", owner, repo, number)
u, err := addOptions(u, opts)
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
var locations []*SecretScanningAlertLocation
resp, err := s.client.Do(ctx, req, &locations)
if err != nil {
return nil, resp, err
}
return locations, resp, nil
}