From 4b519f25b62e57ebca65a7dc181ba819d8b293bd Mon Sep 17 00:00:00 2001 From: Matias Date: Sun, 7 Jul 2019 21:13:43 -0300 Subject: [PATCH] Adding read and write permissions to teams --- src/config/ecosystem.json | 8 +++---- src/models/ecosystem.js | 4 ++++ src/schema/mutations/AddTeam.js | 4 +++- src/schema/mutations/CreateFile.js | 13 +++++++++++- src/schema/queries/FileContent.js | 12 ++++++++++- src/schema/types/Host.js | 3 ++- src/utils/getDevTeamHost.js | 34 ++++++++++++++++++++++++++++++ 7 files changed, 70 insertions(+), 8 deletions(-) create mode 100644 src/utils/getDevTeamHost.js diff --git a/src/config/ecosystem.json b/src/config/ecosystem.json index 3641c3fe7..20982e0fb 100644 --- a/src/config/ecosystem.json +++ b/src/config/ecosystem.json @@ -8,7 +8,7 @@ "url": "https://demo-api-dev.superalgos.org/", "storage": "localStorage", "container": "aamasters", - "accessKey": "" + "accessKey": "09e890b76767ede3e10f0526b5036600a6b8d3faac540336e12b493fdb2aa9ed46f86094c34652f99fb6322e5f09806412f17725807f85e4dc4169bd22dd59f0b23aba5251f5544478a1aabf5a2dea5260e6eb0c29af3a788208af85025a02f94950437d501b91fd55b9d97b3d6ac3af4e1161321f78b73ca0fda524dc3a5d03" }, "bots": [ { @@ -728,7 +728,7 @@ "url": "https://demo-api-dev.superalgos.org/", "container": "aavikings", "storage": "localStorage", - "accessKey": "" + "accessKey": "0f38fea438be5adfc5873228f503c55353fb334406128b16a958851b8e47c118a08f0105dd8985d5479758fc4cc3f56c2e2364c2d7c3d0fdffba8d7e9356056a34629e3b926b1ebb940122d973db30b04fc18fab542cbc1afdc032dccf64b0fe308ee6bbc74f82bd350aaebf723428a50d8a699fa2b4b96d58716986ed24069f" }, "bots": [ { @@ -834,8 +834,8 @@ "host": { "url": "https://demo-api-dev.superalgos.org/", "storage": "localStorage", - "container": "aaplatform", - "accessKey": "" + "container": "aamasters", + "accessKey": "09e890b76767ede3e10f0526b5036600a6b8d3faac540336e12b493fdb2aa9ed46f86094c34652f99fb6322e5f09806412f17725807f85e4dc4169bd22dd59f0b23aba5251f5544478a1aabf5a2dea5260e6eb0c29af3a788208af85025a02f94950437d501b91fd55b9d97b3d6ac3af4e1161321f78b73ca0fda524dc3a5d03" }, "competitions": [ { diff --git a/src/models/ecosystem.js b/src/models/ecosystem.js index bcaf36a46..e50759767 100644 --- a/src/models/ecosystem.js +++ b/src/models/ecosystem.js @@ -31,6 +31,10 @@ const ecosystemSchema = new Schema({ accessKey: { type: String, required: false + }, + ownerKey: { + type: String, + required: false } }, bots: [{ diff --git a/src/schema/mutations/AddTeam.js b/src/schema/mutations/AddTeam.js index 94f557eb6..995f87c6e 100644 --- a/src/schema/mutations/AddTeam.js +++ b/src/schema/mutations/AddTeam.js @@ -6,6 +6,7 @@ import { TeamInputType } from '../types/input' import { copyBot } from '../../storage/CopyBot' import { copySimulator } from '../../storage/CopySimulator' import { createContainer, writeFileContent } from '../../storage/providers/MinioStorage' +import crypto from 'crypto' export const args = { team: { type: TeamInputType } } @@ -51,7 +52,8 @@ const resolve = async (parent, { team }, context) => { url: process.env.NODE_ENDPOINT, storage: 'localStorage', container: team.codeName, - accessKey: '', + accessKey: crypto.randomBytes(128).toString('hex'), + ownerKey: crypto.randomBytes(128).toString('hex') } team.host = nodeEndpoint } diff --git a/src/schema/mutations/CreateFile.js b/src/schema/mutations/CreateFile.js index a1dcdcb6b..0911543cb 100644 --- a/src/schema/mutations/CreateFile.js +++ b/src/schema/mutations/CreateFile.js @@ -2,13 +2,24 @@ import logger from '../../utils/logger' import { FileInputType } from '../types/input' import { GraphQLString } from 'graphql' import { writeFileContent } from '../../storage/providers/MinioStorage' +import { AuthenticationError, WrongArgumentsError } from '../../errors' +import getDevTeamHost from '../../utils/getDevTeamHost' export const args = { file: { type: FileInputType } } const resolve = async (parent, { file }, context) => { logger.debug('createFile -> Entering Function: ' + file.container + '/' + file.filePath) try { - //TODO Add permissions + if (file.accessKey === undefined || file.accessKey === null) { + throw new WrongArgumentsError() + } + + let host = await getDevTeamHost(file.container, undefined, file.accessKey) + + if (host === undefined || host === null) { + throw new AuthenticationError() + } + await writeFileContent(file.container, file.filePath, file.fileContent) return 'File created.' } catch (err) { diff --git a/src/schema/queries/FileContent.js b/src/schema/queries/FileContent.js index 2b328b154..6856b6f93 100644 --- a/src/schema/queries/FileContent.js +++ b/src/schema/queries/FileContent.js @@ -3,6 +3,8 @@ import { FileInputType } from '../types/input' import { GraphQLString } from 'graphql' import { getFileContent } from '../../storage/providers/MinioStorage' import { getFileContentRemote } from '../../storage/providers/AzureStorage' +import { AuthenticationError, WrongArgumentsError } from '../../errors' +import getDevTeamHost from '../../utils/getDevTeamHost' export const args = { file: { type: FileInputType } } @@ -10,10 +12,18 @@ const resolve = async (parent, { file }, context) => { logger.debug('getFileContent -> Entering Function: ' + file.container + '/' + file.filePath) try { + if (file.accessKey === undefined || file.accessKey === null) { + throw new WrongArgumentsError() + } + + let host = await getDevTeamHost(file.container, file.accessKey) + + if (host === undefined || host === null) { + throw new AuthenticationError() + } let fileContent if (file.storage === 'localStorage') { - //TODO Add permissions fileContent = await getFileContent(file.container, file.filePath) } else { fileContent = await getFileContentRemote(file.container, file.filePath, file.storage, file.accessKey) diff --git a/src/schema/types/Host.js b/src/schema/types/Host.js index fbc45bcd1..0b36b8e00 100644 --- a/src/schema/types/Host.js +++ b/src/schema/types/Host.js @@ -9,7 +9,8 @@ const HostType = new GraphQLObjectType({ url: { type: GraphQLString }, storage: { type: GraphQLString }, container: { type: GraphQLString }, - accessKey: { type: GraphQLString } + accessKey: { type: GraphQLString }, + ownerKey: { type: GraphQLString } }), }); diff --git a/src/utils/getDevTeamHost.js b/src/utils/getDevTeamHost.js new file mode 100644 index 000000000..c244cf8e1 --- /dev/null +++ b/src/utils/getDevTeamHost.js @@ -0,0 +1,34 @@ +import { Ecosystem } from '../models' + +let teamCache = new Map() +export default async function getDevTeamHost(devTeamName, accessKey, ownerKey) { + let host + if (accessKey) { + host = teamCache.get(devTeamName + '.accessKey') + } else { + host = teamCache.get(devTeamName + '.ownerKey') + } + if (host) { + if (host.accessKey === accessKey || host.ownerKey === ownerKey) { + return host + } + } else { + let ecosystem = await Ecosystem.find() + + for (var i = 0; i < ecosystem.length; i++) { + for (var j = 0; j < ecosystem[i].devTeams.length; j++) { + if (ecosystem[i].devTeams[j].codeName.toLowerCase() === devTeamName.toLowerCase()) { + if (ecosystem[i].devTeams[j].host.accessKey === accessKey) { + teamCache.set(devTeamName + '.accessKey', ecosystem[i].devTeams[j].host) + return ecosystem[i].devTeams[j].host + } + if (ecosystem[i].devTeams[j].host.ownerKey === ownerKey) { + teamCache.set(devTeamName + '.ownerKey', ecosystem[i].devTeams[j].host) + return ecosystem[i].devTeams[j].host + } + } + } + } + + } +}