forked from cloudflare/cloudflare-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
certificate_packs.go
190 lines (163 loc) · 7.33 KB
/
certificate_packs.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
package cloudflare
import (
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/pkg/errors"
)
// CertificatePackGeoRestrictions is for the structure of the geographic
// restrictions for a TLS certificate.
type CertificatePackGeoRestrictions struct {
Label string `json:"label"`
}
// CertificatePackCertificate is the base structure of a TLS certificate that is
// contained within a certificate pack.
type CertificatePackCertificate struct {
ID int `json:"id"`
Hosts []string `json:"hosts"`
Issuer string `json:"issuer"`
Signature string `json:"signature"`
Status string `json:"status"`
BundleMethod string `json:"bundle_method"`
GeoRestrictions CertificatePackGeoRestrictions `json:"geo_restrictions"`
ZoneID string `json:"zone_id"`
UploadedOn time.Time `json:"uploaded_on"`
ModifiedOn time.Time `json:"modified_on"`
ExpiresOn time.Time `json:"expires_on"`
Priority int `json:"priority"`
}
// CertificatePack is the overarching structure of a certificate pack response.
type CertificatePack struct {
ID string `json:"id"`
Type string `json:"type"`
Hosts []string `json:"hosts"`
Certificates []CertificatePackCertificate `json:"certificates"`
PrimaryCertificate int `json:"primary_certificate"`
}
// CertificatePackRequest is used for requesting a new certificate.
type CertificatePackRequest struct {
Type string `json:"type"`
Hosts []string `json:"hosts"`
}
// CertificatePackAdvancedCertificate is the structure of the advanced
// certificate pack certificate.
type CertificatePackAdvancedCertificate struct {
ID string `json:"id"`
Type string `json:"type"`
Hosts []string `json:"hosts"`
ValidationMethod string `json:"validation_method"`
ValidityDays int `json:"validity_days"`
CertificateAuthority string `json:"certificate_authority"`
CloudflareBranding bool `json:"cloudflare_branding"`
}
// CertificatePacksResponse is for responses where multiple certificates are
// expected.
type CertificatePacksResponse struct {
Response
Result []CertificatePack `json:"result"`
}
// CertificatePacksDetailResponse contains a single certificate pack in the
// response.
type CertificatePacksDetailResponse struct {
Response
Result CertificatePack `json:"result"`
}
// CertificatePacksAdvancedDetailResponse contains a single advanced certificate
// pack in the response.
type CertificatePacksAdvancedDetailResponse struct {
Response
Result CertificatePackAdvancedCertificate `json:"result"`
}
// ListCertificatePacks returns all available TLS certificate packs for a zone.
//
// API Reference: https://api.cloudflare.com/#certificate-packs-list-certificate-packs
func (api *API) ListCertificatePacks(zoneID string) ([]CertificatePack, error) {
uri := fmt.Sprintf("/zones/%s/ssl/certificate_packs?status=all", zoneID)
res, err := api.makeRequest(http.MethodGet, uri, nil)
if err != nil {
return []CertificatePack{}, errors.Wrap(err, errMakeRequestError)
}
var certificatePacksResponse CertificatePacksResponse
err = json.Unmarshal(res, &certificatePacksResponse)
if err != nil {
return []CertificatePack{}, errors.Wrap(err, errUnmarshalError)
}
return certificatePacksResponse.Result, nil
}
// CertificatePack returns a single TLS certificate pack on a zone.
//
// API Reference: https://api.cloudflare.com/#certificate-packs-get-certificate-pack
func (api *API) CertificatePack(zoneID, certificatePackID string) (CertificatePack, error) {
uri := fmt.Sprintf("/zones/%s/ssl/certificate_packs/%s", zoneID, certificatePackID)
res, err := api.makeRequest(http.MethodGet, uri, nil)
if err != nil {
return CertificatePack{}, errors.Wrap(err, errMakeRequestError)
}
var certificatePacksDetailResponse CertificatePacksDetailResponse
err = json.Unmarshal(res, &certificatePacksDetailResponse)
if err != nil {
return CertificatePack{}, errors.Wrap(err, errUnmarshalError)
}
return certificatePacksDetailResponse.Result, nil
}
// CreateCertificatePack creates a new certificate pack associated with a zone.
//
// API Reference: https://api.cloudflare.com/#certificate-packs-order-certificate-pack
func (api *API) CreateCertificatePack(zoneID string, cert CertificatePackRequest) (CertificatePack, error) {
uri := fmt.Sprintf("/zones/%s/ssl/certificate_packs", zoneID)
res, err := api.makeRequest(http.MethodPost, uri, cert)
if err != nil {
return CertificatePack{}, errors.Wrap(err, errMakeRequestError)
}
var certificatePacksDetailResponse CertificatePacksDetailResponse
err = json.Unmarshal(res, &certificatePacksDetailResponse)
if err != nil {
return CertificatePack{}, errors.Wrap(err, errUnmarshalError)
}
return certificatePacksDetailResponse.Result, nil
}
// DeleteCertificatePack removes a certificate pack associated with a zone.
//
// API Reference: https://api.cloudflare.com/#certificate-packs-delete-advanced-certificate-manager-certificate-pack
func (api *API) DeleteCertificatePack(zoneID, certificateID string) error {
uri := fmt.Sprintf("/zones/%s/ssl/certificate_packs/%s", zoneID, certificateID)
_, err := api.makeRequest(http.MethodDelete, uri, nil)
if err != nil {
return errors.Wrap(err, errMakeRequestError)
}
return nil
}
// CreateAdvancedCertificatePack creates a new certificate pack associated with a zone.
//
// API Reference: https://api.cloudflare.com/#certificate-packs-order-certificate-pack
func (api *API) CreateAdvancedCertificatePack(zoneID string, cert CertificatePackAdvancedCertificate) (CertificatePackAdvancedCertificate, error) {
uri := fmt.Sprintf("/zones/%s/ssl/certificate_packs/order", zoneID)
res, err := api.makeRequest(http.MethodPost, uri, cert)
if err != nil {
return CertificatePackAdvancedCertificate{}, errors.Wrap(err, errMakeRequestError)
}
var advancedCertificatePacksDetailResponse CertificatePacksAdvancedDetailResponse
err = json.Unmarshal(res, &advancedCertificatePacksDetailResponse)
if err != nil {
return CertificatePackAdvancedCertificate{}, errors.Wrap(err, errUnmarshalError)
}
return advancedCertificatePacksDetailResponse.Result, nil
}
// RestartAdvancedCertificateValidation kicks off the validation process for a
// pending certificate pack.
//
// API Reference: https://api.cloudflare.com/#certificate-packs-restart-validation-for-advanced-certificate-manager-certificate-pack
func (api *API) RestartAdvancedCertificateValidation(zoneID, certificateID string) (CertificatePackAdvancedCertificate, error) {
uri := fmt.Sprintf("/zones/%s/ssl/certificate_packs/%s", zoneID, certificateID)
res, err := api.makeRequest(http.MethodPatch, uri, nil)
if err != nil {
return CertificatePackAdvancedCertificate{}, errors.Wrap(err, errMakeRequestError)
}
var advancedCertificatePacksDetailResponse CertificatePacksAdvancedDetailResponse
err = json.Unmarshal(res, &advancedCertificatePacksDetailResponse)
if err != nil {
return CertificatePackAdvancedCertificate{}, errors.Wrap(err, errUnmarshalError)
}
return advancedCertificatePacksDetailResponse.Result, nil
}