python-powerdns icon indicating copy to clipboard operation
python-powerdns copied to clipboard

Fix suggest zone to make sure the zone name is a dns component

Open jschewebbn opened this issue 3 years ago • 1 comments

Need to include '.' before the name to keep from picking a zone based on the end of the hostname that isn't the subdomain.

This fixes #30

jschewebbn avatar Aug 09 '22 19:08 jschewebbn

This would introduce another edge case: Apex records/Root records (where the record's FQDN is the same as the zone's)

api_client = PDNSApiClient(...)

server = api_client.servers[0]
zone = server.create_zone('xxx.yyy.zzz.', ...)

# Would now return None instead of 'zone'
server.suggest_zone('xxx.yyy.zzz.')

Though you could probably get around that by either checking for equality in a 2nd step or by prepending a . to the record as well 😉

Another way to implement this would be splitting like

 def suggest_zone(self, r_name: str):
        LOG.info("suggesting zone for: %s", r_name)
        if not r_name.endswith('.'):
            raise PDNSCanonicalError(r_name)
        
        best_match = None
        record_name_split = list(reversed(r_name.split('.')))

        for zone in self.pdns_server.zones:
            zone_name_split = list(reversed(zone.name.split('.')))

            if zone_name_split == record_name_split[: len(zone_name_split)]:
                if not best_match:
                    best_match = zone
                if best_match and len(zone.name) > len(best_match.name):
                    best_match = zone
        logger.info("zone best match: %s", best_match)
        return best_match

I'm not sure, however, if this project is still maintained...

kindlich avatar Nov 25 '22 16:11 kindlich