-
Notifications
You must be signed in to change notification settings - Fork 0
/
nicapi.go
298 lines (241 loc) · 6.54 KB
/
nicapi.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
293
294
295
296
297
298
// +skip_license_check
// This package implements a DNS provider for solving the DNS-01
// challenge using Lumaserv/NicAPI DNS.
// API Documentation: https://docs.nicapi.eu/de/docs/dns/zones#dns-zones
package nicapi
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"os"
"strconv"
"strings"
"time"
"github.com/jetstack/cert-manager/pkg/issuer/acme/dns/util"
pkgutil "github.com/jetstack/cert-manager/pkg/util"
)
const APIURL = "https://connect.nicapi.eu/api/v1"
// DNSProvider is an implementation of the acme.ChallengeProvider interface
type DNSProvider struct {
dns01Nameservers []string
authKey string
}
// NewDNSProvider returns a DNSProvider instance.
// Credentials must be passed in the environment variables
func NewDNSProvider(dns01Nameservers []string) (*DNSProvider, error) {
key := os.Getenv("LUMASERV_API_KEY")
return NewDNSProviderCredentials(key, dns01Nameservers)
}
// NewDNSProviderCredentials uses the supplied credentials to return a
// DNSProvider instance.
func NewDNSProviderCredentials(key string, dns01Nameservers []string) (*DNSProvider, error) {
if key == "" {
return nil, fmt.Errorf("credentials missing")
}
return &DNSProvider{
authKey: key,
dns01Nameservers: dns01Nameservers,
}, nil
}
// Present creates a TXT record to fulfil the dns-01 challenge
func (c *DNSProvider) Present(domain, fqdn, value string) error {
zone, err := c.getHostedZone(fqdn)
if err != nil {
return err
}
record, err := c.findTxtRecord(zone, fqdn)
if err != nil && err != errNoExistingRecord {
// this is a real error
return err
}
recordName := strings.TrimSuffix(util.UnFqdn(fqdn), fmt.Sprintf(".%s", zone.Zone.Name))
if record != nil {
if record.Data == value {
// the record is already set to the desired value
return nil
}
record := dnsPostRecord{
Zone: zone.Zone.Name,
Records: []dnsRecord{
{
Name: recordName,
},
},
}
body, err := json.Marshal(record)
if err != nil {
return err
}
_, err = c.makeRequest("DELETE", "/dns/zones/records/delete", body)
if err != nil {
return err
}
}
recCol := dnsPostRecord{
Zone: zone.Zone.Name,
Records: []dnsRecord{
{
Name: recordName,
TTL: strconv.Itoa(120*60),
Type: "TXT",
Data: value,
},
},
}
body, err := json.Marshal(recCol)
if err != nil {
return err
}
_, err = c.makeRequest("POST", "/dns/zones/records/add", body)
if err != nil {
return err
}
return nil
}
// CleanUp removes the TXT record matching the specified parameters
func (c *DNSProvider) CleanUp(domain, fqdn, value string) error {
zone, err := c.getHostedZone(fqdn)
if err != nil {
return err
}
_, err = c.findTxtRecord(zone, fqdn)
// Nothing to cleanup
if err == errNoExistingRecord {
return nil
}
if err != nil {
return err
}
record := dnsPostRecord{
Zone: zone.Zone.Name,
Records: []dnsRecord{
{
Name: strings.TrimSuffix(util.UnFqdn(fqdn), fmt.Sprintf(".%s", zone.Zone.Name)),
},
},
}
body, err := json.Marshal(record)
if err != nil {
return err
}
_, err = c.makeRequest("DELETE", "/dns/zones/records/delete", body)
if err != nil {
return err
}
return nil
}
func (c *DNSProvider) getHostedZone(fqdn string) (*dnsZone, error) {
authZone, err := util.FindZoneByFqdn(fqdn, c.dns01Nameservers)
if err != nil {
return nil, err
}
request := struct {
Zone string `json:"zone"`
}{
util.UnFqdn(authZone),
}
body, err := json.Marshal(request)
if err != nil {
return nil, err
}
result, err := c.makeRequest("GET", "/dns/zones/show", body)
if err != nil {
return nil, err
}
var hostedZone dnsZone
err = json.Unmarshal(result, &hostedZone)
if err != nil {
return nil, err
}
return &hostedZone, nil
}
var errNoExistingRecord = errors.New("no existing record found")
func (c *DNSProvider) findTxtRecord(zone *dnsZone, fqdn string) (*dnsRecord, error) {
name := strings.TrimSuffix(util.UnFqdn(fqdn), fmt.Sprintf(".%s", zone.Zone.Name))
for _, rec := range zone.Zone.Records {
if rec.Name == name {
return &rec, nil
}
}
return nil, errNoExistingRecord
}
func (c *DNSProvider) makeRequest(method, uri string, body []byte) (json.RawMessage, error) {
// APIError contains error details for failed requests
type APIMessage struct {
Code int `json:"code,omitempty"`
Message string `json:"message,omitempty"`
}
// APIResponse represents a response from API
type APIResponse struct {
MetaData struct {
ClientTransactionId string `json:"clientTransactionId"`
ServerTransactionId string `json:"serverTransactionId"`
} `json:"metadata"`
Messages struct {
Errors [] *APIMessage `json:"errors"`
Warnings [] *APIMessage `json:"warnings"`
Success [] *APIMessage `json:"success"`
} `json:"messages"`
Status string `json:"status"`
Data json.RawMessage `json:"data"`
}
req, err := http.NewRequest(method, fmt.Sprintf("%s%s", APIURL, uri), strings.NewReader(string(body)))
if err != nil {
return nil, err
}
q := req.URL.Query()
q.Add("authToken", c.authKey)
req.URL.RawQuery = q.Encode()
req.Header.Set("User-Agent", pkgutil.CertManagerUserAgent)
req.Header.Set("Content-Type", "application/json")
fmt.Printf("HTTP Request: %s %s\n", method, fmt.Sprintf("%s%s", APIURL, uri))
fmt.Printf("HTTP Body: %s\n", string(body))
client := http.Client{
Timeout: 30 * time.Second,
}
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("error querying API -> %v", err)
}
defer resp.Body.Close()
var r APIResponse
err = json.NewDecoder(resp.Body).Decode(&r)
if err != nil {
return nil, err
}
fmt.Printf("HTTP Transaction: %s\n", r.MetaData.ServerTransactionId)
if r.Status != "success" {
if len(r.Messages.Errors) > 0 {
errStr := ""
for _, apiErr := range r.Messages.Errors {
errStr += fmt.Sprintf("\t Error: %d: %s", apiErr.Code, apiErr.Message)
}
return nil, fmt.Errorf("API Error \n%s", errStr)
}
return nil, fmt.Errorf("API error")
}
return r.Data, nil
}
// dnsRecord represents a DNS record
type dnsRecord struct {
ID int64 `json:"id,omitempty"`
Name string `json:"name"`
TTL string `json:"ttl,omitempty"`
Type string `json:"type"`
Data string `json:"data"`
ZoneID string `json:"zone_id,omitempty"`
}
// dnsZone represents a DNS zone
type dnsZone struct {
Zone struct {
ID int64 `json:"id"`
Name string `json:"name"`
Records [] dnsRecord `json:"records"`
} `json:"zone"`
}
// dnsPostRecord represents the request data to create a new DNS record
type dnsPostRecord struct {
Zone string `json:"zone"`
Records [] dnsRecord `json:"records"`
}