-
Notifications
You must be signed in to change notification settings - Fork 255
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added AWS config and identity store #2208
base: develop
Are you sure you want to change the base?
Changes from all commits
93b2b58
b4598db
16e8b37
2857c15
dafb8ec
9edd8c3
bbf39dd
611625d
abb8a3c
d92a238
92f9a7f
68b63f1
e447b63
3b1e279
08328a8
952110c
87a2c76
81bf324
6a72bd3
778c4cc
63eca88
4edf99d
fde42ff
1b9b46a
e7a0dba
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
export const GITHUB_URL = "https://github.com"; | ||
export const PROFILE_SVC_GITHUB_URL = "https://github.com/Real-Dev-Squad/sample-profile-service"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is this, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will be shared in response for the user to set their email address |
||
|
||
module.exports = { | ||
GITHUB_URL, | ||
PROFILE_SVC_GITHUB_URL, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { PROFILE_SVC_GITHUB_URL } from "../constants/urls"; | ||
import {addUserToGroup, createUser, fetchAwsUserIdByUsername} from "../utils/awsFunctions"; | ||
const dataAccess = require("../services/dataAccessLayer"); | ||
const userDataLevels = require('../constants/userDataLevels'); | ||
Comment on lines
+3
to
+4
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there any specific reason to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It was returning an error like this on using the import, I guess this can be resolved by passing default values to the rest of the fields right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
No idea. I have very little experience with Typescript There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here @samarpan1738, @vinit717 can you help us here please There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @vikhyat187 |
||
|
||
export const addUserToAWSGroup = async (req, res) => { | ||
const { groupId, userId } = req.body; | ||
|
||
try { | ||
const userInfoData = await dataAccess.retrieveUsers({ discordId: userId, level: userDataLevels.ACCESS_LEVEL.INTERNAL, role: 'cloudfare_worker'}); | ||
if (!userInfoData.userExists) { | ||
return res.status(400).json({ error: "User not found" }); | ||
} else if(!userInfoData.user.email) { | ||
return res.status(400).json({ error: `User email is required to create an AWS user. Please update your email by setting up Profile service, url : ${PROFILE_SVC_GITHUB_URL}` }); | ||
Comment on lines
+12
to
+14
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use res.boom? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi Prakash, I tried returning the response using the res.boom, but in the discord slash commands repo, while parsing this response and storing in variable, the variable was showing as Unkown There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here the custom error message is not coming in the discord slash commands service, have tested with this change only bad request is coming in error message There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Comment on lines
+12
to
+14
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NIT: can we only have one return? |
||
} | ||
|
||
let awsUserId = await fetchAwsUserIdByUsername(userInfoData.user.username); | ||
|
||
let userCreationResponse = null; | ||
|
||
if (awsUserId === null){ | ||
// We need to create the user in AWS before and then fetch its Id | ||
userCreationResponse = await createUser(userInfoData.user.username, userInfoData.user.email); | ||
awsUserId = userCreationResponse.UserId; | ||
} | ||
|
||
let userAdditionResponse = await addUserToGroup(groupId, awsUserId) | ||
|
||
if (userAdditionResponse.conflict){ | ||
return res.status(200).json({ | ||
message: `User ${userId} is already part of the AWS group, please try signing in.` | ||
}) | ||
} | ||
|
||
if (userAdditionResponse) | ||
return res.status(200).json({ | ||
message: `User ${userId} successfully added to group ${groupId}.` | ||
}); | ||
} catch (error) { | ||
logger.error(`Error in adding user - ${userId} to AWS group - ${groupId} error - ${error}`); | ||
throw error; | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import express from "express" | ||
import { addUserToAWSGroup } from "../controllers/awsAccess"; | ||
const router = express.Router(); | ||
const { verifyDiscordBot } = require("../middlewares/authorizeBot"); | ||
|
||
router.post("", verifyDiscordBot, addUserToAWSGroup); | ||
|
||
|
||
module.exports = router; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
import chai, {expect} from "chai"; | ||
import sinon from 'sinon'; | ||
import chaiHttp from 'chai-http'; | ||
import * as awsFunctions from '../../utils/awsFunctions'; | ||
import bot from "../utils/generateBotToken"; | ||
import { PROFILE_SVC_GITHUB_URL } from '../../constants/urls'; | ||
|
||
const app = require("../../server"); | ||
const userData = require("../fixtures/user/user")(); | ||
const authorizeBot = require("../../middlewares/authorizeBot"); | ||
const addUser = require("../utils/addUser"); | ||
const cleanDb = require("../utils/cleanDb"); | ||
const { CLOUDFLARE_WORKER } = require("../../constants/bot") | ||
|
||
chai.use(chaiHttp); | ||
|
||
describe('addUserToAWSGroup', function(){ | ||
let req: any; | ||
const AWS_ACCESS_API_URL = `/aws-access?dev=true` | ||
|
||
beforeEach(async () => { | ||
await addUser(userData[0]); | ||
await addUser(userData[1]); | ||
sinon.restore(); | ||
req = { | ||
headers: {}, | ||
}; | ||
const jwtToken = bot.generateToken({ name: CLOUDFLARE_WORKER }); | ||
req.headers.authorization = `Bearer ${jwtToken}`; | ||
}) | ||
|
||
afterEach(async () => { | ||
await cleanDb(); | ||
}); | ||
|
||
it('should return 400 and user not found with wrong discord Id passed', function(done){ | ||
const res = chai | ||
.request(app) | ||
.post(AWS_ACCESS_API_URL) | ||
.set('Authorization', req.headers.authorization) | ||
.send({ | ||
groupId: 'test-group-id', | ||
userId: '3000230293' | ||
}) | ||
.end((err, res) => { | ||
if (err) { | ||
return done(err); | ||
} | ||
expect(res.status).to.be.equal(400); | ||
expect(res.body).to.have.property('error') | ||
.that.equals(`User not found`); | ||
return done(); | ||
}) | ||
}); | ||
|
||
it('should return 400 when user email is missing', function(done) { | ||
const res = chai | ||
.request(app) | ||
.post(AWS_ACCESS_API_URL) | ||
.set('Authorization', req.headers.authorization) | ||
.send({ | ||
groupId: 'test-group-id', | ||
userId: '1234567890' | ||
}) | ||
.end((err, res) => { | ||
if (err) { | ||
return done(err); | ||
} | ||
expect(res.status).to.be.equal(400); | ||
expect(res.body).to.have.property('error') | ||
.that.equals(`User email is required to create an AWS user. Please update your email by setting up Profile service, url : ${PROFILE_SVC_GITHUB_URL}`); | ||
return done(); | ||
}); | ||
}); | ||
|
||
|
||
it("Should create user and add to group, if the user is not present in AWS already", function(done){ | ||
sinon.stub(awsFunctions, "createUser").resolves({ UserId: "new-aws-user-id" }); | ||
sinon.stub(awsFunctions, "addUserToGroup").resolves({ conflict: false }); | ||
sinon.stub(awsFunctions, "fetchAwsUserIdByUsername").resolves(null); | ||
|
||
const res = chai | ||
.request(app) | ||
.post(AWS_ACCESS_API_URL) | ||
.set('Authorization', req.headers.authorization) | ||
.send({ | ||
groupId: 'test-group-id', | ||
userId: '12345' | ||
}) | ||
.end((err, res) => { | ||
if (err) { | ||
return done(err); | ||
} | ||
expect(res.status).to.be.equal(200); | ||
expect(res.body).to.have.property('message', | ||
`User 12345 successfully added to group test-group-id.` | ||
); | ||
return done(); | ||
}); | ||
}); | ||
|
||
it("Should add the user to the group if the user is already part of AWS account", function(done){ | ||
sinon.stub(awsFunctions, "addUserToGroup").resolves({ conflict: false }); | ||
sinon.stub(awsFunctions, "fetchAwsUserIdByUsername").resolves("existing-user-id-123"); | ||
|
||
const res = chai | ||
.request(app) | ||
.post(AWS_ACCESS_API_URL) | ||
.set('Authorization', req.headers.authorization) | ||
.send({ | ||
groupId: 'test-group-id', | ||
userId: '12345' | ||
}) | ||
.end((err, res) => { | ||
if (err) { | ||
return done(err); | ||
} | ||
expect(res.status).to.be.equal(200) | ||
expect(res.body).to.have.property('message', | ||
'User 12345 successfully added to group test-group-id.' | ||
); | ||
return done(); | ||
}); | ||
}); | ||
|
||
it("Should return the signin URL if the user is already added to the group", function(done) { | ||
sinon.stub(awsFunctions, "addUserToGroup").resolves({ conflict: true }); | ||
sinon.stub(awsFunctions, "fetchAwsUserIdByUsername").resolves("existing-user-id-123"); | ||
|
||
const res = chai | ||
.request(app) | ||
.post(AWS_ACCESS_API_URL) | ||
.set('Authorization', req.headers.authorization) | ||
.send({ | ||
groupId: 'test-group-id', | ||
userId: '12345' | ||
}) | ||
.end((err, res) => { | ||
if (err) { | ||
return done(err); | ||
} | ||
expect(res.status).to.be.equal(200); | ||
expect(res.body).to.have.property('message', | ||
'User 12345 is already part of the AWS group, please try signing in.' | ||
); | ||
return done(); | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How to add env keys, please fix this part?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
didn't get this part, do you suggest that the content from production.js cannot be stored in the Github secrets / secrets at the deployment time.
@prakashchoudhary07
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Check how new env keys are added, and update the code accordingly
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure let me check once.