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
19 changes: 9 additions & 10 deletions requests/Switcher API.postman_collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -3786,7 +3786,7 @@
"variable": [
{
"key": "config",
"value": "5e0eceb66f4f994eac9007b2"
"value": "5f504cc2aaea6121c4a35bfc"
}
]
}
Expand Down Expand Up @@ -3815,26 +3815,25 @@
}
},
"url": {
"raw": "{{url}}/config/relay/verify/:config?code=",
"raw": "{{url}}/config/relay/verify/:config/:environment",
"host": [
"{{url}}"
],
"path": [
"config",
"relay",
"verify",
":config"
],
"query": [
{
"key": "code",
"value": ""
}
":config",
":environment"
],
"variable": [
{
"key": "config",
"value": "5e0eceb66f4f994eac9007b2"
"value": "5f504cc2aaea6121c4a35bfc"
},
{
"key": "environment",
"value": "default"
}
]
}
Expand Down
8 changes: 4 additions & 4 deletions src/api-docs/paths/path-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,18 +302,18 @@ export default {
}
}
},
'/config/relay/verify/{id}': {
'/config/relay/verify/{id}/{env}': {
patch: {
tags: ['Config'],
description: 'Verify Config Relay ownership based on given verification code',
description: 'Verify Config Relay ownership',
security: [{ bearerAuth: [] }],
parameters: [
pathParameter('id', 'Config ID', true),
queryParameter('code', 'Verification code', true, 'string')
pathParameter('env', 'Environment name', true)
],
responses: {
'200': {
description: 'Config Relay verification code generated',
description: 'Verify Config Relay using [GET] endpoint/verify',
content: {
'application/json': {
schema: {
Expand Down
7 changes: 5 additions & 2 deletions src/api-docs/schemas/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,11 @@ export const relay = {
description: 'Generated string used to verify Relay endpoint ownership'
},
verified: {
type: 'boolean',
description: 'Valid when true (default: false)'
type: 'object',
additionalProperties: {
type: 'boolean'
},
description: 'Defines when Relay endpoint is verified'
}
}
};
Expand Down
18 changes: 15 additions & 3 deletions src/client/relay/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ export async function resolveValidation(relay, entry, environment) {
};
}

export async function resolveVerification(relay, environment) {
const endpoint = relay.endpoint.get(environment)?.replace(/\/$/, '');
const url = `${endpoint?.substring(0, endpoint.lastIndexOf('/'))}/verify`;
const header = createHeader(relay.auth_prefix, relay.auth_token.get(environment));
const response = await get(url, `?code=${relay.verification_code}`, header);

return response.data?.code;
}

async function post(url, data, headers) {
try {
return await axios.post(url, data, headers);
Expand Down Expand Up @@ -68,9 +77,12 @@ function createHeader(auth_prefix, auth_token, environment) {

headers['Content-Type'] = 'application/json';

if (auth_token && environment in auth_token &&
auth_prefix && auth_token[environment]) {
headers['Authorization'] = `${auth_prefix} ${auth_token[environment]}`;
if (environment) {
if (auth_token && environment in auth_token && auth_prefix) {
headers['Authorization'] = `${auth_prefix} ${auth_token[environment]}`;
}
} else if (auth_token && auth_prefix) {
headers['Authorization'] = `${auth_prefix} ${auth_token}`;
}

return {
Expand Down
2 changes: 1 addition & 1 deletion src/client/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ async function resolveRelay(config, environment, entry, response) {
try {
if (config.relay?.activated[environment]) {
isRelayValid(config.relay);
isRelayVerified(config.relay);
isRelayVerified(config.relay, environment);

if (config.relay.type === RelayTypes.NOTIFICATION) {
resolveNotification(config.relay, entry, environment);
Expand Down
11 changes: 6 additions & 5 deletions src/models/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,9 @@ const configSchema = new mongoose.Schema({
type: String
},
verified: {
type: Boolean,
default: false
type: Map,
of: Boolean,
default: new Map()
}
}
}, {
Expand Down Expand Up @@ -156,11 +157,11 @@ async function recordConfigHistory(config, modifiedField) {
}

function hasRelayEndpointUpdates(config, modifiedField) {
const hasUpdate = modifiedField.filter(field => field.indexOf('relay.endpoint') >= 0);
const hasUpdate = modifiedField.filter(field => field.indexOf('relay.endpoint.') >= 0);

if (hasUpdate.length) {
config.relay.verified = false;
config.relay.verification_code = undefined;
const environment = hasUpdate[0].split('.')[2];
config.relay.verified.set(environment, false);
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/routers/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,12 @@ router.patch('/config/relay/verificationCode/:id', auth, [
}
});

router.patch('/config/relay/verify/:id', auth, [
router.patch('/config/relay/verify/:id/:env', auth, [
check('id').isMongoId(),
query('code').isAscii()
check('env').isLength({ min: 1 })
], validate, async (req, res) => {
try {
const result = await Services.verifyRelay(req.params.id, req.query.code, req.admin);
const result = await Services.verifyRelay(req.params.id, req.params.env, req.admin);
res.send({ status: result });
} catch (e) {
responseException(res, e, 500);
Expand Down
14 changes: 8 additions & 6 deletions src/services/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { checkSwitcher } from '../external/switcher-api-facade';
import { BadRequestError, NotFoundError } from '../exceptions';
import { checkEnvironmentStatusChange_v2 } from '../middleware/validators';
import { getComponentById, getComponents } from './component';
import { resolveVerification } from '../client/relay';

async function verifyAddComponentInput(configId, admin) {
const config = await getConfigById(configId);
Expand Down Expand Up @@ -251,6 +252,7 @@ export async function removeRelay(id, env, admin) {
config.relay.activated.delete(env);
config.relay.endpoint.delete(env);
config.relay.auth_token.delete(env);
config.relay.verified.delete(env);
} else {
config.relay = {};
}
Expand All @@ -268,17 +270,17 @@ export async function getRelayVerificationCode(id, admin) {

config.updatedBy = admin.email;
config.relay.verification_code = randomUUID();
config.relay.verified = false;

return config.save();
}

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

if (!config.relay.verified && Object.is(config.relay.verification_code, code)) {
config.relay.verified = true;
const code = await resolveVerification(config.relay, env);
if (!config.relay.verified?.get(env) && Object.is(config.relay.verification_code, code)) {
config.relay.verified.set(env, true);
await config.save();
return 'verified';
}
Expand All @@ -299,12 +301,12 @@ export function isRelayValid(relay) {
throw new BadRequestError('HTTPS required');
}

export function isRelayVerified(relay) {
export function isRelayVerified(relay, environment) {
const bypass = process.env.RELAY_BYPASS_VERIFICATION === 'true' || false;

if (bypass)
return;

if (!relay.verified)
if (!relay.verified[environment])
throw new BadRequestError('Relay not verified');
}
Loading