-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
186 lines (167 loc) · 5.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
package main
import (
"context"
"encoding/json"
"errors"
"os"
"time"
"github.com/cert-manager/cert-manager/pkg/acme/webhook/apis/acme/v1alpha1"
"github.com/cert-manager/cert-manager/pkg/acme/webhook/cmd"
simplyComClient "github.com/runnerm/simply-com-client"
log "github.com/sirupsen/logrus"
extapi "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)
var GroupName = os.Getenv("GROUP_NAME")
var LogLevel = os.Getenv("LOG_LEVEL")
func main() {
time.Sleep(10 * time.Second)
if GroupName == "" {
panic("GROUP_NAME must be specified")
}
// This will register our custom DNS provider with the webhook serving
// library, making it available as an API under the provided GroupName.
// You can register multiple DNS provider implementations with a single
// webhook, where the Name() method will be used to disambiguate between
// the different implementations.
cmd.RunWebhookServer(GroupName,
&SimplyDnsSolver{},
)
}
type SimplyDNSProviderConfig struct {
// Change the two fields below according to the format of the configuration
// to be decoded.
// These fields will be set by users in the
// `issuer.spec.acme.dns01.providers.webhook.config` field.
SecretRef string `json:"secretName"`
AccountName string `json:"accountName"`
ApiKey string `json:"apiKey"`
}
type SimplyDnsSolver struct {
client simplyComClient.SimplyClient
kubeClient *kubernetes.Clientset
}
func (e *SimplyDnsSolver) Name() string {
return "simply-dns-solver"
}
func (e *SimplyDnsSolver) Present(ch *v1alpha1.ChallengeRequest) error {
log.Info("Presenting challenge for: ", ch.ResolvedFQDN)
err := loadCredentials(ch, e)
if err != nil {
log.Errorf("load credentials failed(check secret configuration): %v", err)
return err
}
id, txtData, fetchErr := e.client.GetRecord(ch.ResolvedFQDN, ch.Key, "TXT")
if fetchErr == nil && id != 0 && txtData != ch.Key {
_, err := e.client.UpdateRecord(id, ch.ResolvedFQDN, ch.Key, "TXT")
if err != nil {
log.Errorf("presenting challenge failed: %v", err)
return err
}
log.Debug("Challenge have been created with record id: ", id)
return nil
} else if fetchErr == nil && id != 0 && txtData == ch.Key {
log.Debug("Challenge have been created with record id: ", id)
return nil
} else {
id, err = e.client.AddRecord(ch.ResolvedFQDN, ch.Key, "TXT")
if err != nil {
log.Errorf("presenting challenge failed: %v", err)
return err
} else {
log.Debug("Challenge have been created with record id: ", id)
}
return nil
}
}
func (e *SimplyDnsSolver) CleanUp(ch *v1alpha1.ChallengeRequest) error {
log.Info("Cleaning up challenge for: ", ch.ResolvedFQDN)
err := loadCredentials(ch, e)
if err != nil {
log.Errorf("load credentials failed(check secret configuration): %v", err)
return err
}
id, _, err := e.client.GetRecord(ch.ResolvedFQDN, ch.Key, "TXT")
if err != nil {
log.Errorf("Record not present have been cleaned up: %v", err)
return nil
}
log.Info("Record(", id, ") fetched for cleanup.")
res := e.client.RemoveRecord(id, ch.ResolvedFQDN)
if res {
log.Debug("Record(", id, ") have been cleaned up.")
return nil
} else {
log.Errorf("record(%d) have not been cleaned up", id)
return err
}
}
func (e *SimplyDnsSolver) Initialize(kubeClientConfig *rest.Config, stopCh <-chan struct{}) error {
if LogLevel == "DEBUG" {
log.SetLevel(log.DebugLevel)
} else {
log.SetLevel(log.InfoLevel)
}
log.Info("Initializing Simply dns solver")
cl, err := kubernetes.NewForConfig(kubeClientConfig)
if err != nil {
log.Errorf("init failed with error: %v", err)
return err
}
e.kubeClient = cl
return nil
}
func loadConfig(cfgJSON *extapi.JSON) (SimplyDNSProviderConfig, error) {
log.Debug("Loading config...")
cfg := SimplyDNSProviderConfig{}
if cfgJSON == nil {
return cfg, nil
}
if err := json.Unmarshal(cfgJSON.Raw, &cfg); err != nil {
log.Errorf("error decoding solver config: %v", err)
return cfg, errors.New("error decoding solver config")
}
log.Debug("Config loaded successfully.")
return cfg, nil
}
func stringFromSecretData(secretData *map[string][]byte, key string) (string, error) {
data, ok := (*secretData)[key]
if !ok {
log.Errorf("key %q not found in secret data", key)
return "", errors.New("key not found in secret data")
}
return string(data), nil
}
func loadCredentials(ch *v1alpha1.ChallengeRequest, e *SimplyDnsSolver) error {
if e.client.Credentials.AccountName != "" && e.client.Credentials.ApiKey != "" {
return nil
}
cfg, err := loadConfig(ch.Config)
if err != nil {
log.Errorf("error on loading config: %v", err)
return err
}
if cfg.AccountName != "" && cfg.ApiKey != "" {
log.Debug("Loading API credentials from config.")
e.client = simplyComClient.CreateSimplyClient(cfg.AccountName, cfg.ApiKey)
return nil
} else {
secretName := cfg.SecretRef
log.Debug("Loading API credentials from secret: ", secretName)
sec, err := e.kubeClient.CoreV1().Secrets(ch.ResourceNamespace).Get(context.TODO(), secretName, metav1.GetOptions{})
if err != nil {
log.Errorf("error on loading secret from kubernetes api: %v", err)
return err
}
accountName, _ := stringFromSecretData(&sec.Data, "account-name")
apiKey, err := stringFromSecretData(&sec.Data, "api-key")
if err != nil {
log.Errorf("error on reading secret: %v", err)
return err
}
e.client = simplyComClient.CreateSimplyClient(accountName, apiKey)
return nil
}
}