-
Notifications
You must be signed in to change notification settings - Fork 21
/
main.go
148 lines (116 loc) · 3.44 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
package main
import (
"fmt"
"strconv"
"strings"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack/dns/v2/recordsets"
"github.com/gophercloud/gophercloud/openstack/dns/v2/zones"
log "github.com/sirupsen/logrus"
"k8s.io/client-go/rest"
"github.com/cert-manager/cert-manager/pkg/acme/webhook"
"github.com/cert-manager/cert-manager/pkg/acme/webhook/apis/acme/v1alpha1"
"github.com/cert-manager/cert-manager/pkg/acme/webhook/cmd"
)
const GroupName = "acme.syseleven.de"
func main() {
cmd.RunWebhookServer(GroupName,
&designateDNSProviderSolver{},
)
}
type designateDNSProviderSolver struct {
client *gophercloud.ServiceClient
}
func New() webhook.Solver {
client, err := createDesignateServiceClient()
if err != nil {
panic(fmt.Errorf("%v", err))
}
return &designateDNSProviderSolver{
client: client,
}
}
func (c *designateDNSProviderSolver) Name() string {
return "designateDNS"
}
func (c *designateDNSProviderSolver) Present(ch *v1alpha1.ChallengeRequest) error {
log.Debugf("Present() called ch.DNSName=%s ch.ResolvedZone=%s ch.ResolvedFQDN=%s ch.Type=%s", ch.DNSName, ch.ResolvedZone, ch.ResolvedFQDN, ch.Type)
listOpts := zones.ListOpts{
Name: ch.ResolvedZone,
}
allPages, err := zones.List(c.client, listOpts).AllPages()
if err != nil {
return err
}
allZones, err := zones.ExtractZones(allPages)
if err != nil {
return err
}
if len(allZones) != 1 {
return fmt.Errorf("Present: Expected to find 1 zone %s, found %v", ch.ResolvedZone, len(allZones))
}
var opts recordsets.CreateOpts
opts.Name = ch.ResolvedFQDN
opts.Type = "TXT"
opts.Records = []string{quoteRecord(ch.Key)}
_, err = recordsets.Create(c.client, allZones[0].ID, opts).Extract()
if err != nil {
return err
}
return nil
}
func (c *designateDNSProviderSolver) CleanUp(ch *v1alpha1.ChallengeRequest) error {
log.Debugf("CleanUp called ch.ResolvedZone=%s ch.ResolvedFQDN=%s", ch.ResolvedZone, ch.ResolvedFQDN)
listOpts := zones.ListOpts{
Name: ch.ResolvedZone,
}
allPages, err := zones.List(c.client, listOpts).AllPages()
if err != nil {
return err
}
allZones, err := zones.ExtractZones(allPages)
if err != nil {
return err
}
if len(allZones) != 1 {
return fmt.Errorf("CleanUp: Expected to find 1 zone %s, found %v", ch.ResolvedZone, len(allZones))
}
recordListOpts := recordsets.ListOpts{
Name: ch.ResolvedFQDN,
Type: "TXT",
Data: quoteRecord(ch.Key),
}
allRecordPages, err := recordsets.ListByZone(c.client, allZones[0].ID, recordListOpts).AllPages()
if err != nil {
return err
}
allRRs, err := recordsets.ExtractRecordSets(allRecordPages)
if err != nil {
return err
}
if len(allRRs) != 1 {
return fmt.Errorf("CleanUp: Expected to find 1 recordset matching %s in zone %s, found %v", ch.ResolvedFQDN, ch.ResolvedZone, len(allRRs))
}
// TODO rather than deleting the whole recordset we may have to delete individual records from it, i.e. perform an update rather than a delete
err = recordsets.Delete(c.client, allZones[0].ID, allRRs[0].ID).ExtractErr()
if err != nil {
return err
}
return nil
}
func (c *designateDNSProviderSolver) Initialize(kubeClientConfig *rest.Config, stopCh <-chan struct{}) error {
log.Debugf("Initialize called")
cl, err := createDesignateServiceClient()
if err != nil {
return err
}
c.client = cl
return nil
}
func quoteRecord(r string) string {
if strings.HasPrefix(r, "\"") && strings.HasSuffix(r, "\"") {
return r
} else {
return strconv.Quote(r)
}
}