Adding integration with key vault

This commit is contained in:
mati108@gmail.com committed 2019-02-13 19:43:20 -03:00
1 parent 8c4377b197
commit 84ed1089ce
21 files changed
+209 -61

No files matched your search

-1
View File
@@ -53,6 +53,5 @@ export class KubernateError extends Error {
message = 'OperationsModule Kubernate Error. ' + this.message
constructor(message){
super(message)
logger.error(this.stack)
}
}
+28
View File
@@ -0,0 +1,28 @@
import logger from '../config/logger'
import axios from 'axios'
const authorizeClon = (authorization, keyId, cloneId, releaseClon) => {
logger.debug('authorizeClon -> Entering function.')
return axios({
url: process.env.GATEWAY_ENDPOINT,
method: 'post',
data: {
query: `
mutation keyVault_AuthorizeClone($keyId: String!, $cloneId: String!, $releaseClon: Boolean!){
keyVault_AuthorizeClone(keyId: $keyId, cloneId: $cloneId, releaseClon:$releaseClon)
}
`,
variables: {
keyId: keyId,
cloneId: cloneId,
releaseClon: releaseClon
}
},
headers: {
authorization: authorization
}
})
}
export default authorizeClon
+2 -2
View File
@@ -1,8 +1,8 @@
import logger from '../config/logger'
import axios from 'axios'
const teamQuery = async(authorization, teamId) => {
logger.debug('GraphQL retrieving team information. %s', teamId)
const teamQuery = (authorization, teamId) => {
logger.debug('teamQuery -> Entering function')
return axios({
url: process.env.GATEWAY_ENDPOINT,
+5
View File
@@ -104,6 +104,11 @@ const createClone = async (clone) => {
"name": "WAIT_TIME",
"value": clone.waitTime.toString()
})
} else{
env.push({
"name": "KEY_ID",
"value": clone.keyId
})
}
} else if(clone.botType === INDICATOR || clone.botType === EXTRACTION ){
if(clone.mode !== NO_TIME){
+1
View File
@@ -23,6 +23,7 @@ const cloneSchema = new Schema({
type: String,
required: true,
},
keyId: String,
mode: {
type: String,
enum: CloneModeEnum,
+5 -5
View File
@@ -2,8 +2,8 @@ import logger from '../config/logger'
import { isDefined } from '../config/utils'
import { AutorizationError } from '../errors'
const cloneDetails = async(authId, team, clone) => {
logger.debug('Retrieving clone details.')
const cloneDetails = (authId, team, clone) => {
logger.debug('cloneDetails -> Entering function.')
if(!(authId === (process.env.AUTH_CLIENT_ID + '@clients') || isAuthorized(team, authId))){
logger.debug('User %s is not authorized to manage team %s.', authId, team.slug )
@@ -22,12 +22,12 @@ const cloneDetails = async(authId, team, clone) => {
clone.teamAvatar = team.profile.avatar
clone.userLoggedIn = team.members[0].member.alias //TODO change by context info if possible
logger.debug('Retrieving clone details complete.')
logger.debug('cloneDetails -> Retrieving clone details complete.')
return clone
};
function getSelectedBot(team, selectedBotId){
logger.debug('Getting selected bot.')
logger.debug('cloneDetails -> Getting selected bot.')
if(isDefined(team)){
for (var i = 0; i < team.fb.length; i++) {
if(team.fb[i].id === selectedBotId){
@@ -38,7 +38,7 @@ function getSelectedBot(team, selectedBotId){
}
function isAuthorized(team, authId){
logger.debug('Checking if the user is authorized by the teams module.')
logger.debug('cloneDetails -> Checking if the user is authorized by the teams module.')
let authorized = false
if(isDefined(team)){
for (var i = 0; i < team.members.length; i++) {
+34 -27
View File
@@ -1,23 +1,26 @@
import { CloneInputType } from '../types/input';
import { CloneType } from '../types';
import { Clone } from '../../models';
import { CloneModeEnum } from '../../enums/CloneMode';
import { BotTypesEnum } from '../../enums/BotTypes';
import { CloneInputType } from '../types/input'
import { CloneType } from '../types'
import { Clone } from '../../models'
import { CloneModeEnum } from '../../enums/CloneMode'
import { BotTypesEnum } from '../../enums/BotTypes'
import teamQuery from '../../graphQLCalls/teamQuery'
import authorizeClone from '../../graphQLCalls/authorizeClone'
import cloneDetails from '../cloneDetails'
import {
AuthentificationError,
WrongArgumentsError,
OperationsError
OperationsError,
AutorizationError
} from '../../errors'
import logger from '../../config/logger'
import createKubernetesClone from '../../kubernetes/createClone'
import { isDefined } from '../../config/utils'
import { LIVE } from '../../enums/CloneMode'
const args = {
clone: {type: CloneInputType }
clone: { type: CloneInputType }
}
const resolve = async(parent, { clone }, context) => {
@@ -27,40 +30,44 @@ const resolve = async(parent, { clone }, context) => {
throw new AuthentificationError()
}
if(!Object.values(CloneModeEnum).includes(clone.mode)){
if (!Object.values(CloneModeEnum).includes(clone.mode)) {
throw new WrongArgumentsError('The mode selected is not valid.')
}
if(!Object.values(BotTypesEnum).includes(clone.botType)){
if (!Object.values(BotTypesEnum).includes(clone.botType)) {
throw new WrongArgumentsError('The bot type selected is not valid.')
}
try{
if (clone.mode === LIVE && !isDefined(clone.keyId)) {
throw new WrongArgumentsError('The key was not provided to run the clone in Live mode.')
}
try {
let team = await teamQuery(context.authorization, clone.teamId)
clone = await cloneDetails(context.userId, team.data.data.teams_TeamById, clone)
clone.createDatetime = new Date().valueOf() / 1000|0
clone.createDatetime = new Date().valueOf() / 1000 | 0
clone.active = true
logger.debug('addClone -> Creating a new clone.')
let newClone = new Clone(clone)
newClone.authId = context.userId
clone.id = newClone._id
await createKubernetesClone(clone)
return new Promise((resolve, reject) => {
newClone.save((err) => {
if (err){
logger.error('addClone -> Error: %s', err.stack)
reject('There has been an error storing the clone.')
}
else {
//TODO transaction on database
logger.debug('addClone -> Save clone sucessful.')
resolve(newClone)
}
})
})
} catch(err) {
logger.debug('addClone -> Creating the clone on the Database.')
let savedClone = await newClone.save()
if (clone.mode === LIVE) {
logger.debug('addClone -> Authorizing clone to use the key.')
let response = await authorizeClone(context.authorization, clone.keyId, clone.id, false)
logger.debug('addClone -> Authorizing clone to use the key.')
if (response.data.data.keyVault_AuthorizeClone === clone.keyId) {
await createKubernetesClone(clone)
} else {
throw new AutorizationError()
}
}
logger.debug('addClone -> Clone created sucessfully.')
return savedClone
} catch (err) {
logger.error('addClone -> Error: %s', err.stack)
throw new OperationsError('There has been an error creating the clone.')
}
@@ -40,6 +40,9 @@ const resolve = async (parent, { id }, context) => {
let team = await teamQuery(context.authorization, clone.teamId)
clone = await cloneDetails(context.userId, team.data.data.teams_TeamById, clone)
logger.debug('removeClone -> Release the clone key.')
await authorizeClone(context.authorization, clone.keyId, clone.id, true)
logger.debug('removeClone -> Removing Clone from Kubernates.')
await removeKuberneteClone(clone)
@@ -13,6 +13,7 @@ import {
import { Clone } from '../../models'
import logger from '../../config/logger'
import teamQuery from '../../graphQLCalls/teamQuery'
import authorizeClone from '../../graphQLCalls/authorizeClone'
import cloneDetails from '../cloneDetails'
import removeKuberneteClone from '../../kubernetes/removeClone'
import { isDefined } from '../../config/utils'
@@ -55,9 +56,6 @@ const resolve = async (parent,
let team = await teamQuery(context.authorization, clone.teamId)
clone = await cloneDetails(context.userId, team.data.data.teams_TeamById, clone)
logger.debug('UpdateExecutionSummary -> Removing Clone from Kubernates.')
await removeKuberneteClone(clone)
const query = { _id: id }
const options = { new: true }
const update = {
+1 -2
View File
@@ -18,7 +18,7 @@ const args = {
}
const resolve = async(parent, { cloneIdList }, context) => {
logger.debug('Get Clones -> Entering Fuction. %j', cloneIdList)
logger.debug('Get Clones -> Entering Fuction.')
try{
let clones = []
for (var i = 0; i < cloneIdList.length; i++) {
@@ -44,7 +44,6 @@ const resolve = async(parent, { cloneIdList }, context) => {
id: cloneIdList[i]
})
}
}
return clones
+2 -1
View File
@@ -37,7 +37,8 @@ const CloneType = new GraphQLObjectType({
teamName: {type: GraphQLString},
botName: {type: GraphQLString},
botAvatar: {type: GraphQLString},
teamAvatar: {type: GraphQLString}
teamAvatar: {type: GraphQLString},
keyId: {type: GraphQLString}
})
})
export default CloneType
+2 -1
View File
@@ -23,7 +23,8 @@ const Type = new GraphQLInputObjectType({
month: { type: GraphQLInt },
interval: { type: GraphQLInt },
processName: { type: new GraphQLNonNull(GraphQLString) },
botType: { type: new GraphQLNonNull(GraphQLString) }
botType: { type: new GraphQLNonNull(GraphQLString) },
keyId: { type: GraphQLString }
})
});