Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions requests/Switcher API.postman_collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -3751,6 +3751,48 @@
},
"response": []
},
{
"name": "Config - Relay - Generate Verification Code",
"request": {
"method": "PATCH",
"header": [
{
"key": "Content-Type",
"name": "Content-Type",
"type": "text",
"value": "application/json"
}
],
"body": {
"mode": "raw",
"raw": "",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "{{url}}/config/relay/verificationCode/:config",
"host": [
"{{url}}"
],
"path": [
"config",
"relay",
"verificationCode",
":config"
],
"variable": [
{
"key": "config",
"value": "5e0eceb66f4f994eac9007b2"
}
]
}
},
"response": []
},
{
"name": "Config - Read Relay Specs",
"request": {
Expand Down
28 changes: 28 additions & 0 deletions src/api-docs/paths/path-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,34 @@ export default {
}
}
},
'/config/relay/verificationCode/{id}': {
patch: {
tags: ['Config'],
description: 'Config Relay generates verification code',
security: [{ bearerAuth: [] }],
parameters: [
pathParameter('id', 'Config ID', true)
],
responses: {
'200': {
description: 'Config Relay verification code generated',
content: {
'application/json': {
schema: {
type: 'object',
properties: {
code: {
type: 'string',
description: 'Verification code'
}
}
}
}
}
}
}
}
},
'/config/spec/relay': {
get: {
tags: ['Config'],
Expand Down
8 changes: 8 additions & 0 deletions src/api-docs/schemas/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ export const relay = {
additionalProperties: {
type: 'string'
}
},
verification_code: {
type: 'string',
description: 'Generated string used to verify Relay endpoint ownership'
},
verified: {
type: 'boolean',
description: 'Valid when true (default: false)'
}
}
};
Expand Down
11 changes: 11 additions & 0 deletions src/routers/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,17 @@ router.patch('/config/removeRelay/:id/:env', auth, [
}
});

router.patch('/config/relay/verificationCode/:id', auth, [
check('id').isMongoId()
], validate, async (req, res) => {
try {
const config = await Services.getRelayVerificationCode(req.params.id, req.admin);
res.send({ code: config.relay.verification_code });
} catch (e) {
responseException(res, e, 500);
}
});

router.get('/config/spec/relay', auth, (_req, res) => {
res.send(relayOptions());
});
Expand Down
15 changes: 15 additions & 0 deletions src/services/config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import mongoose from 'mongoose';
import { randomBytes } from 'crypto';
import { response } from './common';
import { Config } from '../models/config';
import { formatInput, verifyOwnership, checkEnvironmentStatusRemoval } from '../helpers';
Expand Down Expand Up @@ -258,4 +259,18 @@ export async function removeRelay(id, env, admin) {
}

return config;
}

export async function getRelayVerificationCode(id, admin) {
let config = await getConfigById(id);
config = await verifyOwnership(admin, config, config.domain, ActionTypes.UPDATE, RouterTypes.CONFIG);

const buffer = randomBytes(32);
const code = Buffer.from(buffer).toString('base64');

config.updatedBy = admin.email;
config.relay.verification_code = code;
config.relay.verified = false;

return config.save();
}
16 changes: 16 additions & 0 deletions tests/config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -978,6 +978,22 @@ describe('Testing relay association', () => {
expect(response.body).toMatchObject({ methods: [ 'POST', 'GET' ], types: [ 'VALIDATION', 'NOTIFICATION' ] });
});

test('CONFIG_SUITE - Should generate verification code', async () => {
const response = await request(app)
.patch(`/config/relay/verificationCode/${configId1}`)
.set('Authorization', `Bearer ${adminMasterAccountToken}`)
.send().expect(200);

expect(response.body.code).not.toBe(undefined);
});

test('CONFIG_SUITE - Should NOT generate verification code', async () => {
await request(app)
.patch(`/config/relay/verificationCode/${new mongoose.Types.ObjectId()}`)
.set('Authorization', `Bearer ${adminMasterAccountToken}`)
.send(bodyRelayProd).expect(404);
});

});

describe('Testing disable metrics', () => {
Expand Down