-
Notifications
You must be signed in to change notification settings - Fork 615
/
Copy pathorigin_ca.go
233 lines (184 loc) · 6.41 KB
/
origin_ca.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
package cloudflare
import (
"context"
"errors"
"fmt"
"io"
"net/http"
"time"
"github.com/goccy/go-json"
)
// OriginCACertificate represents a Cloudflare-issued certificate.
//
// API reference: https://api.cloudflare.com/#cloudflare-ca
type OriginCACertificate struct {
ID string `json:"id"`
Certificate string `json:"certificate"`
Hostnames []string `json:"hostnames"`
ExpiresOn time.Time `json:"expires_on"`
RequestType string `json:"request_type"`
RequestValidity int `json:"requested_validity"`
RevokedAt time.Time `json:"revoked_at,omitempty"`
CSR string `json:"csr"`
}
type CreateOriginCertificateParams struct {
ID string `json:"id"`
Certificate string `json:"certificate"`
Hostnames []string `json:"hostnames"`
ExpiresOn time.Time `json:"expires_on"`
RequestType string `json:"request_type"`
RequestValidity int `json:"requested_validity"`
RevokedAt time.Time `json:"revoked_at,omitempty"`
CSR string `json:"csr"`
}
// UnmarshalJSON handles custom parsing from an API response to an OriginCACertificate
// http://choly.ca/post/go-json-marshalling/
func (c *OriginCACertificate) UnmarshalJSON(data []byte) error {
type Alias OriginCACertificate
aux := &struct {
ExpiresOn string `json:"expires_on"`
*Alias
}{
Alias: (*Alias)(c),
}
var err error
if err = json.Unmarshal(data, &aux); err != nil {
return err
}
// This format comes from time.Time.String() source
c.ExpiresOn, err = time.Parse("2006-01-02 15:04:05.999999999 -0700 MST", aux.ExpiresOn)
if err != nil {
c.ExpiresOn, err = time.Parse(time.RFC3339, aux.ExpiresOn)
}
if err != nil {
return err
}
return nil
}
// ListOriginCertificatesParams represents the parameters used to list
// Cloudflare-issued certificates.
type ListOriginCertificatesParams struct {
ZoneID string `url:"zone_id,omitempty"`
}
// OriginCACertificateID represents the ID of the revoked certificate from the Revoke Certificate endpoint.
type OriginCACertificateID struct {
ID string `json:"id"`
}
// originCACertificateResponse represents the response from the Create Certificate and the Certificate Details endpoints.
type originCACertificateResponse struct {
Response
Result OriginCACertificate `json:"result"`
}
// originCACertificateResponseList represents the response from the List Certificates endpoint.
type originCACertificateResponseList struct {
Response
Result []OriginCACertificate `json:"result"`
ResultInfo ResultInfo `json:"result_info"`
}
// originCACertificateResponseRevoke represents the response from the Revoke Certificate endpoint.
type originCACertificateResponseRevoke struct {
Response
Result OriginCACertificateID `json:"result"`
}
// CreateOriginCACertificate creates a Cloudflare-signed certificate.
//
// API reference: https://api.cloudflare.com/#cloudflare-ca-create-certificate
func (api *API) CreateOriginCACertificate(ctx context.Context, params CreateOriginCertificateParams) (*OriginCACertificate, error) {
res, err := api.makeRequestContext(ctx, http.MethodPost, "/certificates", params)
if err != nil {
return &OriginCACertificate{}, err
}
var originResponse *originCACertificateResponse
err = json.Unmarshal(res, &originResponse)
if err != nil {
return nil, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
if !originResponse.Success {
return nil, errors.New(errRequestNotSuccessful)
}
return &originResponse.Result, nil
}
// ListOriginCACertificates lists all Cloudflare-issued certificates.
//
// API reference: https://api.cloudflare.com/#cloudflare-ca-list-certificates
func (api *API) ListOriginCACertificates(ctx context.Context, params ListOriginCertificatesParams) ([]OriginCACertificate, error) {
uri := buildURI("/certificates", params)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, err
}
var originResponse *originCACertificateResponseList
err = json.Unmarshal(res, &originResponse)
if err != nil {
return nil, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
if !originResponse.Success {
return nil, errors.New(errRequestNotSuccessful)
}
return originResponse.Result, nil
}
// GetOriginCACertificate returns the details for a Cloudflare-issued
// certificate.
//
// API reference: https://api.cloudflare.com/#cloudflare-ca-certificate-details
func (api *API) GetOriginCACertificate(ctx context.Context, certificateID string) (*OriginCACertificate, error) {
uri := fmt.Sprintf("/certificates/%s", certificateID)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, err
}
var originResponse *originCACertificateResponse
err = json.Unmarshal(res, &originResponse)
if err != nil {
return nil, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
if !originResponse.Success {
return nil, errors.New(errRequestNotSuccessful)
}
return &originResponse.Result, nil
}
// RevokeOriginCACertificate revokes a created certificate for a zone.
//
// API reference: https://api.cloudflare.com/#cloudflare-ca-revoke-certificate
func (api *API) RevokeOriginCACertificate(ctx context.Context, certificateID string) (*OriginCACertificateID, error) {
uri := fmt.Sprintf("/certificates/%s", certificateID)
res, err := api.makeRequestContext(ctx, http.MethodDelete, uri, nil)
if err != nil {
return nil, err
}
var originResponse *originCACertificateResponseRevoke
err = json.Unmarshal(res, &originResponse)
if err != nil {
return nil, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
if !originResponse.Success {
return nil, errors.New(errRequestNotSuccessful)
}
return &originResponse.Result, nil
}
// Gets the Cloudflare Origin CA Root Certificate for a given algorithm in PEM format.
// Algorithm must be one of ['ecc', 'rsa'].
func GetOriginCARootCertificate(algorithm string) ([]byte, error) {
var url string
switch algorithm {
case "ecc":
url = originCARootCertEccURL
case "rsa":
url = originCARootCertRsaURL
default:
return nil, fmt.Errorf("invalid algorithm: must be one of ['ecc', 'rsa']")
}
resp, err := http.Get(url) //nolint:gosec
if err != nil {
return nil, fmt.Errorf("HTTP request failed: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, errors.New(errRequestNotSuccessful)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("Response body could not be read: %w", err)
}
return body, nil
}