| 1 |
from abc import ABC |
| 2 |
import binascii |
| 3 |
import calendar |
| 4 |
from datetime import datetime |
| 5 |
import logging |
| 6 |
import os # requires posix |
| 7 |
import random |
| 8 |
import socket |
| 9 |
import struct |
| 10 |
import time |
| 11 |
from typing import Optional |
| 12 |
|
| 13 |
import dns.dnssec |
| 14 |
import dns.message |
| 15 |
import dns.name |
| 16 |
import dns.rcode |
| 17 |
import dns.rrset |
| 18 |
import dns.tsigkeyring |
| 19 |
|
| 20 |
import pydnstest.augwrap |
| 21 |
import pydnstest.matchpart |
| 22 |
import pydnstest.mock_client |
| 23 |
|
| 24 |
|
| 25 |
def str2bool(v): |
| 26 |
""" Return conversion of JSON-ish string value to boolean. """ |
| 27 |
return v.lower() in ('yes', 'true', 'on', '1') |
| 28 |
|
| 29 |
|
| 30 |
# Global statistics |
| 31 |
g_rtt = 0.0 |
| 32 |
g_nqueries = 0 |
| 33 |
|
| 34 |
|
| 35 |
class DNSBlob(ABC): |
| 36 |
def to_wire(self) -> bytes: |
| 37 |
raise NotImplementedError |
| 38 |
|
| 39 |
def __str__(self) -> str: |
| 40 |
return '<DNSBlob>' |
| 41 |
|
| 42 |
|
| 43 |
class DNSMessage(DNSBlob): |
| 44 |
def __init__(self, message: dns.message.Message) -> None: |
| 45 |
assert message is not None |
| 46 |
self.message = message |
| 47 |
|
| 48 |
def to_wire(self) -> bytes: |
| 49 |
return self.message.to_wire(max_size=65535) |
| 50 |
|
| 51 |
def __str__(self) -> str: |
| 52 |
return str(self.message) |
| 53 |
|
| 54 |
|
| 55 |
class DNSReply(DNSMessage): |
| 56 |
def __init__( |
| 57 |
self, |
| 58 |
message: dns.message.Message, |
| 59 |
query: Optional[dns.message.Message] = None, |
| 60 |
copy_id: bool = False, |
| 61 |
copy_query: bool = False |
| 62 |
) -> None: |
| 63 |
super().__init__(message) |
| 64 |
if copy_id or copy_query: |
| 65 |
if query is None: |
| 66 |
raise ValueError("query must be provided to adjust copy_id/copy_query") |
| 67 |
self.adjust_reply(query, copy_id, copy_query) |
| 68 |
|
| 69 |
def adjust_reply( |
| 70 |
self, |
| 71 |
query: dns.message.Message, |
| 72 |
copy_id: bool = True, |
| 73 |
copy_query: bool = True |
| 74 |
) -> None: |
| 75 |
answer = dns.message.from_wire(self.message.to_wire(), |
| 76 |
xfr=self.message.xfr, |
| 77 |
one_rr_per_rrset=True) |
| 78 |
answer.use_edns(query.edns, query.ednsflags, options=list(self.message.options)) |
| 79 |
if copy_id: |
| 80 |
answer.id = query.id |
| 81 |
# Copy letter-case if the template has QD |
| 82 |
if answer.question: |
| 83 |
answer.question[0].name = query.question[0].name |
| 84 |
if copy_query: |
| 85 |
answer.question = query.question |
| 86 |
# Re-set, as the EDNS might have reset the ext-rcode |
| 87 |
answer.set_rcode(self.message.rcode()) |
| 88 |
|
| 89 |
# sanity check: adjusted answer should be almost the same |
| 90 |
assert len(answer.answer) == len(self.message.answer) |
| 91 |
assert len(answer.authority) == len(self.message.authority) |
| 92 |
assert len(answer.additional) == len(self.message.additional) |
| 93 |
self.message = answer |
| 94 |
|
| 95 |
|
| 96 |
class DNSReplyRaw(DNSBlob): |
| 97 |
def __init__( |
| 98 |
self, |
| 99 |
wire: bytes, |
| 100 |
query: Optional[dns.message.Message] = None, |
| 101 |
copy_id: bool = False |
| 102 |
) -> None: |
| 103 |
assert wire is not None |
| 104 |
self.wire = wire |
| 105 |
if copy_id: |
| 106 |
if query is None: |
| 107 |
raise ValueError("query must be provided to adjust copy_id") |
| 108 |
self.adjust_reply(query, copy_id) |
| 109 |
|
| 110 |
def adjust_reply( |
| 111 |
self, |
| 112 |
query: dns.message.Message, |
| 113 |
raw_id: bool = True |
| 114 |
) -> None: |
| 115 |
if raw_id: |
| 116 |
if len(self.wire) < 2: |
| 117 |
raise ValueError( |
| 118 |
'wire data must contain at least 2 bytes to adjust query id') |
| 119 |
raw_answer = bytearray(self.wire) |
| 120 |
struct.pack_into('!H', raw_answer, 0, query.id) |
| 121 |
self.wire = bytes(raw_answer) |
| 122 |
|
| 123 |
def to_wire(self) -> bytes: |
| 124 |
return self.wire |
| 125 |
|
| 126 |
def __str__(self) -> str: |
| 127 |
return '<DNSReplyRaw>' |
| 128 |
|
| 129 |
|
| 130 |
class DNSReplyServfail(DNSMessage): |
| 131 |
def __init__(self, query: dns.message.Message) -> None: |
| 132 |
message = dns.message.make_response(query) |
| 133 |
message.set_rcode(dns.rcode.SERVFAIL) |
| 134 |
super().__init__(message) |
| 135 |
|
| 136 |
|
| 137 |
class Entry: |
| 138 |
""" |
| 139 |
Data entry represents scripted message and extra metadata, |
| 140 |
notably match criteria and reply adjustments. |
| 141 |
""" |
| 142 |
|
| 143 |
# Globals |
| 144 |
default_ttl = 3600 |
| 145 |
default_cls = 'IN' |
| 146 |
default_rc = 'NOERROR' |
| 147 |
|
| 148 |
def __init__(self, node): |
| 149 |
""" Initialize data entry. """ |
| 150 |
self.node = node |
| 151 |
self.origin = '.' |
| 152 |
self.message = dns.message.Message() |
| 153 |
self.message.use_edns(edns=0, payload=4096) |
| 154 |
self.fired = 0 |
| 155 |
|
| 156 |
# RAW |
| 157 |
self.raw_data = self.process_raw() |
| 158 |
|
| 159 |
# MATCH |
| 160 |
self.match_fields = self.process_match() |
| 161 |
|
| 162 |
# FLAGS (old alias REPLY) |
| 163 |
self.process_reply_line() |
| 164 |
|
| 165 |
# ADJUST |
| 166 |
self.adjust_fields = {m.value for m in node.match("/adjust")} |
| 167 |
|
| 168 |
# MANDATORY |
| 169 |
try: |
| 170 |
self.mandatory = list(node.match("/mandatory"))[0] |
| 171 |
except (KeyError, IndexError): |
| 172 |
self.mandatory = None |
| 173 |
|
| 174 |
# TSIG |
| 175 |
self.process_tsig() |
| 176 |
|
| 177 |
# SECTIONS & RECORDS |
| 178 |
self.sections = self.process_sections() |
| 179 |
|
| 180 |
def process_raw(self): |
| 181 |
try: |
| 182 |
return binascii.unhexlify(self.node["/raw"].value) |
| 183 |
except KeyError: |
| 184 |
return None |
| 185 |
|
| 186 |
def process_match(self): |
| 187 |
try: |
| 188 |
self.node["/match_present"] |
| 189 |
except KeyError: |
| 190 |
return None |
| 191 |
|
| 192 |
fields = set(m.value for m in self.node.match("/match")) |
| 193 |
|
| 194 |
if 'all' in fields: |
| 195 |
fields.remove("all") |
| 196 |
fields |= set(["opcode", "qtype", "qname", "flags", |
| 197 |
"rcode", "answer", "authority", "additional"]) |
| 198 |
|
| 199 |
if 'question' in fields: |
| 200 |
fields.remove("question") |
| 201 |
fields |= set(["qtype", "qname"]) |
| 202 |
|
| 203 |
return fields |
| 204 |
|
| 205 |
def process_reply_line(self): |
| 206 |
"""Extracts flags, rcode and opcode from given node and adjust dns message accordingly""" |
| 207 |
fields = [f.value for f in self.node.match("/reply")] |
| 208 |
if 'DO' in fields: |
| 209 |
self.message.want_dnssec(True) |
| 210 |
opcode = self.get_opcode(fields) |
| 211 |
rcode = self.get_rcode(fields) |
| 212 |
self.message.flags = self.get_flags(fields) |
| 213 |
if rcode is not None: |
| 214 |
self.message.set_rcode(rcode) |
| 215 |
if opcode is not None: |
| 216 |
self.message.set_opcode(opcode) |
| 217 |
|
| 218 |
def process_tsig(self): |
| 219 |
try: |
| 220 |
tsig = list(self.node.match("/tsig"))[0] |
| 221 |
tsig_keyname = tsig["/keyname"].value |
| 222 |
tsig_secret = tsig["/secret"].value |
| 223 |
keyring = dns.tsigkeyring.from_text({tsig_keyname: tsig_secret}) |
| 224 |
self.message.use_tsig(keyring=keyring, keyname=tsig_keyname) |
| 225 |
except (KeyError, IndexError): |
| 226 |
pass |
| 227 |
|
| 228 |
def process_sections(self): |
| 229 |
sections = set() |
| 230 |
for section in self.node.match("/section/*"): |
| 231 |
section_name = os.path.basename(section.path) |
| 232 |
sections.add(section_name) |
| 233 |
for record in section.match("/record"): |
| 234 |
owner = record['/domain'].value |
| 235 |
if not owner.endswith("."): |
| 236 |
owner += self.origin |
| 237 |
try: |
| 238 |
ttl = dns.ttl.from_text(record['/ttl'].value) |
| 239 |
except KeyError: |
| 240 |
ttl = self.default_ttl |
| 241 |
try: |
| 242 |
rdclass = dns.rdataclass.from_text(record['/class'].value) |
| 243 |
except KeyError: |
| 244 |
rdclass = dns.rdataclass.from_text(self.default_cls) |
| 245 |
rdtype = dns.rdatatype.from_text(record['/type'].value) |
| 246 |
rr = dns.rrset.from_text(owner, ttl, rdclass, rdtype) |
| 247 |
if section_name != "question": |
| 248 |
rd = record['/data'].value.split() |
| 249 |
if rd: |
| 250 |
if rdtype == dns.rdatatype.DS: |
| 251 |
rd[1] = f'{dns.dnssec.algorithm_from_text(rd[1])}' |
| 252 |
rd = dns.rdata.from_text(rr.rdclass, rr.rdtype, ' '.join( |
| 253 |
rd), origin=dns.name.from_text(self.origin), relativize=False) |
| 254 |
rr.add(rd) |
| 255 |
if section_name == 'question': |
| 256 |
if rr.rdtype == dns.rdatatype.AXFR: |
| 257 |
self.message.xfr = True |
| 258 |
self.message.question.append(rr) |
| 259 |
elif section_name == 'answer': |
| 260 |
self.message.answer.append(rr) |
| 261 |
elif section_name == 'authority': |
| 262 |
self.message.authority.append(rr) |
| 263 |
elif section_name == 'additional': |
| 264 |
self.message.additional.append(rr) |
| 265 |
return sections |
| 266 |
|
| 267 |
def __str__(self): |
| 268 |
txt = 'ENTRY_BEGIN\n' |
| 269 |
|
| 270 |
if self.raw_data is None: |
| 271 |
match_fields = ' '.join(self.match_fields) |
| 272 |
txt += f'MATCH {match_fields}\n' |
| 273 |
|
| 274 |
adjust_fields = ' '.join(self.adjust_fields) |
| 275 |
txt += f'ADJUST {adjust_fields}\n' |
| 276 |
|
| 277 |
rcode = dns.rcode.to_text(self.message.rcode()) |
| 278 |
flags = ' '.join([ |
| 279 |
dns.flags.to_text(self.message.flags), |
| 280 |
dns.flags.edns_to_text(self.message.ednsflags) |
| 281 |
]) |
| 282 |
txt += f'REPLY {rcode} {flags}\n' |
| 283 |
|
| 284 |
for sect_name in ['question', 'answer', 'authority', 'additional']: |
| 285 |
sect = getattr(self.message, sect_name) |
| 286 |
if not sect: |
| 287 |
continue |
| 288 |
txt += f'SECTION {sect_name.upper()}\n' |
| 289 |
for rr in sect: |
| 290 |
txt += str(rr) |
| 291 |
txt += '\n' |
| 292 |
|
| 293 |
if self.raw_data is not None: |
| 294 |
txt += 'RAW\n' |
| 295 |
if self.raw_data: |
| 296 |
txt += binascii.hexlify(self.raw_data) |
| 297 |
else: |
| 298 |
txt += 'NULL' |
| 299 |
txt += '\n' |
| 300 |
|
| 301 |
txt += 'ENTRY_END\n' |
| 302 |
return txt |
| 303 |
|
| 304 |
@classmethod |
| 305 |
def get_flags(cls, fields): |
| 306 |
"""From `fields` extracts and returns flags""" |
| 307 |
flags = [] |
| 308 |
for code in fields: |
| 309 |
try: |
| 310 |
dns.flags.from_text(code) # throws KeyError on failure |
| 311 |
flags.append(code) |
| 312 |
except KeyError: |
| 313 |
pass |
| 314 |
return dns.flags.from_text(' '.join(flags)) |
| 315 |
|
| 316 |
@classmethod |
| 317 |
def get_rcode(cls, fields): |
| 318 |
""" |
| 319 |
From `fields` extracts and returns rcode. |
| 320 |
Throws `ValueError` if there are more then one rcodes |
| 321 |
""" |
| 322 |
rcodes = [] |
| 323 |
for code in fields: |
| 324 |
try: |
| 325 |
rcodes.append(dns.rcode.from_text(code)) |
| 326 |
except dns.rcode.UnknownRcode: |
| 327 |
pass |
| 328 |
if len(rcodes) > 1: |
| 329 |
raise ValueError("Parse failed, too many rcode values.", rcodes) |
| 330 |
if not rcodes: |
| 331 |
return None |
| 332 |
return rcodes[0] |
| 333 |
|
| 334 |
@classmethod |
| 335 |
def get_opcode(cls, fields): |
| 336 |
""" |
| 337 |
From `fields` extracts and returns opcode. |
| 338 |
Throws `ValueError` if there are more then one opcodes |
| 339 |
""" |
| 340 |
opcodes = [] |
| 341 |
for code in fields: |
| 342 |
try: |
| 343 |
opcodes.append(dns.opcode.from_text(code)) |
| 344 |
except dns.opcode.UnknownOpcode: |
| 345 |
pass |
| 346 |
if len(opcodes) > 1: |
| 347 |
raise ValueError("Parse failed, too many opcode values.") |
| 348 |
if not opcodes: |
| 349 |
return None |
| 350 |
return opcodes[0] |
| 351 |
|
| 352 |
def match(self, msg): |
| 353 |
""" Compare scripted reply to given message based on match criteria. """ |
| 354 |
for code in self.match_fields: |
| 355 |
try: |
| 356 |
pydnstest.matchpart.match_part(self.message, msg, code) |
| 357 |
except pydnstest.matchpart.DataMismatch as ex: |
| 358 |
errstr = f'{ex} in the response:\n{msg.to_text()}' |
| 359 |
# TODO: cisla radku |
| 360 |
raise ValueError(f"{self.node.span}, \"{code}\": {errstr}") from None |
| 361 |
|
| 362 |
def cmp_raw(self, raw_value): |
| 363 |
assert self.raw_data is not None |
| 364 |
expected = None |
| 365 |
if self.raw_data is not None: |
| 366 |
expected = binascii.hexlify(self.raw_data) |
| 367 |
got = None |
| 368 |
if raw_value is not None: |
| 369 |
got = binascii.hexlify(raw_value) |
| 370 |
if expected != got: |
| 371 |
raise ValueError(f"raw message comparsion failed: expected {expected} got {got}") |
| 372 |
|
| 373 |
def reply(self, query) -> Optional[DNSBlob]: |
| 374 |
if 'do_not_answer' in self.adjust_fields: |
| 375 |
return None |
| 376 |
if self.raw_data is not None: |
| 377 |
raw_id = 'raw_id' in self.adjust_fields |
| 378 |
assert self.raw_data is not None |
| 379 |
return DNSReplyRaw(self.raw_data, query, raw_id) |
| 380 |
copy_id = 'copy_id' in self.adjust_fields |
| 381 |
copy_query = 'copy_query' in self.adjust_fields |
| 382 |
return DNSReply(self.message, query, copy_id, copy_query) |
| 383 |
|
| 384 |
def set_edns(self, fields): |
| 385 |
""" Set EDNS version and bufsize. """ |
| 386 |
version = 0 |
| 387 |
bufsize = 4096 |
| 388 |
if fields and fields[0].isdigit(): |
| 389 |
version = int(fields.pop(0)) |
| 390 |
if fields and fields[0].isdigit(): |
| 391 |
bufsize = int(fields.pop(0)) |
| 392 |
if bufsize == 0: |
| 393 |
self.message.use_edns(False) |
| 394 |
return |
| 395 |
opts = [] |
| 396 |
for v in fields: |
| 397 |
k, v = tuple(v.split('=')) if '=' in v else (v, True) |
| 398 |
if k.lower() == 'nsid': |
| 399 |
opts.append(dns.edns.GenericOption(dns.edns.NSID, '' if v is True else v)) |
| 400 |
if k.lower() == 'subnet': |
| 401 |
net = v.split('/') |
| 402 |
subnet_addr = net[0] |
| 403 |
family = socket.AF_INET6 if ':' in subnet_addr else socket.AF_INET |
| 404 |
addr = socket.inet_pton(family, subnet_addr) |
| 405 |
prefix = len(addr) * 8 |
| 406 |
if len(net) > 1: |
| 407 |
prefix = int(net[1]) |
| 408 |
addr = addr[0: (prefix + 7) / 8] |
| 409 |
if prefix % 8 != 0: # Mask the last byte |
| 410 |
addr = addr[:-1] + chr(ord(addr[-1]) & 0xFF << (8 - prefix % 8)) |
| 411 |
opts.append(dns.edns.GenericOption(8, struct.pack( |
| 412 |
"!HBB", 1 if family == socket.AF_INET else 2, prefix, 0) + addr)) |
| 413 |
self.message.use_edns(edns=version, payload=bufsize, options=opts) |
| 414 |
|
| 415 |
|
| 416 |
class Range: |
| 417 |
""" |
| 418 |
Range represents a set of scripted queries valid for given step range. |
| 419 |
""" |
| 420 |
log = logging.getLogger('pydnstest.scenario.Range') |
| 421 |
|
| 422 |
def __init__(self, node): |
| 423 |
""" Initialize reply range. """ |
| 424 |
self.node = node |
| 425 |
self.a = int(node['/from'].value) |
| 426 |
self.b = int(node['/to'].value) |
| 427 |
assert self.a <= self.b |
| 428 |
|
| 429 |
address = node["/address"].value |
| 430 |
self.addresses = {address} if address is not None else set() |
| 431 |
self.addresses |= {a.value for a in node.match("/address/*")} |
| 432 |
self.stored = [Entry(n) for n in node.match("/entry")] |
| 433 |
self.args = {} |
| 434 |
self.received = 0 |
| 435 |
self.sent = 0 |
| 436 |
|
| 437 |
def __del__(self): |
| 438 |
self.log.info('[ RANGE %d-%d ] %s received: %d sent: %d', |
| 439 |
self.a, self.b, self.addresses, self.received, self.sent) |
| 440 |
|
| 441 |
def __str__(self): |
| 442 |
txt = f'\nRANGE_BEGIN {self.a} {self.b}\n' |
| 443 |
for addr in self.addresses: |
| 444 |
txt += f' ADDRESS {addr}\n' |
| 445 |
|
| 446 |
for entry in self.stored: |
| 447 |
txt += '\n' |
| 448 |
txt += str(entry) |
| 449 |
txt += 'RANGE_END\n\n' |
| 450 |
return txt |
| 451 |
|
| 452 |
def eligible(self, ident, address): |
| 453 |
""" Return true if this range is eligible for fetching reply. """ |
| 454 |
if self.a <= ident <= self.b: |
| 455 |
return (None is address |
| 456 |
or set() == self.addresses |
| 457 |
or address in self.addresses) |
| 458 |
return False |
| 459 |
|
| 460 |
def reply(self, query: dns.message.Message) -> Optional[DNSBlob]: |
| 461 |
"""Get answer for given query (adjusted if needed).""" |
| 462 |
self.received += 1 |
| 463 |
for candidate in self.stored: |
| 464 |
try: |
| 465 |
candidate.match(query) |
| 466 |
resp = candidate.reply(query) |
| 467 |
# Probabilistic loss |
| 468 |
if 'LOSS' in self.args: |
| 469 |
if random.random() < float(self.args['LOSS']): |
| 470 |
return DNSReplyServfail(query) |
| 471 |
self.sent += 1 |
| 472 |
candidate.fired += 1 |
| 473 |
return resp |
| 474 |
except ValueError: |
| 475 |
pass |
| 476 |
return DNSReplyServfail(query) |
| 477 |
|
| 478 |
|
| 479 |
class StepLogger(logging.LoggerAdapter): # pylint: disable=too-few-public-methods |
| 480 |
""" |
| 481 |
Prepent Step identification before each log message. |
| 482 |
""" |
| 483 |
def process(self, msg, kwargs): |
| 484 |
return f'[STEP {self.extra["id"]} {self.extra["type"]}] {msg}', kwargs |
| 485 |
|
| 486 |
|
| 487 |
class Step: |
| 488 |
""" |
| 489 |
Step represents one scripted action in a given moment, |
| 490 |
each step has an order identifier, type and optionally data entry. |
| 491 |
""" |
| 492 |
require_data = ['QUERY', 'CHECK_ANSWER', 'REPLY'] |
| 493 |
|
| 494 |
def __init__(self, node): |
| 495 |
""" Initialize single scenario step. """ |
| 496 |
self.node = node |
| 497 |
self.id = int(node.value) |
| 498 |
self.type = node["/type"].value |
| 499 |
self.log = StepLogger(logging.getLogger('pydnstest.scenario.Step'), |
| 500 |
{'id': self.id, 'type': self.type}) |
| 501 |
try: |
| 502 |
self.delay = int(node["/timestamp"].value) |
| 503 |
except KeyError: |
| 504 |
pass |
| 505 |
self.data = [Entry(n) for n in node.match("/entry")] |
| 506 |
self.queries = [] |
| 507 |
self.has_data = self.type in Step.require_data |
| 508 |
self.answer = None |
| 509 |
self.raw_answer = None |
| 510 |
self.repeat_if_fail = 0 |
| 511 |
self.pause_if_fail = 0 |
| 512 |
self.next_if_fail = -1 |
| 513 |
|
| 514 |
def __str__(self): |
| 515 |
txt = f'\nSTEP {self.id} {self.type}' |
| 516 |
if self.repeat_if_fail: |
| 517 |
txt += f' REPEAT {self.repeat_if_fail}' |
| 518 |
elif self.pause_if_fail: |
| 519 |
txt += f' PAUSE {self.pause_if_fail}' |
| 520 |
elif self.next_if_fail != -1: |
| 521 |
txt += f' NEXT {self.next_if_fail}' |
| 522 |
# if self.args: |
| 523 |
# txt += ' ' |
| 524 |
# txt += ' '.join(self.args) |
| 525 |
txt += '\n' |
| 526 |
|
| 527 |
for data in self.data: |
| 528 |
# from IPython.core.debugger import Tracer |
| 529 |
# Tracer()() |
| 530 |
txt += str(data) |
| 531 |
return txt |
| 532 |
|
| 533 |
def play(self, ctx): |
| 534 |
""" Play one step from a scenario. """ |
| 535 |
if self.type == 'QUERY': |
| 536 |
self.log.info('') |
| 537 |
self.log.debug(self.data[0].message.to_text()) |
| 538 |
# Parse QUERY-specific parameters |
| 539 |
choice, tcp, src_address = None, False, ctx.deckard_address |
| 540 |
return self.__query(ctx, tcp=tcp, choice=choice, src_address=src_address) |
| 541 |
elif self.type == 'CHECK_OUT_QUERY': # ignore |
| 542 |
self.log.info('') |
| 543 |
return None |
| 544 |
elif self.type in ('CHECK_ANSWER', 'ANSWER'): |
| 545 |
self.log.info('') |
| 546 |
return self.__check_answer(ctx) |
| 547 |
elif self.type == 'TIME_PASSES ELAPSE': |
| 548 |
self.log.info('') |
| 549 |
return self.__time_passes() |
| 550 |
elif self.type in ('REPLY', 'MOCK'): |
| 551 |
self.log.info('') |
| 552 |
return None |
| 553 |
else: |
| 554 |
raise NotImplementedError(f'step {self.id:03} type {self.type} unsupported') |
| 555 |
|
| 556 |
def __check_answer(self, ctx): |
| 557 |
""" Compare answer from previously resolved query. """ |
| 558 |
if not self.data: |
| 559 |
raise ValueError("response definition required") |
| 560 |
expected = self.data[0] |
| 561 |
if expected.raw_data is not None: |
| 562 |
self.log.debug("raw answer: %s", ctx.last_raw_answer.to_text()) |
| 563 |
expected.cmp_raw(ctx.last_raw_answer) |
| 564 |
else: |
| 565 |
if ctx.last_answer is None: |
| 566 |
raise ValueError("no answer from preceding query") |
| 567 |
self.log.debug("answer: %s", ctx.last_answer.to_text()) |
| 568 |
expected.match(ctx.last_answer) |
| 569 |
|
| 570 |
def __query(self, ctx, tcp=False, choice=None, src_address=None): |
| 571 |
""" |
| 572 |
Send query and wait for an answer (if the query is not RAW). |
| 573 |
|
| 574 |
The received answer is stored in self.answer and ctx.last_answer. |
| 575 |
""" |
| 576 |
if not self.data: |
| 577 |
raise ValueError("query definition required") |
| 578 |
if self.data[0].raw_data is not None: |
| 579 |
data_to_wire = self.data[0].raw_data |
| 580 |
else: |
| 581 |
# Don't use a message copy as the EDNS data portion is not copied. |
| 582 |
data_to_wire = self.data[0].message.to_wire() |
| 583 |
if choice is None or not choice: |
| 584 |
choice = list(ctx.client.keys())[0] |
| 585 |
if choice not in ctx.client: |
| 586 |
raise ValueError(f'step {self.id:03} invalid QUERY target: {choice}') |
| 587 |
|
| 588 |
tstart = datetime.now() |
| 589 |
|
| 590 |
# Send query and wait for answer |
| 591 |
answer = None |
| 592 |
sock = pydnstest.mock_client.setup_socket(ctx.client[choice][0], |
| 593 |
ctx.client[choice][1], |
| 594 |
tcp, |
| 595 |
src_address=src_address) |
| 596 |
with sock: |
| 597 |
pydnstest.mock_client.send_query(sock, data_to_wire) |
| 598 |
if self.data[0].raw_data is None: |
| 599 |
answer = pydnstest.mock_client.get_answer(sock) |
| 600 |
|
| 601 |
# Track RTT |
| 602 |
rtt = (datetime.now() - tstart).total_seconds() * 1000 |
| 603 |
global g_rtt, g_nqueries |
| 604 |
g_nqueries += 1 |
| 605 |
g_rtt += rtt |
| 606 |
|
| 607 |
# Remember last answer for checking later |
| 608 |
self.raw_answer = answer |
| 609 |
ctx.last_raw_answer = answer |
| 610 |
if self.raw_answer is not None: |
| 611 |
self.answer = dns.message.from_wire(self.raw_answer, one_rr_per_rrset=True) |
| 612 |
else: |
| 613 |
self.answer = None |
| 614 |
ctx.last_answer = self.answer |
| 615 |
|
| 616 |
def __time_passes(self): |
| 617 |
""" Modify system time. """ |
| 618 |
file_old = os.environ["FAKETIME_TIMESTAMP_FILE"] |
| 619 |
file_next = os.environ["FAKETIME_TIMESTAMP_FILE"] + ".next" |
| 620 |
with open(file_old, 'r', encoding='utf-8') as time_file: |
| 621 |
line = time_file.readline().strip() |
| 622 |
t = time.mktime(datetime.strptime(line, '@%Y-%m-%d %H:%M:%S').timetuple()) |
| 623 |
t += self.delay |
| 624 |
with open(file_next, 'w', encoding='utf-8') as time_file: |
| 625 |
time_file.write(datetime.fromtimestamp(t).strftime('@%Y-%m-%d %H:%M:%S') + "\n") |
| 626 |
time_file.flush() |
| 627 |
os.replace(file_next, file_old) |
| 628 |
|
| 629 |
|
| 630 |
class Scenario: |
| 631 |
log = logging.getLogger('pydnstest.scenatio.Scenario') |
| 632 |
|
| 633 |
def __init__(self, node, filename, deckard_address=None): |
| 634 |
""" Initialize scenario with description. """ |
| 635 |
self.node = node |
| 636 |
self.info = node.value |
| 637 |
self.file = filename |
| 638 |
self.ranges = [Range(n) for n in node.match("/range")] |
| 639 |
self.current_range = None |
| 640 |
self.steps = [Step(n) for n in node.match("/step")] |
| 641 |
self.current_step = None |
| 642 |
self.client = {} |
| 643 |
self.deckard_address = deckard_address |
| 644 |
|
| 645 |
def __str__(self): |
| 646 |
txt = 'SCENARIO_BEGIN' |
| 647 |
if self.info: |
| 648 |
txt += f' {self.info}' |
| 649 |
txt += '\n' |
| 650 |
for range_ in self.ranges: |
| 651 |
txt += str(range_) |
| 652 |
for step in self.steps: |
| 653 |
txt += str(step) |
| 654 |
txt += "\nSCENARIO_END" |
| 655 |
return txt |
| 656 |
|
| 657 |
def reply(self, query: dns.message.Message, address=None) -> Optional[DNSBlob]: |
| 658 |
"""Generate answer packet for given query.""" |
| 659 |
current_step_id = self.current_step.id |
| 660 |
# Unknown address, select any match |
| 661 |
# TODO: workaround until the server supports stub zones |
| 662 |
all_addresses = set() # type: ignore |
| 663 |
for rng in self.ranges: |
| 664 |
all_addresses.update(rng.addresses) |
| 665 |
if address not in all_addresses: |
| 666 |
address = None |
| 667 |
# Find current valid query response range |
| 668 |
for rng in self.ranges: |
| 669 |
if rng.eligible(current_step_id, address): |
| 670 |
self.current_range = rng |
| 671 |
return rng.reply(query) |
| 672 |
# Find any prescripted one-shot replies |
| 673 |
for step in self.steps: |
| 674 |
if step.id < current_step_id or step.type != 'REPLY': |
| 675 |
continue |
| 676 |
try: |
| 677 |
candidate = step.data[0] |
| 678 |
candidate.match(query) |
| 679 |
step.data.remove(candidate) |
| 680 |
return candidate.reply(query) |
| 681 |
except (IndexError, ValueError): |
| 682 |
pass |
| 683 |
return DNSReplyServfail(query) |
| 684 |
|
| 685 |
def play(self, paddr): |
| 686 |
""" Play given scenario. """ |
| 687 |
# Store test subject => address mapping |
| 688 |
self.client = paddr |
| 689 |
|
| 690 |
step = None |
| 691 |
i = 0 |
| 692 |
while i < len(self.steps): |
| 693 |
step = self.steps[i] |
| 694 |
self.current_step = step |
| 695 |
try: |
| 696 |
step.play(self) |
| 697 |
except ValueError as ex: |
| 698 |
if step.repeat_if_fail > 0: |
| 699 |
self.log.info("[play] step %d: exception - '%s', retrying step %d (%d left)", |
| 700 |
step.id, ex, step.next_if_fail, step.repeat_if_fail) |
| 701 |
step.repeat_if_fail -= 1 |
| 702 |
if step.pause_if_fail > 0: |
| 703 |
time.sleep(step.pause_if_fail) |
| 704 |
if step.next_if_fail != -1: |
| 705 |
next_steps = [j for j in range(len(self.steps)) if self.steps[ |
| 706 |
j].id == step.next_if_fail] |
| 707 |
if not next_steps: |
| 708 |
raise ValueError( |
| 709 |
f'step {step.id}: ' |
| 710 |
f'wrong NEXT value "{step.next_if_fail}"' |
| 711 |
) from ex |
| 712 |
next_step = next_steps[0] |
| 713 |
if next_step < len(self.steps): |
| 714 |
i = next_step |
| 715 |
else: |
| 716 |
raise ValueError( |
| 717 |
f'step {step.id}: ' |
| 718 |
f'Can\'t branch to NEXT value "{step.next_if_fail}"' |
| 719 |
) from ex |
| 720 |
continue |
| 721 |
ex_details = ex if self.log.isEnabledFor(logging.DEBUG) else None |
| 722 |
raise ValueError(f'{self.file} step {step.id} {ex}') from ex_details |
| 723 |
i += 1 |
| 724 |
|
| 725 |
for r in self.ranges: |
| 726 |
for e in r.stored: |
| 727 |
if e.mandatory and e.fired == 0: |
| 728 |
# TODO: cisla radku |
| 729 |
raise ValueError(f'Mandatory section at {e.mandatory.span} not fired') |
| 730 |
|
| 731 |
|
| 732 |
def get_next(file_in, skip_empty=True): |
| 733 |
""" Return next token from the input stream. """ |
| 734 |
while True: |
| 735 |
line = file_in.readline() |
| 736 |
if not line: |
| 737 |
return False |
| 738 |
quoted, escaped = False, False |
| 739 |
for i, char in enumerate(line): |
| 740 |
if char == '\\': |
| 741 |
escaped = not escaped |
| 742 |
if not escaped and char == '"': |
| 743 |
quoted = not quoted |
| 744 |
if char == ';' and not quoted: |
| 745 |
line = line[0:i] |
| 746 |
break |
| 747 |
if char != '\\': |
| 748 |
escaped = False |
| 749 |
tokens = ' '.join(line.strip().split()).split() |
| 750 |
if not tokens: |
| 751 |
if skip_empty: |
| 752 |
continue |
| 753 |
return '', [] |
| 754 |
op = tokens.pop(0) |
| 755 |
return op, tokens |
| 756 |
|
| 757 |
|
| 758 |
def parse_config(scn_cfg, qmin, installdir): # FIXME: pylint: disable=too-many-statements |
| 759 |
""" |
| 760 |
Transform scene config (key, value) pairs into dict filled with defaults. |
| 761 |
Returns tuple: |
| 762 |
context dict: {Jinja2 variable: value} |
| 763 |
trust anchor dict: {domain: [TA lines for particular domain]} |
| 764 |
""" |
| 765 |
# defaults |
| 766 |
do_not_query_localhost = True |
| 767 |
harden_glue = True |
| 768 |
sockfamily = 0 # auto-select value for socket.getaddrinfo |
| 769 |
trust_anchor_list = [] |
| 770 |
trust_anchor_files = {} |
| 771 |
negative_ta_list = [] |
| 772 |
stub_addr = None |
| 773 |
stub_name = "k.root-servers.net" |
| 774 |
override_timestamp = None |
| 775 |
forward_addr = None |
| 776 |
do_ip6 = True |
| 777 |
do_ip4 = True |
| 778 |
features = {} |
| 779 |
feature_list_delimiter = ';' |
| 780 |
feature_pair_delimiter = '=' |
| 781 |
|
| 782 |
for k, v in scn_cfg: |
| 783 |
# Enable selectively for some tests |
| 784 |
if k == 'do-not-query-localhost': |
| 785 |
do_not_query_localhost = str2bool(v) |
| 786 |
elif k == 'domain-insecure': |
| 787 |
negative_ta_list.append(v) |
| 788 |
elif k == 'harden-glue': |
| 789 |
harden_glue = str2bool(v) |
| 790 |
elif k == 'query-minimization': |
| 791 |
qmin = str2bool(v) |
| 792 |
elif k == 'trust-anchor': |
| 793 |
trust_anchor = v.strip('"\'') |
| 794 |
trust_anchor_list.append(trust_anchor) |
| 795 |
domain = dns.name.from_text(trust_anchor.split()[0]).canonicalize() |
| 796 |
if domain not in trust_anchor_files: |
| 797 |
trust_anchor_files[domain] = [] |
| 798 |
trust_anchor_files[domain].append(trust_anchor) |
| 799 |
elif k == 'val-override-timestamp': |
| 800 |
override_timestamp_str = v.strip('"\'') |
| 801 |
override_timestamp = int(override_timestamp_str) |
| 802 |
elif k == 'val-override-date': |
| 803 |
override_date_str = v.strip('"\'') |
| 804 |
ovr_yr = override_date_str[0:4] |
| 805 |
ovr_mnt = override_date_str[4:6] |
| 806 |
ovr_day = override_date_str[6:8] |
| 807 |
ovr_hr = override_date_str[8:10] |
| 808 |
ovr_min = override_date_str[10:12] |
| 809 |
ovr_sec = override_date_str[12:] |
| 810 |
override_date_str_arg = \ |
| 811 |
f'{ovr_yr} {ovr_mnt} {ovr_day} {ovr_hr} {ovr_min} {ovr_sec}' |
| 812 |
override_date = time.strptime(override_date_str_arg, "%Y %m %d %H %M %S") |
| 813 |
override_timestamp = calendar.timegm(override_date) |
| 814 |
elif k == 'stub-addr': |
| 815 |
stub_addr = v.strip('"\'') |
| 816 |
elif k == 'stub-name': |
| 817 |
stub_name = v |
| 818 |
elif k == 'features': |
| 819 |
feature_list = v.split(feature_list_delimiter) |
| 820 |
try: |
| 821 |
for f_item in feature_list: |
| 822 |
if f_item.find(feature_pair_delimiter) != -1: |
| 823 |
f_key, f_value = [x.strip() |
| 824 |
for x |
| 825 |
in f_item.split(feature_pair_delimiter, 1)] |
| 826 |
else: |
| 827 |
f_key = f_item.strip() |
| 828 |
f_value = "" |
| 829 |
features[f_key] = f_value |
| 830 |
except KeyError as ex: |
| 831 |
raise KeyError(f"can't parse features ({v}) in config section ({ex})") from ex |
| 832 |
elif k == 'feature-list': |
| 833 |
try: |
| 834 |
f_key, f_value = [x.strip() for x in v.split(feature_pair_delimiter, 1)] |
| 835 |
if f_key not in features: |
| 836 |
features[f_key] = [] |
| 837 |
f_value = f_value.replace("{{INSTALL_DIR}}", installdir) |
| 838 |
features[f_key].append(f_value) |
| 839 |
except KeyError as ex: |
| 840 |
raise KeyError(f"can't parse feature-list ({v}) in config section ({ex})") from ex |
| 841 |
elif k == 'force-ipv6' and v.upper() == 'TRUE': |
| 842 |
sockfamily = socket.AF_INET6 |
| 843 |
elif k == 'forward-addr': # currently forwards everything |
| 844 |
forward_addr = v.strip('"\'') |
| 845 |
elif k == 'do-ip4': |
| 846 |
do_ip4 = str2bool(v) |
| 847 |
elif k == 'do-ip6': |
| 848 |
do_ip6 = str2bool(v) |
| 849 |
else: |
| 850 |
raise NotImplementedError(f'unsupported CONFIG key "{k}"') |
| 851 |
|
| 852 |
ctx = { |
| 853 |
"DO_NOT_QUERY_LOCALHOST": str(do_not_query_localhost).lower(), |
| 854 |
"NEGATIVE_TRUST_ANCHORS": negative_ta_list, |
| 855 |
"FEATURES": features, |
| 856 |
"HARDEN_GLUE": str(harden_glue).lower(), |
| 857 |
"INSTALL_DIR": installdir, |
| 858 |
"QMIN": str(qmin).lower(), |
| 859 |
"TRUST_ANCHORS": trust_anchor_list, |
| 860 |
"TRUST_ANCHOR_FILES": trust_anchor_files, |
| 861 |
"FORWARD_ADDR": forward_addr, |
| 862 |
"DO_IP6": str(do_ip6).lower(), |
| 863 |
"DO_IP4": str(do_ip4).lower(), |
| 864 |
} |
| 865 |
if stub_addr: |
| 866 |
ctx['ROOT_ADDR'] = stub_addr |
| 867 |
ctx['ROOT_NAME'] = stub_name |
| 868 |
# determine and verify socket family for specified root address |
| 869 |
gai = socket.getaddrinfo(stub_addr, 53, sockfamily, 0, |
| 870 |
socket.IPPROTO_UDP, socket.AI_NUMERICHOST) |
| 871 |
assert len(gai) == 1 |
| 872 |
sockfamily = gai[0][0] |
| 873 |
if not sockfamily: |
| 874 |
sockfamily = socket.AF_INET # default to IPv4 |
| 875 |
ctx['_SOCKET_FAMILY'] = sockfamily |
| 876 |
if override_timestamp: |
| 877 |
ctx['_OVERRIDE_TIMESTAMP'] = override_timestamp |
| 878 |
return ctx |
| 879 |
|
| 880 |
|
| 881 |
def parse_file(path, deckard_address=None): |
| 882 |
""" Parse scenario from a file. """ |
| 883 |
|
| 884 |
aug = pydnstest.augwrap.AugeasWrapper( |
| 885 |
confpath=path, lens='Deckard', loadpath=os.path.dirname(__file__)) |
| 886 |
node = aug.tree |
| 887 |
config = [] |
| 888 |
for line in [c.value for c in node.match("/config/*")]: |
| 889 |
if line: |
| 890 |
if not line.startswith(';'): |
| 891 |
if '#' in line: |
| 892 |
line = line[0:line.index('#')] |
| 893 |
# Break to key-value pairs |
| 894 |
# e.g.: ['minimization', 'on'] |
| 895 |
kv = [x.strip() for x in line.split(':', 1)] |
| 896 |
if len(kv) >= 2: |
| 897 |
config.append(kv) |
| 898 |
scenario = Scenario(node["/scenario"], os.path.basename(node.path), deckard_address) |
| 899 |
return scenario, config |