forked from cert-manager/webhook-example
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
174 lines (139 loc) · 4.6 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
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/cert-manager/cert-manager/pkg/acme/webhook/cmd"
v1 "github.com/cert-manager/cert-manager/pkg/apis/meta/v1"
"github.com/cert-manager/cert-manager/pkg/issuer/acme/dns/util"
"github.com/nrdcg/desec"
"k8s.io/client-go/kubernetes"
"k8s.io/klog/v2"
"os"
"strings"
extapi "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/rest"
"github.com/cert-manager/cert-manager/pkg/acme/webhook/apis/acme/v1alpha1"
)
var GroupName = os.Getenv("GROUP_NAME")
func main() {
if GroupName == "" {
panic("GROUP_NAME must be specified")
}
cmd.RunWebhookServer(GroupName,
&desecDNSProviderSolver{},
)
}
type desecDNSProviderSolver struct {
client *kubernetes.Clientset
}
type desecDNSProviderConfig struct {
APIKeySecretRef v1.SecretKeySelector `json:"apiKeySecretRef"`
}
func (c *desecDNSProviderSolver) Name() string {
return "desec"
}
func (c *desecDNSProviderSolver) Present(ch *v1alpha1.ChallengeRequest) error {
klog.V(1).Infof("preset record '%s'", ch.ResolvedFQDN)
cfg, err := loadConfig(ch.Config)
klog.V(5).Info("retrieving secret")
apiToken, err := c.getSecretKey(cfg.APIKeySecretRef, ch.ResourceNamespace)
klog.V(5).Info("creating desec client")
api := c.getClient(apiToken)
klog.V(1).Infof("retrieving domain %s", util.UnFqdn(ch.ResolvedZone))
domain, subName, err := c.getRecordInfo(api, ch)
if err != nil {
return err
}
recordSet := new(desec.RRSet)
recordSet.Domain = domain.Name
recordSet.SubName = subName
recordSet.Records = append(recordSet.Records, "\""+ch.Key+"\"")
recordSet.Type = "TXT"
recordSet.TTL = 3600
klog.V(5).Info(recordSet)
record, err := api.Records.Create(context.Background(), *recordSet)
if err != nil {
klog.Fatal(err)
return err
}
klog.V(5).Infof("Record %s", record)
return nil
}
func (c *desecDNSProviderSolver) CleanUp(ch *v1alpha1.ChallengeRequest) error {
klog.V(1).Infof("cleanup record '%s'", ch.ResolvedFQDN)
cfg, err := loadConfig(ch.Config)
apiToken, err := c.getSecretKey(cfg.APIKeySecretRef, ch.ResourceNamespace)
api := c.getClient(apiToken)
domain, subName, err := c.getRecordInfo(api, ch)
if err != nil {
return err
}
recError := api.Records.Delete(context.Background(), domain.Name, subName, "TXT")
if recError != nil {
return recError
}
klog.V(1).Infof("Record %s in zone %s deleted", subName, domain.Name)
return nil
}
func (c *desecDNSProviderSolver) Initialize(kubeClientConfig *rest.Config, stopCh <-chan struct{}) error {
cl, err := kubernetes.NewForConfig(kubeClientConfig)
if err != nil {
return err
}
c.client = cl
return nil
}
func loadConfig(cfgJSON *extapi.JSON) (desecDNSProviderConfig, error) {
cfg := desecDNSProviderConfig{}
// handle the 'base case' where no configuration has been provided
if cfgJSON == nil {
return cfg, nil
}
if err := json.Unmarshal(cfgJSON.Raw, &cfg); err != nil {
return cfg, fmt.Errorf("error decoding solver config: %v", err)
}
return cfg, nil
}
func (c *desecDNSProviderSolver) getDomain(client desec.Client, subname string) (*desec.Domain, error) {
domains, err := client.Domains.GetAll(context.Background())
if err != nil {
panic(err)
}
for _, v := range domains {
if strings.HasSuffix(subname, v.Name) {
return &v, nil
}
}
return nil, fmt.Errorf("domain not found")
}
func (c *desecDNSProviderSolver) getRecordInfo(api desec.Client, ch *v1alpha1.ChallengeRequest) (*desec.Domain, string, error) {
klog.V(5).Infof("%s record", ch.ResolvedFQDN)
// Remove trailing dots from zone and fqdn
zone := util.UnFqdn(ch.ResolvedZone)
fqdn := util.UnFqdn(ch.ResolvedFQDN)
domain, err := c.getDomain(api, zone)
if err != nil {
return nil, "", err
}
// Get the subdomain portion of fqdn
subName := fqdn[:len(fqdn)-len(domain.Name)-1]
return domain, subName, nil
}
func (c *desecDNSProviderSolver) getClient(apiToken string) desec.Client {
return *desec.New(apiToken, desec.NewDefaultClientOptions())
}
// getSecretKey fetch a secret key based on a selector and a namespace
func (c *desecDNSProviderSolver) getSecretKey(secret v1.SecretKeySelector, namespace string) (string, error) {
klog.V(5).Infof("retrieving key `%s` in secret `%s/%s`", secret.Key, namespace, secret.Name)
sec, err := c.client.CoreV1().Secrets(namespace).Get(context.Background(), secret.Name, metav1.GetOptions{})
if err != nil {
return "", fmt.Errorf("secret `%s/%s` not found", namespace, secret.Name)
}
data, ok := sec.Data[secret.Key]
if !ok {
return "", fmt.Errorf("key `%q` not found in secret `%s/%s`", secret.Key, namespace, secret.Name)
}
return string(data), nil
}