-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.go
242 lines (217 loc) · 6.85 KB
/
setup.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
package dnsimple
import (
"context"
"fmt"
"math/rand"
"net"
"net/http"
"os"
"strconv"
"strings"
"time"
"github.com/coredns/caddy"
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/pkg/fall"
clog "github.com/coredns/coredns/plugin/pkg/log"
"github.com/dnsimple/dnsimple-go/dnsimple"
"golang.org/x/oauth2"
)
var log = clog.NewWithPlugin("dnsimple")
func init() { plugin.Register("dnsimple", setup) }
const defaultUserAgent = "coredns-plugin-dnsimple"
type Options struct {
accountId string
accessToken string
apiCaller DNSimpleApiCaller
customDNSResolver string
clientDNSResolver string
identifier string
maxRetries int
refresh time.Duration
customHTTPDialer func(ctx context.Context, network, address string) (net.Conn, error)
}
// exposed for testing
var newDnsimpleService = func(ctx context.Context, options Options, accessToken string, baseUrl string) (dnsimpleService, error) {
httpClient := dnsimple.StaticTokenHTTPClient(ctx, accessToken)
if options.clientDNSResolver != "" {
if httpClient.Transport.(*oauth2.Transport).Base != nil {
httpClient.Transport.(*oauth2.Transport).Base.(*http.Transport).DialContext = options.customHTTPDialer
} else {
transport := http.DefaultTransport.(*http.Transport).Clone()
transport.DialContext = options.customHTTPDialer
httpClient.Transport.(*oauth2.Transport).Base = transport
}
}
client := dnsimple.NewClient(httpClient)
client.BaseURL = baseUrl
client.SetUserAgent(defaultUserAgent + "/" + PluginVersion)
return dnsimpleClient{client}, nil
}
func setup(c *caddy.Controller) error {
rand.Seed(time.Now().UTC().UnixNano())
for c.Next() {
keyPairs := map[string]struct{}{}
keys := map[string][]string{}
opts := Options{}
var accessToken string
var baseUrl string
var fall fall.F
args := c.RemainingArgs()
for i := 0; i < len(args); i++ {
parts := strings.SplitN(args[i], ":", 2)
if len(parts) < 1 {
return plugin.Error("dnsimple", c.Errf("invalid zone %q", args[i]))
}
zone, region := parts[0], "global"
if len(parts) > 1 && parts[1] != "" {
region = parts[1]
}
if zone == "" || region == "" {
return plugin.Error("dnsimple", c.Errf("invalid zone %q", args[i]))
}
if _, ok := keyPairs[args[i]]; ok {
return plugin.Error("dnsimple", c.Errf("conflict zone %q", args[i]))
}
keyPairs[args[i]] = struct{}{}
keys[zone] = append(keys[zone], region)
}
if len(keys) == 0 {
return plugin.Error("dnsimple", c.Errf("no zone(s) specified"))
}
for c.NextBlock() {
switch c.Val() {
case "access_token":
if !c.NextArg() {
return plugin.Error("dnsimple", c.ArgErr())
}
if c.Val() != "" {
log.Warning("consider using alternative ways of providing credentials, such as environment variables")
}
opts.accessToken = c.Val()
case "account_id":
if !c.NextArg() {
return plugin.Error("dnsimple", c.ArgErr())
}
opts.accountId = c.Val()
case "base_url":
if !c.NextArg() {
return plugin.Error("dnsimple", c.ArgErr())
}
baseUrl = c.Val()
case "custom_dns_resolver":
if !c.NextArg() {
return plugin.Error("dnsimple", c.ArgErr())
}
opts.customDNSResolver = c.Val()
case "client_dns_resolver":
if !c.NextArg() {
return plugin.Error("dnsimple", c.ArgErr())
}
opts.clientDNSResolver = c.Val()
case "fallthrough":
fall.SetZonesFromArgs(c.RemainingArgs())
case "identifier":
if !c.NextArg() {
return plugin.Error("dnsimple", c.ArgErr())
}
opts.identifier = c.Val()
case "max_retries":
if !c.NextArg() {
return plugin.Error("dnsimple", c.ArgErr())
}
maxRetriesStr := c.Val()
var err error
if opts.maxRetries, err = strconv.Atoi(maxRetriesStr); err != nil {
return plugin.Error("dnsimple", c.Errf("unable to parse max retries: %v", err))
}
if opts.maxRetries < 0 {
return plugin.Error("dnsimple", c.Err("max retries cannot be less than zero"))
}
case "refresh":
if !c.NextArg() {
return plugin.Error("dnsimple", c.ArgErr())
}
var err error
refreshStr := c.Val()
if _, err = strconv.Atoi(refreshStr); err == nil {
refreshStr = fmt.Sprintf("%ss", c.Val())
}
if opts.refresh, err = time.ParseDuration(refreshStr); err != nil {
return plugin.Error("dnsimple", c.Errf("unable to parse duration: %v", err))
}
if opts.refresh <= 60 {
return plugin.Error("dnsimple", c.Errf("refresh interval must be greater than 60 seconds: %q", refreshStr))
}
default:
return plugin.Error("dnsimple", c.Errf("unknown property %q", c.Val()))
}
}
// Set default values.
if accessToken == "" {
// Keep this environment variable name consistent across all our integrations (e.g. SDKs, Terraform provider).
accessToken = os.Getenv("DNSIMPLE_TOKEN")
}
// Still blank, return error
if accessToken == "" {
return plugin.Error("dnsimple", c.Err("access token must be provided via the Corefile or DNSIMPLE_TOKEN environment variable"))
}
if opts.accountId == "" {
opts.accountId = os.Getenv("DNSIMPLE_ACCOUNT_ID")
}
// Still blank, return error
if opts.accountId == "" {
return plugin.Error("dnsimple", c.Err("account ID must be provided via the Corefile or DNSIMPLE_TOKEN environment variable"))
}
if baseUrl == "" {
// Default to production
baseUrl = "https://api.dnsimple.com"
}
if opts.identifier == "" {
opts.identifier = "default"
}
if opts.refresh < 60 {
// Default update frequency of 1 minute.
opts.refresh = time.Duration(1) * time.Minute
}
if opts.clientDNSResolver == "" {
dialer := &net.Dialer{
Resolver: &net.Resolver{
PreferGo: true,
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
d := net.Dialer{
Timeout: time.Second * 5,
}
return d.DialContext(ctx, network, opts.clientDNSResolver)
},
},
}
opts.customHTTPDialer = func(ctx context.Context, network, address string) (net.Conn, error) {
return dialer.DialContext(ctx, network, address)
}
}
opts.apiCaller = createDNSimpleAPICaller(opts, baseUrl, accessToken, defaultUserAgent+"/"+PluginVersion)
ctx, cancel := context.WithCancel(context.Background())
client, err := newDnsimpleService(ctx, opts, accessToken, baseUrl)
if err != nil {
cancel()
return err
}
h, err := New(ctx, client, keys, opts)
if err != nil {
cancel()
return plugin.Error("dnsimple", c.Errf("failed to create dnsimple plugin: %v", err))
}
h.Fall = fall
if err := h.Run(ctx); err != nil {
cancel()
return plugin.Error("dnsimple", c.Errf("failed to initialize dnsimple plugin: %v", err))
}
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
h.Next = next
return h
})
c.OnShutdown(func() error { cancel(); return nil })
}
return nil
}