Skip to content

Commit

Permalink
fix: Use DefaultTransport with custom CA bundles (#19)
Browse files Browse the repository at this point in the history
Ensures that other default settings in DefaultTransport, like proxy settings, are used.

Resolves #17
  • Loading branch information
zachomedia authored Jul 12, 2022
1 parent b625a57 commit d5fdd7f
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 12 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ clean:
go clean -testcache

verify:
TEST_ASSET_ETCD=_out/kubebuilder/bin/etcd TEST_ASSET_KUBE_APISERVER=_out/kubebuilder/bin/kube-apiserver TEST_ASSET_KUBECTL=_out/kubebuilder/bin/kubectl TEST_DNS_SERVER="127.0.0.1:53" TEST_ZONE_NAME=example.ca. go test .
TEST_ASSET_ETCD=_out/kubebuilder/bin/etcd TEST_ASSET_KUBE_APISERVER=_out/kubebuilder/bin/kube-apiserver TEST_ASSET_KUBECTL=_out/kubebuilder/bin/kubectl TEST_DNS_SERVER="127.0.0.1:53" TEST_ZONE_NAME=example.ca. go test -v -run "^TestNoProxy.*"
TEST_ASSET_ETCD=_out/kubebuilder/bin/etcd TEST_ASSET_KUBE_APISERVER=_out/kubebuilder/bin/kube-apiserver TEST_ASSET_KUBECTL=_out/kubebuilder/bin/kubectl TEST_DNS_SERVER="127.0.0.1:53" TEST_ZONE_NAME=example.ca. HTTP_PROXY="127.0.0.1:3128" HTTPS_PROXY="127.0.0.1:3128" go test -v -run "^TestProxy.*"

test: verify

Expand Down
7 changes: 7 additions & 0 deletions docker-compose.test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,12 @@ services:
- ./testdata/pdns/docker/nginx/default.conf:/etc/nginx/conf.d/default.conf
depends_on:
- pdns

proxy:
image: ubuntu/squid:5.2-22.04_beta
ports:
- '3128:3128'
volumes:
- ./testdata/pdns/docker/squid/squid.conf:/etc/squid/squid.conf
volumes:
data: {}
9 changes: 5 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,12 @@ func (c *powerDNSProviderSolver) init(config *apiextensionsv1.JSON, namespace st
return nil, cfg, fmt.Errorf("failed to load certificate(s) from CA bundle")
}

httpClient.Transport = &http.Transport{
TLSClientConfig: &tls.Config{
RootCAs: caBundle,
},
transport := http.DefaultTransport.(*http.Transport).Clone()
transport.TLSClientConfig = &tls.Config{
RootCAs: caBundle,
}

httpClient.Transport = transport
}

return powerdns.NewClient(cfg.Host, "localhost", map[string]string{"X-API-Key": apiKey}, httpClient), cfg, nil
Expand Down
12 changes: 10 additions & 2 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,22 @@ func test(t *testing.T, manifestPath string) {
fixture.RunConformance(t)
}

func TestRunsSuiteNoTLS(t *testing.T) {
func TestNoProxyNoTLS(t *testing.T) {
test(t, "_out/testdata/no-tls")
}

func TestRunsSuiteTLS(t *testing.T) {
func TestNoProxyTLS(t *testing.T) {
test(t, "_out/testdata/tls")
}

func TestProxyNoTLS(t *testing.T) {
test(t, "_out/testdata/no-tls-with-proxy")
}

func TestProxyTLS(t *testing.T) {
test(t, "_out/testdata/tls-with-proxy")
}

func getEnv(key, fallback string) string {
if value, ok := os.LookupEnv(key); ok {
return value
Expand Down
15 changes: 10 additions & 5 deletions scripts/setup-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,21 @@ subjectAltName = @alternate_names
[ alternate_names ]
DNS.1 = localhost
DNS.2 = web
IP.1 = 127.0.0.1
IP.2 = ::1
EOF

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'

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

# No TLS
mkdir -p _out/testdata/no-tls
cp testdata/pdns/test/no-tls/{config.json,apikey.yml} _out/testdata/no-tls
for suite in no-tls no-tls-with-proxy; do
mkdir -p _out/testdata/${suite}
cp testdata/pdns/test/${suite}/{config.json,apikey.yml} _out/testdata/${suite}
done
4 changes: 4 additions & 0 deletions testdata/pdns/docker/squid/squid.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
http_access allow all
http_port 3128
coredump_dir /var/spool/squid
logfile_rotate 0
7 changes: 7 additions & 0 deletions testdata/pdns/test/no-tls-with-proxy/apikey.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: v1
kind: Secret
metadata:
name: pdns-api-key
type: Opaque
data:
key: dGVzdDEyMw==
8 changes: 8 additions & 0 deletions testdata/pdns/test/no-tls-with-proxy/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"host": "http://pdns:8080",
"apiKeySecretRef": {
"name": "pdns-api-key",
"key": "key"
},
"ttl": 10
}
7 changes: 7 additions & 0 deletions testdata/pdns/test/tls-with-proxy/apikey.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: v1
kind: Secret
metadata:
name: pdns-api-key
type: Opaque
data:
key: dGVzdDEyMw==
9 changes: 9 additions & 0 deletions testdata/pdns/test/tls-with-proxy/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"host": "https://web:8443",
"apiKeySecretRef": {
"name": "pdns-api-key",
"key": "key"
},
"ttl": 10,
"caBundle": "__CERT__"
}

0 comments on commit d5fdd7f

Please sign in to comment.