2018-05-06 18:57:27 -07:00
'use strict' ;
exports = module . exports = {
2021-08-13 17:22:28 -07:00
removePrivateFields ,
injectPrivateFields ,
upsert ,
get ,
del ,
wait ,
2022-01-05 22:41:41 -08:00
verifyDomainConfig
2018-05-06 18:57:27 -07:00
} ;
2025-08-14 11:17:38 +05:30
const assert = require ( 'node: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 18:57:27 -07:00
debug = require ( 'debug' ) ( 'box:dns/gandi' ) ,
2022-02-04 13:58:29 -08:00
dig = require ( '../dig.js' ) ,
2021-08-13 17:22:28 -07:00
dns = require ( '../dns.js' ) ,
2022-02-04 13:58:29 -08:00
safe = require ( 'safetydance' ) ,
2025-07-10 10:55:52 +02:00
superagent = require ( '@cloudron/superagent' ) ,
2019-01-04 18:44:54 -08:00
waitForDns = require ( './waitfordns.js' ) ;
2018-05-06 18:57:27 -07:00
2021-08-13 17:22:28 -07:00
const GANDI _API = 'https://dns.api.gandi.net/api/v5' ;
2018-05-06 18:57:27 -07:00
function formatError ( response ) {
2025-02-14 17:26:54 +01:00
return ` Gandi DNS error [ ${ response . status } ] ${ response . text } ` ;
2018-05-06 18:57:27 -07:00
}
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
}
2024-10-08 17:06:53 +02:00
function createRequest ( method , url , domainConfig ) {
assert . strictEqual ( typeof method , 'string' ) ;
assert . strictEqual ( typeof url , 'string' ) ;
assert . strictEqual ( typeof domainConfig , 'object' ) ;
2025-02-14 17:26:54 +01:00
const request = superagent . request ( method , url ) . timeout ( 30 * 1000 ) . ok ( ( ) => true ) ;
2024-10-08 17:06:53 +02:00
// https://api.gandi.net/docs/authentication/
if ( domainConfig . tokenType === 'ApiKey' ) {
request . set ( 'X-Api-Key' , domainConfig . token ) ;
request . set ( 'Authorization' , ` Apikey ${ domainConfig . token } ` ) ;
} else { // PAT
request . set ( 'Authorization' , ` Bearer ${ domainConfig . token } ` ) ;
}
return request ;
}
2022-02-04 13:58:29 -08:00
async function upsert ( domainObject , location , type , values ) {
2019-01-04 18:44:54 -08:00
assert . strictEqual ( typeof domainObject , 'object' ) ;
assert . strictEqual ( typeof location , 'string' ) ;
2018-05-06 18:57:27 -07:00
assert . strictEqual ( typeof type , 'string' ) ;
2021-05-02 11:26:08 -07:00
assert ( Array . isArray ( values ) ) ;
2018-05-06 18:57:27 -07:00
2022-01-05 22:41:41 -08:00
const domainConfig = domainObject . config ,
2019-01-04 18:44:54 -08:00
zoneName = domainObject . zoneName ,
2021-08-13 17:22:28 -07:00
name = dns . getName ( domainObject , location , type ) || '@' ;
2018-05-06 18:57:27 -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 18:57:27 -07:00
2022-02-04 13:58:29 -08:00
const data = {
2018-05-06 18:57:27 -07:00
'rrset_ttl' : 300 , // this is the minimum allowed
2018-05-06 22:14:39 -07:00
'rrset_values' : values // for mx records, value is already of the '<priority> <server>' format
2018-05-06 18:57:27 -07:00
} ;
2024-10-08 17:06:53 +02:00
const [ error , response ] = await safe ( createRequest ( 'PUT' , ` ${ GANDI _API } /domains/ ${ zoneName } /records/ ${ name } / ${ type } ` , domainConfig )
. send ( data ) ) ;
2022-02-04 13:58:29 -08:00
2024-11-19 17:08:19 +05:30
if ( error ) throw new BoxError ( BoxError . NETWORK _ERROR , error ) ;
2025-02-14 17:26:54 +01:00
if ( response . status === 403 || response . status === 401 ) throw new BoxError ( BoxError . ACCESS _DENIED , formatError ( response ) ) ;
if ( response . status === 400 ) throw new BoxError ( BoxError . BAD _FIELD , formatError ( response ) ) ;
if ( response . status !== 201 ) throw new BoxError ( BoxError . EXTERNAL _ERROR , formatError ( response ) ) ;
2018-05-06 18:57:27 -07:00
}
2022-02-04 13:58:29 -08:00
async function get ( domainObject , location , type ) {
2019-01-04 18:44:54 -08:00
assert . strictEqual ( typeof domainObject , 'object' ) ;
assert . strictEqual ( typeof location , 'string' ) ;
2018-05-06 18:57:27 -07:00
assert . strictEqual ( typeof type , 'string' ) ;
2022-01-05 22:41:41 -08:00
const domainConfig = domainObject . config ,
2019-01-04 18:44:54 -08:00
zoneName = domainObject . zoneName ,
2021-08-13 17:22:28 -07:00
name = dns . getName ( domainObject , location , type ) || '@' ;
2018-05-06 18:57:27 -07:00
2019-01-04 18:44:54 -08:00
debug ( ` get: ${ name } in zone ${ zoneName } of type ${ type } ` ) ;
2018-05-06 18:57:27 -07:00
2024-10-08 17:06:53 +02:00
const [ error , response ] = await safe ( createRequest ( 'GET' , ` ${ GANDI _API } /domains/ ${ zoneName } /records/ ${ name } / ${ type } ` , domainConfig ) ) ;
2018-05-06 18:57:27 -07:00
2024-11-19 17:08:19 +05:30
if ( error ) throw new BoxError ( BoxError . NETWORK _ERROR , error ) ;
2025-02-14 17:26:54 +01:00
if ( response . status === 403 || response . status === 401 ) throw new BoxError ( BoxError . ACCESS _DENIED , formatError ( response ) ) ;
if ( response . status === 404 ) return [ ] ;
if ( response . status !== 200 ) throw new BoxError ( BoxError . EXTERNAL _ERROR , formatError ( response ) ) ;
2018-05-06 18:57:27 -07:00
2022-02-04 13:58:29 -08:00
return response . body . rrset _values ;
2018-05-06 18:57:27 -07:00
}
2022-02-04 13:58:29 -08:00
async function del ( domainObject , location , type , values ) {
2019-01-04 18:44:54 -08:00
assert . strictEqual ( typeof domainObject , 'object' ) ;
assert . strictEqual ( typeof location , 'string' ) ;
2018-05-06 18:57:27 -07:00
assert . strictEqual ( typeof type , 'string' ) ;
2021-05-02 11:26:08 -07:00
assert ( Array . isArray ( values ) ) ;
2018-05-06 18:57:27 -07:00
2022-01-05 22:41:41 -08:00
const domainConfig = domainObject . config ,
2019-01-04 18:44:54 -08:00
zoneName = domainObject . zoneName ,
2021-08-13 17:22:28 -07:00
name = dns . getName ( domainObject , location , type ) || '@' ;
2018-05-06 18:57:27 -07: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-06 18:57:27 -07:00
2024-10-08 17:06:53 +02:00
const [ error , response ] = await safe ( createRequest ( 'DELETE' , ` ${ GANDI _API } /domains/ ${ zoneName } /records/ ${ name } / ${ type } ` , domainConfig ) ) ;
2018-05-06 18:57:27 -07:00
2024-11-19 17:08:19 +05:30
if ( error ) throw new BoxError ( BoxError . NETWORK _ERROR , error ) ;
2025-02-14 17:26:54 +01:00
if ( response . status === 404 ) return ;
if ( response . status === 403 || response . status === 401 ) throw new BoxError ( BoxError . ACCESS _DENIED , formatError ( response ) ) ;
if ( response . status !== 204 ) throw new BoxError ( BoxError . EXTERNAL _ERROR , formatError ( response ) ) ;
2018-05-06 18:57:27 -07:00
}
2022-02-03 16:15:14 -08:00
async function wait ( domainObject , subdomain , type , value , options ) {
2019-01-04 18:44:54 -08:00
assert . strictEqual ( typeof domainObject , 'object' ) ;
2022-02-03 16:15:14 -08:00
assert . strictEqual ( typeof subdomain , 'string' ) ;
2019-01-04 18:44:54 -08:00
assert . strictEqual ( typeof type , 'string' ) ;
assert . strictEqual ( typeof value , 'string' ) ;
assert ( options && typeof options === 'object' ) ; // { interval: 5000, times: 50000 }
2022-11-28 21:23:06 +01:00
const fqdn = dns . fqdn ( subdomain , domainObject . domain ) ;
2019-01-04 18:44:54 -08:00
2022-02-03 16:15:14 -08:00
await waitForDns ( fqdn , domainObject . zoneName , type , value , options ) ;
2019-01-04 18:44:54 -08:00
}
2022-02-04 13:58:29 -08:00
async function verifyDomainConfig ( domainObject ) {
2019-01-04 18:44:54 -08:00
assert . strictEqual ( typeof domainObject , 'object' ) ;
2018-05-06 18:57:27 -07:00
2022-01-05 22:41:41 -08:00
const domainConfig = domainObject . config ,
2019-01-04 18:44:54 -08:00
zoneName = domainObject . zoneName ;
2022-02-04 13:58:29 -08:00
if ( ! domainConfig . token || typeof domainConfig . token !== 'string' ) throw new BoxError ( BoxError . BAD _FIELD , 'token must be a non-empty string' ) ;
2024-10-08 17:06:53 +02:00
if ( domainConfig . tokenType !== 'PAT' && domainConfig . tokenType !== 'ApiKey' ) throw new BoxError ( BoxError . BAD _FIELD , 'tokenType is required' ) ;
2025-03-02 07:27:09 +01:00
if ( 'customNameservers' in domainConfig && typeof domainConfig . customNameservers !== 'boolean' ) throw new BoxError ( BoxError . BAD _FIELD , 'customNameservers must be a boolean' ) ;
2018-06-17 21:44:08 -07:00
2022-02-04 13:58:29 -08:00
const credentials = {
2024-10-08 17:06:53 +02:00
token : domainConfig . token ,
tokenType : domainConfig . tokenType ,
2025-03-02 07:27:09 +01:00
customNameservers : ! ! domainConfig . customNameservers
2018-05-06 18:57:27 -07:00
} ;
2019-01-04 18:44:54 -08:00
const ip = '127.0.0.1' ;
2023-10-01 13:52:19 +05:30
if ( constants . TEST ) return credentials ; // this shouldn't be here
2018-05-06 18:57:27 -07:00
2022-02-04 13:58:29 -08:00
const [ error , nameservers ] = await safe ( dig . resolve ( zoneName , 'NS' , { timeout : 5000 } ) ) ;
if ( error && error . code === 'ENOTFOUND' ) throw new BoxError ( BoxError . BAD _FIELD , 'Unable to resolve nameservers for this domain' ) ;
if ( error || ! nameservers ) throw new BoxError ( BoxError . BAD _FIELD , error ? error . message : 'Unable to get nameservers' ) ;
2018-05-06 18:57:27 -07:00
2022-02-04 13:58:29 -08:00
if ( ! nameservers . every ( function ( n ) { return n . toLowerCase ( ) . indexOf ( '.gandi.net' ) !== - 1 ; } ) ) {
debug ( 'verifyDomainConfig: %j does not contain Gandi NS' , nameservers ) ;
2025-03-02 07:27:09 +01:00
if ( ! domainConfig . customNameservers ) throw new BoxError ( BoxError . BAD _FIELD , 'Domain nameservers are not set to Gandi' ) ;
2022-02-04 13:58:29 -08:00
}
2018-05-06 18:57:27 -07:00
2022-02-04 13:58:29 -08:00
const location = 'cloudrontestdns' ;
2018-05-06 18:57:27 -07:00
2022-02-04 13:58:29 -08:00
await upsert ( domainObject , location , 'A' , [ ip ] ) ;
debug ( 'verifyDomainConfig: Test A record added' ) ;
2018-05-06 18:57:27 -07:00
2022-02-04 13:58:29 -08:00
await del ( domainObject , location , 'A' , [ ip ] ) ;
debug ( 'verifyDomainConfig: Test A record removed again' ) ;
2018-05-06 18:57:27 -07:00
2022-02-04 13:58:29 -08:00
return credentials ;
2018-05-06 18:57:27 -07:00
}