-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
90 lines (77 loc) · 2.36 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
package main
import (
"context"
"fmt"
"os"
"strconv"
"strings"
"github.com/dnsimple/dnsimple-go/dnsimple"
"golang.org/x/oauth2"
"k8s.io/client-go/rest"
"github.com/jetstack/cert-manager/pkg/acme/webhook/apis/acme/v1alpha1"
"github.com/jetstack/cert-manager/pkg/acme/webhook/cmd"
"github.com/jetstack/cert-manager/pkg/issuer/acme/dns/util"
)
var GroupName = os.Getenv("GROUP_NAME")
func main() {
if GroupName == "" {
panic("GROUP_NAME must be specified")
}
cmd.RunWebhookServer(GroupName, &dnsimpleSolver{})
}
// dnsimpleSolver implements the webhook.Solver interface.
// See: github.com/jetstack/cert-manager/pkg/acme/webhook
type dnsimpleSolver struct {
client *dnsimple.Client
accountID string
}
func (s *dnsimpleSolver) Name() string {
return "dnsimple"
}
func (s *dnsimpleSolver) Present(ch *v1alpha1.ChallengeRequest) error {
_, err := s.client.Zones.CreateRecord(s.accountID, strings.TrimRight(ch.ResolvedZone, "."), dnsimple.ZoneRecord{
Name: extractRecordName(ch.ResolvedFQDN, ch.ResolvedZone),
Type: "TXT",
TTL: 300,
Content: ch.Key,
})
if err != nil && strings.Contains(err.Error(), "record already exists") {
return nil
}
return err
}
func (s *dnsimpleSolver) CleanUp(ch *v1alpha1.ChallengeRequest) error {
resp, err := s.client.Zones.ListRecords(s.accountID, util.UnFqdn(ch.ResolvedZone), &dnsimple.ZoneRecordListOptions{
Name: extractRecordName(ch.ResolvedFQDN, ch.ResolvedZone),
})
if err != nil {
return fmt.Errorf("listing zone records: %v", err)
}
for _, r := range resp.Data {
_, err := s.client.Zones.DeleteRecord(s.accountID, util.UnFqdn(ch.ResolvedZone), r.ID)
if err != nil {
return fmt.Errorf("deleting record: %v", err)
}
}
return nil
}
func (s *dnsimpleSolver) Initialize(kubeClientConfig *rest.Config, stopCh <-chan struct{}) error {
if s.client == nil {
s.client = dnsimple.NewClient(oauth2.NewClient(context.Background(), oauth2.StaticTokenSource(&oauth2.Token{AccessToken: os.Getenv("DNSIMPLE_ACCESS_TOKEN")})))
}
if s.accountID == "" {
resp, err := dnsimple.Whoami(s.client)
if err != nil {
return fmt.Errorf("whoami request: %v", err)
}
s.accountID = strconv.FormatInt(resp.Account.ID, 10)
}
return nil
}
func extractRecordName(fqdn, domain string) string {
name := util.UnFqdn(fqdn)
if idx := strings.Index(name, "."+util.UnFqdn(domain)); idx != -1 {
return name[:idx]
}
return name
}