forked from vxcontrol/rancher-letsencrypt
-
Notifications
You must be signed in to change notification settings - Fork 1
/
certificate.go
95 lines (75 loc) · 2.47 KB
/
certificate.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
package rancher
import (
"fmt"
"github.com/Sirupsen/logrus"
rancherClient "github.com/rancher/go-rancher/v2"
)
// AddCertificate creates a new certificate resource using the given private key and PEM encoded certificate
func (r *Client) AddCertificate(name, descr string, privateKey, cert []byte) (*rancherClient.Certificate, error) {
certString := string(cert[:])
keyString := string(privateKey[:])
config := &rancherClient.Certificate{
Name: name,
Description: descr,
Cert: certString,
Key: keyString,
}
rancherCert, err := r.client.Certificate.Create(config)
if err != nil {
return nil, err
}
logrus.Debugf("Waiting for new certificate '%s' to become active", rancherCert.Name)
if err := r.WaitCertificate(rancherCert); err != nil {
return nil, err
}
return rancherCert, nil
}
// UpdateCertificate updates an existing certificate resource using the given PEM encoded certificate
func (r *Client) UpdateCertificate(certId, descr string, privateKey, cert []byte) error {
certString := string(cert[:])
keyString := string(privateKey[:])
rancherCert, err := r.client.Certificate.ById(certId)
if err != nil {
return err
}
rancherCert, err = r.client.Certificate.Update(rancherCert, &rancherClient.Certificate{
Description: descr,
Cert: certString,
Key: keyString,
})
if err != nil {
return err
}
logrus.Debugf("Waiting for updated certificate '%s' to become active", rancherCert.Name)
return r.WaitCertificate(rancherCert)
}
// FindCertByName retrieves an existing certificate
func (r *Client) FindCertByName(name string) (*rancherClient.Certificate, error) {
logrus.Debugf("Looking up Rancher certificate by name: %s", name)
certificates, err := r.client.Certificate.List(&rancherClient.ListOpts{
Filters: map[string]interface{}{
"name": name,
"removed_null": nil,
},
})
if err != nil {
return nil, err
}
if len(certificates.Data) == 0 {
return nil, nil
}
logrus.Debugf("Found existing Rancher certificate by name: %s", name)
return &certificates.Data[0], nil
}
// GetCertById retrieves an existing certificate by ID
func (r *Client) GetCertById(certId string) (*rancherClient.Certificate, error) {
rancherCert, err := r.client.Certificate.ById(certId)
if err != nil {
return nil, err
}
if rancherCert == nil {
return nil, fmt.Errorf("No such certificate with ID %s", certId)
}
logrus.Debugf("Got Rancher certificate %s by ID %s", rancherCert.Name, certId)
return rancherCert, nil
}