-
Notifications
You must be signed in to change notification settings - Fork 773
Expand file tree
/
Copy pathleaked_credential_check.go
More file actions
190 lines (164 loc) · 7.33 KB
/
Copy pathleaked_credential_check.go
File metadata and controls
190 lines (164 loc) · 7.33 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
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
package cloudflare
import (
"context"
"fmt"
"net/http"
"github.com/goccy/go-json"
)
type LeakedCredentialCheckGetStatusParams struct{}
type LeakedCredentialCheckStatus struct {
Enabled *bool `json:"enabled"`
}
type LeakCredentialCheckStatusResponse struct {
Response
Result LeakedCredentialCheckStatus `json:"result"`
}
type LeakCredentialCheckSetStatusParams struct {
Enabled *bool `json:"enabled"`
}
type LeakedCredentialCheckListDetectionsParams struct{}
type LeakedCredentialCheckDetectionEntry struct {
ID string `json:"id"`
Username string `json:"username"`
Password string `json:"password"`
}
type LeakedCredentialCheckListDetectionsResponse struct {
Response
Result []LeakedCredentialCheckDetectionEntry `json:"result"`
}
type LeakedCredentialCheckCreateDetectionParams struct {
Username string `json:"username"`
Password string `json:"password"`
}
type LeakedCredentialCheckCreateDetectionResponse struct {
Response
Result LeakedCredentialCheckDetectionEntry `json:"result"`
}
type LeakedCredentialCheckDeleteDetectionParams struct {
DetectionID string
}
type LeakedCredentialCheckDeleteDetectionResponse struct {
Response
Result []struct{} `json:"result"`
}
type LeakedCredentialCheckUpdateDetectionParams struct {
LeakedCredentialCheckDetectionEntry
}
type LeakedCredentialCheckUpdateDetectionResponse struct {
Response
Result LeakedCredentialCheckDetectionEntry
}
// LeakCredentialCheckGetStatus returns whether Leaked credential check is enabled or not. It is false by default.
//
// API reference: https://developers.cloudflare.com/api/resources/leaked_credential_checks/methods/get/
func (api *API) LeakedCredentialCheckGetStatus(ctx context.Context, rc *ResourceContainer, params LeakedCredentialCheckGetStatusParams) (LeakedCredentialCheckStatus, error) {
if rc.Identifier == "" {
return LeakedCredentialCheckStatus{}, ErrMissingZoneID
}
uri := fmt.Sprintf("/zones/%s/leaked-credential-checks", rc.Identifier)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return LeakedCredentialCheckStatus{}, err
}
result := LeakCredentialCheckStatusResponse{}
if err := json.Unmarshal(res, &result); err != nil {
return LeakedCredentialCheckStatus{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return result.Result, nil
}
// LeakedCredentialCheckSetStatus enable or disable the Leak Credential Check. Returns the status.
//
// API reference: https://developers.cloudflare.com/api/resources/leaked_credential_checks/methods/create/
func (api *API) LeakedCredentialCheckSetStatus(ctx context.Context, rc *ResourceContainer, params LeakCredentialCheckSetStatusParams) (LeakedCredentialCheckStatus, error) {
if rc.Identifier == "" {
return LeakedCredentialCheckStatus{}, ErrMissingZoneID
}
uri := fmt.Sprintf("/zones/%s/leaked-credential-checks", rc.Identifier)
res, err := api.makeRequestContext(ctx, http.MethodPost, uri, params)
if err != nil {
return LeakedCredentialCheckStatus{}, err
}
result := LeakCredentialCheckStatusResponse{}
if err := json.Unmarshal(res, &result); err != nil {
return LeakedCredentialCheckStatus{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return result.Result, nil
}
// LeakedCredentialCheckListDetections lists user-defined detection patterns for Leaked Credential Checks.
//
// API reference: https://developers.cloudflare.com/api/resources/leaked_credential_checks/subresources/detections/methods/list/
func (api *API) LeakedCredentialCheckListDetections(ctx context.Context, rc *ResourceContainer, params LeakedCredentialCheckListDetectionsParams) ([]LeakedCredentialCheckDetectionEntry, error) {
if rc.Identifier == "" {
return []LeakedCredentialCheckDetectionEntry{}, ErrMissingZoneID
}
uri := fmt.Sprintf("/zones/%s/leaked-credential-checks/detections", rc.Identifier)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, params)
if err != nil {
return []LeakedCredentialCheckDetectionEntry{}, err
}
result := LeakedCredentialCheckListDetectionsResponse{}
if err := json.Unmarshal(res, &result); err != nil {
return []LeakedCredentialCheckDetectionEntry{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return result.Result, nil
}
// LeakedCredentialCheckCreateDetection creates user-defined detection pattern for Leaked Credential Checks
//
// API reference: https://developers.cloudflare.com/api/resources/leaked_credential_checks/subresources/detections/methods/create/
func (api *API) LeakedCredentialCheckCreateDetection(ctx context.Context, rc *ResourceContainer, params LeakedCredentialCheckCreateDetectionParams) (LeakedCredentialCheckDetectionEntry, error) {
if rc.Identifier == "" {
return LeakedCredentialCheckDetectionEntry{}, ErrMissingZoneID
}
uri := fmt.Sprintf("/zones/%s/leaked-credential-checks/detections", rc.Identifier)
res, err := api.makeRequestContext(ctx, http.MethodPost, uri, params)
if err != nil {
return LeakedCredentialCheckDetectionEntry{}, err
}
result := LeakedCredentialCheckCreateDetectionResponse{}
if err := json.Unmarshal(res, &result); err != nil {
return LeakedCredentialCheckDetectionEntry{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return result.Result, nil
}
// LeakedCredentialCheckDeleteDetection removes user-defined detection pattern for Leaked Credential Checks
//
// API reference: https://developers.cloudflare.com/api/resources/leaked_credential_checks/subresources/detections/methods/delete/
func (api *API) LeakedCredentialCheckDeleteDetection(ctx context.Context, rc *ResourceContainer, params LeakedCredentialCheckDeleteDetectionParams) (LeakedCredentialCheckDeleteDetectionResponse, error) {
if rc.Identifier == "" {
return LeakedCredentialCheckDeleteDetectionResponse{}, ErrMissingZoneID
}
if params.DetectionID == "" {
return LeakedCredentialCheckDeleteDetectionResponse{}, ErrMissingDetectionID
}
uri := fmt.Sprintf("/zones/%s/leaked-credential-checks/detections/%s", rc.Identifier, params.DetectionID)
res, err := api.makeRequestContext(ctx, http.MethodDelete, uri, nil)
if err != nil {
return LeakedCredentialCheckDeleteDetectionResponse{}, err
}
result := LeakedCredentialCheckDeleteDetectionResponse{}
if err := json.Unmarshal(res, &result); err != nil {
return LeakedCredentialCheckDeleteDetectionResponse{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return result, nil
}
// LeakedCredentialCheckUpdateDetection updates user-defined detection pattern for Leaked Credential Checks. Returns updated detection.
//
// API reference: https://developers.cloudflare.com/api/resources/leaked_credential_checks/subresources/detections/methods/update/
func (api *API) LeakedCredentialCheckUpdateDetection(ctx context.Context, rc *ResourceContainer, params LeakedCredentialCheckUpdateDetectionParams) (LeakedCredentialCheckDetectionEntry, error) {
if rc.Identifier == "" {
return LeakedCredentialCheckDetectionEntry{}, ErrMissingZoneID
}
if params.ID == "" {
return LeakedCredentialCheckDetectionEntry{}, ErrMissingDetectionID
}
uri := fmt.Sprintf("/zones/%s/leaked-credential-checks/detections/%s", rc.Identifier, params.ID)
res, err := api.makeRequestContext(ctx, http.MethodPut, uri, params)
if err != nil {
return LeakedCredentialCheckDetectionEntry{}, err
}
result := LeakedCredentialCheckUpdateDetectionResponse{}
if err := json.Unmarshal(res, &result); err != nil {
return LeakedCredentialCheckDetectionEntry{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return result.Result, nil
}