Commit a6481e45 authored by Aleš Mrázek's avatar Aleš Mrázek
Browse files

removal load and write functions from package and move them to examples scripts

parent 75a9f8a4
......@@ -21,12 +21,12 @@
project = 'Resolvers-YANG'
copyright = '2018, CZ.NIC, z. s. p. o.'
author = 'Aleš Mrázek'
author = 'Ales Mrazek'
# The short X.Y version
version = ''
version = '0.1'
# The full version, including alpha/beta/rc tags
release = '0.1'
release = '0.1beta'
# -- General configuration ---------------------------------------------------
......
{
"cznic-deckard:deckard":{
"mock-data": "mock-data/iter_badglue.rpl"
"mock-data": "mock-data/iter_cycle_noh.rpl"
},
"cznic-resolver-common:dns-resolver": {
"server": {
......
import sys
from yangson.datamodel import DataModel
from json import load
from resolvers_yang.generator import Generator
......@@ -13,28 +14,35 @@ unb_path = "unbound.rpl"
# load model
model = DataModel.from_file(yangdir + "/yanglib-deckard.json", [yangdir])
# create configuration generator for specific data model
conf_generator = Generator(model)
# load data from json
with open(json_path) as infile:
ri = load(infile)
# load, validate data from json
conf_data = conf_generator.load_from_json(json_path)
data = model.from_raw(ri)
# validate against data model
data.validate()
# get path where is mock data located
mock_path = conf_data["cznic-deckard:deckard"]["mock-data"].value
mock_path = data["cznic-deckard:deckard"]["mock-data"].value
# load text file from path as string
# load mock data text file from path as string
mock_data = open(mock_path).read()
# write configurations to files
unb_conf = conf_generator.unbound(conf_data)
kresd_conf = conf_generator.kresd(conf_data)
# slicing mock_data
mock_begin = mock_data.find("SCENARIO_BEGIN")
mock_data = mock_data[mock_begin:]
# generate configuration strings
unb_conf = Generator.unbound(data)
kresd_conf = Generator.kresd(data)
# write kresd.conf
knot_file = open(kresd_path, "w+")
knot_file.write(kresd_conf)
knot_file.close()
# write unbound.rpl
# write unbound.rpl = unbconf string + mock data
unb_file = open(unb_path, "w+")
unb_file.write(unb_conf + "\nCONFIG_END\n\n" + mock_data)
unb_file.close()
; config options
harden-glue: "no"
; target-fetch-policy: "0 0 0 0 0"
; name: "."
stub-addr: 193.0.14.129 # K.ROOT-SERVERS.NET.
CONFIG_END
SCENARIO_BEGIN Test resolution where disabled scrubber avoids cycle
; query for ns.example.com, needs ns.example.net, needs ns.example.com.
; scrubber disabled, so extra glue is trusted.
; K.ROOT-SERVERS.NET.
RANGE_BEGIN 0 100
ADDRESS 193.0.14.129
ENTRY_BEGIN
MATCH opcode qtype qname
ADJUST copy_id
REPLY QR NOERROR
SECTION QUESTION
. IN NS
SECTION ANSWER
. IN NS K.ROOT-SERVERS.NET.
SECTION ADDITIONAL
K.ROOT-SERVERS.NET. IN A 193.0.14.129
ENTRY_END
ENTRY_BEGIN
MATCH opcode subdomain
ADJUST copy_id copy_query
REPLY QR NOERROR
SECTION QUESTION
com. IN A
SECTION AUTHORITY
com. IN NS a.gtld-servers.net.
SECTION ADDITIONAL
a.gtld-servers.net. IN A 192.5.6.30
ENTRY_END
ENTRY_BEGIN
MATCH opcode subdomain
ADJUST copy_id copy_query
REPLY QR NOERROR
SECTION QUESTION
net. IN A
SECTION AUTHORITY
net. IN NS e.gtld-servers.net.
SECTION ADDITIONAL
e.gtld-servers.net. IN A 192.12.94.30
ENTRY_END
ENTRY_BEGIN
MATCH opcode qtype qname
ADJUST copy_id
REPLY QR NOERROR
SECTION QUESTION
ns.example.net. IN AAAA
SECTION AUTHORITY
net. IN NS e.gtld-servers.net.
SECTION ADDITIONAL
e.gtld-servers.net. IN A 192.12.94.30
ENTRY_END
RANGE_END
; a.gtld-servers.net.
RANGE_BEGIN 0 100
ADDRESS 192.5.6.30
ENTRY_BEGIN
MATCH opcode qtype qname
ADJUST copy_id
REPLY QR NOERROR
SECTION QUESTION
com. IN NS
SECTION ANSWER
com. IN NS a.gtld-servers.net.
SECTION ADDITIONAL
a.gtld-servers.net. IN A 192.5.6.30
ENTRY_END
ENTRY_BEGIN
MATCH opcode subdomain
ADJUST copy_id copy_query
REPLY QR NOERROR
SECTION QUESTION
example.com. IN A
SECTION AUTHORITY
example.com. IN NS ns.example.net.
SECTION ADDITIONAL
ns.example.net. IN A 1.2.3.1
ENTRY_END
; gtld-servers.net.
ENTRY_BEGIN
MATCH opcode qtype qname
ADJUST copy_id
REPLY QR NOERROR
SECTION QUESTION
gtld-servers.net. IN NS
SECTION ANSWER
gtld-servers.net. IN NS a.gtld-servers.net.
gtld-servers.net. IN NS e.gtld-servers.net.
SECTION ADDITIONAL
a.gtld-servers.net. IN A 192.5.6.30
e.gtld-servers.net. IN A 192.12.94.30
ENTRY_END
ENTRY_BEGIN
MATCH opcode qname
ADJUST copy_id
REPLY QR NOERROR
SECTION QUESTION
gtld-servers.net. IN A
SECTION AUTHORITY
gtld-servers.net. IN SOA . . 0 0 0 0 0
ENTRY_END
ENTRY_BEGIN
MATCH opcode qtype qname
ADJUST copy_id
REPLY QR NOERROR
SECTION QUESTION
a.gtld-servers.net. IN A
SECTION ANSWER
a.gtld-servers.net. IN A 192.5.6.30
SECTION ADDITIONAL
ENTRY_END
ENTRY_BEGIN
MATCH opcode qtype qname
ADJUST copy_id
REPLY QR NOERROR
SECTION QUESTION
a.gtld-servers.net. IN AAAA
SECTION AUTHORITY
gtld-servers.net. IN SOA . . 0 0 0 0 0
ENTRY_END
ENTRY_BEGIN
MATCH opcode qtype qname
ADJUST copy_id
REPLY QR NOERROR
SECTION QUESTION
e.gtld-servers.net. IN A
SECTION ANSWER
e.gtld-servers.net. IN A 192.12.94.30
SECTION ADDITIONAL
ENTRY_END
ENTRY_BEGIN
MATCH opcode qtype qname
ADJUST copy_id
REPLY QR NOERROR
SECTION QUESTION
e.gtld-servers.net. IN AAAA
SECTION AUTHORITY
gtld-servers.net. IN SOA . . 0 0 0 0 0
ENTRY_END
; root-servers.net.
ENTRY_BEGIN
MATCH opcode qtype qname
ADJUST copy_id
REPLY QR NOERROR
SECTION QUESTION
root-servers.net. IN NS
SECTION ANSWER
root-servers.net. IN NS k.root-servers.net.
SECTION ADDITIONAL
k.root-servers.net. IN A 193.0.14.129
ENTRY_END
ENTRY_BEGIN
MATCH opcode qname
ADJUST copy_id
REPLY QR NOERROR
SECTION QUESTION
root-servers.net. IN A
SECTION AUTHORITY
root-servers.net. IN SOA . . 0 0 0 0 0
ENTRY_END
ENTRY_BEGIN
MATCH opcode qtype qname
ADJUST copy_id
REPLY QR NOERROR
SECTION QUESTION
k.root-servers.net. IN A
SECTION ANSWER
k.root-servers.net. IN A 193.0.14.129
SECTION ADDITIONAL
ENTRY_END
ENTRY_BEGIN
MATCH opcode qtype qname
ADJUST copy_id
REPLY QR NOERROR
SECTION QUESTION
k.root-servers.net. IN AAAA
SECTION AUTHORITY
root-servers.net. IN SOA . . 0 0 0 0 0
ENTRY_END
RANGE_END
; e.gtld-servers.net.
RANGE_BEGIN 0 100
ADDRESS 192.12.94.30
ENTRY_BEGIN
MATCH opcode qtype qname
ADJUST copy_id
REPLY QR NOERROR
SECTION QUESTION
net. IN NS
SECTION ANSWER
net. IN NS e.gtld-servers.net.
SECTION ADDITIONAL
e.gtld-servers.net. IN A 192.12.94.30
ENTRY_END
ENTRY_BEGIN
MATCH opcode subdomain
ADJUST copy_id copy_query
REPLY QR NOERROR
SECTION QUESTION
example.net. IN A
SECTION AUTHORITY
example.net. IN NS ns.example.com.
SECTION ADDITIONAL
ns.example.com. IN A 1.2.3.2
ENTRY_END
; gtld-servers.net.
ENTRY_BEGIN
MATCH opcode qtype qname
ADJUST copy_id
REPLY QR NOERROR
SECTION QUESTION
gtld-servers.net. IN NS
SECTION ANSWER
gtld-servers.net. IN NS a.gtld-servers.net.
gtld-servers.net. IN NS e.gtld-servers.net.
SECTION ADDITIONAL
a.gtld-servers.net. IN A 192.5.6.30
e.gtld-servers.net. IN A 192.12.94.30
ENTRY_END
ENTRY_BEGIN
MATCH opcode qname
ADJUST copy_id
REPLY QR NOERROR
SECTION QUESTION
gtld-servers.net. IN A
SECTION AUTHORITY
gtld-servers.net. IN SOA . . 0 0 0 0 0
ENTRY_END
ENTRY_BEGIN
MATCH opcode qtype qname
ADJUST copy_id
REPLY QR NOERROR
SECTION QUESTION
a.gtld-servers.net. IN A
SECTION ANSWER
a.gtld-servers.net. IN A 192.5.6.30
SECTION ADDITIONAL
ENTRY_END
ENTRY_BEGIN
MATCH opcode qtype qname
ADJUST copy_id
REPLY QR NOERROR
SECTION QUESTION
a.gtld-servers.net. IN AAAA
SECTION AUTHORITY
gtld-servers.net. IN SOA . . 0 0 0 0 0
ENTRY_END
ENTRY_BEGIN
MATCH opcode qtype qname
ADJUST copy_id
REPLY QR NOERROR
SECTION QUESTION
e.gtld-servers.net. IN A
SECTION ANSWER
e.gtld-servers.net. IN A 192.12.94.30
SECTION ADDITIONAL
ENTRY_END
ENTRY_BEGIN
MATCH opcode qtype qname
ADJUST copy_id
REPLY QR NOERROR
SECTION QUESTION
e.gtld-servers.net. IN AAAA
SECTION AUTHORITY
gtld-servers.net. IN SOA . . 0 0 0 0 0
ENTRY_END
; root-servers.net.
ENTRY_BEGIN
MATCH opcode qtype qname
ADJUST copy_id
REPLY QR NOERROR
SECTION QUESTION
root-servers.net. IN NS
SECTION ANSWER
root-servers.net. IN NS k.root-servers.net.
SECTION ADDITIONAL
k.root-servers.net. IN A 193.0.14.129
ENTRY_END
ENTRY_BEGIN
MATCH opcode qname
ADJUST copy_id
REPLY QR NOERROR
SECTION QUESTION
root-servers.net. IN A
SECTION AUTHORITY
root-servers.net. IN SOA . . 0 0 0 0 0
ENTRY_END
ENTRY_BEGIN
MATCH opcode qtype qname
ADJUST copy_id
REPLY QR NOERROR
SECTION QUESTION
k.root-servers.net. IN A
SECTION ANSWER
k.root-servers.net. IN A 193.0.14.129
SECTION ADDITIONAL
ENTRY_END
ENTRY_BEGIN
MATCH opcode qtype qname
ADJUST copy_id
REPLY QR NOERROR
SECTION QUESTION
k.root-servers.net. IN AAAA
SECTION AUTHORITY
root-servers.net. IN SOA . . 0 0 0 0 0
ENTRY_END
RANGE_END
; ns.example.net.
RANGE_BEGIN 0 100
ADDRESS 1.2.3.1
ENTRY_BEGIN
MATCH opcode qtype qname
ADJUST copy_id
REPLY QR NOERROR
SECTION QUESTION
example.net. IN NS
SECTION ANSWER
example.net. IN NS ns.example.com.
SECTION ADDITIONAL
ns.example.com. IN A 1.2.3.2
ENTRY_END
ENTRY_BEGIN
MATCH opcode qtype qname
ADJUST copy_id
REPLY QR NOERROR
SECTION QUESTION
example.com. IN NS
SECTION ANSWER
example.com. IN NS ns.example.net.
SECTION ADDITIONAL
ns.example.net. IN A 1.2.3.1
ENTRY_END
ENTRY_BEGIN
MATCH opcode qtype qname
ADJUST copy_id
REPLY QR NOERROR
SECTION QUESTION
ns.example.com. IN A
SECTION ANSWER
ns.example.com. IN A 1.2.3.2
SECTION AUTHORITY
example.com. IN NS ns.example.net.
SECTION ADDITIONAL
ns.example.net. IN A 1.2.3.1
ENTRY_END
RANGE_END
STEP 1 QUERY
ENTRY_BEGIN
REPLY RD
SECTION QUESTION
ns.example.com. IN A
ENTRY_END
; recursion happens here.
; cycle detected and it fails.
STEP 20 CHECK_ANSWER
ENTRY_BEGIN
MATCH all
REPLY QR RD RA
SECTION QUESTION
ns.example.com. IN A
SECTION ANSWER
ns.example.com. IN A 1.2.3.2
;SECTION AUTHORITY
;example.com. IN NS ns.example.net.
; scrubbed off
; SECTION ADDITIONAL
; ns.example.net. IN A 1.2.3.1
ENTRY_END
SCENARIO_END
import sys
from json import load
from yangson.datamodel import DataModel
from resolvers_yang.generator import Generator
......@@ -10,24 +12,30 @@ kresd_path = "kresd.conf"
unb_path = "unbound.conf"
# load data model
model = DataModel.from_file(yangdir + "/yanglib-complete.json", [yangdir])
model = DataModel.from_file(yangdir + "/yanglib.json", [yangdir])
# load data from json
with open(json_path) as infile:
ri = load(infile)
# create configuration generator for specific data model
conf_generator = Generator(model)
data = model.from_raw(ri)
# load, validate data from json
conf_data = conf_generator.load_from_json(json_path)
# validate against data model
data.validate()
# write configurations to files
unb_conf = conf_generator.unbound(conf_data)
kresd_conf = conf_generator.kresd(conf_data)
# adding missing default values
data_defaults = data.add_defaults()
# generate configuration strings
unb_conf = Generator.unbound(data_defaults)
kresd_conf = Generator.kresd(data_defaults)
# write kresd.conf
knot_file = open(kresd_path, "w+")
knot_file.write(kresd_conf)
knot_file.close()
# write kresd.conf
# write unbound.conf
unb_file = open(unb_path, "w+")
unb_file.write(unb_conf)
unb_file.close()
import sys
from json import dump
from yangson.datamodel import DataModel
from resolvers_yang.converter import Converter
yangdir = "../yang-modules"
model = DataModel.from_file(yangdir + "/yanglib-complete.json", [yangdir])
unb_path = sys.argv[1]
# setup of path where json file will be created
data_json_path = "unb-data.json"
# create unbound converter for specific data model
converter = Converter(model)
# load data model
model = DataModel.from_file(yangdir + "/yanglib.json", [yangdir])
unbconf_path = sys.argv[1]
# load unbound.conf file on specific path
unb_data = converter.load_unbound_conf(unb_path) # unbound.conf"
# setup of path where json file will be created
converter.data_json_path = "unb-data.json"
with open(unbconf_path, "r") as unb_file:
unbconf_data = unb_file.readlines()
# call of conversion functions
json_data = converter.convert_from_unbound(unb_data)
# call of conversion function
json_data = Converter.from_unbound(unbconf_data)
# save converted configuration to json
converter.save_to_json(json_data)
model_data = model.from_raw(json_data)
# validate data against data model
model_data.validate()
# save data to json
with open(data_json_path, 'w') as json_file:
dump(json_data, json_file, indent=2, sort_keys=False)
import re
from json import dump
from yangson import DataModel
from pathlib import Path
# regular expressions
ipv4_address = re.compile('^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$')
......@@ -9,7 +6,7 @@ ipv4_address = re.compile('^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$')
unbound_regex = {
'server': re.compile(r'server:\s'),
'user': re.compile(r'^\susername:\s"(?P<username>.*)"\n'), # yes
'rz-file': re.compile(r'^\sroot-hints:\s"(?P<rz_file>.*)"\n'), # yes
'rz-file': re.compile(r'^\sroot-hints:\s"(?P<rz_file>.*)"\n'), # yes
'interface': re.compile(r'interface:\s(?P<adress>.*)@(?P<port>\d+)\n'), # yes
'outgoing-interface': re.compile(r'outgoing-interface:\s(?P<out_interface>.*)\n'), # yes
'do-ip4': re.compile(r'do-ip4\s=\s(?P<do_ipv4>yes|no)\n'), # yes
......@@ -26,6 +23,7 @@ unbound_regex = {
'cache-max-ttl': re.compile(r'cache-max-ttl:\s(?P<max_ttl>\d+)\n'), # yes
'cache-min-ttl': re.compile(r'cache-min-ttl:\s(?P<min_ttl>\d+)\n'), # yes
'dns64-prefix': re.compile(r'dns64-prefix:\s(?P<dns64_prefix>.*)\n'), # yes
'stub-zone': re.compile(r'stub-zone:\s'), # yes
'name': re.compile(r'^\sname:\s"(?P<name>.*)"\n'), # yes
'stub-addr': re.compile(r'stub-addr:\s"(?P<adress>.*)@(?P<port>\d+)"\n'), # yes
......@@ -36,9 +34,6 @@ unbound_regex = {
class Converter:
def __init__(self, _model: DataModel):
self.model = _model
@staticmethod
def parse_line(line):
......@@ -49,15 +44,8 @@ class Converter:
# if there are no matches
return None, None
def load_unbound_conf(self, file_path: str):
unbound_conf = [] # type: list
return unbound_conf
def save_to_json(self, data: dict):
pass
def convert_from_unbound(self, unbound_data: list):
@staticmethod
def from_unbound(unbound_data: list):
data = {'cznic-resolver-common:dns-resolver': {}}
interface_num = 0
......@@ -90,7 +78,7 @@ class Converter: