-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprovider.go
More file actions
187 lines (154 loc) · 4.78 KB
/
provider.go
File metadata and controls
187 lines (154 loc) · 4.78 KB
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
//go:build ignore
package websupport
import (
"context"
"crypto/hmac"
"crypto/sha1"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"time"
"github.com/libdns/libdns"
)
// Provider implements the libdns interfaces for Websupport's DNS API.
type Provider struct {
APIKey string `json:"api_key,omitempty"`
APISecret string `json:"api_secret,omitempty"`
APIBase string `json:"api_base,omitempty"`
ServiceID string `json:"service_id,omitempty"` // Service ID for the domain
HTTPClient *http.Client
Timeout time.Duration
}
// ensureClient initializes the HTTP client if not set.
func (p *Provider) ensureClient() {
if p.HTTPClient == nil {
p.HTTPClient = &http.Client{Timeout: 30 * time.Second}
}
if p.Timeout == 0 {
p.Timeout = 30 * time.Second
}
}
// calculateSignature generates HMAC-SHA1 signature for Websupport API authentication
func (p *Provider) calculateSignature(method, path string, timestamp int64) string {
canonicalRequest := fmt.Sprintf("%s %s %d", method, path, timestamp)
h := hmac.New(sha1.New, []byte(p.APISecret))
h.Write([]byte(canonicalRequest))
return hex.EncodeToString(h.Sum(nil))
}
// addAuthHeaders adds required authentication headers to the request
func (p *Provider) addAuthHeaders(req *http.Request, method, path string) {
timestamp := time.Now().Unix()
signature := p.calculateSignature(method, path, timestamp)
req.SetBasicAuth(p.APIKey, signature)
req.Header.Set("X-Date", time.Unix(timestamp, 0).UTC().Format("20060102T150405Z"))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
}
// AppendRecords creates DNS records (used for ACME TXT records).
func (p *Provider) AppendRecords(ctx context.Context, zone string, recs []libdns.Record) ([]libdns.Record, error) {
p.ensureClient()
if p.ServiceID == "" {
return nil, fmt.Errorf("ServiceID is required - set WEBSUPPORT_SERVICE_ID environment variable")
}
var created []libdns.Record
for _, rec := range recs {
r, ok := rec.(*libdns.TXT)
if !ok {
continue
}
if r.TTL == 0 {
r.TTL = 120 * time.Second
}
body := fmt.Sprintf(`{"type":"TXT","name":"%s","content":"%s","ttl":%d}`,
r.Name, r.Text, int(r.TTL.Seconds()))
urlPath := fmt.Sprintf("/service/%s/dns/record", p.ServiceID)
sigPath := fmt.Sprintf("/v2/service/%s/dns/record", p.ServiceID)
req, err := http.NewRequestWithContext(ctx, "POST",
p.APIBase+urlPath,
strings.NewReader(body))
if err != nil {
return nil, err
}
p.addAuthHeaders(req, "POST", sigPath)
resp, err := p.HTTPClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 204 {
bodyBytes, _ := io.ReadAll(resp.Body)
return nil, fmt.Errorf("failed to create record: %s, body: %s", resp.Status, string(bodyBytes))
}
time.Sleep(1 * time.Second)
allRecs, err := p.GetRecords(ctx, zone)
if err == nil {
normalizedName := strings.TrimSuffix(r.Name, ".")
for _, existingRec := range allRecs {
if txtRec, ok := existingRec.(*libdns.TXT); ok {
existingName := strings.TrimSuffix(txtRec.Name, ".")
existingName = strings.TrimSuffix(existingName, "."+strings.TrimSuffix(zone, "."))
if existingName == normalizedName && txtRec.Text == r.Text {
r.ProviderData = txtRec.ProviderData
break
}
}
}
}
created = append(created, r)
}
return created, nil
}
// DeleteRecords removes DNS records by ID.
func (p *Provider) DeleteRecords(ctx context.Context, zone string, recs []libdns.Record) ([]libdns.Record, error) {
p.ensureClient()
if p.ServiceID == "" {
return nil, fmt.Errorf("ServiceID is required - set WEBSUPPORT_SERVICE_ID environment variable")
}
var deleted []libdns.Record
for _, rec := range recs {
r, ok := rec.(*libdns.TXT)
if !ok {
continue
}
id, ok := r.ProviderData.(string)
if !ok || id == "" {
allRecs, err := p.GetRecords(ctx, zone)
if err != nil {
continue
}
for _, existingRec := range allRecs {
if txtRec, ok := existingRec.(*libdns.TXT); ok {
if txtRec.Name == r.Name && txtRec.Text == r.Text {
id, _ = txtRec.ProviderData.(string)
break
}
}
}
if id == "" {
continue
}
}
urlPath := fmt.Sprintf("/service/%s/dns/record/%s", p.ServiceID, id)
sigPath := fmt.Sprintf("/v2/service/%s/dns/record/%s", p.ServiceID, id)
req, err := http.NewRequestWithContext(ctx, "DELETE",
p.APIBase+urlPath, nil)
if err != nil {
return nil, err
}
p.addAuthHeaders(req, "DELETE", sigPath)
resp, err := p.HTTPClient.Do(req)
if err != nil {
return nil, err
}
resp.Body.Close()
if resp.StatusCode != 204 {
return nil, fmt.Errorf("failed to delete record: %s", resp.Status)
}
deleted = append(deleted, r)
}
return deleted, nil
}
// GetRecords is implemented in the original provider package file.