diff --git a/go.mod b/go.mod index eddfc46b..13b6ce27 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/nameserver-systems/pdns-distribute -go 1.19 +go 1.21 require ( github.com/google/uuid v1.3.1 diff --git a/internal/app/pdns-zone-provider/powerdns/powerdns.go b/internal/app/pdns-zone-provider/powerdns/powerdns.go index d269121a..e8fe160e 100644 --- a/internal/app/pdns-zone-provider/powerdns/powerdns.go +++ b/internal/app/pdns-zone-provider/powerdns/powerdns.go @@ -113,32 +113,9 @@ func getZoneDataFromPowerDNS(zoneid string, conf *config.ServiceConfiguration, w powerdnsdnstcpcalltotal.Inc() powerdnsaxfrquery.Inc() - /* Use always AXFR for quering zones instead of api. Because LUA Records are not recognized by libs and pdns can interpret format - if strings.Contains(zonefile, "CLASS1") { - //CLASS1 is used by miek/dns due to not recognize LUA records - zonefile, err = getZoneDataFromPowerDNSAPI(zoneid, conf) - if err != nil { - powerdnsapierrtotal.Inc() - return - } - - powerdnsapicalltotal.Inc() - } - */ - return zonefile, nil } -func getZoneDataFromPowerDNSAPI(zoneid string, conf *config.ServiceConfiguration) (string, error) { //nolint:unused,deadcode - // does not work for dnssec signed zones - // used for zones contain LUA records - serverid := conf.PowerDNSServerID - apitoken := conf.PowerDNSAPIToken - zoneDataFileURL := conf.PowerDNSURL + "/api/v1/servers/" + serverid + "/zones/" + zoneid + "/export" - - return httputils.ExecutePowerDNSRequest(http.MethodGet, zoneDataFileURL, apitoken, nil) -} - func getZoneMetadataFromPowerDNS(zonename string, conf *config.ServiceConfiguration) (dnssec, presigned bool, err error) { zonedata, requesterr := getZoneDetailsPerPowerdnsAPI(conf, zonename) if requesterr != nil { @@ -161,7 +138,7 @@ func getZoneMetadataFromPowerDNS(zonename string, conf *config.ServiceConfigurat func getZoneDetailsPerPowerdnsAPI(conf *config.ServiceConfiguration, zonename string) (modelpowerdns.Zone, error) { serverid := conf.PowerDNSServerID apitoken := conf.PowerDNSAPIToken - zoneDataFileURL := conf.PowerDNSURL + "/api/v1/servers/" + serverid + "/zones/" + zonename + zoneDataFileURL := conf.PowerDNSURL + powerdnsutils.PowerDNSServerBaseURL + serverid + powerdnsutils.PowerDNSZoneURLPath + zonename detailresponse, httperr := httputils.ExecutePowerDNSRequest(http.MethodGet, zoneDataFileURL, apitoken, nil) if httperr != nil { diff --git a/internal/pkg/eventutils/eventutils.go b/internal/pkg/eventutils/eventutils.go index 197635ba..0b5e73ec 100644 --- a/internal/pkg/eventutils/eventutils.go +++ b/internal/pkg/eventutils/eventutils.go @@ -21,7 +21,7 @@ func PublishDeleteZoneEvent(ms *microservice.Microservice, topic string, zoneid } ms.MessageBroker.Publish(topic, payload) - logger.DebugLog("[Delete Zone Event] triggered for zone: " + zoneid + " on topic: " + topic) + logger.DebugLog("[Delete Zone Event] triggered for zone: " + zoneid + " on topic: " + topic) //nolint:goconst } func PublishChangeZoneEvent(ms *microservice.Microservice, topic string, zoneid string) { diff --git a/internal/pkg/powerdnsutils/contants.go b/internal/pkg/powerdnsutils/contants.go new file mode 100644 index 00000000..5c19d26d --- /dev/null +++ b/internal/pkg/powerdnsutils/contants.go @@ -0,0 +1,6 @@ +package powerdnsutils + +const ( + PowerDNSServerBaseURL = "/api/v1/servers/" + PowerDNSZoneURLPath = "/zones/" +) diff --git a/internal/pkg/powerdnsutils/powerdnsutils_api.go b/internal/pkg/powerdnsutils/powerdnsutils_api.go index abe3b512..9c877e2a 100644 --- a/internal/pkg/powerdnsutils/powerdnsutils_api.go +++ b/internal/pkg/powerdnsutils/powerdnsutils_api.go @@ -37,7 +37,7 @@ func TransferPowerDNSDNSSECZonesIntoZoneStateMap(actualzones []modelpowerdns.Zon func GetZoneList(con modelpowerdns.PDNSconnectionobject, zoneid string, dnssecinfo bool) (string, error) { serverid := con.ServerID apitoken := con.Apitoken - zoneDataFileURL := con.PowerDNSurl + "/api/v1/servers/" + serverid + "/zones?rrsets=false&dnssec=" + + zoneDataFileURL := con.PowerDNSurl + PowerDNSServerBaseURL + serverid + "/zones?rrsets=false&dnssec=" + strconv.FormatBool(dnssecinfo) if zoneid != "" { @@ -55,7 +55,7 @@ func GetZoneList(con modelpowerdns.PDNSconnectionobject, zoneid string, dnssecin func DoesZoneExist(con modelpowerdns.PDNSconnectionobject, zoneID string) (bool, error) { serverID := con.ServerID apiToken := con.Apitoken - zoneDataFileURL := con.PowerDNSurl + "/api/v1/servers/" + serverID + "/zones?zone=" + zoneID + zoneDataFileURL := con.PowerDNSurl + PowerDNSServerBaseURL + serverID + "/zones?zone=" + zoneID response, requesterr := httputils.ExecutePowerDNSRequest(http.MethodGet, zoneDataFileURL, apiToken, nil) if requesterr != nil { @@ -79,7 +79,7 @@ func DoesZoneExist(con modelpowerdns.PDNSconnectionobject, zoneID string) (bool, func DeleteZone(con modelpowerdns.PDNSconnectionobject, zoneid string) error { serverid := con.ServerID apitoken := con.Apitoken - zoneDataFileURL := con.PowerDNSurl + "/api/v1/servers/" + serverid + "/zones/" + zoneid + zoneDataFileURL := con.PowerDNSurl + PowerDNSServerBaseURL + serverid + PowerDNSZoneURLPath + zoneid _, delerr := httputils.ExecutePowerDNSRequest(http.MethodDelete, zoneDataFileURL, apitoken, nil) if delerr != nil { @@ -92,7 +92,7 @@ func DeleteZone(con modelpowerdns.PDNSconnectionobject, zoneid string) error { func CreateZone(con modelpowerdns.PDNSconnectionobject, storepayload []byte) error { serverid := con.ServerID apitoken := con.Apitoken - zoneDataFileURL := con.PowerDNSurl + "/api/v1/servers/" + serverid + "/zones?rrsets=false" + zoneDataFileURL := con.PowerDNSurl + PowerDNSServerBaseURL + serverid + "/zones?rrsets=false" // T8D8 _, storeerr := httputils.ExecutePowerDNSRequest(http.MethodPost, zoneDataFileURL, apitoken, @@ -107,7 +107,7 @@ func CreateZone(con modelpowerdns.PDNSconnectionobject, storepayload []byte) err func ClearCache(zoneid string, con modelpowerdns.PDNSconnectionobject) { serverid := con.ServerID apitoken := con.Apitoken - zoneDataFileURL := con.PowerDNSurl + "/api/v1/servers/" + serverid + "/cache/flush?domain=" + zoneid + zoneDataFileURL := con.PowerDNSurl + PowerDNSServerBaseURL + serverid + "/cache/flush?domain=" + zoneid _, clearerr := httputils.ExecutePowerDNSRequest(http.MethodPut, zoneDataFileURL, apitoken, nil) if clearerr != nil { @@ -118,7 +118,7 @@ func ClearCache(zoneid string, con modelpowerdns.PDNSconnectionobject) { func GetZoneMetaData(con modelpowerdns.PDNSconnectionobject, zoneid, metadatakind string) (string, error) { serverid := con.ServerID apitoken := con.Apitoken - zoneDataFileURL := con.PowerDNSurl + "/api/v1/servers/" + serverid + "/zones/" + zoneid + "/metadata/" + metadatakind + zoneDataFileURL := con.PowerDNSurl + PowerDNSServerBaseURL + serverid + PowerDNSZoneURLPath + zoneid + "/metadata/" + metadatakind resp, storeerr := httputils.ExecutePowerDNSRequest(http.MethodGet, zoneDataFileURL, apitoken, nil) if storeerr != nil { @@ -131,7 +131,7 @@ func GetZoneMetaData(con modelpowerdns.PDNSconnectionobject, zoneid, metadatakin func RectifyZone(con modelpowerdns.PDNSconnectionobject, zoneid string) error { serverid := con.ServerID apitoken := con.Apitoken - zoneDataFileURL := con.PowerDNSurl + "/api/v1/servers/" + serverid + "/zones/" + zoneid + "/rectify" + zoneDataFileURL := con.PowerDNSurl + PowerDNSServerBaseURL + serverid + PowerDNSZoneURLPath + zoneid + "/rectify" _, storeerr := httputils.ExecutePowerDNSRequest(http.MethodPut, zoneDataFileURL, apitoken, nil) if storeerr != nil { @@ -144,7 +144,7 @@ func RectifyZone(con modelpowerdns.PDNSconnectionobject, zoneid string) error { func AXFRRetrieve(con modelpowerdns.PDNSconnectionobject, zoneid string) error { serverid := con.ServerID apitoken := con.Apitoken - zoneDataFileURL := con.PowerDNSurl + "/api/v1/servers/" + serverid + "/zones/" + zoneid + "/axfr-retrieve" + zoneDataFileURL := con.PowerDNSurl + PowerDNSServerBaseURL + serverid + PowerDNSZoneURLPath + zoneid + "/axfr-retrieve" _, storeerr := httputils.ExecutePowerDNSRequest(http.MethodPut, zoneDataFileURL, apitoken, nil) if storeerr != nil { @@ -157,7 +157,7 @@ func AXFRRetrieve(con modelpowerdns.PDNSconnectionobject, zoneid string) error { func SetZoneMetaData(con modelpowerdns.PDNSconnectionobject, zoneid string, storepayload []byte) error { serverid := con.ServerID apitoken := con.Apitoken - zoneDataFileURL := con.PowerDNSurl + "/api/v1/servers/" + serverid + "/zones/" + zoneid + "/metadata" + zoneDataFileURL := con.PowerDNSurl + PowerDNSServerBaseURL + serverid + PowerDNSZoneURLPath + zoneid + "/metadata" _, storeerr := httputils.ExecutePowerDNSRequest(http.MethodPost, zoneDataFileURL, apitoken, bytes.NewBuffer(storepayload)) diff --git a/pkg/microservice/servicediscovery/servicediscovery_test.go b/pkg/microservice/servicediscovery/servicediscovery_test.go index d0c36ff0..09086505 100644 --- a/pkg/microservice/servicediscovery/servicediscovery_test.go +++ b/pkg/microservice/servicediscovery/servicediscovery_test.go @@ -6,6 +6,7 @@ import ( consul "github.com/hashicorp/consul/api" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestServiceDiscovery_newClient(t *testing.T) { @@ -21,7 +22,7 @@ func TestServiceDiscovery_newClient(t *testing.T) { err := sd.newClient() - assert.NoError(t, err) + require.NoError(t, err) }) t.Run("fail_client_exists", func(t *testing.T) { @@ -36,7 +37,7 @@ func TestServiceDiscovery_newClient(t *testing.T) { err := sd.newClient() - assert.NoError(t, err) + require.NoError(t, err) }) } diff --git a/pkg/microservice/servicediscovery/serviceregistration_test.go b/pkg/microservice/servicediscovery/serviceregistration_test.go index 1b1609e5..1c2a45e9 100644 --- a/pkg/microservice/servicediscovery/serviceregistration_test.go +++ b/pkg/microservice/servicediscovery/serviceregistration_test.go @@ -27,14 +27,14 @@ func TestServiceRegistration_generateServiceDiscoveryRegistration(t *testing.T) require.NoError(t, err) require.NotNil(t, actual) - assert.Equal(t, actual, api.AgentServiceRegistration{ + assert.Equal(t, api.AgentServiceRegistration{ ID: "a1b2-c3d4-e5f6", Name: "testservice", Tags: []string{"test", "test2"}, Port: 4321, Address: "test.local", Meta: map[string]string{"test": "TEST"}, - }) + }, actual) }) t.Run("fail", func(t *testing.T) { @@ -55,7 +55,7 @@ func TestServiceRegistration_generateServiceDiscoveryRegistration(t *testing.T) require.Error(t, err) require.NotNil(t, actual) - assert.Equal(t, actual, api.AgentServiceRegistration{}) + assert.Equal(t, api.AgentServiceRegistration{}, actual) }) t.Run("port_parsing", func(t *testing.T) { sr := ServiceRegistration{ @@ -74,7 +74,7 @@ func TestServiceRegistration_generateServiceDiscoveryRegistration(t *testing.T) require.Error(t, err) require.NotNil(t, actual) - assert.Equal(t, actual, api.AgentServiceRegistration{}) + assert.Equal(t, api.AgentServiceRegistration{}, actual) }) }) } diff --git a/pkg/microservice/utils/utils_test.go b/pkg/microservice/utils/utils_test.go index ec6ec0bb..59174274 100644 --- a/pkg/microservice/utils/utils_test.go +++ b/pkg/microservice/utils/utils_test.go @@ -14,21 +14,21 @@ func Test_convertStringToInt(t *testing.T) { t.Run("normal", func(t *testing.T) { num, err := ConvertStringToInt("5") - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, 5, num) }) t.Run("negative", func(t *testing.T) { num, err := ConvertStringToInt("-5") - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, -5, num) }) t.Run("zero", func(t *testing.T) { num, err := ConvertStringToInt("0") - assert.NoError(t, err) + require.NoError(t, err) assert.Zero(t, num) }) }) @@ -37,14 +37,14 @@ func Test_convertStringToInt(t *testing.T) { t.Run("no_number", func(t *testing.T) { num, err := ConvertStringToInt("abcd5") - assert.Error(t, err) + require.Error(t, err) assert.Empty(t, num) }) t.Run("empty", func(t *testing.T) { num, err := ConvertStringToInt("") - assert.Error(t, err) + require.Error(t, err) assert.Empty(t, num) }) }) @@ -113,7 +113,7 @@ func Test_GenerateUUID(t *testing.T) { t.Run("ok", func(t *testing.T) { id, err := GenerateUUID() - assert.NoError(t, err) + require.NoError(t, err) assert.NotNil(t, id) assert.Len(t, id, 36) }) @@ -139,14 +139,14 @@ func Test_EnsurePathExist(t *testing.T) { err := EnsurePathExist(newTmpDir) - assert.NoError(t, err) + require.NoError(t, err) assert.DirExists(t, newTmpDir) }) t.Run("dir_exists", func(t *testing.T) { err := EnsurePathExist(tmpDir) - assert.NoError(t, err) + require.NoError(t, err) assert.DirExists(t, tmpDir) }) }) @@ -155,7 +155,7 @@ func Test_EnsurePathExist(t *testing.T) { t.Run("no_path_given", func(t *testing.T) { err := EnsurePathExist("") - assert.EqualError(t, err, "mkdir : no such file or directory") + require.EqualError(t, err, "mkdir : no such file or directory") }) }) }