-
Notifications
You must be signed in to change notification settings - Fork 593
/
access_mutual_tls_certificates.go
279 lines (236 loc) · 10.5 KB
/
access_mutual_tls_certificates.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
272
273
274
275
276
277
278
279
package cloudflare
import (
"context"
"fmt"
"net/http"
"time"
"github.com/goccy/go-json"
)
// AccessMutualTLSCertificate is the structure of a single Access Mutual TLS
// certificate.
type AccessMutualTLSCertificate struct {
ID string `json:"id,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
UpdatedAt time.Time `json:"updated_at,omitempty"`
ExpiresOn time.Time `json:"expires_on,omitempty"`
Name string `json:"name,omitempty"`
Fingerprint string `json:"fingerprint,omitempty"`
Certificate string `json:"certificate,omitempty"`
AssociatedHostnames []string `json:"associated_hostnames,omitempty"`
}
// AccessMutualTLSCertificateListResponse is the API response for all Access
// Mutual TLS certificates.
type AccessMutualTLSCertificateListResponse struct {
Response
Result []AccessMutualTLSCertificate `json:"result"`
ResultInfo `json:"result_info"`
}
// AccessMutualTLSCertificateDetailResponse is the API response for a single
// Access Mutual TLS certificate.
type AccessMutualTLSCertificateDetailResponse struct {
Response
Result AccessMutualTLSCertificate `json:"result"`
}
type ListAccessMutualTLSCertificatesParams struct {
ResultInfo
}
type CreateAccessMutualTLSCertificateParams struct {
ExpiresOn time.Time `json:"expires_on,omitempty"`
Name string `json:"name,omitempty"`
Fingerprint string `json:"fingerprint,omitempty"`
Certificate string `json:"certificate,omitempty"`
AssociatedHostnames []string `json:"associated_hostnames,omitempty"`
}
type UpdateAccessMutualTLSCertificateParams struct {
ID string `json:"-"`
ExpiresOn time.Time `json:"expires_on,omitempty"`
Name string `json:"name,omitempty"`
Fingerprint string `json:"fingerprint,omitempty"`
Certificate string `json:"certificate,omitempty"`
AssociatedHostnames []string `json:"associated_hostnames,omitempty"`
}
type AccessMutualTLSHostnameSettings struct {
ChinaNetwork *bool `json:"china_network,omitempty"`
ClientCertificateForwarding *bool `json:"client_certificate_forwarding,omitempty"`
Hostname string `json:"hostname,omitempty"`
}
type GetAccessMutualTLSHostnameSettingsResponse struct {
Response
Result []AccessMutualTLSHostnameSettings `json:"result"`
}
type UpdateAccessMutualTLSHostnameSettingsParams struct {
Settings []AccessMutualTLSHostnameSettings `json:"settings,omitempty"`
}
// ListAccessMutualTLSCertificates returns all Access TLS certificates
//
// Account API Reference: https://developers.cloudflare.com/api/operations/access-mtls-authentication-list-mtls-certificates
// Zone API Reference: https://developers.cloudflare.com/api/operations/zone-level-access-mtls-authentication-list-mtls-certificates
func (api *API) ListAccessMutualTLSCertificates(ctx context.Context, rc *ResourceContainer, params ListAccessMutualTLSCertificatesParams) ([]AccessMutualTLSCertificate, *ResultInfo, error) {
baseURL := fmt.Sprintf(
"/%s/%s/access/certificates",
rc.Level,
rc.Identifier,
)
autoPaginate := true
if params.PerPage >= 1 || params.Page >= 1 {
autoPaginate = false
}
if params.PerPage < 1 {
params.PerPage = 25
}
if params.Page < 1 {
params.Page = 1
}
var accessCertificates []AccessMutualTLSCertificate
var r AccessMutualTLSCertificateListResponse
for {
uri := buildURI(baseURL, params)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return []AccessMutualTLSCertificate{}, &ResultInfo{}, fmt.Errorf("%s: %w", errMakeRequestError, err)
}
err = json.Unmarshal(res, &r)
if err != nil {
return []AccessMutualTLSCertificate{}, &ResultInfo{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
accessCertificates = append(accessCertificates, r.Result...)
params.ResultInfo = r.ResultInfo.Next()
if params.ResultInfo.Done() || !autoPaginate {
break
}
}
return accessCertificates, &r.ResultInfo, nil
}
// GetAccessMutualTLSCertificate returns a single Access Mutual TLS
// certificate.
//
// Account API Reference: https://developers.cloudflare.com/api/operations/access-mtls-authentication-get-an-mtls-certificate
// Zone API Reference: https://developers.cloudflare.com/api/operations/zone-level-access-mtls-authentication-get-an-mtls-certificate
func (api *API) GetAccessMutualTLSCertificate(ctx context.Context, rc *ResourceContainer, certificateID string) (AccessMutualTLSCertificate, error) {
uri := fmt.Sprintf(
"/%s/%s/access/certificates/%s",
rc.Level,
rc.Identifier,
certificateID,
)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return AccessMutualTLSCertificate{}, fmt.Errorf("%s: %w", errMakeRequestError, err)
}
var accessMutualTLSCertificateDetailResponse AccessMutualTLSCertificateDetailResponse
err = json.Unmarshal(res, &accessMutualTLSCertificateDetailResponse)
if err != nil {
return AccessMutualTLSCertificate{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return accessMutualTLSCertificateDetailResponse.Result, nil
}
// CreateAccessMutualTLSCertificate creates an Access TLS Mutual
// certificate.
//
// Account API Reference: https://developers.cloudflare.com/api/operations/access-mtls-authentication-add-an-mtls-certificate
// Zone API Reference: https://developers.cloudflare.com/api/operations/zone-level-access-mtls-authentication-add-an-mtls-certificate
func (api *API) CreateAccessMutualTLSCertificate(ctx context.Context, rc *ResourceContainer, params CreateAccessMutualTLSCertificateParams) (AccessMutualTLSCertificate, error) {
uri := fmt.Sprintf(
"/%s/%s/access/certificates",
rc.Level,
rc.Identifier,
)
res, err := api.makeRequestContext(ctx, http.MethodPost, uri, params)
if err != nil {
return AccessMutualTLSCertificate{}, fmt.Errorf("%s: %w", errMakeRequestError, err)
}
var accessMutualTLSCertificateDetailResponse AccessMutualTLSCertificateDetailResponse
err = json.Unmarshal(res, &accessMutualTLSCertificateDetailResponse)
if err != nil {
return AccessMutualTLSCertificate{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return accessMutualTLSCertificateDetailResponse.Result, nil
}
// UpdateAccessMutualTLSCertificate updates an account level Access TLS Mutual
// certificate.
//
// Account API Reference: https://developers.cloudflare.com/api/operations/access-mtls-authentication-update-an-mtls-certificate
// Zone API Reference: https://developers.cloudflare.com/api/operations/zone-level-access-mtls-authentication-update-an-mtls-certificate
func (api *API) UpdateAccessMutualTLSCertificate(ctx context.Context, rc *ResourceContainer, params UpdateAccessMutualTLSCertificateParams) (AccessMutualTLSCertificate, error) {
uri := fmt.Sprintf(
"/%s/%s/access/certificates/%s",
rc.Level,
rc.Identifier,
params.ID,
)
res, err := api.makeRequestContext(ctx, http.MethodPut, uri, params)
if err != nil {
return AccessMutualTLSCertificate{}, fmt.Errorf("%s: %w", errMakeRequestError, err)
}
var accessMutualTLSCertificateDetailResponse AccessMutualTLSCertificateDetailResponse
err = json.Unmarshal(res, &accessMutualTLSCertificateDetailResponse)
if err != nil {
return AccessMutualTLSCertificate{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return accessMutualTLSCertificateDetailResponse.Result, nil
}
// DeleteAccessMutualTLSCertificate destroys an Access Mutual
// TLS certificate.
//
// Account API Reference: https://developers.cloudflare.com/api/operations/access-mtls-authentication-delete-an-mtls-certificate
// Zone API Reference: https://developers.cloudflare.com/api/operations/zone-level-access-mtls-authentication-delete-an-mtls-certificate
func (api *API) DeleteAccessMutualTLSCertificate(ctx context.Context, rc *ResourceContainer, certificateID string) error {
uri := fmt.Sprintf(
"/%s/%s/access/certificates/%s",
rc.Level,
rc.Identifier,
certificateID,
)
res, err := api.makeRequestContext(ctx, http.MethodDelete, uri, nil)
if err != nil {
return fmt.Errorf("%s: %w", errMakeRequestError, err)
}
var accessMutualTLSCertificateDetailResponse AccessMutualTLSCertificateDetailResponse
err = json.Unmarshal(res, &accessMutualTLSCertificateDetailResponse)
if err != nil {
return fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return nil
}
// GetAccessMutualTLSHostnameSettings returns all Access mTLS hostname settings.
//
// Account API Reference: https://developers.cloudflare.com/api/operations/access-mtls-authentication-update-an-mtls-certificate-settings
// Zone API Reference: https://developers.cloudflare.com/api/operations/zone-level-access-mtls-authentication-list-mtls-certificates-hostname-settings
func (api *API) GetAccessMutualTLSHostnameSettings(ctx context.Context, rc *ResourceContainer) ([]AccessMutualTLSHostnameSettings, error) {
uri := fmt.Sprintf(
"/%s/%s/access/certificates/settings",
rc.Level,
rc.Identifier,
)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return []AccessMutualTLSHostnameSettings{}, fmt.Errorf("%s: %w", errMakeRequestError, err)
}
var accessMutualTLSHostnameSettingsResponse GetAccessMutualTLSHostnameSettingsResponse
err = json.Unmarshal(res, &accessMutualTLSHostnameSettingsResponse)
if err != nil {
return []AccessMutualTLSHostnameSettings{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return accessMutualTLSHostnameSettingsResponse.Result, nil
}
// UpdateAccessMutualTLSHostnameSettings updates Access mTLS certificate hostname settings.
//
// Account API Reference: https://developers.cloudflare.com/api/operations/access-mtls-authentication-update-an-mtls-certificate-settings
// Zone API Reference: https://developers.cloudflare.com/api/operations/zone-level-access-mtls-authentication-update-an-mtls-certificate-settings
func (api *API) UpdateAccessMutualTLSHostnameSettings(ctx context.Context, rc *ResourceContainer, params UpdateAccessMutualTLSHostnameSettingsParams) ([]AccessMutualTLSHostnameSettings, error) {
uri := fmt.Sprintf(
"/%s/%s/access/certificates/settings",
rc.Level,
rc.Identifier,
)
res, err := api.makeRequestContext(ctx, http.MethodPut, uri, params)
if err != nil {
return []AccessMutualTLSHostnameSettings{}, fmt.Errorf("%s: %w", errMakeRequestError, err)
}
var accessMutualTLSHostnameSettingsResponse GetAccessMutualTLSHostnameSettingsResponse
err = json.Unmarshal(res, &accessMutualTLSHostnameSettingsResponse)
if err != nil {
return []AccessMutualTLSHostnameSettings{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return accessMutualTLSHostnameSettingsResponse.Result, nil
}