@@ -179,3 +179,139 @@ describe('describeZoneSettings', () => {
179179 expect ( lines ) . toContain ( 'already as declared' )
180180 } )
181181} )
182+
183+ describe ( 'delegating to Route 53' , ( ) => {
184+ const NAMESERVERS = [ 'ns-1.awsdns-01.org' , 'ns-2.awsdns-02.com' , 'ns-3.awsdns-03.net' , 'ns-4.awsdns-04.co.uk' ]
185+
186+ /** A zone host that keeps what it is given, as a hosted zone would. */
187+ function hostedZone ( options : { drops ?: ( record : any ) => boolean } = { } ) {
188+ const records : any [ ] = [ ]
189+ const created : string [ ] = [ ]
190+ const add = async ( _domain : string , record : any ) => {
191+ if ( ! options . drops ?.( record ) )
192+ records . push ( { ...record } )
193+ return { success : true }
194+ }
195+ return {
196+ name : 'route53' ,
197+ records,
198+ created,
199+ createZone : async ( domain : string ) => {
200+ created . push ( domain )
201+ return { id : 'Z123' , name : domain , status : 'active' , nameServers : NAMESERVERS , created : true }
202+ } ,
203+ createRecord : add ,
204+ upsertRecord : add ,
205+ deleteRecord : async ( ) => ( { success : true } ) ,
206+ listRecords : async ( ) => ( { success : true , records : records . map ( r => ( { ...r } ) ) } ) ,
207+ canManageDomain : async ( ) => true ,
208+ listDomains : async ( ) => [ 'example.com' ] ,
209+ }
210+ }
211+
212+ /** A registrar holding a zone and a set of nameservers. */
213+ function registrarWith ( records : any [ ] , nameservers = [ 'a.ns.porkbun.com' , 'b.ns.porkbun.com' ] ) {
214+ const state = { nameservers }
215+ return {
216+ name : 'porkbun' ,
217+ state,
218+ getNameServers : async ( ) => state . nameservers ,
219+ updateNameServers : async ( _domain : string , next : string [ ] ) => {
220+ state . nameservers = next
221+ return true
222+ } ,
223+ createRecord : async ( ) => ( { success : true } ) ,
224+ upsertRecord : async ( ) => ( { success : true } ) ,
225+ deleteRecord : async ( ) => ( { success : true } ) ,
226+ listRecords : async ( ) => ( { success : true , records } ) ,
227+ canManageDomain : async ( ) => true ,
228+ listDomains : async ( ) => [ 'example.com' ] ,
229+ }
230+ }
231+
232+ const RECORDS = [
233+ { name : 'example.com' , type : 'A' , content : '203.0.113.7' , ttl : 600 } ,
234+ { name : 'example.com' , type : 'MX' , content : 'mail.example.com' , priority : 10 , ttl : 600 } ,
235+ { name : 'example.com' , type : 'TXT' , content : 'v=spf1 mx -all' , ttl : 600 } ,
236+ { name : 'example.com' , type : 'NS' , content : 'a.ns.porkbun.com' , ttl : 600 } ,
237+ ]
238+ const CONFIG = { domain : 'example.com' , provider : 'route53' as const , registrar : { provider : 'porkbun' as const } }
239+
240+ it ( 'creates the zone, copies the records and points the registrar at it' , async ( ) => {
241+ const host = hostedZone ( )
242+ const registrar = registrarWith ( RECORDS )
243+ const result = await delegateZoneFromConfig ( CONFIG , { credentials : CREDS , hosts : { route53 : host as any } , registrar : registrar as any } )
244+
245+ expect ( result ) . toMatchObject ( { status : 'delegated' , domain : 'example.com' , targetNameservers : NAMESERVERS } )
246+ expect ( host . created ) . toEqual ( [ 'example.com' ] )
247+ // The registrar's own NS records are the delegation being replaced: never copied.
248+ expect ( host . records . map ( r => r . type ) . sort ( ) ) . toEqual ( [ 'A' , 'MX' , 'TXT' ] )
249+ expect ( registrar . state . nameservers ) . toEqual ( NAMESERVERS )
250+ expect ( describeDelegation ( result ) [ 0 ] ) . toContain ( 'delegated to ns-1.awsdns-01.org' )
251+ } )
252+
253+ it ( 'leaves the nameservers alone when a record did not arrive' , async ( ) => {
254+ const host = hostedZone ( { drops : record => record . type === 'MX' } )
255+ const registrar = registrarWith ( RECORDS )
256+ const result = await delegateZoneFromConfig ( CONFIG , { credentials : CREDS , hosts : { route53 : host as any } , registrar : registrar as any } )
257+
258+ expect ( result . status ) . toBe ( 'blocked' )
259+ expect ( registrar . state . nameservers ) . toEqual ( [ 'a.ns.porkbun.com' , 'b.ns.porkbun.com' ] )
260+ expect ( ( result as any ) . missing . map ( ( r : any ) => r . type ) ) . toEqual ( [ 'MX' ] )
261+ } )
262+
263+ it ( 'is a no-op once the registrar points at the hosted zone' , async ( ) => {
264+ const host = hostedZone ( )
265+ const registrar = registrarWith ( RECORDS , NAMESERVERS . map ( ns => `${ ns . toUpperCase ( ) } .` ) )
266+ const result = await delegateZoneFromConfig ( CONFIG , { credentials : CREDS , hosts : { route53 : host as any } , registrar : registrar as any } )
267+
268+ expect ( result . status ) . toBe ( 'already-delegated' )
269+ expect ( host . records ) . toHaveLength ( 0 )
270+ } )
271+
272+ it ( 'a dry run copies nothing and moves nothing' , async ( ) => {
273+ const host = hostedZone ( )
274+ const registrar = registrarWith ( RECORDS )
275+ const result = await delegateZoneFromConfig (
276+ { ...CONFIG , registrar : { provider : 'porkbun' , dryRun : true } } ,
277+ { credentials : CREDS , hosts : { route53 : host as any } , registrar : registrar as any } ,
278+ )
279+ expect ( result . status ) . toBe ( 'ready' )
280+ expect ( host . records ) . toHaveLength ( 0 )
281+ expect ( registrar . state . nameservers ) . toEqual ( [ 'a.ns.porkbun.com' , 'b.ns.porkbun.com' ] )
282+ } )
283+
284+ it ( 'says that proxied hosts mean nothing where there is no edge' , async ( ) => {
285+ const host = hostedZone ( )
286+ const result = await delegateZoneFromConfig (
287+ { ...CONFIG , registrar : { provider : 'porkbun' , proxied : [ 'example.com' ] } } ,
288+ { credentials : CREDS , hosts : { route53 : host as any } , registrar : registrarWith ( RECORDS ) as any } ,
289+ )
290+ expect ( result . status ) . toBe ( 'delegated' )
291+ expect ( host . records . every ( r => r . proxied !== true ) ) . toBe ( true )
292+ expect ( ( result as any ) . warnings . join ( ' ' ) ) . toContain ( 'proxied is ignored' )
293+ } )
294+
295+ it ( 'skips, rather than throws, without AWS credentials' , async ( ) => {
296+ const result = await delegateZoneFromConfig ( CONFIG , { credentials : { ...CREDS , aws : false } } )
297+ expect ( result ) . toMatchObject ( { status : 'skipped' } )
298+ expect ( ( result as any ) . reason ) . toContain ( 'AWS' )
299+ } )
300+
301+ it ( 'finds AWS credentials in the environment in any of the usual forms' , async ( ) => {
302+ const skipped = await delegateZoneFromConfig ( CONFIG , { env : { PORKBUN_API_KEY : 'pk' , PORKBUN_SECRET_KEY : 'sk' } } )
303+ expect ( ( skipped as any ) . reason ) . toContain ( 'AWS' )
304+ // A profile is enough to get past the credential check to the registrar's.
305+ const next = await delegateZoneFromConfig ( CONFIG , { env : { AWS_PROFILE : 'work' } } )
306+ expect ( ( next as any ) . reason ) . toContain ( 'PORKBUN' )
307+ } )
308+
309+ it ( 'does not delegate to a registrar\'s own nameservers' , async ( ) => {
310+ const result = await delegateZoneFromConfig (
311+ { domain : 'example.com' , provider : 'godaddy' , registrar : { provider : 'porkbun' } } ,
312+ { credentials : CREDS } ,
313+ )
314+ expect ( result ) . toMatchObject ( { status : 'skipped' } )
315+ expect ( ( result as any ) . reason ) . toContain ( 'delegation to godaddy is not supported' )
316+ } )
317+ } )
0 commit comments