Skip to content

Commit

Permalink
Merge branch 'master' into remove_channel_versions
Browse files Browse the repository at this point in the history
  • Loading branch information
dalehille committed Feb 15, 2021
2 parents 98d14ab + c377349 commit 7081f3c
Show file tree
Hide file tree
Showing 22 changed files with 4,222 additions and 208 deletions.
18 changes: 18 additions & 0 deletions app/apollo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,23 @@ const initModule = require(`./init.${AUTH_MODEL}`);

const pubSub = GraphqlPubSub.getInstance();

const i18next = require('i18next');
const i18nextMiddleware = require('i18next-http-middleware');
const i18nextBackend = require('i18next-fs-backend');
i18next.use(i18nextBackend).use(i18nextMiddleware.LanguageDetector).init({
//debug: true,
backend: {
loadPath: './locales/{{lng}}/razee-resources.json'
},
fallbackLng: 'en',
supportedLngs:['en', 'de', 'es', 'fr', 'it', 'ja', 'ko', 'pt-br', 'zh-cn', 'zh-tw'],
load: 'languageOnly',
saveMissing: false,
initImmediate: true,
nsSeparator: '#||#',
keySeparator: '#|#'
});

const createDefaultApp = () => {
const app = express();
app.set('trust proxy', true);
Expand All @@ -54,6 +71,7 @@ const createDefaultApp = () => {
}
return next();
});
app.use(i18nextMiddleware.handle(i18next));
return app;
};

Expand Down
68 changes: 34 additions & 34 deletions app/apollo/resolvers/channel.js

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions app/apollo/resolvers/cluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ const clusterResolvers = {
}).lean({ virtuals: true });

if(!cluster){
throw new NotFoundError(`Could not find the cluster with Id ${clusterId}.`, context);
throw new NotFoundError(context.req.t('Could not find the cluster with Id {{clusterId}}.', {'clusterId':clusterId}), context);
}

await validAuth(me, orgId, ACTIONS.READ, TYPES.CLUSTER, queryName, context, [clusterId, cluster.name]);
Expand Down Expand Up @@ -135,7 +135,7 @@ const clusterResolvers = {
}).lean({ virtuals: true });

if(!cluster){
throw new NotFoundError(`Could not find the cluster with name ${clusterName}.`, context);
throw new NotFoundError(context.req.t('Could not find the cluster with name {{clusterName}}.', {'clusterName':clusterName}), context);
}

await validAuth(me, orgId, ACTIONS.READ, TYPES.CLUSTER, queryName, context, [cluster.id, cluster.name, clusterName]);
Expand Down Expand Up @@ -328,7 +328,7 @@ const clusterResolvers = {

} catch (error) {
logger.error({req_id, user: whoIs(me), org_id, cluster_id, error } , `${queryName} error encountered`);
throw new RazeeQueryError(`Query ${queryName} error. ${error.message}`, context);
throw new RazeeQueryError(context.req.t('Query {{queryName}} error. {{error.message}}', {'queryName':queryName, 'error.message':error.message}), context);
}
}, // end delete cluster by org_id and cluster_id

Expand Down Expand Up @@ -358,7 +358,7 @@ const clusterResolvers = {

} catch (error) {
logger.error({req_id, user: whoIs(me), org_id, error } , `${queryName} error encountered`);
throw new RazeeQueryError(`Query ${queryName} error. ${error.message}`, context);
throw new RazeeQueryError(context.req.t('Query {{queryName}} error. {{error.message}}', {'queryName':queryName, 'error.message':error.message}), context);
}
}, // end delete cluster by org_id

Expand All @@ -371,19 +371,19 @@ const clusterResolvers = {

try {
if (!registration.name) {
throw new RazeeValidationError('A cluster name is not defined in the registration data', context);
throw new RazeeValidationError(context.req.t('A cluster name is not defined in the registration data'), context);
}

// validate the number of total clusters are under the limit
const total = await models.Cluster.count({org_id});
if (total >= CLUSTER_LIMITS.MAX_TOTAL ) { // *** shoud be just >
throw new RazeeValidationError(`You have exceeded the maximum amount of clusters for this org - ${org_id}`, context);
throw new RazeeValidationError(context.req.t('You have exceeded the maximum amount of clusters for this org - {{org_id}}', {'org_id':org_id}), context);
}

// validate the number of pending clusters are under the limit
const total_pending = await models.Cluster.count({org_id, reg_state: {$in: [CLUSTER_REG_STATES.REGISTERING, CLUSTER_REG_STATES.PENDING]}});
if (total_pending > CLUSTER_LIMITS.MAX_PENDING ) {
throw new RazeeValidationError(`You have exeeded the maximum amount of pending clusters for this org - ${org_id}.`, context);
throw new RazeeValidationError(context.req.t('You have exeeded the maximum amount of pending clusters for this org - {{org_id}}.', {'org_id':org_id}), context);
}

// we do not handle cluster groups here, it is handled by groupCluster Api
Expand All @@ -396,7 +396,7 @@ const clusterResolvers = {
{'metadata.name': registration.name },
]}
]}).lean()) {
throw new RazeeValidationError(`Another cluster already exists with the same registration name ${registration.name}`, context);
throw new RazeeValidationError(context.req.t('Another cluster already exists with the same registration name {{registration.name}}', {'registration.name':registration.name}), context);
}

