-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathoptions.go
90 lines (77 loc) · 2.28 KB
/
options.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 pdns
import (
"crypto/tls"
"crypto/x509"
"github.com/mittwald/go-powerdns/pdnshttp"
"io"
"io/ioutil"
"net/http"
)
// WithBaseURL sets a client's base URL
func WithBaseURL(baseURL string) ClientOption {
return func(c *client) error {
c.baseURL = baseURL
return nil
}
}
// WithHTTPClient can be used to override a client's HTTP client.
// Otherwise, the default HTTP client will be used
func WithHTTPClient(httpClient *http.Client) ClientOption {
return func(c *client) error {
c.httpClient = httpClient
return nil
}
}
// WithAPIKeyAuthentication adds API-key based authentication to the PowerDNS client.
// In effect, each HTTP request will have an additional header that contains the API key
// supplied to this function:
// X-API-Key: {{ key }}
func WithAPIKeyAuthentication(key string) ClientOption {
return func(c *client) error {
c.authenticator = &pdnshttp.APIKeyAuthenticator{
APIKey: key,
}
return nil
}
}
// WithBasicAuthentication adds basic authentication to the PowerDNS client.
func WithBasicAuthentication(username string, password string) ClientOption {
return func(c *client) error {
c.authenticator = &pdnshttp.BasicAuthenticator{
Username: username,
Password: password,
}
return nil
}
}
// WithTLSAuthentication configures TLS-based authentication for the PowerDNS client.
// This is not a feature that is provided by PowerDNS natively, but might be implemented
// when the PowerDNS API is run behind a reverse proxy.
func WithTLSAuthentication(caFile string, clientCertFile string, clientKeyFile string) ClientOption {
return func(c *client) error {
cert, err := tls.LoadX509KeyPair(clientCertFile, clientKeyFile)
if err != nil {
return err
}
caBytes, err := ioutil.ReadFile(caFile)
if err != nil {
return err
}
ca, err := x509.ParseCertificates(caBytes)
auth := pdnshttp.TLSClientCertificateAuthenticator{
ClientCert: cert,
ClientKey: cert.PrivateKey,
CACerts: ca,
}
c.authenticator = &auth
return nil
}
}
// WithDebuggingOutput can be used to supply an io.Writer to the client into which all
// outgoing HTTP requests and their responses will be logged. Useful for debugging.
func WithDebuggingOutput(out io.Writer) ClientOption {
return func(c *client) error {
c.debugOutput = out
return nil
}
}