2018-05-09 12:24:33 +02:00
'use strict' ;
exports = module . exports = {
2019-02-08 11:11:49 +01:00
removePrivateFields : removePrivateFields ,
2019-02-09 19:08:15 +01:00
injectPrivateFields : injectPrivateFields ,
2018-05-09 12:24:33 +02:00
upsert : upsert ,
get : get ,
del : del ,
2019-01-04 18:44:54 -08:00
wait : wait ,
2018-05-09 12:24:33 +02:00
verifyDnsConfig : verifyDnsConfig
} ;
var assert = require ( 'assert' ) ,
2019-10-23 10:02:04 -07:00
BoxError = require ( '../boxerror.js' ) ,
2020-05-14 23:01:44 +02:00
constants = require ( '../constants.js' ) ,
2018-05-09 12:24:33 +02:00
debug = require ( 'debug' ) ( 'box:dns/namecom' ) ,
dns = require ( '../native-dns.js' ) ,
2019-01-04 18:44:54 -08:00
domains = require ( '../domains.js' ) ,
safe = require ( 'safetydance' ) ,
superagent = require ( 'superagent' ) ,
util = require ( 'util' ) ,
waitForDns = require ( './waitfordns.js' ) ;
2018-05-09 12:24:33 +02:00
const NAMECOM _API = 'https://api.name.com/v4' ;
function formatError ( response ) {
return ` Name.com DNS error [ ${ response . statusCode } ] ${ response . text } ` ;
}
2019-02-08 11:11:49 +01:00
function removePrivateFields ( domainObject ) {
2020-05-14 23:01:44 +02:00
domainObject . config . token = constants . SECRET _PLACEHOLDER ;
2019-02-08 11:11:49 +01:00
return domainObject ;
}
2019-02-09 19:08:15 +01:00
function injectPrivateFields ( newConfig , currentConfig ) {
2020-05-14 23:01:44 +02:00
if ( newConfig . token === constants . SECRET _PLACEHOLDER ) newConfig . token = currentConfig . token ;
2019-02-09 19:08:15 +01:00
}
2019-01-04 18:44:54 -08:00
function addRecord ( dnsConfig , zoneName , name , type , values , callback ) {
2018-05-09 12:24:33 +02:00
assert . strictEqual ( typeof dnsConfig , 'object' ) ;
assert . strictEqual ( typeof zoneName , 'string' ) ;
2019-01-04 18:44:54 -08:00
assert . strictEqual ( typeof name , 'string' ) ;
2018-05-09 12:24:33 +02:00
assert . strictEqual ( typeof type , 'string' ) ;
assert ( Array . isArray ( values ) ) ;
assert . strictEqual ( typeof callback , 'function' ) ;
2019-01-04 18:44:54 -08:00
debug ( ` add: ${ name } in zone ${ zoneName } of type ${ type } with values ${ JSON . stringify ( values ) } ` ) ;
2018-05-09 12:24:33 +02:00
var data = {
2019-01-04 18:44:54 -08:00
host : name ,
2018-05-09 12:24:33 +02:00
type : type ,
ttl : 300 // 300 is the lowest
} ;
2018-05-24 09:38:01 -07:00
if ( type === 'MX' ) {
data . priority = parseInt ( values [ 0 ] . split ( ' ' ) [ 0 ] , 10 ) ;
data . answer = values [ 0 ] . split ( ' ' ) [ 1 ] ;
2020-05-14 01:03:08 +02:00
} else if ( type === 'TXT' ) {
// we have to strip the quoting for some odd reason for name.com! If you change that also change updateRecord
let tmp = values [ 0 ] ;
data . answer = tmp . indexOf ( '"' ) === 0 && tmp . lastIndexOf ( '"' ) === tmp . length - 1 ? tmp . slice ( 1 , tmp . length - 1 ) : tmp ;
2018-05-24 09:38:01 -07:00
} else {
data . answer = values [ 0 ] ;
}
2018-05-09 12:24:33 +02:00
superagent . post ( ` ${ NAMECOM _API } /domains/ ${ zoneName } /records ` )
. auth ( dnsConfig . username , dnsConfig . token )
. timeout ( 30 * 1000 )
. send ( data )
. end ( function ( error , result ) {
2019-10-23 10:02:04 -07:00
if ( error && ! error . response ) return callback ( new BoxError ( BoxError . NETWORK _ERROR , error . message ) ) ;
if ( result . statusCode === 403 ) return callback ( new BoxError ( BoxError . ACCESS _DENIED , formatError ( result ) ) ) ;
if ( result . statusCode !== 200 ) return callback ( new BoxError ( BoxError . EXTERNAL _ERROR , formatError ( result ) ) ) ;
2018-05-09 12:24:33 +02:00
2018-05-09 18:58:09 +02:00
return callback ( null , 'unused-id' ) ;
2018-05-09 12:24:33 +02:00
} ) ;
2018-05-24 09:38:01 -07:00
}
2018-05-09 12:24:33 +02:00
2019-01-04 18:44:54 -08:00
function updateRecord ( dnsConfig , zoneName , recordId , name , type , values , callback ) {
2018-05-09 12:24:33 +02:00
assert . strictEqual ( typeof dnsConfig , 'object' ) ;
assert . strictEqual ( typeof zoneName , 'string' ) ;
assert . strictEqual ( typeof recordId , 'number' ) ;
2019-01-04 18:44:54 -08:00
assert . strictEqual ( typeof name , 'string' ) ;
2018-05-09 12:24:33 +02:00
assert . strictEqual ( typeof type , 'string' ) ;
assert ( Array . isArray ( values ) ) ;
assert . strictEqual ( typeof callback , 'function' ) ;
2019-01-04 18:44:54 -08:00
debug ( ` update: ${ recordId } on ${ name } in zone ${ zoneName } of type ${ type } with values ${ JSON . stringify ( values ) } ` ) ;
2018-05-09 12:24:33 +02:00
var data = {
2019-01-04 18:44:54 -08:00
host : name ,
2018-05-09 12:24:33 +02:00
type : type ,
ttl : 300 // 300 is the lowest
} ;
2018-05-24 09:38:01 -07:00
if ( type === 'MX' ) {
data . priority = parseInt ( values [ 0 ] . split ( ' ' ) [ 0 ] , 10 ) ;
data . answer = values [ 0 ] . split ( ' ' ) [ 1 ] ;
2020-05-14 01:03:08 +02:00
} else if ( type === 'TXT' ) {
// we have to strip the quoting for some odd reason for name.com! If you change that also change addRecord
let tmp = values [ 0 ] ;
data . answer = tmp . indexOf ( '"' ) === 0 && tmp . lastIndexOf ( '"' ) === tmp . length - 1 ? tmp . slice ( 1 , tmp . length - 1 ) : tmp ;
2018-05-24 09:38:01 -07:00
} else {
data . answer = values [ 0 ] ;
}
2018-05-09 12:24:33 +02:00
superagent . put ( ` ${ NAMECOM _API } /domains/ ${ zoneName } /records/ ${ recordId } ` )
. auth ( dnsConfig . username , dnsConfig . token )
. timeout ( 30 * 1000 )
. send ( data )
. end ( function ( error , result ) {
2019-10-23 10:02:04 -07:00
if ( error && ! error . response ) return callback ( new BoxError ( BoxError . NETWORK _ERROR , error . message ) ) ;
if ( result . statusCode === 403 ) return callback ( new BoxError ( BoxError . ACCESS _DENIED , formatError ( result ) ) ) ;
if ( result . statusCode !== 200 ) return callback ( new BoxError ( BoxError . EXTERNAL _ERROR , formatError ( result ) ) ) ;
2018-05-09 12:24:33 +02:00
2018-06-29 22:25:34 +02:00
return callback ( null ) ;
2018-05-09 12:24:33 +02:00
} ) ;
}
2019-01-04 18:44:54 -08:00
function getInternal ( dnsConfig , zoneName , name , type , callback ) {
2018-05-09 12:24:33 +02:00
assert . strictEqual ( typeof dnsConfig , 'object' ) ;
assert . strictEqual ( typeof zoneName , 'string' ) ;
2019-01-04 18:44:54 -08:00
assert . strictEqual ( typeof name , 'string' ) ;
2018-05-09 12:24:33 +02:00
assert . strictEqual ( typeof type , 'string' ) ;
assert . strictEqual ( typeof callback , 'function' ) ;
2019-01-04 18:44:54 -08:00
debug ( ` getInternal: ${ name } in zone ${ zoneName } of type ${ type } ` ) ;
2018-05-09 12:24:33 +02:00
superagent . get ( ` ${ NAMECOM _API } /domains/ ${ zoneName } /records ` )
. auth ( dnsConfig . username , dnsConfig . token )
. timeout ( 30 * 1000 )
. end ( function ( error , result ) {
2019-10-23 10:02:04 -07:00
if ( error && ! error . response ) return callback ( new BoxError ( BoxError . NETWORK _ERROR , error . message ) ) ;
if ( result . statusCode === 403 ) return callback ( new BoxError ( BoxError . ACCESS _DENIED , formatError ( result ) ) ) ;
if ( result . statusCode !== 200 ) return callback ( new BoxError ( BoxError . EXTERNAL _ERROR , formatError ( result ) ) ) ;
2018-05-09 12:24:33 +02:00
// name.com does not return the correct content-type
result . body = safe . JSON . parse ( result . text ) ;
if ( ! result . body . records ) result . body . records = [ ] ;
result . body . records . forEach ( function ( r ) {
// name.com api simply strips empty properties
2019-06-11 20:32:41 +02:00
r . host = r . host || '' ;
2018-05-09 12:24:33 +02:00
} ) ;
var results = result . body . records . filter ( function ( r ) {
2019-01-04 18:44:54 -08:00
return ( r . host === name && r . type === type ) ;
2018-05-09 12:24:33 +02:00
} ) ;
debug ( 'getInternal: %j' , results ) ;
return callback ( null , results ) ;
} ) ;
}
2019-01-04 18:44:54 -08:00
function upsert ( domainObject , location , type , values , callback ) {
assert . strictEqual ( typeof domainObject , 'object' ) ;
assert . strictEqual ( typeof location , 'string' ) ;
2018-05-09 12:24:33 +02:00
assert . strictEqual ( typeof type , 'string' ) ;
2019-01-04 18:44:54 -08:00
assert ( util . isArray ( values ) ) ;
2018-05-09 12:24:33 +02:00
assert . strictEqual ( typeof callback , 'function' ) ;
2019-01-04 18:44:54 -08:00
const dnsConfig = domainObject . config ,
zoneName = domainObject . zoneName ,
2019-06-11 20:32:41 +02:00
name = domains . getName ( domainObject , location , type ) || '' ;
2018-05-09 12:24:33 +02:00
2019-01-04 18:44:54 -08:00
debug ( ` upsert: ${ name } in zone ${ zoneName } of type ${ type } with values ${ JSON . stringify ( values ) } ` ) ;
2018-05-09 12:24:33 +02:00
2019-01-04 18:44:54 -08:00
getInternal ( dnsConfig , zoneName , name , type , function ( error , result ) {
2018-05-09 12:24:33 +02:00
if ( error ) return callback ( error ) ;
2019-01-04 18:44:54 -08:00
if ( result . length === 0 ) return addRecord ( dnsConfig , zoneName , name , type , values , callback ) ;
2018-05-09 12:24:33 +02:00
2019-01-04 18:44:54 -08:00
return updateRecord ( dnsConfig , zoneName , result [ 0 ] . id , name , type , values , callback ) ;
2018-05-09 12:24:33 +02:00
} ) ;
}
2019-01-04 18:44:54 -08:00
function get ( domainObject , location , type , callback ) {
assert . strictEqual ( typeof domainObject , 'object' ) ;
assert . strictEqual ( typeof location , 'string' ) ;
2018-05-09 12:24:33 +02:00
assert . strictEqual ( typeof type , 'string' ) ;
assert . strictEqual ( typeof callback , 'function' ) ;
2019-01-04 18:44:54 -08:00
const dnsConfig = domainObject . config ,
zoneName = domainObject . zoneName ,
2019-06-11 20:32:41 +02:00
name = domains . getName ( domainObject , location , type ) || '' ;
2019-01-04 18:44:54 -08:00
getInternal ( dnsConfig , zoneName , name , type , function ( error , result ) {
2018-05-09 12:24:33 +02:00
if ( error ) return callback ( error ) ;
var tmp = result . map ( function ( record ) { return record . answer ; } ) ;
debug ( 'get: %j' , tmp ) ;
return callback ( null , tmp ) ;
} ) ;
}
2019-01-04 18:44:54 -08:00
function del ( domainObject , location , type , values , callback ) {
assert . strictEqual ( typeof domainObject , 'object' ) ;
assert . strictEqual ( typeof location , 'string' ) ;
2018-05-09 12:24:33 +02:00
assert . strictEqual ( typeof type , 'string' ) ;
2019-01-04 18:44:54 -08:00
assert ( util . isArray ( values ) ) ;
2018-05-09 12:24:33 +02:00
assert . strictEqual ( typeof callback , 'function' ) ;
2019-01-04 18:44:54 -08:00
const dnsConfig = domainObject . config ,
zoneName = domainObject . zoneName ,
2019-06-11 20:32:41 +02:00
name = domains . getName ( domainObject , location , type ) || '' ;
2018-05-09 12:24:33 +02:00
2019-01-04 18:44:54 -08:00
debug ( ` del: ${ name } in zone ${ zoneName } of type ${ type } with values ${ JSON . stringify ( values ) } ` ) ;
2018-05-09 12:24:33 +02:00
2019-01-04 18:44:54 -08:00
getInternal ( dnsConfig , zoneName , name , type , function ( error , result ) {
2018-05-09 12:24:33 +02:00
if ( error ) return callback ( error ) ;
if ( result . length === 0 ) return callback ( ) ;
superagent . del ( ` ${ NAMECOM _API } /domains/ ${ zoneName } /records/ ${ result [ 0 ] . id } ` )
. auth ( dnsConfig . username , dnsConfig . token )
. timeout ( 30 * 1000 )
. end ( function ( error , result ) {
2019-10-23 10:02:04 -07:00
if ( error && ! error . response ) return callback ( new BoxError ( BoxError . NETWORK _ERROR , error . message ) ) ;
if ( result . statusCode === 403 ) return callback ( new BoxError ( BoxError . ACCESS _DENIED , formatError ( result ) ) ) ;
if ( result . statusCode !== 200 ) return callback ( new BoxError ( BoxError . EXTERNAL _ERROR , formatError ( result ) ) ) ;
2018-05-09 12:24:33 +02:00
return callback ( null ) ;
} ) ;
} ) ;
}
2019-01-04 18:44:54 -08:00
function wait ( domainObject , location , type , value , options , callback ) {
assert . strictEqual ( typeof domainObject , 'object' ) ;
assert . strictEqual ( typeof location , 'string' ) ;
assert . strictEqual ( typeof type , 'string' ) ;
assert . strictEqual ( typeof value , 'string' ) ;
assert ( options && typeof options === 'object' ) ; // { interval: 5000, times: 50000 }
2018-05-09 12:24:33 +02:00
assert . strictEqual ( typeof callback , 'function' ) ;
2019-01-04 18:44:54 -08:00
const fqdn = domains . fqdn ( location , domainObject ) ;
waitForDns ( fqdn , domainObject . zoneName , type , value , options , callback ) ;
}
function verifyDnsConfig ( domainObject , callback ) {
assert . strictEqual ( typeof domainObject , 'object' ) ;
assert . strictEqual ( typeof callback , 'function' ) ;
const dnsConfig = domainObject . config ,
zoneName = domainObject . zoneName ;
2019-10-23 10:02:04 -07:00
if ( typeof dnsConfig . username !== 'string' ) return callback ( new BoxError ( BoxError . BAD _FIELD , 'username must be a string' , { field : 'username' } ) ) ;
if ( typeof dnsConfig . token !== 'string' ) return callback ( new BoxError ( BoxError . BAD _FIELD , 'token must be a string' , { field : 'token' } ) ) ;
2018-08-25 18:12:55 -07:00
2018-05-09 12:24:33 +02:00
var credentials = {
username : dnsConfig . username ,
2018-09-11 21:24:04 -07:00
token : dnsConfig . token
2018-05-09 12:24:33 +02:00
} ;
2019-01-04 18:44:54 -08:00
const ip = '127.0.0.1' ;
2018-05-09 12:24:33 +02:00
if ( process . env . BOX _ENV === 'test' ) return callback ( null , credentials ) ; // this shouldn't be here
dns . resolve ( zoneName , 'NS' , { timeout : 5000 } , function ( error , nameservers ) {
2019-10-23 10:02:04 -07:00
if ( error && error . code === 'ENOTFOUND' ) return callback ( new BoxError ( BoxError . BAD _FIELD , 'Unable to resolve nameservers for this domain' , { field : 'nameservers' } ) ) ;
if ( error || ! nameservers ) return callback ( new BoxError ( BoxError . BAD _FIELD , error ? error . message : 'Unable to get nameservers' , { field : 'nameservers' } ) ) ;
2018-05-09 12:24:33 +02:00
if ( ! nameservers . every ( function ( n ) { return n . toLowerCase ( ) . indexOf ( '.name.com' ) !== - 1 ; } ) ) {
debug ( 'verifyDnsConfig: %j does not contain Name.com NS' , nameservers ) ;
2019-10-23 10:02:04 -07:00
return callback ( new BoxError ( BoxError . BAD _FIELD , 'Domain nameservers are not set to name.com' , { field : 'nameservers' } ) ) ;
2018-05-09 12:24:33 +02:00
}
2019-01-04 18:44:54 -08:00
const location = 'cloudrontestdns' ;
2018-05-09 12:24:33 +02:00
2019-01-04 18:44:54 -08:00
upsert ( domainObject , location , 'A' , [ ip ] , function ( error ) {
2018-05-09 12:24:33 +02:00
if ( error ) return callback ( error ) ;
2019-01-04 18:44:54 -08:00
debug ( 'verifyDnsConfig: Test A record added' ) ;
2018-05-09 12:24:33 +02:00
2019-01-04 18:44:54 -08:00
del ( domainObject , location , 'A' , [ ip ] , function ( error ) {
2018-05-09 12:24:33 +02:00
if ( error ) return callback ( error ) ;
debug ( 'verifyDnsConfig: Test A record removed again' ) ;
callback ( null , credentials ) ;
} ) ;
} ) ;
} ) ;
}