forked from PalmStoneGames/kube-cert-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
k8s.go
319 lines (269 loc) · 8.42 KB
/
k8s.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
// Copyright 2016 Google Inc. All Rights Reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"bytes"
"crypto"
"encoding/json"
"fmt"
"log"
"net/http"
"time"
"encoding/pem"
"crypto/x509"
"github.com/pkg/errors"
"github.com/xenolf/lego/acme"
)
const (
apiHost = "http://127.0.0.1:8001"
certificatesEndpoint = "/apis/stable.k8s.psg.io/v1/namespaces/default/certificates"
certificatesWatchEndpoint = "/apis/stable.k8s.psg.io/v1/namespaces/default/certificates?watch=true"
secretsEndpoint = "/api/v1/namespaces/default/secrets"
)
type CertificateEvent struct {
Type string `json:"type"`
Object Certificate `json:"object"`
}
type Certificate struct {
ApiVersion string `json:"apiVersion"`
Kind string `json:"kind"`
Metadata map[string]string `json:"metadata"`
Spec CertificateSpec `json:"spec"`
}
type CertificateSpec struct {
Domain string `json:"domain"`
Provider string `json:"provider"`
Email string `json:"email"`
}
type CertificateList struct {
ApiVersion string `json:"apiVersion"`
Kind string `json:"kind"`
Metadata map[string]string `json:"metadata"`
Items []Certificate `json:"items"`
}
type Secret struct {
Kind string `json:"kind"`
ApiVersion string `json:"apiVersion"`
Metadata map[string]interface{} `json:"metadata"`
Data map[string][]byte `json:"data"`
Type string `json:"type"`
}
type ACMECertData struct {
DomainName string
Cert []byte
PrivateKey []byte
}
type ACMEUserData struct {
Email string `json:"email"`
Registration *acme.RegistrationResource `json:"registration"`
Key []byte `json:"key"`
}
type ACMECertDetails struct {
Domain string `json:"domain"`
CertURL string `json:"certUrl"`
CertStableURL string `json:"certStableUrl"`
AccountRef string `json:"accountRef,omitempty"`
}
func (u *ACMEUserData) GetEmail() string {
return u.Email
}
func (u *ACMEUserData) GetRegistration() *acme.RegistrationResource {
return u.Registration
}
func (u *ACMEUserData) GetPrivateKey() crypto.PrivateKey {
pemBlock, _ := pem.Decode(u.Key)
if pemBlock.Type != "RSA PRIVATE KEY" {
log.Printf("Invalid PEM user key: Expected RSA PRIVATE KEY, got %v", pemBlock.Type)
}
privateKey, err := x509.ParsePKCS1PrivateKey(pemBlock.Bytes)
if err != nil {
log.Printf("Error while parsing private key: %v", err)
}
return privateKey
}
func (c *ACMECertData) ToSecret(prefix string) (*Secret, error) {
metadata := make(map[string]interface{})
metadata["name"] = prefix + c.DomainName
metadata["labels"] = map[string]string{"domain": c.DomainName}
data := make(map[string][]byte)
data["tls.crt"] = c.Cert
data["tls.key"] = c.PrivateKey
return &Secret{
ApiVersion: "v1",
Data: data,
Kind: "Secret",
Metadata: metadata,
Type: "kubernetes.io/tls",
}, nil
}
func NewACMECertDataFromSecret(s *Secret) (ACMECertData, error) {
var acmeCertData ACMECertData
var ok bool
labels, ok := s.Metadata["labels"].(map[string]interface{})
if !ok {
return acmeCertData, errors.Errorf("Could not cast labels, expected map[string]interface{}, got %T", s.Metadata["labels"])
}
acmeCertData.DomainName, ok = labels["domain"].(string)
if !ok {
return acmeCertData, errors.Errorf("Could not find metadata domain in secret %v", s.Metadata["name"])
}
acmeCertData.Cert, ok = s.Data["tls.crt"]
if !ok {
return acmeCertData, errors.Errorf("Could not find key tls.crt in secret %v", s.Metadata["name"])
}
acmeCertData.PrivateKey, ok = s.Data["tls.key"]
if !ok {
return acmeCertData, errors.Errorf("Could not find key tls.key in secret %v", s.Metadata["name"])
}
return acmeCertData, nil
}
func NewACMECertDetailsFromResource(certRes acme.CertificateResource) ACMECertDetails {
return ACMECertDetails{
Domain: certRes.Domain,
CertURL: certRes.CertURL,
CertStableURL: certRes.CertStableURL,
AccountRef: certRes.AccountRef,
}
}
func (certDetails *ACMECertDetails) ToCertResource() acme.CertificateResource {
return acme.CertificateResource{
Domain: certDetails.Domain,
CertURL: certDetails.CertURL,
CertStableURL: certDetails.CertStableURL,
AccountRef: certDetails.AccountRef,
}
}
func getSecret(key string) (*Secret, error) {
// Run the http request
url := apiHost + secretsEndpoint + "/" + key
resp, err := http.Get(url)
if err != nil {
return nil, errors.Wrapf(err, "Error while running http request on url: %v", url)
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusNotFound {
return nil, nil
} else if resp.StatusCode != http.StatusOK {
return nil, errors.Errorf("Unexpected http status response while fetching secret on url %q: %v", url, resp.Status)
}
// Deserialize the secret
var secret Secret
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&secret)
if err != nil {
return nil, errors.Wrap(err, "Error while deserializing secret")
}
return &secret, nil
}
func saveSecret(secret *Secret, isUpdate bool) error {
if secret.Metadata["name"] == "" {
return errors.New("Secret name must be specified in metadata")
}
// Serialize the secret
buffer := new(bytes.Buffer)
err := json.NewEncoder(buffer).Encode(secret)
if err != nil {
return errors.Wrapf(err, "Error while encoding secret: %v", err)
}
// Determine http method and url
var url string
var method string
if isUpdate {
url = apiHost + secretsEndpoint + "/" + secret.Metadata["name"].(string)
method = "PUT"
} else {
url = apiHost + secretsEndpoint
method = "POST"
}
req, err := http.NewRequest(method, url, buffer)
if err != nil {
return errors.Wrapf(err, "Error while creating http request on url: %v", url)
}
req.Header.Add("Content-Type", "application/json")
// Actually do the request
resp, err := http.DefaultClient.Do(req)
if err != nil {
return errors.Wrapf(err, "Error while running http request on url: %v", url)
}
defer resp.Body.Close()
if isUpdate && resp.StatusCode != 200 {
return errors.Errorf("Non OK status while updating secret: %v", resp.Status)
} else if !isUpdate && resp.StatusCode != 201 {
return errors.Errorf("Non Created status while creating secret: %v", resp.Status)
}
return nil
}
func deleteSecret(key string) error {
// Create DELETE request
url := apiHost + secretsEndpoint + "/" + key
req, err := http.NewRequest("DELETE", url, nil)
if err != nil {
return errors.Wrapf(err, "Error while creating http request for url: %v", url)
}
// Actually do the request
resp, err := http.DefaultClient.Do(req)
if err != nil {
return errors.Wrapf(err, "Error while running http request on url: %v", url)
}
if resp.StatusCode != 200 {
return fmt.Errorf("Deleting %s secret failed: %s", key, resp.Status)
}
return nil
}
func getCertificates() ([]Certificate, error) {
var resp *http.Response
var err error
for {
resp, err = http.Get(apiHost + certificatesEndpoint)
if err != nil {
log.Printf("Error while retrieving certificate: %v. Retrying in 5 seconds", err)
time.Sleep(5 * time.Second)
continue
}
break
}
var certList CertificateList
err = json.NewDecoder(resp.Body).Decode(&certList)
if err != nil {
return nil, err
}
return certList.Items, nil
}
func monitorCertificateEvents() (<-chan CertificateEvent, <-chan error) {
events := make(chan CertificateEvent)
errc := make(chan error, 1)
go func() {
for {
resp, err := http.Get(apiHost + certificatesWatchEndpoint)
if err != nil {
errc <- err
time.Sleep(5 * time.Second)
continue
}
if resp.StatusCode != 200 {
errc <- errors.New("Invalid status code: " + resp.Status)
time.Sleep(5 * time.Second)
continue
}
decoder := json.NewDecoder(resp.Body)
for {
var event CertificateEvent
err = decoder.Decode(&event)
if err != nil {
errc <- err
break
}
events <- event
}
}
}()
return events, errc
}