Skip to content

Commit 75f22dd

Browse files
vikhyat187Achintya-Chatterjeeprakashchoudhary07
authored
Added AWS config and identity store (#2208)
* aws setup * lint-fix * fixed linting issues * Removed the flow to take credentials from .aws folder in local * resolving merge conflicts * added unit tests for the code * aws setup * Revert "aws setup" This reverts commit e447b63. * added integration tests * refactored the route and removed console log * updated the integration tests and added fixture * changes 1. Fixed all integration tests 2. changed error status to 400 3. removed unnessary code: * resolving PR comments * resolving PR comments * Fixed test cases and added test case for User not found case * added feature flag to backend API and updated test cases * Changes 1. changed route to /aws/groups/access 2. refactored code * code refactor and removed one comment * Returning the error response to the user * refactored the condition --------- Co-authored-by: Achintya Chatterjee <[email protected]> Co-authored-by: Prakash Choudhary <[email protected]>
1 parent 1060bea commit 75f22dd

File tree

14 files changed

+1379
-5
lines changed

14 files changed

+1379
-5
lines changed

config/custom-environment-variables.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,22 @@ module.exports = {
1010
__name: "PORT",
1111
__format: "number",
1212
},
13+
14+
aws: {
15+
region: {
16+
__name: "AWS_REGION",
17+
},
18+
access_key: {
19+
__name: "AWS_ACCESS_KEY",
20+
},
21+
secret_key: {
22+
__name: "AWS_SECRET_KEY",
23+
},
24+
identity_store_id: {
25+
__name: "IDENTITY_STORE_ID",
26+
},
27+
},
28+
1329
enableFileLogs: {
1430
__name: "ENABLE_FILE_LOGS",
1531
__format: "boolean",

config/default.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ module.exports = {
2020
org: "Real-Dev-Squad",
2121
},
2222

23+
aws: {
24+
region: "<aws-region>",
25+
access_key: "<aws-access-key>",
26+
secret_key: "<aws-secret-key>",
27+
identity_store_id: "<identity-store-id>",
28+
},
29+
2330
githubOauth: {
2431
clientId: "<clientId>",
2532
clientSecret: "<clientSecret>",

constants/urls.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
export const GITHUB_URL = "https://github.com";
2+
export const PROFILE_SVC_GITHUB_URL = "https://github.com/Real-Dev-Squad/sample-profile-service";
23

34
module.exports = {
45
GITHUB_URL,
6+
PROFILE_SVC_GITHUB_URL,
57
};

constants/userDataLevels.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const ACCESS_LEVEL = {
77

88
const ROLE_LEVEL = {
99
private: ["super_user"],
10-
internal: ["super_user"],
10+
internal: ["super_user", "cloudfare_worker"],
1111
confidential: ["super_user"],
1212
};
1313

controllers/awsAccess.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { PROFILE_SVC_GITHUB_URL } from "../constants/urls";
2+
import {addUserToGroup, createUser, fetchAwsUserIdByUsername} from "../utils/awsFunctions";
3+
const dataAccess = require("../services/dataAccessLayer");
4+
const userDataLevels = require('../constants/userDataLevels');
5+
6+
export const addUserToAWSGroup = async (req, res) => {
7+
const { groupId, userId } = req.body;
8+
9+
try {
10+
const userInfoData = await dataAccess.retrieveUsers({ discordId: userId, level: userDataLevels.ACCESS_LEVEL.INTERNAL, role: 'cloudfare_worker'});
11+
if (!userInfoData.userExists) {
12+
return res.status(400).json({ error: "User not found" });
13+
} else if(!userInfoData.user.email) {
14+
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}` });
15+
}
16+
17+
let awsUserId = await fetchAwsUserIdByUsername(userInfoData.user.username);
18+
19+
let userCreationResponse = null;
20+
21+
if (awsUserId === null){
22+
// We need to create the user in AWS before and then fetch its Id
23+
userCreationResponse = await createUser(userInfoData.user.username, userInfoData.user.email);
24+
awsUserId = userCreationResponse.UserId;
25+
}
26+
27+
let userAdditionResponse = await addUserToGroup(groupId, awsUserId)
28+
29+
if (userAdditionResponse){
30+
if (userAdditionResponse.conflict){
31+
return res.status(200).json({
32+
message: `User ${userId} is already part of the AWS group, please try signing in.`
33+
})
34+
} else {
35+
return res.status(200).json({
36+
message: `User ${userId} successfully added to group ${groupId}.`
37+
});
38+
}
39+
}
40+
} catch (error) {
41+
logger.error(`Error in adding user - ${userId} to AWS group - ${groupId} error - ${error}`);
42+
return res.status(500).json({
43+
error: `Something went wrong, please try again`
44+
});
45+
}
46+
};

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"tdd:watch": "sh scripts/tests/tdd.sh"
1717
},
1818
"dependencies": {
19+
"@aws-sdk/client-identitystore": "^3.665.0",
1920
"@types/nodemailer": "^6.4.15",
2021
"axios": "1.7.2",
2122
"cloudinary": "2.0.3",

routes/awsAccess.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import express from "express"
2+
import { addUserToAWSGroup } from "../controllers/awsAccess";
3+
const router = express.Router();
4+
const { verifyDiscordBot } = require("../middlewares/authorizeBot");
5+
6+
router.post("/access", verifyDiscordBot, addUserToAWSGroup);
7+
8+
module.exports = router;

routes/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import express from "express";
22
const app = express.Router();
33
import { devFlagMiddleware } from "../middlewares/devFlag";
44

5+
app.use("/aws/groups", devFlagMiddleware, require("./awsAccess"))
56
app.use("/answers", require("./answers"));
67
app.use("/auctions", require("./auctions"));
78
app.use("/arts", require("./arts"));

test/config/test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ module.exports = {
2323
clientId: "clientId",
2424
clientSecret: "clientSecret",
2525
},
26+
aws: {
27+
region: "us-east-1",
28+
access_key: "test-access-key",
29+
secret_key: "test-secret-key",
30+
identity_store_id: "test-identity-store-id",
31+
},
32+
2633
firestore: `{
2734
"type": "service_account",
2835
"project_id": "test-project-id-for-emulator",

test/fixtures/user/user.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ module.exports = () => {
5454
twitter_id: "whatifi",
5555
discordJoinedAt: "2023-04-06T01:47:34.488000+00:00",
5656
phone: "1234567891",
57-
5857
picture: {
5958
publicId: "profile/mtS4DhUvNYsKqI7oCWVB/aenklfhtjldc5ytei3ar",
6059
url: "https://res.cloudinary.com/realdevsquad/image/upload/v1667685133/profile/mtS4DhUvNYsKqI7oCWVB/aenklfhtjldc5ytei3ar.jpg",

0 commit comments

Comments
 (0)