const cluster_id = UUID();
Expand All @@ -418,7 +418,7 @@ const clusterResolvers = {
}

logger.error({ req_id, user: whoIs(me), org_id, error }, `${queryName} error encountered`);
throw new RazeeQueryError(`Query ${queryName} error. ${error.message}`, context);
throw new RazeeQueryError(context.req.t('Query {{queryName}} error. {{error.message}}', {'queryName':queryName, 'error.message':error.message}), context);
}
}, // end registerCluster

Expand Down Expand Up @@ -448,7 +448,7 @@ const clusterResolvers = {
}
} catch (error) {
logger.error({ req_id, user: whoIs(me), org_id, error }, `${queryName} error encountered`);
throw new RazeeQueryError(`Query ${queryName} error. ${error.message}`, context);
throw new RazeeQueryError(context.req.t('Query {{queryName}} error. {{error.message}}', {'queryName':queryName, 'error.message':error.message}), context);
}
}, // end enableRegistrationUrl
}
Expand Down
17 changes: 7 additions & 10 deletions app/apollo/resolvers/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ const validClusterAuth = async (me, queryName, context) => {
if(me && me.type == 'cluster'){
const result = await models.User.isValidOrgKey(models, me);
if(!result){
throw new RazeeForbiddenError(
`Invalid razee-org-key was submitted for ${queryName}`,
context
throw new RazeeForbiddenError(context.req.t(
'Invalid razee-org-key was submitted for {{queryName}}', {'queryName':queryName}), context
);
}
return;
Expand Down Expand Up @@ -153,9 +152,9 @@ const validAuth = async (me, org_id, action, type, queryName, context, attrs = n
const result = await models.User.userTokenIsAuthorized(me, org_id, action, type, context);
if(!result){
throw new RazeeForbiddenError(
`You are not allowed to ${action} on ${type} under organization ${org_id} for the query ${queryName}. (using userToken)`,
context
);
context.req.t('You are not allowed to {{action}} on {{type}} under organization {{org_id}} for the query {{queryName}}.', {'action':action, 'type':type, 'org_id':org_id, 'queryName':queryName, interpolation: { escapeValue: false }}
), context);

}
return;
}
Expand All @@ -164,10 +163,8 @@ const validAuth = async (me, org_id, action, type, queryName, context, attrs = n
if (type === TYPES.RESOURCE){
return true;
} else {
throw new RazeeForbiddenError(
`You are not allowed to ${action} on ${type} under organization ${org_id} for the query ${queryName}.`,
context
);
throw new RazeeForbiddenError(context.req.t('You are not allowed to {{action}} on {{type}} under organization {{org_id}} for the query {{queryName}}.', {'action':action, 'type':type, 'org_id':org_id, 'queryName':queryName, interpolation: { escapeValue: false }}), context);

}
}
};
Expand Down
24 changes: 12 additions & 12 deletions app/apollo/resolvers/group.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const groupResolvers = {
try{
let group = await models.Group.findOne({ org_id: orgId, uuid }).lean({ virtuals: true });
if (!group) {
throw new NotFoundError(`could not find group with uuid ${uuid}.`);
throw new NotFoundError(context.req.t('could not find group with uuid {{uuid}}.', {'uuid':uuid}), context);
}
await validAuth(me, orgId, ACTIONS.READ, TYPES.GROUP, queryName, context, [group.uuid, group.name]);
await applyQueryFieldsToGroups([group], queryFields, { orgId }, context);
Expand All @@ -77,7 +77,7 @@ const groupResolvers = {
try{
let group = await models.Group.findOne({ org_id: orgId, name }).lean({ virtuals: true });
if (!group) {
throw new NotFoundError(`could not find group with name ${name}.`);
throw new NotFoundError(context.req.t('could not find group with name {{name}}.', {'name':name}));
}
await validAuth(me, orgId, ACTIONS.READ, TYPES.GROUP, queryName, context, [group.uuid, group.name]);

Expand All @@ -101,7 +101,7 @@ const groupResolvers = {
// might not necessary with unique index. Worth to check to return error better.
const group = await models.Group.findOne({ org_id: org_id, name });
if(group){
throw new ValidationError(`The group name ${name} already exists.`);
throw new ValidationError(context.req.t('The group name {{name}} already exists.', {'name':name}));
}
const uuid = UUID();
await models.Group.create({
Expand Down Expand Up @@ -129,14 +129,14 @@ const groupResolvers = {
try{
const group = await models.Group.findOne({ uuid, org_id: org_id }).lean();
if(!group){
throw new NotFoundError(`group uuid "${uuid}" not found`);
throw new NotFoundError(context.req.t('group uuid "{{uuid}}" not found', {'uuid':uuid}));
}

await validAuth(me, org_id, ACTIONS.MANAGE, TYPES.GROUP, queryName, context, [group.uuid, group.name]);
const subCount = await models.Subscription.count({ org_id: org_id, groups: group.name });

if(subCount > 0){
throw new ValidationError(`${subCount} subscriptions depend on this cluster group. Please update/remove them before removing this group.`);
throw new ValidationError(context.req.t('{{subCount}} subscriptions depend on this cluster group. Please update/remove them before removing this group.', {'subCount':subCount}));
}

const clusterIds = await models.Cluster.distinct('cluster_id', { org_id: org_id, 'groups.uuid': group.uuid });
Expand Down Expand Up @@ -167,14 +167,14 @@ const groupResolvers = {
try{
const group = await models.Group.findOne({ name, org_id: org_id }).lean();
if(!group){
throw new NotFoundError(`group name "${name}" not found`);
throw new NotFoundError(context.req.t('group name "{{name}}" not found', {'name':name}));
}

await validAuth(me, org_id, ACTIONS.MANAGE, TYPES.GROUP, queryName, context, [group.uuid, group.name]);

const subCount = await models.Subscription.count({ org_id: org_id, groups: group.name });
if(subCount > 0){
throw new ValidationError(`${subCount} subscriptions depend on this cluster group. Please update/remove them before removing this group.`);
throw new ValidationError(context.req.t('{{subCount}} subscriptions depend on this cluster group. Please update/remove them before removing this group.', {'subCount':subCount}));
}

const uuid = group.uuid;
Expand All @@ -185,7 +185,7 @@ const groupResolvers = {

const clusterCount = await models.Cluster.count({ org_id: org_id, 'groups.uuid': group.uuid });
if(clusterCount > 0){
throw new ValidationError(`${clusterCount} clusters depend on this group. Please update/remove the group from the clusters.`);
throw new ValidationError(context.req.t('{{clusterCount}} clusters depend on this group. Please update/remove the group from the clusters.', {'clusterCount':clusterCount}));
}

await models.Group.deleteOne({ org_id: org_id, uuid:group.uuid });
Expand Down Expand Up @@ -216,7 +216,7 @@ const groupResolvers = {
try {
var groups = await models.Group.find({org_id: orgId, uuid: {$in: groupUuids}});
if (groups.length < 1) {
throw new NotFoundError('None of the passed group uuids were found');
throw new NotFoundError(context.req.t('None of the passed group uuids were found'));
}
groupUuids = _.map(groups, 'uuid');
var groupObjsToAdd = _.map(groups, (group)=>{
Expand Down Expand Up @@ -318,7 +318,7 @@ const groupResolvers = {
try {
var groups = await models.Group.find({ org_id: orgId, uuid: { $in: groupUuids } });
if (groups.length != groupUuids.length) {
throw new NotFoundError('One or more of the passed group uuids were not found');
throw new NotFoundError(context.req.t('One or more of the passed group uuids were not found'));
}
groupUuids = _.map(groups, 'uuid');
var groupObjsToAdd = _.map(groups, (group)=>{
Expand Down Expand Up @@ -356,7 +356,7 @@ const groupResolvers = {
// validate the group exits in the db first.
const group = await models.Group.findOne({ org_id: org_id, uuid });
if(!group){
throw new NotFoundError(`group uuid "${uuid}" not found`);
throw new NotFoundError(context.req.t('group uuid "{{uuid}}" not found', {'uuid':uuid}));
}

await validAuth(me, org_id, ACTIONS.MANAGE, TYPES.GROUP, queryName, context, [group.uuid, group.name]);
Expand Down Expand Up @@ -387,7 +387,7 @@ const groupResolvers = {
// validate the group exits in the db first.
const group = await models.Group.findOne({ org_id: org_id, uuid });
if(!group){
throw new NotFoundError(`group uuid "${uuid}" not found`);
throw new NotFoundError(context.req.t('group uuid "{{uuid}}" not found', {'uuid':uuid}));
}

await validAuth(me, org_id, ACTIONS.MANAGE, TYPES.GROUP, queryName, context, [group.uuid, group.name]);
Expand Down
Loading

0 comments on commit 7081f3c

Please sign in to comment.