Skip to content

Commit

Permalink
Merge branch 'master' into cluster_subscription_gql
Browse files Browse the repository at this point in the history
  • Loading branch information
dalehille committed May 26, 2020
2 parents b1aab40 + baad123 commit 7844787
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 31 deletions.
4 changes: 4 additions & 0 deletions app/apollo/models/channel.schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ const ChannelSchema = new mongoose.Schema({
location: {
type: String,
},
created: {
type: Date,
default: Date.now,
}
}
],
});
Expand Down
33 changes: 19 additions & 14 deletions app/apollo/resolvers/channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
const _ = require('lodash');
const { v4: UUID } = require('uuid');
const crypto = require('crypto');
const { UserInputError, ValidationError } = require('apollo-server');

const { ACTIONS, TYPES } = require('../models/const');
const { whoIs, validAuth } = require ('./common');
const { whoIs, validAuth, NotFoundError} = require ('./common');

const { encryptOrgData, decryptOrgData} = require('../../utils/orgs');

Expand Down Expand Up @@ -65,18 +66,18 @@ const channelResolvers = {

const channel = await models.Channel.findOne({ uuid: channel_uuid, org_id });
if(!channel){
throw `Could not find the channel with uuid "${channel_uuid}"`;
throw new NotFoundError(`Could not find the channel with uuid ${channel_uuid}.`);
}

const versionObj = channel.versions.find(v => v.uuid === version_uuid);
if (!versionObj) {
throw `versionObj "${version_uuid}" is not found for ${channel.name}:${channel.uuid}.`;
throw NotFoundError(`versionObj "${version_uuid}" is not found for ${channel.name}:${channel.uuid}`);
}

if (versionObj.location === 'mongo') {
const deployableVersionObj = await models.DeployableVersion.findOne({org_id, channel_id: channel_uuid, uuid: version_uuid });
if (!deployableVersionObj) {
throw `DeployableVersion is not found for ${channel.name}:${channel.uuid}/${versionObj.name}:${versionObj.uuid}.`;
throw new NotFoundError(`DeployableVersion is not found for ${channel.name}:${channel.uuid}/${versionObj.name}:${versionObj.uuid}.`);
}
deployableVersionObj.content = await decryptOrgData(orgKey, deployableVersionObj.content);
return deployableVersionObj;
Expand All @@ -97,9 +98,13 @@ const channelResolvers = {
logger.debug({ req_id, user: whoIs(me), org_id, name }, `${queryName} enter`);
await validAuth(me, org_id, ACTIONS.MANAGE, TYPES.CHANNEL, queryName, context);

try{
try {
// might not necessary with uunique index. Worth to check to return error better.
const channel = await models.Channel.findOne({ name, org_id });
if(channel){
throw new ValidationError(`The channel name ${name} already exists.`);
}
const uuid = UUID();

await models.Channel.create({
_id: UUID(),
uuid, org_id, name, versions: [],
Expand All @@ -121,7 +126,7 @@ const channelResolvers = {
try{
const channel = await models.Channel.findOne({ uuid, org_id });
if(!channel){
throw `channel uuid "${uuid}" not found`;
throw new NotFoundError(`channel uuid "${uuid}" not found`);
}

await models.Channel.updateOne({ org_id, uuid }, { $set: { name } });
Expand All @@ -147,7 +152,7 @@ const channelResolvers = {
const orgKey = _.first(org.orgKeys);

if(!name){
throw 'A name was not included';
throw new UserInputError('A name was not included');
}
if(!type){
throw 'A "type" of application/json or application/yaml must be included';
Expand All @@ -158,7 +163,7 @@ const channelResolvers = {

const channel = await models.Channel.findOne({ uuid: channel_uuid, org_id });
if(!channel){
throw `channel uuid "${channel_uuid}" not found`;
throw new NotFoundError(`channel uuid "${channel_uuid}" not found`);
}

const versions = await models.DeployableVersion.find({ org_id, channel_id: channel_uuid });
Expand All @@ -167,7 +172,7 @@ const channelResolvers = {
});

if(versionNameExists) {
throw `The version name ${name} already exists`;
throw new ValidationError(`The version name ${name} already exists`);
}

const iv = crypto.randomBytes(16);
Expand Down Expand Up @@ -227,13 +232,13 @@ const channelResolvers = {
type,
};

const dObj = await models.DeployableVersion.create(deployableVersionObj);
const versionObj = {
uuid: deployableVersionObj.uuid,
name, description, location,
created: dObj.created
};

await models.DeployableVersion.create(deployableVersionObj);

await models.Channel.updateOne(
{ org_id, uuid: channel.uuid },
{ $push: { versions: versionObj } }
Expand All @@ -253,14 +258,14 @@ const channelResolvers = {
try{
const channel = await models.Channel.findOne({ uuid, org_id });
if(!channel){
throw `channel uuid "${uuid}" not found`;
throw new NotFoundError(`channel uuid "${uuid}" not found`);
}
const channel_uuid = channel.uuid;

const subCount = await models.Subscription.count({ org_id, channel_uuid });

if(subCount > 0){
throw `${subCount} subscriptions depend on this channel. Please update/remove them before removing this channel.`;
throw new ValidationError(`${subCount} subscriptions depend on this channel. Please update/remove them before removing this channel.`);
}

await models.Channel.deleteOne({ org_id, uuid });
Expand Down
21 changes: 15 additions & 6 deletions app/apollo/resolvers/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const { AuthenticationError } = require('apollo-server');
const { ForbiddenError, ApolloError } = require('apollo-server');


const whoIs = me => {
if (me === null || me === undefined) return 'null';
Expand All @@ -33,7 +34,7 @@ const validAuth = async (me, org_id, action, type, queryName, context) => {
if(me && me.type == 'userToken'){
const result = await models.User.userTokenIsAuthorized(me, org_id, action, type, null, context);
if(!result){
throw new AuthenticationError(
throw new ForbiddenError(
`You are not allowed to ${action} on ${type} under organization ${org_id} for the query ${queryName}. (using userToken)`,
);
}
Expand All @@ -44,19 +45,27 @@ const validAuth = async (me, org_id, action, type, queryName, context) => {
if(me && me.type == 'cluster'){
const result = await models.User.isValidOrgKey(models, me);
if(!result){
throw new AuthenticationError(
throw new ForbiddenError(
`You are not allowed to ${action} on ${type} for the query ${queryName}. (using razee-org-key)`,
);
}
return;
}

if (me === null || !(await models.User.isAuthorized(me, org_id, action, type, null, context))) {
logger.error({req_id, me: whoIs(me), org_id, action, type}, `AuthenticationError - ${queryName}`);
throw new AuthenticationError(
logger.error({req_id, me: whoIs(me), org_id, action, type}, `ForbiddenError - ${queryName}`);
throw new ForbiddenError(
`You are not allowed to ${action} on ${type} under organization ${org_id} for the query ${queryName}.`,
);
}
};

module.exports = { whoIs, validAuth };
// Not Found Error when look up db
class NotFoundError extends ApolloError {
constructor(message) {
super(message, 'NOT_FOUND');
Object.defineProperty(this, 'name', { value: 'NotFoundError' });
}
}

module.exports = { whoIs, validAuth, NotFoundError };
14 changes: 7 additions & 7 deletions app/apollo/resolvers/subscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const { v4: UUID } = require('uuid');
const { withFilter } = require('apollo-server');
const { ForbiddenError } = require('apollo-server');
const { ACTIONS, TYPES } = require('../models/const');
const { whoIs, validAuth } = require ('./common');
const { whoIs, validAuth, NotFoundError } = require ('./common');
const getSubscriptionUrls = require('../../utils/subscriptions.js').getSubscriptionUrls;
const tagsStrToArr = require('../../utils/subscriptions.js').tagsStrToArr;
const { EVENTS, GraphqlPubSub, getStreamingTopic } = require('../subscription');
Expand Down Expand Up @@ -118,15 +118,15 @@ const subscriptionResolvers = {
// loads the channel
var channel = await models.Channel.findOne({ org_id, uuid: channel_uuid });
if(!channel){
throw `channel uuid "${channel_uuid}" not found`;
throw new NotFoundError(`channel uuid "${channel_uuid}" not found`);
}

// loads the version
var version = channel.versions.find((version)=>{
return (version.uuid == version_uuid);
});
if(!version){
throw `version uuid "${version_uuid}" not found`;
throw new NotFoundError(`version uuid "${version_uuid}" not found`);
}

await models.Subscription.create({
Expand Down Expand Up @@ -155,21 +155,21 @@ const subscriptionResolvers = {
try{
var subscription = await models.Subscription.findOne({ org_id, uuid });
if(!subscription){
throw `subscription { uuid: "${uuid}", org_id:${org_id} } not found`;
throw new NotFoundError(`subscription { uuid: "${uuid}", org_id:${org_id} } not found`);
}

// loads the channel
var channel = await models.Channel.findOne({ org_id, uuid: channel_uuid });
if(!channel){
throw `channel uuid "${channel_uuid}" not found`;
throw new NotFoundError(`channel uuid "${channel_uuid}" not found`);
}

// loads the version
var version = channel.versions.find((version)=>{
return (version.uuid == version_uuid);
});
if(!version){
throw `version uuid "${version_uuid}" not found`;
throw new NotFoundError(`version uuid "${version_uuid}" not found`);
}

var sets = {
Expand Down Expand Up @@ -200,7 +200,7 @@ const subscriptionResolvers = {
try{
var subscription = await models.Subscription.findOne({ org_id, uuid });
if(!subscription){
throw `subscription uuid "${uuid}" not found`;
throw new NotFoundError(`subscription uuid "${uuid}" not found`);
}
await subscription.deleteOne();

Expand Down
3 changes: 2 additions & 1 deletion app/apollo/schema/channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ const channelSchema = gql`
uuid: String!
name: String!
description: String
location: String!
location: String!
created: Date
}
type Channel {
uuid: String!
Expand Down
12 changes: 12 additions & 0 deletions app/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,18 @@ async function initialize(){
options: { name: 'org_id.cluster_id'}
}
],
channels: [
{
keys: { org_id: 1, name: 1 },
options: { name: 'org_id.name', unique: true }
}
],
deployableVersions: [
{
keys: { org_id: 1, channel_id: 1, name: 1},
options: { name: 'org_id.channel_id.name', unique: true }
}
],
resourceStats:[
{
keys: { org_id: 1 },
Expand Down
8 changes: 6 additions & 2 deletions app/routes/kube/kube.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@
*/

const express = require('express');
const asyncHandler = require('express-async-handler');
const router = express.Router();

// /kube/liveness
const kube = router.get('/liveness', (req, res) => {
const kube = router.get('/liveness', asyncHandler(async(req, res) => {
// does a db call to make sure we didnt disconnect
await require('../../apollo/models').models.Cluster.find({}, { _id:1 }, { limit:1 });

return res.sendStatus(200);
});
}));

module.exports = kube;
2 changes: 1 addition & 1 deletion kubernetes/razeedash-api/resource.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ items:
path: /api/kube/liveness
port: 3333
initialDelaySeconds: 5
periodSeconds: 10
periodSeconds: 20
timeoutSeconds: 2
resources:
requests:
Expand Down

0 comments on commit 7844787

Please sign in to comment.