-
Notifications
You must be signed in to change notification settings - Fork 40
/
main.go
292 lines (231 loc) · 7.42 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
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
package main
import (
"bytes"
"context"
"errors"
"io"
"net/http"
"regexp"
"strings"
"encoding/json"
"fmt"
"os"
extapi "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/client-go/rest"
"github.com/cert-manager/cert-manager/pkg/acme/webhook/apis/acme/v1alpha1"
"github.com/cert-manager/cert-manager/pkg/acme/webhook/cmd"
"github.com/vadimkim/cert-manager-webhook-hetzner/internal"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/klog/v2"
)
var GroupName = os.Getenv("GROUP_NAME")
func main() {
if GroupName == "" {
panic("GROUP_NAME must be specified")
}
cmd.RunWebhookServer(GroupName,
&hetznerDNSProviderSolver{},
)
}
type hetznerDNSProviderSolver struct {
client *kubernetes.Clientset
}
type hetznerDNSProviderConfig struct {
SecretRef string `json:"secretName"`
ZoneName string `json:"zoneName"`
ApiUrl string `json:"apiUrl"`
}
func (c *hetznerDNSProviderSolver) Name() string {
return "hetzner"
}
func (c *hetznerDNSProviderSolver) Present(ch *v1alpha1.ChallengeRequest) error {
klog.V(6).Infof("call function Present: namespace=%s, zone=%s, fqdn=%s",
ch.ResourceNamespace, ch.ResolvedZone, ch.ResolvedFQDN)
config, err := clientConfig(c, ch)
if err != nil {
return fmt.Errorf("unable to get secret `%s`; %v", ch.ResourceNamespace, err)
}
addTxtRecord(config, ch)
klog.Infof("Presented txt record %v", ch.ResolvedFQDN)
return nil
}
func (c *hetznerDNSProviderSolver) CleanUp(ch *v1alpha1.ChallengeRequest) error {
config, err := clientConfig(c, ch)
if err != nil {
return fmt.Errorf("unable to get secret `%s`; %v", ch.ResourceNamespace, err)
}
zoneId, err := searchZoneId(config)
if err != nil {
return fmt.Errorf("unable to find id for zone name `%s`; %v", config.ZoneName, err)
}
var url = config.ApiUrl + "/records?zone_id=" + zoneId
// Get all DNS records
dnsRecords, err := callDnsApi(url, "GET", nil, config)
if err != nil {
return fmt.Errorf("unable to get DNS records %v", err)
}
// Unmarshall response
records := internal.RecordResponse{}
readErr := json.Unmarshal(dnsRecords, &records)
if readErr != nil {
return fmt.Errorf("unable to unmarshal response %v", readErr)
}
var recordId string
name := recordName(ch.ResolvedFQDN, config.ZoneName)
for i := len(records.Records) - 1; i >= 0; i-- {
if strings.EqualFold(records.Records[i].Name, name) {
recordId = records.Records[i].Id
break
}
}
// Delete TXT record
url = config.ApiUrl + "/records/" + recordId
del, err := callDnsApi(url, "DELETE", nil, config)
if err != nil {
klog.Error(err)
}
klog.Infof("Delete TXT record result: %s", string(del))
return nil
}
func (c *hetznerDNSProviderSolver) Initialize(kubeClientConfig *rest.Config, stopCh <-chan struct{}) error {
k8sClient, err := kubernetes.NewForConfig(kubeClientConfig)
klog.V(6).Infof("Input variable stopCh is %d length", len(stopCh))
if err != nil {
return err
}
c.client = k8sClient
return nil
}
func loadConfig(cfgJSON *extapi.JSON) (hetznerDNSProviderConfig, error) {
cfg := hetznerDNSProviderConfig{}
// 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 stringFromSecretData(secretData map[string][]byte, key string) (string, error) {
data, ok := secretData[key]
if !ok {
return "", fmt.Errorf("key %q not found in secret data", key)
}
return string(data), nil
}
func addTxtRecord(config internal.Config, ch *v1alpha1.ChallengeRequest) {
url := config.ApiUrl + "/records"
name := recordName(ch.ResolvedFQDN, config.ZoneName)
zoneId, err := searchZoneId(config)
if err != nil {
klog.Errorf("unable to find id for zone name `%s`; %v", config.ZoneName, err)
}
var jsonStr = fmt.Sprintf(`{"value":%q, "ttl":120, "type":"TXT", "name":%q, "zone_id":%q}`, ch.Key, name, zoneId)
add, err := callDnsApi(url, "POST", bytes.NewBuffer([]byte(jsonStr)), config)
if err != nil {
klog.Error(err)
}
klog.Infof("Added TXT record result: %s", string(add))
}
func clientConfig(c *hetznerDNSProviderSolver, ch *v1alpha1.ChallengeRequest) (internal.Config, error) {
var config internal.Config
cfg, err := loadConfig(ch.Config)
if err != nil {
return config, err
}
config.ZoneName = cfg.ZoneName
config.ApiUrl = cfg.ApiUrl
secretName := cfg.SecretRef
sec, err := c.client.CoreV1().Secrets(ch.ResourceNamespace).Get(context.TODO(), secretName, metav1.GetOptions{})
if err != nil {
return config, fmt.Errorf("unable to get secret `%s/%s`; %v", secretName, ch.ResourceNamespace, err)
}
apiKey, err := stringFromSecretData(sec.Data, "api-key")
config.ApiKey = apiKey
if err != nil {
return config, fmt.Errorf("unable to get api-key from secret `%s/%s`; %v", secretName, ch.ResourceNamespace, err)
}
// Get ZoneName by api search if not provided by config
if config.ZoneName == "" {
foundZone, err := searchZoneName(config, ch.ResolvedZone)
if err != nil {
return config, err
}
config.ZoneName = foundZone
}
return config, nil
}
/*
Domain name in Hetzner is divided in 2 parts: record + zone name. API works
with record name that is FQDN without zone name. Subdomains is a part of
record name and is separated by "."
*/
func recordName(fqdn, domain string) string {
r := regexp.MustCompile("(.+)\\." + domain + "\\.")
name := r.FindStringSubmatch(fqdn)
if len(name) != 2 {
klog.Errorf("splitting domain name %s failed!", fqdn)
return ""
}
return name[1]
}
func callDnsApi(url, method string, body io.Reader, config internal.Config) ([]byte, error) {
ctx := context.Background()
req, err := http.NewRequestWithContext(ctx, method, url, body)
if err != nil {
return []byte{}, fmt.Errorf("unable to execute request %v", err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Auth-API-Token", config.ApiKey)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer func() {
err := resp.Body.Close()
if err != nil {
klog.Fatal(err)
}
}()
respBody, _ := io.ReadAll(resp.Body)
if resp.StatusCode == http.StatusOK {
return respBody, nil
}
text := "Error calling API status:" + resp.Status + " url: " + url + " method: " + method
klog.Error(text)
return nil, errors.New(text)
}
func searchZoneId(config internal.Config) (string, error) {
url := config.ApiUrl + "/zones?name=" + config.ZoneName
// Get Zone configuration
zoneRecords, err := callDnsApi(url, "GET", nil, config)
if err != nil {
return "", fmt.Errorf("unable to get zone info %v", err)
}
// Unmarshall response
zones := internal.ZoneResponse{}
readErr := json.Unmarshal(zoneRecords, &zones)
if readErr != nil {
return "", fmt.Errorf("unable to unmarshal response %v", readErr)
}
if zones.Meta.Pagination.TotalEntries != 1 {
return "", fmt.Errorf("wrong number of zones in response %d must be exactly = 1", zones.Meta.Pagination.TotalEntries)
}
return zones.Zones[0].Id, nil
}
func searchZoneName(config internal.Config, searchZone string) (string, error) {
parts := strings.Split(searchZone, ".")
parts = parts[:len(parts)-1]
for i := 0; i <= len(parts)-2; i++ {
config.ZoneName = strings.Join(parts[i:], ".")
zoneId, _ := searchZoneId(config)
if zoneId != "" {
klog.Infof("Found ID with ZoneName: %s", config.ZoneName)
return config.ZoneName, nil
}
}
return "", fmt.Errorf("unable to find hetzner dns zone with: %s", searchZone)
}