knotctl/knotctl/__init__.py

588 lines
20 KiB
Python
Raw Normal View History

2022-10-23 10:19:03 +02:00
#!/usr/bin/env python3
import argparse
2022-10-24 13:24:38 +02:00
import getpass
2022-10-24 19:13:24 +02:00
import os
2022-10-24 11:11:41 +02:00
import sys
2022-10-23 10:19:03 +02:00
from typing import Union
import argcomplete
import requests
from requests.models import HTTPBasicAuth
2022-10-25 09:10:11 +02:00
from simplejson.errors import JSONDecodeError as SimplejsonJSONDecodeError
2024-09-30 16:34:34 +02:00
2025-01-17 11:44:01 +01:00
from .config import Config
from .openstack import get_openstack_addresses
from .utils import error, output, setup_url, split_url
try:
from requests.exceptions import JSONDecodeError as RequestsJSONDecodeError
except ImportError:
from requests.exceptions import InvalidJSONError as RequestsJSONDecodeError
2022-10-23 10:19:03 +02:00
2025-01-17 11:44:01 +01:00
class Knotctl:
def __init__(self):
self.conf = Config()
self.config = self.get_config()
self.config_filename = self.conf.config_filename
def get_config(self):
config = self.conf.get_config()
if not config:
print("You need to configure knotctl before proceeding")
run_config()
return config
2022-10-24 19:13:24 +02:00
# Define the runner for each command
2022-10-24 13:24:38 +02:00
def run_add(url: str, jsonout: bool, headers: dict):
parsed = split_url(url)
2022-10-24 11:11:41 +02:00
response = requests.put(url, headers=headers)
out = response.json()
if isinstance(out, list):
for record in out:
if (record["data"] == parsed["data"]
and record["name"] == parsed["name"]
and record["rtype"] == parsed["rtype"]):
output(record, jsonout)
break
else:
output(out, jsonout)
2022-10-23 10:19:03 +02:00
def run_log(url: str, jsonout: bool, headers: dict):
2024-09-30 16:34:34 +02:00
response = requests.get(url, headers=headers)
string = response.content.decode("utf-8")
if jsonout:
out = []
lines = string.splitlines()
index = 0
text = ""
timestamp = ""
while index < len(lines):
line = lines[index]
index += 1
cur_has_timestamp = line.startswith("[")
next_has_timestamp = index < len(
lines) and lines[index].startswith("[")
2024-09-30 16:34:34 +02:00
# Simple case, just one line with timestamp
if cur_has_timestamp and next_has_timestamp:
timestamp = line.split("]")[0].split("[")[1]
text = line.split("]")[1].lstrip(":").strip()
out.append({"timestamp": timestamp, "text": text})
2024-09-30 16:34:34 +02:00
text = ""
timestamp = ""
2024-09-30 16:43:35 +02:00
# Start of multiline
2024-09-30 16:34:34 +02:00
elif cur_has_timestamp:
timestamp = line.split("]")[0].split("[")[1]
text = line.split("]")[1].lstrip(":").strip()
2024-09-30 16:43:35 +02:00
# End of multiline
2024-09-30 16:34:34 +02:00
elif next_has_timestamp:
text += f"\n{line.strip()}"
out.append({"timestamp": timestamp, "text": text})
2024-09-30 16:34:34 +02:00
text = ""
timestamp = ""
2024-09-30 16:43:35 +02:00
# Middle of multiline
2024-09-30 16:34:34 +02:00
else:
text += f"\n{line.strip()}"
2024-09-30 16:34:34 +02:00
else:
out = string
output(out, jsonout)
2022-10-24 19:13:24 +02:00
def run_complete(shell: Union[None, str]):
if not shell or shell in ["bash", "zsh"]:
os.system("register-python-argcomplete knotctl")
elif shell == "fish":
os.system("register-python-argcomplete --shell fish knotctl")
elif shell == "tcsh":
2022-10-25 08:27:48 +02:00
os.system("register-python-argcomplete --shell tcsh knotctl")
2022-10-24 19:13:24 +02:00
2022-10-24 13:24:38 +02:00
def run_config(
context: Union[None, str] = None,
2022-10-24 13:24:38 +02:00
baseurl: Union[None, str] = None,
2024-08-30 11:21:27 +02:00
list_config: bool = False,
2022-10-24 13:24:38 +02:00
username: Union[None, str] = None,
password: Union[None, str] = None,
2024-09-30 17:00:38 +02:00
current: Union[None, str] = None,
2022-10-24 13:24:38 +02:00
):
2025-01-17 11:44:01 +01:00
conf = Config()
2024-09-30 17:00:38 +02:00
if current:
2025-01-17 11:44:01 +01:00
print(conf.get_current())
2024-09-30 17:00:38 +02:00
return
2022-10-24 13:24:38 +02:00
config = {"baseurl": baseurl, "username": username, "password": password}
needed = []
if context:
2025-01-17 11:44:01 +01:00
found = conf.set_context(context)
if found:
return
2024-08-30 11:21:27 +02:00
if list_config:
2025-01-17 11:44:01 +01:00
config_data = conf.get_config_data()
2024-08-30 11:21:27 +02:00
output(config_data)
return
2022-10-24 13:24:38 +02:00
if not baseurl:
needed.append("baseurl")
if not username:
needed.append("username")
for need in needed:
if need == "":
output(
2023-01-09 08:38:09 +01:00
error(
"Can not configure without {}".format(need),
"No {}".format(need),
))
2022-10-24 13:24:38 +02:00
sys.exit(1)
2023-01-09 08:38:44 +01:00
config[need] = input("Enter {}: ".format(need))
2022-10-24 13:24:38 +02:00
if not password:
try:
config["password"] = getpass.getpass()
except EOFError:
output(error("Can not configure without password", "No password"))
sys.exit(1)
2025-01-17 11:44:01 +01:00
conf.set_config(config)
2022-10-24 13:24:38 +02:00
def run_delete(url: str, jsonout: bool, headers: dict):
2022-10-24 11:11:41 +02:00
response = requests.delete(url, headers=headers)
2022-10-24 17:49:22 +02:00
reply = response.json()
if not reply and response.status_code == requests.codes.ok:
reply = [{"Code": 200, "Description": "{} deleted".format(url)}]
output(reply, jsonout)
2022-10-24 11:11:41 +02:00
2022-10-23 10:19:03 +02:00
def run_list(url: str,
jsonout: bool,
headers: dict,
ret=False) -> Union[None, str]:
2022-10-24 11:11:41 +02:00
response = requests.get(url, headers=headers)
2022-11-10 12:50:18 +01:00
string = response.json()
if ret:
return string
else:
output(string, jsonout)
2022-10-23 10:19:03 +02:00
2022-10-24 11:11:41 +02:00
def run_openstack_sync(cloud: str, name: str, zone: str, headers: dict,
baseurl: str, jsonout: bool):
url = setup_url(
baseurl,
2025-01-10 05:32:22 +01:00
None, # arguments,
None, # data,
name,
None, # rtype,
None, # ttl,
zone,
)
current_records = run_list(url, jsonout=True, headers=headers, ret=True)
openstack_addresses = get_openstack_addresses(cloud, name)
2025-01-10 05:32:22 +01:00
if current_records["Code"] == 404:
for address in openstack_addresses:
rtype = None
2025-01-10 05:32:22 +01:00
if address["version"] == 4:
rtype = "A"
2025-01-10 05:32:22 +01:00
elif address["version"] == 6:
rtype = "AAAA"
if rtype:
2025-01-10 05:32:22 +01:00
url = setup_url(
baseurl,
None, # arguments,
address["addr"], # data,
name,
rtype,
None, # ttl,
zone,
)
run_add(url, jsonout, headers)
else:
2025-01-10 05:32:22 +01:00
previpv4 = False
previpv6 = False
curripv4 = False
curripv6 = False
for record in current_records:
2025-01-10 05:32:22 +01:00
if record.type == "A":
previpv4 = record.data
elif record.type == "AAAA":
previpv6 = record.data
for address in openstack_addresses:
rtype = None
if address.version == 4:
rtype = "A"
curripv4 = True
elif address.version == 6:
rtype = "AAAA"
curripv6 = True
2025-01-17 11:44:01 +01:00
if rtype and record.type == rtype:
2025-01-10 05:32:22 +01:00
if record.data == address.addr:
continue
else:
2025-01-10 05:32:22 +01:00
url = setup_url(
baseurl,
None, # arguments,
address.addr, # data,
name,
record.type,
None, # ttl,
zone,
)
run_update(url, jsonout, headers)
if previpv4 and not curripv4:
2025-01-10 05:32:22 +01:00
url = setup_url(
baseurl,
None, # arguments,
previpv4, # data,
name,
"A",
None, # ttl,
zone,
)
run_delete(url, jsonout, headers)
if previpv6 and not curripv6:
2025-01-10 05:32:22 +01:00
url = setup_url(
baseurl,
None, # arguments,
previpv6, # data,
name,
"AAAA",
None, # ttl,
zone,
)
run_delete(url, jsonout, headers)
if curripv4 and not previpv4:
2025-01-10 05:32:22 +01:00
url = setup_url(
baseurl,
None, # arguments,
curripv4, # data,
name,
"A",
None, # ttl,
zone,
)
run_add(url, jsonout, headers)
if curripv6 and not previpv6:
2025-01-10 05:32:22 +01:00
url = setup_url(
baseurl,
None, # arguments,
curripv6, # data,
name,
"AAAA",
None, # ttl,
zone,
)
run_add(url, jsonout, headers)
2022-10-24 13:24:38 +02:00
def run_update(url: str, jsonout: bool, headers: dict):
2022-10-24 11:11:41 +02:00
response = requests.patch(url, headers=headers)
output(response.json(), jsonout)
def run_zone(url: str,
jsonout: bool,
headers: dict,
ret=False) -> Union[None, str]:
response = requests.get(url, headers=headers)
zones = response.json()
for zone in zones:
del zone["records"]
string = zones
if ret:
return string
else:
output(string, jsonout)
def get_parser() -> dict:
description = """Manage DNS records with knot dns rest api:
* https://gitlab.nic.cz/knot/knot-dns-rest"""
epilog = """
The Domain Name System specifies a database of information
elements for network resources. The types of information
elements are categorized and organized with a list of DNS
record types, the resource records (RRs). Each record has a
name, a type, an expiration time (time to live), and
type-specific data.
The following is a list of terms used in this program:
----------------------------------------------------------------
| Vocabulary | Description |
----------------------------------------------------------------
| zone | A DNS zone is a specific portion of the DNS |
| | namespace in the Domain Name System (DNS), |
| | which a specific organization or administrator |
| | manages. |
----------------------------------------------------------------
| name | In the Internet, a domain name is a string that |
| | identifies a realm of administrative autonomy, |
| | authority or control. Domain names are often |
| | used to identify services provided through the |
| | Internet, such as websites, email services and |
| | more. |
----------------------------------------------------------------
| rtype | A record type indicates the format of the data |
| | and it gives a hint of its intended use. For |
| | example, the A record is used to translate from |
| | a domain name to an IPv4 address, the NS record |
| | lists which name servers can answer lookups on |
| | a DNS zone, and the MX record specifies the |
| | mail server used to handle mail for a domain |
| | specified in an e-mail address. |
----------------------------------------------------------------
| data | A records data is of type-specific relevance, |
| | such as the IP address for address records, or |
| | the priority and hostname for MX records. |
----------------------------------------------------------------
This information was compiled from Wikipedia:
* https://en.wikipedia.org/wiki/DNS_zone
* https://en.wikipedia.org/wiki/Domain_Name_System
* https://en.wikipedia.org/wiki/Zone_file
"""
2022-10-24 13:24:38 +02:00
# Grab user input
2024-09-30 16:34:34 +02:00
parser = argparse.ArgumentParser(
description=description,
epilog=epilog,
formatter_class=argparse.RawDescriptionHelpFormatter,
)
2022-10-24 13:24:38 +02:00
parser.add_argument("--json", action=argparse.BooleanOptionalAction)
subparsers = parser.add_subparsers(dest="command")
add_description = "Add a new record to the zone."
addcmd = subparsers.add_parser("add", description=add_description)
2022-10-24 17:49:22 +02:00
addcmd.add_argument("-d", "--data", required=True)
addcmd.add_argument("-n", "--name", required=True)
addcmd.add_argument("-r", "--rtype", required=True)
2022-11-10 12:50:18 +01:00
addcmd.add_argument("-t", "--ttl")
2022-10-24 17:49:22 +02:00
addcmd.add_argument("-z", "--zone", required=True)
auditlog_description = "Audit the log file for errors."
subparsers.add_parser("auditlog", description=auditlog_description)
changelog_description = "View the changelog of a zone."
changelogcmd = subparsers.add_parser("changelog",
description=changelog_description)
changelogcmd.add_argument("-z", "--zone", required=True)
2024-09-30 16:34:34 +02:00
complete_description = "Generate shell completion script."
completecmd = subparsers.add_parser("completion",
description=complete_description)
2022-10-24 19:13:24 +02:00
completecmd.add_argument("-s", "--shell")
config_description = "Configure access to knot-dns-rest-api."
configcmd = subparsers.add_parser("config", description=config_description)
2022-10-24 17:49:22 +02:00
configcmd.add_argument("-b", "--baseurl")
configcmd.add_argument("-c", "--context")
configcmd.add_argument("-C",
"--current",
action=argparse.BooleanOptionalAction)
2025-01-10 05:11:23 +01:00
configcmd.add_argument("-l",
"--list",
action=argparse.BooleanOptionalAction,
dest="list_config")
2022-10-24 17:49:22 +02:00
configcmd.add_argument("-p", "--password")
configcmd.add_argument("-u", "--username")
delete_description = "Delete a record from the zone."
deletecmd = subparsers.add_parser("delete", description=delete_description)
2022-10-24 17:49:22 +02:00
deletecmd.add_argument("-d", "--data")
deletecmd.add_argument("-n", "--name")
deletecmd.add_argument("-r", "--rtype")
deletecmd.add_argument("-z", "--zone", required=True)
list_description = "List records."
listcmd = subparsers.add_parser("list", description=list_description)
2022-10-24 17:49:22 +02:00
listcmd.add_argument("-d", "--data")
listcmd.add_argument("-n", "--name")
listcmd.add_argument("-r", "--rtype")
listcmd.add_argument("-z", "--zone", required=False)
2022-10-24 17:49:22 +02:00
openstack_description = "Sync records with openstack."
openstackcmd = subparsers.add_parser("openstack-sync",
description=openstack_description)
openstackcmd.add_argument("-n", "--name", required=True)
openstackcmd.add_argument("-c", "--cloud", required=True)
openstackcmd.add_argument("-z", "--zone", required=True)
user_description = "View user information."
usercmd = subparsers.add_parser("user", description=user_description)
usercmd.add_argument("-u", "--username", default=None)
2024-09-30 16:34:34 +02:00
update_description = (
"Update a record in the zone. The record must exist in the zone.\n")
2024-09-30 16:34:34 +02:00
update_description += (
"In this case --data, --name, --rtype and --ttl switches are used\n")
2024-09-30 16:34:34 +02:00
update_description += (
"for searching for the appropriate record, while the --argument\n")
update_description += "switches are used for updating the record."
update_epilog = """Available arguments are:
data: New record data.
name: New record domain name.
rtype: New record type.
ttl: New record time to live (TTL)."""
2024-09-30 16:34:34 +02:00
updatecmd = subparsers.add_parser(
"update",
description=update_description,
epilog=update_epilog,
formatter_class=argparse.RawDescriptionHelpFormatter,
)
2022-10-27 15:47:54 +02:00
updatecmd.add_argument(
"-a",
"--argument",
action="append",
metavar="KEY=VALUE",
help="Specify key - value pairs to be updated: name=dns1.example.com."
+ " or data=127.0.0.1 for example. --argument can be repeated",
2022-10-27 15:47:54 +02:00
required=True,
)
2022-10-24 17:49:22 +02:00
updatecmd.add_argument("-d", "--data", required=True)
updatecmd.add_argument("-n", "--name", required=True)
updatecmd.add_argument("-r", "--rtype", required=True)
2022-10-27 15:47:54 +02:00
updatecmd.add_argument("-t", "--ttl")
2022-10-24 17:49:22 +02:00
updatecmd.add_argument("-z", "--zone", required=True)
2022-10-24 13:24:38 +02:00
zone_description = "View zones."
subparsers.add_parser("zone", description=zone_description)
2022-10-24 13:24:38 +02:00
argcomplete.autocomplete(parser)
return parser
def get_token(config) -> str:
# Authenticate
baseurl = config["baseurl"]
username = config["username"]
password = config["password"]
basic = HTTPBasicAuth(username, password)
response = requests.get(baseurl + "/user/login", auth=basic)
token = ""
try:
token = response.json()["token"]
except KeyError:
output(response.json())
except requests.exceptions.JSONDecodeError:
output(
error("Could not decode api response as JSON", "Could not decode"))
return token
def run(url, args, headers, baseurl, parser, username):
try:
if args.command == "add":
run_add(url, args.json, headers)
elif args.command == "delete":
run_delete(url, args.json, headers)
elif args.command == "list":
run_list(url, args.json, headers)
elif args.command == "update":
run_update(url, args.json, headers)
elif args.command == "user":
url = baseurl + f"/user/info/{username}"
run_list(url, args.json, headers)
elif args.command == "auditlog":
url = baseurl + "/user/auditlog"
run_log(url, args.json, headers)
elif args.command == "changelog":
url = baseurl + f"/zones/changelog/{args.zone.rstrip('.')}"
run_log(url, args.json, headers)
elif args.command == "zone":
url = baseurl + "/zones"
run_zone(url, args.json, headers)
elif args.command == "openstack-sync":
run_openstack_sync(args.cloud, args.name, args.zone, headers,
baseurl, args.json)
else:
parser.print_help(sys.stderr)
return 2
except requests.exceptions.RequestException as e:
output(error(e, "Could not connect to server"))
except (RequestsJSONDecodeError, SimplejsonJSONDecodeError):
output(
error("Could not decode api response as JSON", "Could not decode"))
return 0
# Entry point to program
def main() -> int:
parser = get_parser()
2022-10-24 13:24:38 +02:00
args = parser.parse_args()
if args.command == "completion":
2022-10-24 19:13:24 +02:00
run_complete(args.shell)
return 0
2025-01-17 11:44:01 +01:00
knotctl = Knotctl()
2022-10-24 19:13:24 +02:00
2022-10-25 08:27:48 +02:00
if args.command == "config":
2024-09-30 16:34:34 +02:00
run_config(
args.context,
args.baseurl,
2025-01-10 05:11:23 +01:00
args.list_config,
args.username,
args.password,
args.current,
2024-09-30 16:34:34 +02:00
)
2022-10-25 08:27:48 +02:00
return 0
2025-01-17 11:44:01 +01:00
config = knotctl.get_config()
2022-10-24 19:13:24 +02:00
baseurl = config["baseurl"]
token = get_token(config)
if token == "":
print("Could not get token, exiting")
2023-06-26 09:18:26 +02:00
return 1
2022-10-24 19:13:24 +02:00
headers = {"Authorization": "Bearer {}".format(token)}
2022-10-24 13:24:38 +02:00
# Route based on command
2024-10-02 10:53:37 +02:00
url = ""
2022-10-24 17:49:22 +02:00
ttl = None
user = config["username"]
2023-01-09 08:38:09 +01:00
if "ttl" in args:
2022-10-24 17:49:22 +02:00
ttl = args.ttl
2022-11-10 09:54:38 +01:00
if args.command != "update":
args.argument = None
2022-11-10 12:50:18 +01:00
if args.command == "add" and not ttl:
2023-01-09 08:38:09 +01:00
if args.zone.endswith("."):
2022-11-10 12:50:18 +01:00
zname = args.zone
else:
2023-01-09 08:38:09 +01:00
zname = args.zone + "."
soa_url = setup_url(baseurl, None, None, zname, "SOA", None, args.zone)
2022-11-10 12:50:18 +01:00
soa_json = run_list(soa_url, True, headers, ret=True)
ttl = soa_json[0]["ttl"]
if args.command == "user":
if args.username:
user = args.username
if args.command in [
"auditlog", "changelog", "openstack-sync", "user", "zone"
]:
pass
2024-09-30 16:34:34 +02:00
else:
try:
url = setup_url(
baseurl,
args.argument,
args.data,
args.name,
args.rtype,
ttl,
args.zone,
)
except AttributeError:
parser.print_help(sys.stderr)
return 1
2022-11-10 09:06:25 +01:00
return run(url, args, headers, baseurl, parser, user)
2022-10-24 13:24:38 +02:00
if __name__ == "__main__":
sys.exit(main())