From 5eb87685e22a0b2b65ffdef6e3bd899d8b67f066 Mon Sep 17 00:00:00 2001 From: jotasi <10883104+jotasi@users.noreply.github.com> Date: Mon, 27 May 2024 22:18:24 +0200 Subject: [PATCH] pdns: reconstruct zone URLs to enable non-root folder API endpoints (#2141) Co-authored-by: Fernandez Ludovic --- providers/dns/pdns/internal/client.go | 4 +- providers/dns/pdns/internal/client_test.go | 52 ++++++++++++++++++++++ 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/providers/dns/pdns/internal/client.go b/providers/dns/pdns/internal/client.go index 7eed57e303..bc525c5789 100644 --- a/providers/dns/pdns/internal/client.go +++ b/providers/dns/pdns/internal/client.go @@ -117,7 +117,7 @@ func (c *Client) GetHostedZone(ctx context.Context, authZone string) (*HostedZon } func (c *Client) UpdateRecords(ctx context.Context, zone *HostedZone, sets RRSets) error { - endpoint := c.joinPath("/", zone.URL) + endpoint := c.joinPath("/", "servers", c.serverName, "zones", zone.ID) req, err := newJSONRequest(ctx, http.MethodPatch, endpoint, sets) if err != nil { @@ -137,7 +137,7 @@ func (c *Client) Notify(ctx context.Context, zone *HostedZone) error { return nil } - endpoint := c.joinPath("/", zone.URL, "/notify") + endpoint := c.joinPath("/", "servers", c.serverName, "zones", zone.ID, "notify") req, err := newJSONRequest(ctx, http.MethodPut, endpoint, nil) if err != nil { diff --git a/providers/dns/pdns/internal/client_test.go b/providers/dns/pdns/internal/client_test.go index 8d0aaab68d..b0eb9d2ed0 100644 --- a/providers/dns/pdns/internal/client_test.go +++ b/providers/dns/pdns/internal/client_test.go @@ -256,6 +256,7 @@ func TestClient_GetHostedZone_v0(t *testing.T) { func TestClient_UpdateRecords(t *testing.T) { client := setupTest(t, http.MethodPatch, "/api/v1/servers/localhost/zones/example.org.", http.StatusOK, "zone.json") client.apiVersion = 1 + client.serverName = "localhost" zone := &HostedZone{ ID: "example.org.", @@ -282,9 +283,41 @@ func TestClient_UpdateRecords(t *testing.T) { require.NoError(t, err) } +func TestClient_UpdateRecords_NonRootApi(t *testing.T) { + client := setupTest(t, http.MethodPatch, "/some/path/api/v1/servers/localhost/zones/example.org.", http.StatusOK, "zone.json") + client.Host = client.Host.JoinPath("some", "path") + client.apiVersion = 1 + client.serverName = "localhost" + + zone := &HostedZone{ + ID: "example.org.", + Name: "example.org.", + URL: "some/path/api/v1/servers/server/zones/example.org.", + Kind: "Master", + } + + rrSets := RRSets{ + RRSets: []RRSet{{ + Name: "example.org.", + Type: "NS", + ChangeType: "REPLACE", + Records: []Record{{ + Content: "192.0.2.5", + Name: "ns1.example.org.", + TTL: 86400, + Type: "A", + }}, + }}, + } + + err := client.UpdateRecords(context.Background(), zone, rrSets) + require.NoError(t, err) +} + func TestClient_UpdateRecords_v0(t *testing.T) { client := setupTest(t, http.MethodPatch, "/servers/localhost/zones/example.org.", http.StatusOK, "zone.json") client.apiVersion = 0 + client.serverName = "localhost" zone := &HostedZone{ ID: "example.org.", @@ -314,6 +347,7 @@ func TestClient_UpdateRecords_v0(t *testing.T) { func TestClient_Notify(t *testing.T) { client := setupTest(t, http.MethodPut, "/api/v1/servers/localhost/zones/example.org./notify", http.StatusOK, "") client.apiVersion = 1 + client.serverName = "localhost" zone := &HostedZone{ ID: "example.org.", @@ -326,8 +360,26 @@ func TestClient_Notify(t *testing.T) { require.NoError(t, err) } +func TestClient_Notify_NonRootApi(t *testing.T) { + client := setupTest(t, http.MethodPut, "/some/path/api/v1/servers/localhost/zones/example.org./notify", http.StatusOK, "") + client.Host = client.Host.JoinPath("some", "path") + client.apiVersion = 1 + client.serverName = "localhost" + + zone := &HostedZone{ + ID: "example.org.", + Name: "example.org.", + URL: "/some/path/api/v1/servers/server/zones/example.org.", + Kind: "Master", + } + + err := client.Notify(context.Background(), zone) + require.NoError(t, err) +} + func TestClient_Notify_v0(t *testing.T) { client := setupTest(t, http.MethodPut, "/api/v1/servers/localhost/zones/example.org./notify", http.StatusOK, "") + client.apiVersion = 0 zone := &HostedZone{ ID: "example.org.",