Skip to content

Commit

Permalink
zr: add rr tests for many types
Browse files Browse the repository at this point in the history
  • Loading branch information
msimerson committed Mar 11, 2024
1 parent bbc8d33 commit 4619084
Show file tree
Hide file tree
Showing 19 changed files with 284 additions and 98 deletions.
File renamed without changes.
8 changes: 8 additions & 0 deletions lib/test/rrs/aaaa.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"id": 4096,
"zid": 4096,
"owner": "test.example.com.",
"ttl": 86400,
"type": "AAAA",
"address": "2001:0db8:0020:000a:0000:0000:0000:0004"
}
10 changes: 10 additions & 0 deletions lib/test/rrs/caa.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"id": 4096,
"zid": 4096,
"owner": "ns1.example.com.",
"ttl": 86400,
"type": "CAA",
"flags": 0,
"tag": "issue",
"value": "http://letsencrypt.org"
}
11 changes: 11 additions & 0 deletions lib/test/rrs/cert.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"id": 4096,
"zid": 4096,
"owner": "mail.example.com.",
"ttl": 86400,
"type": "CERT",
"cert type": "PGP",
"key tag": 0,
"algorithm": 0,
"certificate": "hexidecimalkeystring1"
}
8 changes: 8 additions & 0 deletions lib/test/rrs/cname.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"id": 4096,
"zid": 4096,
"owner": "ns1.example.com.",
"ttl": 86400,
"type": "CNAME",
"cname": "ns2.example.com."
}
8 changes: 8 additions & 0 deletions lib/test/rrs/dname.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"id": 4096,
"zid": 4096,
"owner": "_tcp.example.com.",
"ttl": 86400,
"type": "DNAME",
"target": "_tcp.example.net."
}
11 changes: 11 additions & 0 deletions lib/test/rrs/dnskey.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"id": 4096,
"zid": 4096,
"owner": "mail.example.com.",
"ttl": 86400,
"type": "CERT",
"cert type": "PGP",
"key tag": 0,
"algorithm": 0,
"certificate": "hexidecimalkeystring1"
}
11 changes: 11 additions & 0 deletions lib/test/rrs/ds.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"id": 4096,
"zid": 4096,
"ttl": 86400,
"type": "DS",
"owner": "dskey.example.com.",
"key tag": 60485,
"algorithm": 5,
"digest type": 1,
"digest": "( 2BB183AF5F22588179A53B0A 98631FAD1A292118 )"
}
9 changes: 9 additions & 0 deletions lib/test/rrs/hinfo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"id": 4096,
"zid": 4096,
"owner": "server-under-my-desk.example.com.",
"ttl": 86400,
"type": "HINFO",
"cpu": "PDP-11/73",
"os": "UNIX"
}
9 changes: 9 additions & 0 deletions lib/test/rrs/mx.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"id": 4096,
"zid": 4096,
"owner": "test.example.com.",
"ttl": 86400,
"type": "MX",
"preference": 0,
"exchange": "mail.example.com."
}
8 changes: 8 additions & 0 deletions lib/test/rrs/ns.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"id": 4096,
"zid": 4096,
"owner": "example.com.",
"ttl": 86400,
"type": "NS",
"dname": "ns1.example.com."
}
8 changes: 8 additions & 0 deletions lib/test/rrs/spf.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"id": 4096,
"zid": 4096,
"owner": "mar2024._domainkey.example.com.",
"ttl": 86400,
"type": "SPF",
"data": "v=DKIM1;p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAoyUzGOTSOmakY8BcxXgi0mN/nFegLBPs7aaGQUtjHfa8yUrt9T2j6GSXgdjLuG3R43WjePQv3RHzc+bwwOkdw0XDOXiztn5mhrlaflbVr5PMSTrv64/cpFQKLtgQx8Vgqp7Dh3jw13rLomRTqJFgMrMHdhIibZEa69gtuAfDqoeXo6QDSGk5JuBAeRHEH27FriHulg5ob4F4lmh7fMFVsDGkQEF6jaIVYqvRjDyyQed3R3aTJX3fpb3QrtRqvfn/LAf+3kzW58AjsERpsNCSTD2RquxbnyoR/1wdGKb8cUlD/EXvqtvpVnOzHeSeMEqex3kQI8HOGsEehWZlKd+GqwIDAQAB"
}
11 changes: 11 additions & 0 deletions lib/test/rrs/srv.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"id": 4096,
"zid": 4096,
"owner": "_imaps._tcp.example.com.",
"ttl": 86400,
"type": "SRV",
"target": "mail.example.com.",
"priority": 1,
"weight": 0,
"port": 993
}
10 changes: 10 additions & 0 deletions lib/test/rrs/sshfp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"id": 4096,
"zid": 4096,
"owner": "mail.example.com.",
"ttl": 86400,
"type": "SSHFP",
"fingerprint": "ed8c6e16fdae4f633eee6a7b8f64fdd356bbb32841d535565d777014c9ea4c26",
"fptype": 0,
"algorithm": 0
}
10 changes: 10 additions & 0 deletions lib/test/rrs/svcb.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"id": 4096,
"zid": 4096,
"owner": "_8443._foo.api.example.com.",
"priority": 0,
"target name": "svc4.example.net.",
"params": "alpn=\"bar\" port=\"8004\" ech=\"...\"",
"ttl": 86400,
"type": "SVCB"
}
8 changes: 8 additions & 0 deletions lib/test/rrs/txt.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"id": 4096,
"zid": 4096,
"owner": "mar2024._domainkey.example.com.",
"ttl": 86400,
"type": "TXT",
"data": "v=DKIM1;p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAoyUzGOTSOmakY8BcxXgi0mN/nFegLBPs7aaGQUtjHfa8yUrt9T2j6GSXgdjLuG3R43WjePQv3RHzc+bwwOkdw0XDOXiztn5mhrlaflbVr5PMSTrv64/cpFQKLtgQx8Vgqp7Dh3jw13rLomRTqJFgMrMHdhIibZEa69gtuAfDqoeXo6QDSGk5JuBAeRHEH27FriHulg5ob4F4lmh7fMFVsDGkQEF6jaIVYqvRjDyyQed3R3aTJX3fpb3QrtRqvfn/LAf+3kzW58AjsERpsNCSTD2RquxbnyoR/1wdGKb8cUlD/EXvqtvpVnOzHeSeMEqex3kQI8HOGsEehWZlKd+GqwIDAQAB"
}
10 changes: 10 additions & 0 deletions lib/test/rrs/uri.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"id": 4096,
"zid": 4096,
"owner": "www.example.com.",
"ttl": 86400,
"type": "URI",
"target": "www2.example.com.",
"priority": 0,
"weight": 0
}
132 changes: 79 additions & 53 deletions lib/zone_record.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,7 @@ class ZoneRecord {
if (g.length === 1) return g[0].id
}

