Verified Commit 90e00548 authored by Sebastiaan Deckers's avatar Sebastiaan Deckers 馃悜

feat: IPv4 address as body payload for DDNS

parent aa54f175
......@@ -4,11 +4,21 @@
Scope: `ddns`
Only available for machine-to-machine applications using [client credentials](https://auth0.com/docs/api-auth/tutorials/client-credentials), i.e. client ID and client secret.
Dynamic DNS for edge servers to update the DNS type A record.
Dynamic DNS for edge servers. The server's DNS type A record is updated to point at the IPv4 source address of the API request.
Requires [client credentials](https://auth0.com/docs/api-auth/tutorials/client-credentials), i.e. client ID and client secret. Only available for machine-to-machine applications.
Responds with `200` status code and the ipv4 address.
Request body must include the public IPv4 address of the server.
Body:
```js
{
ipv4: '10.20.30.40'
}
```
Responds with `200` status code and the `ipv4` address.
Response:
......
......@@ -44,8 +44,16 @@ module.exports = async (fastify, options) => {
fastify.route({
method: 'PUT',
url: '/servers/:id/ddns',
schema: {
body: {
type: 'object',
properties: {
ipv4: { type: 'string' }
}
}
},
handler: async (request, reply) => {
const { ip, params: { id: serverId } } = request
const { body: { ipv4: ip }, params: { id: serverId } } = request
if (!serverIdRegex.test(serverId)) {
throw new BadRequest('Invalid server ID')
}
......
......@@ -42,7 +42,7 @@ test('Mock Constellix API', async (t) => {
ttl: 60,
roundRobin: [{
disableFlag: false,
value: '127.0.0.1'
value: '10.20.30.40'
}]
})
response.end()
......@@ -67,25 +67,24 @@ test('Setup database fixtures', async (t) => {
await mongo.db.collection('servers').deleteMany()
await mongo.db.collection('servers').insertMany([
{
// IP will change to 127.0.0.1
// IP will change to 10.20.30.40
serverId: 'aa-zzz-1',
modified: new Date(),
allowDdns: true,
ipv4: '10.20.30.40'
ipv4: '10.10.10.10'
},
{
// DDNS disabled
serverId: 'aa-zzz-2',
modified: new Date(),
allowDdns: false,
ipv4: '127.0.0.1'
allowDdns: false
},
{
// Same IP already set
serverId: 'aa-zzz-3',
modified: new Date(),
allowDdns: true,
ipv4: '127.0.0.1'
ipv4: '10.20.30.40'
}
])
})
......@@ -97,12 +96,14 @@ test('Update DDNS', async (t) => {
const response = await fetch(url, {
method: 'PUT',
headers: {
'content-type': 'application/json',
'authorization': `Bearer ${await getDdnsAccessToken()}`
}
},
body: JSON.stringify({ ipv4: '10.20.30.40' })
})
t.is(response.status, HTTP_STATUS_OK)
const body = await response.json()
t.deepEqual(body, { ipv4: '127.0.0.1' })
t.deepEqual(body, { ipv4: '10.20.30.40' })
})
test('Reject if DDNS is not enabled', async (t) => {
......@@ -111,7 +112,11 @@ test('Reject if DDNS is not enabled', async (t) => {
const url = `${origin}/v2/servers/aa-zzz-2/ddns`
const response = await fetch(url, {
method: 'PUT',
headers: { 'authorization': `Bearer ${await getDdnsAccessToken()}` }
headers: {
'content-type': 'application/json',
'authorization': `Bearer ${await getDdnsAccessToken()}`
},
body: JSON.stringify({ ipv4: '10.20.30.40' })
})
t.is(response.status, HTTP_STATUS_FORBIDDEN)
})
......@@ -122,11 +127,15 @@ test('Idempotent if same IP is already set', async (t) => {
const url = `${origin}/v2/servers/aa-zzz-3/ddns`
const response = await fetch(url, {
method: 'PUT',
headers: { 'authorization': `Bearer ${await getDdnsAccessToken()}` }
headers: {
'content-type': 'application/json',
'authorization': `Bearer ${await getDdnsAccessToken()}`
},
body: JSON.stringify({ ipv4: '10.20.30.40' })
})
t.is(response.status, HTTP_STATUS_OK)
const body = await response.json()
t.deepEqual(body, { ipv4: '127.0.0.1' })
t.deepEqual(body, { ipv4: '10.20.30.40' })
})
test('Reject if not a valid server ID', async (t) => {
......@@ -135,7 +144,11 @@ test('Reject if not a valid server ID', async (t) => {
const url = `${origin}/v2/servers/foo-bar-baz/ddns`
const response = await fetch(url, {
method: 'PUT',
headers: { 'authorization': `Bearer ${await getDdnsAccessToken()}` }
headers: {
'content-type': 'application/json',
'authorization': `Bearer ${await getDdnsAccessToken()}`
},
body: JSON.stringify({ ipv4: '10.20.30.40' })
})
t.is(response.status, HTTP_STATUS_BAD_REQUEST)
})
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment