Skip to content

Commit

Permalink
Merge pull request #783 from razee-io/kube_owner_name
Browse files Browse the repository at this point in the history
added kubeOwnerName to subscriptions on creation
  • Loading branch information
dalehille committed Feb 18, 2021
2 parents d697f62 + 65b3aef commit bb7b890
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 16 deletions.
3 changes: 3 additions & 0 deletions app/apollo/models/subscription.schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ const SubscriptionSchema = new mongoose.Schema({
owner: {
type: String,
},
kubeOwnerName: {
type: String,
},
created: {
type: Date,
default: Date.now,
Expand Down
14 changes: 10 additions & 4 deletions app/apollo/models/user.default.schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ const UserDefaultSchema = new mongoose.Schema({
strict:'throw',
});

UserDefaultSchema.statics.getKubeOwnerName = async(context)=>{ // eslint-disable-line no-unused-vars
return null;
};

UserDefaultSchema.statics.getMeFromRequest = async function(req, context) {
const {req_id, logger} = context;
const apiKey = req.get('x-api-key');
Expand All @@ -66,7 +70,9 @@ UserDefaultSchema.statics.getMeFromRequest = async function(req, context) {
} else {
type = 'cluster';
}
return {apiKey, orgKey, type, _id: id};
return {
apiKey, orgKey, type, _id: id,
};
};

UserDefaultSchema.statics.getMeFromConnectionParams = async function(connectionParams, context){
Expand All @@ -85,7 +91,7 @@ UserDefaultSchema.statics.userTokenIsAuthorized = async function(me, orgId, acti
logger.error('A user was not found for this apiKey');
throw new ForbiddenError('user not found');
}

// make sure that the user is a member of the orgId that was passed in
const orgs = user.orgs || [];
const orgNames = orgs.map( (org) => org.name );
Expand Down Expand Up @@ -122,12 +128,12 @@ UserDefaultSchema.statics.isAuthorizedBatch = async function(me, orgId, objectAr
if(!orgName) {
logger.error('An org has not been set for this user');
return new Array(objectArray.length).fill(false);
}
}
const org = await models.Organization.findOne({ name: orgName }).lean();
if(!org || org._id !== orgId) {
logger.error('User is not authorized for this organization');
return new Array(objectArray.length).fill(false);
}
}
return new Array(objectArray.length).fill(true);
};

Expand Down
9 changes: 6 additions & 3 deletions app/apollo/models/user.local.schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ const UserLocalSchema = new mongoose.Schema({
type: String,
},
},

services: {
local: {
username: {
Expand Down Expand Up @@ -160,6 +159,10 @@ UserLocalSchema.statics.createToken = async (user, secret, expiresIn) => {
});
};

UserLocalSchema.statics.getKubeOwnerName = async(context)=>{ // eslint-disable-line no-unused-vars
return null;
};

UserLocalSchema.statics.getCurrentUser = ({me , req_id, logger}) => {
let result = me;
let data = me.meta.orgs[0];
Expand Down Expand Up @@ -210,7 +213,7 @@ UserLocalSchema.statics.getMeFromRequest = async function(req, context) {
const orgKey = req.get('razee-org-key');
if (orgKey) {
// cluster facing api (e.g. subscriptionsByCluster)
return {orgKey, type: 'cluster'};
return {orgKey, type: 'cluster'};
}
let token = req.headers['authorization'];
if (token) {
Expand Down Expand Up @@ -313,7 +316,7 @@ UserLocalSchema.statics.isAuthorized = async function(me, orgId, action, type, a
// say no for if it is cluster facing api
return false;
}

const orgMeta = me.meta.orgs.find((o)=>{
return (o._id == orgId);
});
Expand Down
12 changes: 8 additions & 4 deletions app/apollo/models/user.passport.local.schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ async function getOrCreateOrganization(models, args) {

UserPassportLocalSchema.statics.createUser = async function(models, args) {
const org = await getOrCreateOrganization(models, args);

const user = await this.create({
_id: `${uuid()}`,
type: 'passportlocal',
Expand Down Expand Up @@ -165,6 +165,10 @@ UserPassportLocalSchema.statics.createToken = async (
});
};

UserPassportLocalSchema.statics.getKubeOwnerName = async(context)=>{ // eslint-disable-line no-unused-vars
return null;
};

UserPassportLocalSchema.statics.getCurrentUser = ({me , req_id, logger}) => {
let result = me;
let data = me.meta.orgs[0];
Expand All @@ -176,7 +180,7 @@ UserPassportLocalSchema.statics.getCurrentUser = ({me , req_id, logger}) => {
type: me.type,
id: me._id,
email: me.email,
identifier: me.identifier,
identifier: me.identifier,
orgId: me.org_id,
role: me.role,
meta: me.meta,
Expand Down Expand Up @@ -218,7 +222,7 @@ UserPassportLocalSchema.statics.getMeFromRequest = async function(req, context)
const orgKey = req.get('razee-org-key');
if (orgKey) {
// cluster facing api (e.g. subscriptionsByCluster)
return {orgKey, type: 'cluster'};
return {orgKey, type: 'cluster'};
}
// user facing api
let token = req.headers['authorization'];
Expand Down Expand Up @@ -325,7 +329,7 @@ UserPassportLocalSchema.statics.isAuthorized = async function(me, orgId, action,
// say no for if it is cluster facing api
return false;
}

if (action === ACTIONS.READ) {
return me.org_id === orgId;
} else {
Expand Down
2 changes: 2 additions & 0 deletions app/apollo/resolvers/subscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,11 +324,13 @@ const subscriptionResolvers = {
throw new NotFoundError(context.req.t('version uuid "{{version_uuid}}" not found', {'version_uuid':version_uuid}), context);
}

const kubeOwnerName = await models.User.getKubeOwnerName(context);
await models.Subscription.create({
_id: UUID(),
uuid, org_id, name, groups, owner: me._id,
channelName: channel.name, channel_uuid, version: version.name, version_uuid,
clusterId,
kubeOwnerName,
});

pubSub.channelSubChangedFunc({org_id: org_id}, context);
Expand Down
2 changes: 2 additions & 0 deletions app/apollo/schema/subscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const subscriptionSchema = gql`
version: String!
versionUuid: String!
owner: BasicUser!
kubeOwnerName: String
resources: [Resource!]
created: Date!
updated: Date!
Expand Down Expand Up @@ -77,6 +78,7 @@ const subscriptionSchema = gql`
subscriptionVersion: String!,
subscriptionUuid: String!,
url: String!
kubeOwnerName: String
}
type SubscriptionUpdated {
hasUpdates: Boolean
Expand Down
2 changes: 1 addition & 1 deletion app/apollo/schema/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,4 @@ const userSchema = gql`
}
`;

module.exports = userSchema;
module.exports = userSchema;
9 changes: 5 additions & 4 deletions app/utils/subscriptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,26 @@ const getSubscriptionUrls = async(orgId, matchingSubscriptions) => {
org_id: orgId,
name: { $in: _.map(matchingSubscriptions, 'channelName') },
});

const matchingChannelsByName = _.keyBy(matchingChannels, 'name');

let urls = _.map(matchingSubscriptions, (subscription)=>{
const deployable = matchingChannelsByName[subscription.channelName];
const foundVersion = deployable.versions.filter( (ver) => {
return (ver.name === subscription.version);
});

let url;
if(foundVersion.length > 0) {
url = `api/v1/channels/${subscription.channelName}/${foundVersion[0].uuid}`;
}
}
return {
subscriptionName: subscription.name,
subscriptionChannel: subscription.channelName,
subscriptionVersion: subscription.version,
subscriptionUuid: subscription.uuid,
url: url
url: url,
kubeOwnerName: subscription.kubeOwnerName,
};
});
urls = urls.filter(Boolean);
Expand Down

0 comments on commit bb7b890

Please sign in to comment.