try {
const zr = new RR[args.type](args)
console.log(zr)
} catch (e) {
console.error(e.message)
}
new RR[args.type](args)

args = objectToDb(args)

Expand Down Expand Up @@ -109,6 +104,9 @@ function dbToObject(rows) {
row.type = RR.typeMap[row.type_id]
delete row.type_id

const map = getMap(row.type)
if (map) unApplyMap(row, map)

for (const f of [
'description',
'other',
Expand All @@ -127,79 +125,107 @@ function dbToObject(rows) {
function objectToDb(obj) {
obj = JSON.parse(JSON.stringify(obj))

switch (obj.type) {
const map = getMap(obj.type)
if (map) applyMap(obj, map)

obj.type_id = RR.typeMap[obj.type]
delete obj.type

return obj
}

function applyMap(obj, map) {
for (const [key, value] of Object.entries(map)) {
obj[key] = obj[value]
delete obj[value]
}
}

function unApplyMap(obj, map) {
for (const [key, value] of Object.entries(map)) {
switch (value) {
case 'key tag': // DS record
case 'port': // SRV
obj[value] = parseInt(obj[key])
break
default:
obj[value] = obj[key]
}
delete obj[key]
}
}

function getMap(rrType) {
switch (rrType) {
case 'CAA':
applyMap(obj, {
return {
weight: 'flags',
priority: 'tag',
other: 'tag',
address: 'value',
})
break
}
case 'CERT':
return {
other: 'cert type',
priority: 'key tag',
weight: 'algorithm',
address: 'certificate',
}
case 'CNAME':
applyMap(obj, { address: 'cname' })
break
return { address: 'cname' }
case 'DNAME':
applyMap(obj, { address: 'target' })
break
return { address: 'target' }
case 'DNSKEY':
applyMap(obj, {
return {
address: 'public key',
weight: 'flags',
priority: 'protocol',
other: 'algorithm',
})
break
}
case 'DS':
return {
address: 'digest',
weight: 'digest type',
priority: 'algorithm',
other: 'key tag',
}
case 'HINFO':
obj.name = `${obj.cpu} ${obj.os}`
delete obj.cpu
delete obj.os
obj.address = obj.description
delete obj.description
break
return { address: 'os', other: 'cpu' }
case 'IPSECKEY':
applyMap(obj, {
return {
address: 'gateway',
description: 'publickey',
weight: 'precedence',
priority: 'gateway type',
other: 'algorithm',
})
break
}
case 'MX':
return { weight: 'preference', address: 'exchange' }
case 'NAPTR':
applyMap(obj, {
return {
weight: 'order',
priority: 'preference',
description: 'replacement',
})
obj.address = `${obj.flags} ${obj.service} ${obj.regexp}`
delete obj.flags
delete obj.service
delete obj.regexp
break
}
case 'NS':
return { address: 'dname' }
case 'SPF':
return { address: 'data' }
case 'SSHFP':
applyMap(obj, {
return {
address: 'fingerprint',
weight: 'algorithm',
priority: 'fptype',
})
break
}
case 'SRV':
applyMap(obj, { other: 'port' })
break
return { address: 'target', other: 'port' }
case 'SVCB':
return {
address: 'target name',
other: 'params',
}
case 'TXT':
return { address: 'data' }
case 'URI':
applyMap(obj, { address: 'target' })
break
}

obj.type_id = RR.typeMap[obj.type]
delete obj.type

return obj
}

function applyMap(obj, map) {
for (const [key, value] of Object.entries(map)) {
obj[key] = obj[value]
delete obj[value]
return { address: 'target' }
}
}
Loading

0 comments on commit 4619084

Please sign in to comment.