-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
197 lines (159 loc) · 5.01 KB
/
main.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
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"strings"
whapi "github.com/cert-manager/cert-manager/pkg/acme/webhook/apis/acme/v1alpha1"
"github.com/cert-manager/cert-manager/pkg/acme/webhook/cmd"
"github.com/civo/civogo"
log "github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
restclient "k8s.io/client-go/rest"
)
func init() {
log.SetFormatter(&log.JSONFormatter{})
log.SetOutput(os.Stdout)
log.SetLevel(log.InfoLevel)
}
func main() {
ctx := context.Background()
region := os.Getenv("REGION")
if region == "" {
region = "NYC1"
}
groupName := os.Getenv("GROUP_NAME")
if groupName == "" {
panic("GROUP_NAME must be specified")
}
cmd.RunWebhookServer(groupName,
&civoDNSProviderSolver{ctx: ctx},
)
}
type civoDNSProviderSolver struct {
client *kubernetes.Clientset
ctx context.Context
region string
}
type civoDNSProviderConfig struct {
SecretNameRef string `json:"secretName"`
SecretKeyRef string `json:"secretKey"`
}
func (c *civoDNSProviderSolver) Initialize(kubeClientConfig *restclient.Config, stopCh <-chan struct{}) error {
cl, err := kubernetes.NewForConfig(kubeClientConfig)
if err != nil {
return err
}
c.client = cl
return nil
}
func (c *civoDNSProviderSolver) Present(ch *whapi.ChallengeRequest) error {
log.Infof("Presenting challenge for fqdn=%s zone=%s", ch.ResolvedFQDN, ch.ResolvedZone)
client, err := c.newClientFromConfig(ch)
if err != nil {
log.Errorf("failed to get client from ChallengeRequest: %s", err)
return err
}
rn := strings.TrimSuffix(ch.ResolvedFQDN, "."+ch.ResolvedZone)
d, err := client.GetDNSDomain(strings.TrimSuffix(ch.ResolvedZone, "."))
if err != nil {
log.Errorf("failed to get DNS domain '%s' from civo: %s", ch.ResolvedZone, err)
return err
}
r := &civogo.DNSRecordConfig{
Name: rn,
Value: ch.Key,
Type: civogo.DNSRecordTypeTXT,
Priority: 10,
TTL: 600}
log.Infof("creating DNS record %s/%s", d.ID, r.Name)
_, err = client.CreateDNSRecord(d.ID, r)
if err != nil {
log.Errorf("failed to create DNS Record '%s': %s", r.Name, err)
return err
}
log.Infof("Successfully created txt record for fqdn=%s zone=%s", ch.ResolvedFQDN, ch.ResolvedZone)
return nil
}
func (c *civoDNSProviderSolver) CleanUp(ch *whapi.ChallengeRequest) error {
log.Infof("Cleaning up entry for fqdn=%s", ch.ResolvedFQDN)
client, err := c.newClientFromConfig(ch)
if err != nil {
log.Errorf("failed to get client from ChallengeRequest: %s", err)
return fmt.Errorf("failed to get client from ChallengeRequest: %w", err)
}
rn := strings.TrimSuffix(ch.ResolvedFQDN, "."+ch.ResolvedZone)
r, err := getDNSRecord(client, rn, strings.TrimSuffix(ch.ResolvedZone, "."), ch.Key)
if err != nil {
return err
}
resp, err := client.DeleteDNSRecord(r)
if err != nil {
log.Errorf("failed to delete DNS record '%s': %s", r.Name, err)
return fmt.Errorf("failed to delete DNS record '%s': %s", r.Name, err)
}
if resp.Result == "success" {
return nil
}
log.Errorf("failed to delete DNS record '%s': %s", r.Name, resp)
return fmt.Errorf("failed to delete DNS record '%s': %s", r.Name, resp)
}
func (c *civoDNSProviderSolver) Name() string {
return "civo"
}
func (c *civoDNSProviderSolver) newClientFromConfig(ch *whapi.ChallengeRequest) (*civogo.Client, error) {
cfg, err := c.loadConfig(ch)
if err != nil {
return nil, err
}
apiKey, err := c.getSecretData(cfg.SecretNameRef, ch.ResourceNamespace, cfg.SecretKeyRef)
if err != nil {
return nil, err
}
return civogo.NewClient(apiKey, c.region)
}
func (c *civoDNSProviderSolver) loadConfig(ch *whapi.ChallengeRequest) (*civoDNSProviderConfig, error) {
cfg := &civoDNSProviderConfig{}
if ch.Config == nil {
return cfg, nil
}
if err := json.Unmarshal(ch.Config.Raw, cfg); err != nil {
return cfg, fmt.Errorf("error decoding solver config: %v", err)
}
return cfg, nil
}
func (c *civoDNSProviderSolver) getSecretData(secretName string, ns string, secretKey string) (string, error) {
secret, err := c.client.CoreV1().Secrets(ns).Get(c.ctx, secretName, metav1.GetOptions{})
if err != nil {
return "", fmt.Errorf("failed to load secret %s/%s: %w", ns, secretName, err)
}
if data, ok := secret.Data[secretKey]; ok {
return string(data), nil
}
return "", fmt.Errorf("no key %s in secret %s/%s", secretKey, ns, secretName)
}
func getDNSRecord(client *civogo.Client, rn, domain, key string) (*civogo.DNSRecord, error) {
log.Infof("getting domain %s from civo", domain)
d, err := client.GetDNSDomain(domain)
if err != nil {
log.Errorf("failed to get DNS host '%s' in domain '%s' from civo: %s", rn, domain, err)
return nil, err
}
log.Infof("getting DNS record %s/%s from civo", d.ID, rn)
rs, err := client.ListDNSRecords(d.ID)
if err != nil {
log.Errorf("failed to get DNS Records for '%s': %s", d.ID, err)
return nil, err
}
for _, r := range rs {
if r.Name == rn {
if r.Value == key {
return &r, nil
}
log.Infof("Records value for %s does not match %s", r.Name, key)
}
}
return nil, fmt.Errorf("record not found")
}