forked from cloudflare/cloudflare-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
certificate_packs.go
161 lines (140 loc) · 6.41 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
package cloudflare
import (
"context"
"encoding/json"
"fmt"
"net/http"
"time"
)
// 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 string `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 string `json:"primary_certificate"`
ValidationRecords []SSLValidationRecord `json:"validation_records,omitempty"`
ValidationErrors []SSLValidationError `json:"validation_errors,omitempty"`
ValidationMethod string `json:"validation_method"`
ValidityDays int `json:"validity_days"`
CertificateAuthority string `json:"certificate_authority"`
CloudflareBranding bool `json:"cloudflare_branding"`
}
// CertificatePackRequest is used for requesting a new certificate.
type CertificatePackRequest struct {
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"`
}
// 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(ctx context.Context, zoneID string) ([]CertificatePack, error) {
uri := fmt.Sprintf("/zones/%s/ssl/certificate_packs?status=all", zoneID)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return []CertificatePack{}, err
}
var certificatePacksResponse CertificatePacksResponse
err = json.Unmarshal(res, &certificatePacksResponse)
if err != nil {
return []CertificatePack{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
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(ctx context.Context, zoneID, certificatePackID string) (CertificatePack, error) {
uri := fmt.Sprintf("/zones/%s/ssl/certificate_packs/%s", zoneID, certificatePackID)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return CertificatePack{}, err
}
var certificatePacksDetailResponse CertificatePacksDetailResponse
err = json.Unmarshal(res, &certificatePacksDetailResponse)
if err != nil {
return CertificatePack{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return certificatePacksDetailResponse.Result, nil
}
// CreateCertificatePack creates a new certificate pack associated with a zone.
//
// API Reference: https://api.cloudflare.com/#certificate-packs-order-advanced-certificate-manager-certificate-pack
func (api *API) CreateCertificatePack(ctx context.Context, zoneID string, cert CertificatePackRequest) (CertificatePack, error) {
uri := fmt.Sprintf("/zones/%s/ssl/certificate_packs/order", zoneID)
res, err := api.makeRequestContext(ctx, http.MethodPost, uri, cert)
if err != nil {
return CertificatePack{}, err
}
var certificatePacksDetailResponse CertificatePacksDetailResponse
err = json.Unmarshal(res, &certificatePacksDetailResponse)
if err != nil {
return CertificatePack{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
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(ctx context.Context, zoneID, certificateID string) error {
uri := fmt.Sprintf("/zones/%s/ssl/certificate_packs/%s", zoneID, certificateID)
_, err := api.makeRequestContext(ctx, http.MethodDelete, uri, nil)
if err != nil {
return err
}
return nil
}
// RestartCertificateValidation 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) RestartCertificateValidation(ctx context.Context, zoneID, certificateID string) (CertificatePack, error) {
uri := fmt.Sprintf("/zones/%s/ssl/certificate_packs/%s", zoneID, certificateID)
res, err := api.makeRequestContext(ctx, http.MethodPatch, uri, nil)
if err != nil {
return CertificatePack{}, err
}
var certificatePackResponse CertificatePacksDetailResponse
err = json.Unmarshal(res, &certificatePackResponse)
if err != nil {
return CertificatePack{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return certificatePackResponse.Result, nil
}