2018-05-06 22:22:42 -07: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-06 22:22:42 -07:00
upsert : upsert ,
get : get ,
del : del ,
2019-01-04 18:44:54 -08:00
wait : wait ,
2018-05-06 22:22:42 -07: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-06 22:22:42 -07:00
debug = require ( 'debug' ) ( 'box:dns/godaddy' ) ,
dns = require ( '../native-dns.js' ) ,
2019-01-04 18:44:54 -08:00
domains = require ( '../domains.js' ) ,
2018-05-06 22:22:42 -07:00
superagent = require ( 'superagent' ) ,
2019-01-04 18:44:54 -08:00
util = require ( 'util' ) ,
waitForDns = require ( './waitfordns.js' ) ;
2018-05-06 22:22:42 -07:00
// const GODADDY_API_OTE = 'https://api.ote-godaddy.com/v1/domains';
const GODADDY _API = 'https://api.godaddy.com/v1/domains' ;
2018-05-07 08:17:56 -07:00
// this is a workaround for godaddy not having a delete API
// https://stackoverflow.com/questions/39347464/delete-record-libcloud-godaddy-api
const GODADDY _INVALID _IP = '0.0.0.0' ;
2018-09-28 14:35:45 -07:00
const GODADDY _INVALID _TXT = '""' ;
2018-05-07 08:17:56 -07:00
2018-05-06 22:22:42 -07:00
function formatError ( response ) {
return util . format ( ` GoDaddy DNS error [ ${ response . statusCode } ] ${ response . body . message } ` ) ;
}
2019-02-08 11:11:49 +01:00
function removePrivateFields ( domainObject ) {
2020-05-14 23:01:44 +02:00
domainObject . config . apiSecret = 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 . apiSecret === constants . SECRET _PLACEHOLDER ) newConfig . apiSecret = currentConfig . apiSecret ;
2019-02-09 19:08:15 +01:00
}
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-06 22:22:42 -07:00
assert . strictEqual ( typeof type , 'string' ) ;
assert ( util . isArray ( values ) ) ;
assert . strictEqual ( typeof callback , 'function' ) ;
2019-01-04 18:44:54 -08:00
const dnsConfig = domainObject . config ,
zoneName = domainObject . zoneName ,
name = domains . getName ( domainObject , location , type ) || '@' ;
2018-05-06 22:22:42 -07: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-06 22:22:42 -07:00
var records = [ ] ;
values . forEach ( function ( value ) {
var record = { ttl : 600 } ; // 600 is the min ttl
if ( type === 'MX' ) {
record . priority = parseInt ( value . split ( ' ' ) [ 0 ] , 10 ) ;
record . data = value . split ( ' ' ) [ 1 ] ;
} else {
record . data = value ;
}
records . push ( record ) ;
} ) ;
2019-01-04 18:44:54 -08:00
superagent . put ( ` ${ GODADDY _API } / ${ zoneName } /records/ ${ type } / ${ name } ` )
2018-05-06 22:22:42 -07:00
. set ( 'Authorization' , ` sso-key ${ dnsConfig . apiKey } : ${ dnsConfig . apiSecret } ` )
. timeout ( 30 * 1000 )
. send ( records )
. 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 || result . statusCode === 401 ) return callback ( new BoxError ( BoxError . ACCESS _DENIED , formatError ( result ) ) ) ;
if ( result . statusCode === 400 ) return callback ( new BoxError ( BoxError . BAD _FIELD , formatError ( result ) ) ) ; // no such zone
if ( result . statusCode === 422 ) return callback ( new BoxError ( BoxError . BAD _FIELD , formatError ( result ) ) ) ; // conflict
if ( result . statusCode !== 200 ) return callback ( new BoxError ( BoxError . EXTERNAL _ERROR , formatError ( result ) ) ) ;
2018-05-06 22:22:42 -07:00
2018-06-29 22:25:34 +02:00
return callback ( null ) ;
2018-05-06 22:22:42 -07: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-06 22:22:42 -07: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 ,
name = domains . getName ( domainObject , location , type ) || '@' ;
2018-05-06 22:22:42 -07:00
2019-01-04 18:44:54 -08:00
debug ( ` get: ${ name } in zone ${ zoneName } of type ${ type } ` ) ;
2018-05-06 22:22:42 -07:00
2019-01-04 18:44:54 -08:00
superagent . get ( ` ${ GODADDY _API } / ${ zoneName } /records/ ${ type } / ${ name } ` )
2018-05-06 22:22:42 -07:00
. set ( 'Authorization' , ` sso-key ${ dnsConfig . apiKey } : ${ dnsConfig . apiSecret } ` )
. 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 || result . statusCode === 401 ) return callback ( new BoxError ( BoxError . ACCESS _DENIED , formatError ( result ) ) ) ;
2018-05-06 22:22:42 -07:00
if ( result . statusCode === 404 ) return callback ( null , [ ] ) ;
2019-10-23 10:02:04 -07:00
if ( result . statusCode !== 200 ) return callback ( new BoxError ( BoxError . EXTERNAL _ERROR , formatError ( result ) ) ) ;
2018-05-06 22:22:42 -07:00
debug ( 'get: %j' , result . body ) ;
var values = result . body . map ( function ( record ) { return record . data ; } ) ;
2018-05-07 08:17:56 -07:00
if ( values . length === 1 && values [ 0 ] === GODADDY _INVALID _IP ) return callback ( null , [ ] ) ; // pretend this record doesn't exist
2018-05-06 22:22:42 -07:00
return callback ( null , values ) ;
} ) ;
}
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-06 22:22:42 -07:00
assert . strictEqual ( typeof type , 'string' ) ;
assert ( util . isArray ( values ) ) ;
assert . strictEqual ( typeof callback , 'function' ) ;
2019-01-04 18:44:54 -08:00
const dnsConfig = domainObject . config ,
zoneName = domainObject . zoneName ,
name = domains . getName ( domainObject , location , type ) || '@' ;
2018-05-06 22:22:42 -07:00
2019-01-04 18:44:54 -08:00
debug ( ` get: ${ name } in zone ${ zoneName } of type ${ type } with values ${ JSON . stringify ( values ) } ` ) ;
2018-05-06 22:22:42 -07:00
2019-12-04 10:29:06 -08:00
if ( type !== 'A' && type !== 'TXT' ) return callback ( new BoxError ( BoxError . EXTERNAL _ERROR , 'Record deletion is not supported by GoDaddy API' ) ) ;
2018-05-07 08:17:56 -07:00
// check if the record exists at all so that we don't insert the "Dead" record for no reason
2019-01-04 18:44:54 -08:00
get ( domainObject , location , type , function ( error , values ) {
2018-05-07 08:17:56 -07:00
if ( error ) return callback ( error ) ;
if ( values . length === 0 ) return callback ( ) ;
// godaddy does not have a delete API. so fill it up with an invalid IP that we can ignore in future get()
var records = [ {
ttl : 600 ,
2018-09-28 14:35:45 -07:00
data : type === 'A' ? GODADDY _INVALID _IP : GODADDY _INVALID _TXT
2018-05-07 08:17:56 -07:00
} ] ;
2019-01-04 18:44:54 -08:00
superagent . put ( ` ${ GODADDY _API } / ${ zoneName } /records/ ${ type } / ${ name } ` )
2018-05-07 08:17:56 -07:00
. set ( 'Authorization' , ` sso-key ${ dnsConfig . apiKey } : ${ dnsConfig . apiSecret } ` )
. send ( records )
. 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 ) ) ;
2018-05-07 08:17:56 -07:00
if ( result . statusCode === 404 ) return callback ( null ) ;
2019-10-23 10:02:04 -07:00
if ( result . statusCode === 403 || result . statusCode === 401 ) return callback ( new BoxError ( BoxError . ACCESS _DENIED , formatError ( result ) ) ) ;
if ( result . statusCode !== 200 ) return callback ( new BoxError ( BoxError . EXTERNAL _ERROR , formatError ( result ) ) ) ;
2018-05-07 08:17:56 -07:00
debug ( 'del: done' ) ;
return callback ( null ) ;
} ) ;
} ) ;
2018-05-06 22:22:42 -07:00
}
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 }
assert . strictEqual ( typeof callback , 'function' ) ;
const fqdn = domains . fqdn ( location , domainObject ) ;
waitForDns ( fqdn , domainObject . zoneName , type , value , options , callback ) ;
}
function verifyDnsConfig ( domainObject , callback ) {
assert . strictEqual ( typeof domainObject , 'object' ) ;
2018-05-06 22:22:42 -07:00
assert . strictEqual ( typeof callback , 'function' ) ;
2019-01-04 18:44:54 -08:00
const dnsConfig = domainObject . config ,
zoneName = domainObject . zoneName ;
2019-10-23 10:02:04 -07:00
if ( ! dnsConfig . apiKey || typeof dnsConfig . apiKey !== 'string' ) return callback ( new BoxError ( BoxError . BAD _FIELD , 'apiKey must be a non-empty string' , { field : 'apiKey' } ) ) ;
if ( ! dnsConfig . apiSecret || typeof dnsConfig . apiSecret !== 'string' ) return callback ( new BoxError ( BoxError . BAD _FIELD , 'apiSecret must be a non-empty string' , { field : 'apiSecret' } ) ) ;
2018-06-17 21:44:08 -07:00
2019-01-04 18:44:54 -08:00
const ip = '127.0.0.1' ;
2018-05-06 22:22:42 -07:00
var credentials = {
apiKey : dnsConfig . apiKey ,
2018-09-11 21:24:04 -07:00
apiSecret : dnsConfig . apiSecret
2018-05-06 22:22:42 -07: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-06 22:22:42 -07:00
if ( ! nameservers . every ( function ( n ) { return n . toLowerCase ( ) . indexOf ( '.domaincontrol.com' ) !== - 1 ; } ) ) {
debug ( 'verifyDnsConfig: %j does not contain GoDaddy NS' , nameservers ) ;
2019-10-23 10:02:04 -07:00
return callback ( new BoxError ( BoxError . BAD _FIELD , 'Domain nameservers are not set to GoDaddy' , { field : 'nameservers' } ) ) ;
2018-05-06 22:22:42 -07:00
}
2019-01-04 18:44:54 -08:00
const location = 'cloudrontestdns' ;
2018-05-06 22:22:42 -07:00
2019-01-04 18:44:54 -08:00
upsert ( domainObject , location , 'A' , [ ip ] , function ( error ) {
2018-05-06 22:22:42 -07:00
if ( error ) return callback ( error ) ;
2019-01-04 18:44:54 -08:00
debug ( 'verifyDnsConfig: Test A record added' ) ;
2018-05-06 22:22:42 -07:00
2019-01-04 18:44:54 -08:00
del ( domainObject , location , 'A' , [ ip ] , function ( error ) {
2018-05-06 22:22:42 -07:00
if ( error ) return callback ( error ) ;
debug ( 'verifyDnsConfig: Test A record removed again' ) ;
callback ( null , credentials ) ;
} ) ;
} ) ;
} ) ;
}