forked from cloudflare/cloudflare-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
access_mutual_tls_certificates.go
224 lines (190 loc) · 8.78 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
package cloudflare
import (
"context"
"encoding/json"
"fmt"
"net/http"
"time"
)
// 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"`
}
// AccessMutualTLSCertificateDetailResponse is the API response for a single
// Access Mutual TLS certificate.
type AccessMutualTLSCertificateDetailResponse struct {
Response
Result AccessMutualTLSCertificate `json:"result"`
}
// AccessMutualTLSCertificates returns all Access TLS certificates for the account
// level.
//
// API reference: https://api.cloudflare.com/#access-mutual-tls-authentication-properties
func (api *API) AccessMutualTLSCertificates(ctx context.Context, accountID string) ([]AccessMutualTLSCertificate, error) {
return api.accessMutualTLSCertificates(ctx, accountID, AccountRouteRoot)
}
// ZoneAccessMutualTLSCertificates returns all Access TLS certificates for the
// zone level.
//
// API reference: https://api.cloudflare.com/#zone-level-access-mutual-tls-authentication-properties
func (api *API) ZoneAccessMutualTLSCertificates(ctx context.Context, zoneID string) ([]AccessMutualTLSCertificate, error) {
return api.accessMutualTLSCertificates(ctx, zoneID, ZoneRouteRoot)
}
func (api *API) accessMutualTLSCertificates(ctx context.Context, id string, routeRoot RouteRoot) ([]AccessMutualTLSCertificate, error) {
uri := fmt.Sprintf(
"/%s/%s/access/certificates",
routeRoot,
id,
)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return []AccessMutualTLSCertificate{}, err
}
var accessMutualTLSCertificateListResponse AccessMutualTLSCertificateListResponse
err = json.Unmarshal(res, &accessMutualTLSCertificateListResponse)
if err != nil {
return []AccessMutualTLSCertificate{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return accessMutualTLSCertificateListResponse.Result, nil
}
// AccessMutualTLSCertificate returns a single account level Access Mutual TLS
// certificate.
//
// API reference: https://api.cloudflare.com/#access-mutual-tls-authentication-access-certificate-details
func (api *API) AccessMutualTLSCertificate(ctx context.Context, accountID, certificateID string) (AccessMutualTLSCertificate, error) {
return api.accessMutualTLSCertificate(ctx, accountID, certificateID, AccountRouteRoot)
}
// ZoneAccessMutualTLSCertificate returns a single zone level Access Mutual TLS
// certificate.
//
// API reference: https://api.cloudflare.com/#zone-level-access-mutual-tls-authentication-access-certificate-details
func (api *API) ZoneAccessMutualTLSCertificate(ctx context.Context, zoneID, certificateID string) (AccessMutualTLSCertificate, error) {
return api.accessMutualTLSCertificate(ctx, zoneID, certificateID, ZoneRouteRoot)
}
func (api *API) accessMutualTLSCertificate(ctx context.Context, id, certificateID string, routeRoot RouteRoot) (AccessMutualTLSCertificate, error) {
uri := fmt.Sprintf(
"/%s/%s/access/certificates/%s",
routeRoot,
id,
certificateID,
)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return AccessMutualTLSCertificate{}, 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 account level Access TLS Mutual
// certificate.
//
// API reference: https://api.cloudflare.com/#access-mutual-tls-authentication-create-access-certificate
func (api *API) CreateAccessMutualTLSCertificate(ctx context.Context, accountID string, certificate AccessMutualTLSCertificate) (AccessMutualTLSCertificate, error) {
return api.createAccessMutualTLSCertificate(ctx, accountID, certificate, AccountRouteRoot)
}
// CreateZoneAccessMutualTLSCertificate creates a zone level Access TLS Mutual
// certificate.
//
// API reference: https://api.cloudflare.com/#zone-level-access-mutual-tls-authentication-create-access-certificate
func (api *API) CreateZoneAccessMutualTLSCertificate(ctx context.Context, zoneID string, certificate AccessMutualTLSCertificate) (AccessMutualTLSCertificate, error) {
return api.createAccessMutualTLSCertificate(ctx, zoneID, certificate, ZoneRouteRoot)
}
func (api *API) createAccessMutualTLSCertificate(ctx context.Context, id string, certificate AccessMutualTLSCertificate, routeRoot RouteRoot) (AccessMutualTLSCertificate, error) {
uri := fmt.Sprintf(
"/%s/%s/access/certificates",
routeRoot,
id,
)
res, err := api.makeRequestContext(ctx, http.MethodPost, uri, certificate)
if err != nil {
return AccessMutualTLSCertificate{}, 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.
//
// API reference: https://api.cloudflare.com/#access-mutual-tls-authentication-update-access-certificate
func (api *API) UpdateAccessMutualTLSCertificate(ctx context.Context, accountID, certificateID string, certificate AccessMutualTLSCertificate) (AccessMutualTLSCertificate, error) {
return api.updateAccessMutualTLSCertificate(ctx, accountID, certificateID, certificate, AccountRouteRoot)
}
// UpdateZoneAccessMutualTLSCertificate updates a zone level Access TLS Mutual
// certificate.
//
// API reference: https://api.cloudflare.com/#zone-level-access-mutual-tls-authentication-update-access-certificate
func (api *API) UpdateZoneAccessMutualTLSCertificate(ctx context.Context, zoneID, certificateID string, certificate AccessMutualTLSCertificate) (AccessMutualTLSCertificate, error) {
return api.updateAccessMutualTLSCertificate(ctx, zoneID, certificateID, certificate, ZoneRouteRoot)
}
func (api *API) updateAccessMutualTLSCertificate(ctx context.Context, id string, certificateID string, certificate AccessMutualTLSCertificate, routeRoot RouteRoot) (AccessMutualTLSCertificate, error) {
uri := fmt.Sprintf(
"/%s/%s/access/certificates/%s",
routeRoot,
id,
certificateID,
)
res, err := api.makeRequestContext(ctx, http.MethodPut, uri, certificate)
if err != nil {
return AccessMutualTLSCertificate{}, 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 account level Access Mutual
// TLS certificate.
//
// API reference: https://api.cloudflare.com/#access-mutual-tls-authentication-update-access-certificate
func (api *API) DeleteAccessMutualTLSCertificate(ctx context.Context, accountID, certificateID string) error {
return api.deleteAccessMutualTLSCertificate(ctx, accountID, certificateID, AccountRouteRoot)
}
// DeleteZoneAccessMutualTLSCertificate destroys a zone level Access Mutual TLS
// certificate.
//
// API reference: https://api.cloudflare.com/#zone-level-access-mutual-tls-authentication-update-access-certificate
func (api *API) DeleteZoneAccessMutualTLSCertificate(ctx context.Context, zoneID, certificateID string) error {
return api.deleteAccessMutualTLSCertificate(ctx, zoneID, certificateID, ZoneRouteRoot)
}
func (api *API) deleteAccessMutualTLSCertificate(ctx context.Context, id, certificateID string, routeRoot RouteRoot) error {
uri := fmt.Sprintf(
"/%s/%s/access/certificates/%s",
routeRoot,
id,
certificateID,
)
res, err := api.makeRequestContext(ctx, http.MethodDelete, uri, nil)
if err != nil {
return err
}
var accessMutualTLSCertificateDetailResponse AccessMutualTLSCertificateDetailResponse
err = json.Unmarshal(res, &accessMutualTLSCertificateDetailResponse)
if err != nil {
return fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return nil
}