-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add domain nameservers checking hooks
- Loading branch information
Showing
7 changed files
with
190 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
[bumpversion] | ||
current_version = 1.2.2 | ||
current_version = 1.3.0 | ||
|
||
[bumpversion:file:setup.cfg] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
"""Script that checks if the nameservers of a domain ends with a defined string.""" | ||
|
||
import argparse | ||
import sys | ||
|
||
import dns.resolver | ||
|
||
|
||
def nameservers_endswith(domain, nameserver, quiet=False): | ||
"""Check that the nameservers of a domain endswith a strip after stripping | ||
it all newlines and points. | ||
Parameters | ||
---------- | ||
domain : str | ||
Domain whose nameserves will be checked. | ||
nameserver: str | ||
String that should match with the end of the string for the nameservers | ||
handlers of the domain. For example, passing ``cloudflare.com`` you | ||
can check if a domain is handled by Cloudflare. | ||
quiet : bool, optional | ||
Don't print the error in STDERR if a nameserver doesn't match. | ||
""" | ||
response = True | ||
|
||
for ns in sorted(dns.resolver.resolve(domain, "NS")): | ||
ns_ = ns.to_text().strip().strip(".") | ||
if not ns_.endswith(nameserver): | ||
if not quiet: | ||
sys.stderr.write( | ||
f"Found invalid nameserver '{ns_}' for domain '{domain}'.\n" | ||
) | ||
response = False | ||
return response | ||
|
||
|
||
def main(): | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument("-q", "--quiet", action="store_true", help="Supress output") | ||
parser.add_argument( | ||
"-d", | ||
"-domain", | ||
"--domain", | ||
type=str, | ||
metavar="DOMAIN", | ||
required=True, | ||
default=None, | ||
dest="domain", | ||
help="Domain which nameservers will be checked.", | ||
) | ||
parser.add_argument( | ||
"-ns", | ||
"-nameserver", | ||
"--nameserver", | ||
type=str, | ||
metavar="end", | ||
required=True, | ||
default=None, | ||
dest="nameserver", | ||
help=( | ||
"End of the string that must match the nameservers handlers" | ||
" of the domain." | ||
), | ||
) | ||
args = parser.parse_args() | ||
|
||
return ( | ||
0 if nameservers_endswith(args.domain, args.nameserver, quiet=args.quiet) else 1 | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
sys.exit(main()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
"""Tests for 'nameserver-endswith' hook.""" | ||
|
||
import contextlib | ||
import io | ||
|
||
import pytest | ||
|
||
from hooks.nameservers_endswith import nameservers_endswith | ||
|
||
|
||
@pytest.mark.parametrize("quiet", (True, False), ids=("quiet=True", "quiet=False")) | ||
@pytest.mark.parametrize( | ||
("domain", "nameserver", "expected_result", "expected_stderr"), | ||
( | ||
pytest.param( | ||
"mkdocs-mdpo.ga", | ||
"cloudflare.com", | ||
True, | ||
"", | ||
id="domain=mkdocs-mdpo.ga-nameserver=cloudflare.com", | ||
), | ||
pytest.param( | ||
"mkdocs-mdpo.ga", | ||
"cloudflare.co", | ||
False, | ||
( | ||
"Found invalid nameserver 'irma.ns.cloudflare.com' for domain" | ||
" 'mkdocs-mdpo.ga'.\nFound invalid nameserver" | ||
" 'craig.ns.cloudflare.com' for domain 'mkdocs-mdpo.ga'.\n" | ||
), | ||
id="domain=mkdocs-mdpo.ga-nameserver=cloudflare.co", | ||
), | ||
), | ||
) | ||
def test_nameservers_endswith( | ||
domain, nameserver, expected_result, expected_stderr, quiet | ||
): | ||
stderr = io.StringIO() | ||
|
||
with contextlib.redirect_stderr(stderr): | ||
result = nameservers_endswith( | ||
domain, | ||
nameserver, | ||
quiet=quiet, | ||
) | ||
|
||
assert result is expected_result | ||
|
||
if not quiet: | ||
assert expected_stderr == stderr.getvalue() | ||
else: | ||
assert not stderr.getvalue() |