Skip to content

Commit eda5e49

Browse files
tnsimonsimontien47
andauthored
Authorization header support (#45)
* feat: suppert auth headers * docs: update readme optional config * fix: update setup --------- Co-authored-by: Simon Tien <[email protected]>
1 parent d473389 commit eda5e49

File tree

8 files changed

+78
-4
lines changed

8 files changed

+78
-4
lines changed

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,16 @@ spec:
5757
### OPTIONAL
5858
###
5959

60+
# API Key scheme https://www.iana.org/assignments/http-authschemes/http-authschemes.xhtml
61+
# default: no scheme ""
62+
apiKeyScheme: ""
63+
64+
# Header name for API key
65+
#
66+
# This defaults to X-API-Key when unset but supports customizations
67+
# e.g. Authorization
68+
apiKeyHeaderName: ""
69+
6070
# Server ID for the PowerDNS API.
6171
# When unset, defaults to "localhost".
6272
#

main.go

+23-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ import (
2828
"github.com/joeig/go-powerdns/v3"
2929
)
3030

31+
const (
32+
defaultAuthHeader = "X-API-Key"
33+
defaultScheme = ""
34+
)
35+
3136
var GroupName = os.Getenv("GROUP_NAME")
3237

3338
func main() {
@@ -81,6 +86,17 @@ type powerDNSProviderConfig struct {
8186
// secret which contains the PowerDNS API Key.
8287
APIKeySecretRef *cmmeta.SecretKeySelector `json:"apiKeySecretRef"`
8388

89+
// Scheme supports HTTP AuthSchemes
90+
// https://www.iana.org/assignments/http-authschemes/http-authschemes.xhtml
91+
//
92+
// +optional default ""
93+
APIKeyScheme string `json:"apiKeyScheme"`
94+
95+
// APIKeyHeaderName is the header name where apiKey will be set
96+
//
97+
// +optional default "X-API-Key"
98+
APIKeyHeaderName string `json:"apiKeyHeaderName"`
99+
84100
// ServerID is the server ID in the PowerDNS API.
85101
// When unset, defaults to "localhost".
86102
ServerID string `json:"serverID"`
@@ -245,7 +261,10 @@ func (c *powerDNSProviderSolver) Initialize(kubeClientConfig *rest.Config, stopC
245261
// loadConfig is a small helper function that decodes JSON configuration into
246262
// the typed config struct.
247263
func loadConfig(cfgJSON *apiextensionsv1.JSON) (*powerDNSProviderConfig, error) {
248-
cfg := &powerDNSProviderConfig{}
264+
cfg := &powerDNSProviderConfig{
265+
APIKeyScheme: defaultScheme,
266+
APIKeyHeaderName: defaultAuthHeader,
267+
}
249268
// handle the 'base case' where no configuration has been provided
250269
if cfgJSON == nil {
251270
return cfg, nil
@@ -325,7 +344,9 @@ func (c *powerDNSProviderSolver) init(config *apiextensionsv1.JSON, namespace st
325344

326345
// Add request headers
327346
headers := map[string]string{
328-
"X-API-Key": apiKey,
347+
cfg.APIKeyHeaderName: strings.TrimLeft(
348+
strings.Trim(cfg.APIKeyScheme, " ")+" "+apiKey,
349+
" "),
329350
"Content-Type": "application/json",
330351
}
331352
maps.Copy(headers, cfg.Headers)

main_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,18 @@ func TestNoProxyNoTLS(t *testing.T) {
3232
test(t, "_out/testdata/no-tls")
3333
}
3434

35+
func TestNoProxyNoTLSAuthHdr(t *testing.T) {
36+
test(t, "_out/testdata/no-tls-auth-hdr")
37+
}
38+
3539
func TestNoProxyTLS(t *testing.T) {
3640
test(t, "_out/testdata/tls")
3741
}
3842

43+
func TestNoProxyTLSAuthHdr(t *testing.T) {
44+
test(t, "_out/testdata/tls-auth-hdr")
45+
}
46+
3947
func TestProxyNoTLS(t *testing.T) {
4048
test(t, "_out/testdata/no-tls-with-proxy")
4149
}

scripts/setup-tests.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ EOF
2121

2222
openssl req -x509 -config _out/openssl.conf -newkey rsa:4096 -keyout _out/key.pem -out _out/cert.pem -sha256 -days 30 -nodes -subj '/CN=localhost'
2323

24-
for suite in tls tls-with-proxy; do
24+
for suite in tls tls-with-proxy tls-auth-hdr; do
2525
mkdir -p _out/testdata/${suite}
2626
cp testdata/pdns/test/${suite}/apikey.yml _out/testdata/${suite}/apikey.yml
2727
sed "s#__CERT__#$(base64 -w0 _out/cert.pem)#g" testdata/pdns/test/${suite}/config.json > _out/testdata/${suite}/config.json
2828
done
2929

3030
# No TLS
31-
for suite in no-tls no-tls-with-proxy; do
31+
for suite in no-tls no-tls-with-proxy no-tls-auth-hdr; do
3232
mkdir -p _out/testdata/${suite}
3333
cp testdata/pdns/test/${suite}/{config.json,apikey.yml} _out/testdata/${suite}
3434
done
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
apiVersion: v1
2+
kind: Secret
3+
metadata:
4+
name: pdns-api-key
5+
type: Opaque
6+
data:
7+
key: dGVzdDEyMw==
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"host": "http://127.0.0.1:8080",
3+
"apiKeySecretRef": {
4+
"name": "pdns-api-key",
5+
"key": "key"
6+
},
7+
"apiKeyScheme": "",
8+
"apiKeyHeaderName": "X-API-Key",
9+
"ttl": 10
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
apiVersion: v1
2+
kind: Secret
3+
metadata:
4+
name: pdns-api-key
5+
type: Opaque
6+
data:
7+
key: dGVzdDEyMw==
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"host": "https://127.0.0.1:8443",
3+
"apiKeySecretRef": {
4+
"name": "pdns-api-key",
5+
"key": "key"
6+
},
7+
"apiKeyScheme": "",
8+
"apiKeyHeaderName": "X-API-Key",
9+
"ttl": 10,
10+
"caBundle": "__CERT__"
11+
}

0 commit comments

Comments
 (0)