WIP create new zone

This commit is contained in:
AJ ONeal 2018-01-30 22:03:05 -07:00
parent 49b1aa0ce7
commit 92ad26d18e
3 changed files with 116 additions and 13 deletions

View File

@ -180,6 +180,15 @@ module.exports.create = function (cli, engine/*, dnsd*/) {
res.send({ zones: zones }); res.send({ zones: zones });
}); });
}); });
app.post('/api/zones', jsonParser, hasClaim('+rw@adns.org'), function (req, res) {
var upzone = req.body || {};
console.log('create zone', upzone);
engine.zones.save(upzone, function (err, zone) {
if (err) { res.send({ error: { message: err.message } }); return; }
console.log('create zone result', zone);
res.send(zone);
});
});
function mapRecord(r) { function mapRecord(r) {
return { return {
id: r.id id: r.id
@ -247,12 +256,13 @@ module.exports.create = function (cli, engine/*, dnsd*/) {
if ('SOA' === record.type) { if ('SOA' === record.type) {
// TODO be strict about what can be edited // TODO be strict about what can be edited
engine.zones.save(record, function (err/*, record*/) { engine.zones.save(record, function (err, zone) {
if (!err) { if (err) {
res.send({ success: true }); res.send({ error: { message: err.message } });
return; return;
} }
res.send({ error: { message: err.message } }); // { success: true }
res.send(zone);
}); });
} else { } else {
engine.records.save(record, function (err, record) { engine.records.save(record, function (err, record) {

View File

@ -323,7 +323,9 @@
console.log('nss:'); console.log('nss:');
console.log(nss); console.log(nss);
if (!$qs('input.js-zone-form-vanityns').checked) { zone.vanity = false;
if ($qs('input.js-zone-form-vanityns').checked) {
zone.vanity = true;
zone.vanityNs = nss; zone.vanityNs = nss;
} }
$qs('.js-zone-form-tpl').innerHTML = ''; $qs('.js-zone-form-tpl').innerHTML = '';
@ -347,7 +349,8 @@
console.log('result:', data); console.log('result:', data);
if (!zone.id) { if (!zone.id) {
zone.id = data.id; zone.id = data.id;
renderRecords(); cache.zones.push(data);
renderZones();
} }
}); });
}); });

View File

@ -94,8 +94,8 @@ module.exports.create = function (opts) {
, email_addr: domain.admin || ('admin.' + domain.name) , email_addr: domain.admin || ('admin.' + domain.name)
// serial -- the version, for cache-busting of secondary nameservers. suggested format: YYYYMMDDnn // serial -- the version, for cache-busting of secondary nameservers. suggested format: YYYYMMDDnn
, serial: domain.serial || Math.round((domain.updatedAt || domain.createdAt || 0) / 1000) , serial: domain.serial || engine.zones._dateToSerial(domain.updatedAt || domain.createdAt || Date.now())
, sn: domain.serial || Math.round((domain.updatedAt || domain.createdAt || 0) / 1000) , sn: domain.serial || engine.zones._dateToSerial(domain.updatedAt || domain.createdAt || Date.now())
// refresh -- only used when nameservers following the DNS NOTIFY spec talk // refresh -- only used when nameservers following the DNS NOTIFY spec talk
, refresh: domain.refresh || 1800 , refresh: domain.refresh || 1800
@ -106,8 +106,8 @@ module.exports.create = function (opts) {
, ret: domain.retry || 600 , ret: domain.retry || 600
// expiration -- how long other nameservers should continue when the primary goes down // expiration -- how long other nameservers should continue when the primary goes down
, expiration: domain.expiration || 2419200 , expiration: domain.expiration || 2419200 // 4 weeks
, ex: domain.expiration || 2419200 , ex: domain.expiration || 2419200 // 4 weeks
// minimum -- how long to cache a non-existent domain (also the default ttl for BIND) // minimum -- how long to cache a non-existent domain (also the default ttl for BIND)
, minimum: domain.minimum || 5 , minimum: domain.minimum || 5
@ -138,7 +138,15 @@ module.exports.create = function (opts) {
} }
}; };
engine.zones = { engine.zones = {
all: function (cb) { _immutableKeys: [ 'id', 'name', 'primary', 'serial', 'revokedAt', 'changedAt', 'insertedAt', 'updatedAt', 'deletedAt' ]
, _mutableKeys: [ 'admin', 'expiration', 'minimum', 'refresh', 'retry', 'ttl', 'vanity' ]
, _dateToSerial: function (date) {
// conventionally the format is YYYYMMDDxx,
// but since it's an integer and I don't want to keep track of incrementing xx,
// epoch in seconds will do
return parseInt(Math.round(date/1000).toString().slice(-10), 10);
}
, all: function (cb) {
process.nextTick(function () { process.nextTick(function () {
cb(null, db.zones.slice(0).filter(notDeleted)); cb(null, db.zones.slice(0).filter(notDeleted));
}); });
@ -203,8 +211,7 @@ module.exports.create = function (opts) {
console.log(existing); console.log(existing);
console.log(zone); console.log(zone);
Object.keys(zone).forEach(function (key) { Object.keys(zone).forEach(function (key) {
var keys = [ 'name', 'id', 'revokedAt', 'changedAt', 'insertedAt', 'updatedAt', 'deletedAt' ]; if (-1 !== engine.zones._immutableKeys.indexOf(key)) { return; }
if (-1 !== keys.indexOf(key)) { return; }
if (existing[key] !== zone[key]) { if (existing[key] !== zone[key]) {
dirty = true; dirty = true;
console.log('existing key', key, existing[key], zone[key]); console.log('existing key', key, existing[key], zone[key]);
@ -222,6 +229,89 @@ module.exports.create = function (opts) {
cb(err, !err && existing || null); cb(err, !err && existing || null);
}); });
} }
, create: function (zone, cb) {
var newZone = { id: crypto.randomBytes(16).toString('hex') };
var existing;
var nss = [];
zone.name = (zone.name||'').toLowerCase();
db.zones.some(function (z) {
if (z.name === zone.name) {
existing = z;
return true;
}
});
if (existing) {
cb(new Error("tried to create new zone, but '" + existing.name + "' already exists"));
return;
}
newZone.name = zone.name;
newZone.createdAt = Date.now();
newZone.updatedAt = newZone.createdAt;
Object.keys(zone).forEach(function (key) {
//if (-1 !== engine.zones._immutableKeys.indexOf(key)) { return; }
if (-1 === engine.zones._mutableKeys.indexOf(key)) { return; }
newZone[key] = zone[key];
});
// TODO create NS and A records for normal and vanity nameservers
if (zone.vanity) {
newZone.vanity = true;
} else {
newZone.vanity = false;
}
db.primaryNameservers.forEach(function (ns, i) {
var nsx = 'ns' + (i + 1);
var nsZone;
var ttl = 43200; // 12h // TODO pick a well-reasoned number
var now = Date.now();
if (zone.vanity) {
nsZone = nsx + '.' + newZone.name;
} else {
nsZone = ns.name;
}
// NS example.com ns1.example.com 43200
nss.push({
id: crypto.randomBytes(16).toString('hex')
, createdAt: Date.now()
, updatedAt: Date.now()
, changedAt: Date.now()
, zone: newZone.name
, soa: true
, type: 'NS'
, data: nsZone
, name: newZone.name
, ttl: ttl
});
// A ns1.example.com 127.0.0.1 43200
nss.push({
id: crypto.randomBytes(16).toString('hex')
, createdAt: now
, updatedAt: now
, changedAt: now
, zone: newZone.name
, soa: true
, type: ns.type
, name: nsZone
, address: ns.address
, ttl: 43200 // 12h // TODO pick a good number
});
});
db.zones.push(newZone);
nss.forEach(function (ns) {
db.records.push(ns);
});
console.log('saving...');
db.save(function (err) {
cb(err, !err && newZone || null);
});
}
}; };
engine.records = { engine.records = {
all: function (cb) { all: function (cb) {