Skip to content

Commit

Permalink
Added Logs to user functions in BE (#1568)
Browse files Browse the repository at this point in the history
* added function to console log attemp/userIP/role; put in getUser, getUserList, updateUserStatus

* changed the ipAddress to use events.requestContext.identity.sourceIp

* removed the andie console logs
  • Loading branch information
andieswift authored Dec 17, 2024
1 parent 6696a62 commit 05ac99d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
5 changes: 5 additions & 0 deletions services/app-api/getMyUserList.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
getActiveTerritories,
} from "cmscommonlib";
import { getUser } from "./getUser";
import { logAttempt } from "./utils/logAttempt";

export const buildParams = (role, territory) => {
const startParams = {
Expand Down Expand Up @@ -52,15 +53,18 @@ export const buildParams = (role, territory) => {
};

export const getMyUserList = async (event) => {
const ipAddress = event.requestContext.identity.sourceIp;
try {
// get the rest of the details about the current user
const doneBy = await getUser(event.queryStringParameters.email);

if (!doneBy) {
logAttempt("getMyUserList", false, ipAddress);
return RESPONSE_CODE.USER_NOT_FOUND;
}

if (!getUserRoleObj(doneBy?.roleList).canAccessUserManagement) {
logAttempt("getMyUserList", false, ipAddress, doneBy);
return RESPONSE_CODE.USER_NOT_AUTHORIZED;
}

Expand All @@ -76,6 +80,7 @@ export const getMyUserList = async (event) => {
buildParams(umRole, territories.shift())
);

logAttempt("getMyUserList", true, ipAddress, doneBy);
return listResult.Items;
} catch (e) {
console.log("getMyUserList exception? ", e);
Expand Down
14 changes: 11 additions & 3 deletions services/app-api/getUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import handler from "./libs/handler-lib";
import dynamoDb from "./libs/dynamodb-lib";

import { getUserRoleObj } from "cmscommonlib";
import { logAttempt } from "./utils/logAttempt";

/**
* returns the User Table entry who's id is this email
* @param {String} userEmail User to return
* @param {String} ipAddress users ip address
* @returns {Object} the User json object
*/
export const getUser = async (userEmail) => {
export const getUser = async (userEmail, ipAddress) => {
const cParams = {
TableName: process.env.oneMacTableName,
// 'Key' defines the partition key and sort key of the item to be retrieved
Expand All @@ -20,6 +22,7 @@ export const getUser = async (userEmail) => {
ProjectionExpression: "email, fullName, phoneNumber",
};

console.log("cParams", cParams);
const params = {
TableName: process.env.oneMacTableName,
// 'Key' defines the partition key and sort key of the item to be retrieved
Expand Down Expand Up @@ -48,6 +51,7 @@ export const getUser = async (userEmail) => {
}
} catch (dbError) {
console.log(`Error happened while reading from DB: ${dbError}`);
logAttempt("getUser", false, ipAddress);
throw dbError;
}

Expand All @@ -63,13 +67,17 @@ export const getUser = async (userEmail) => {
const returnUser = cResult.Item;
returnUser.roleList = result.Items;
console.log(`Selected User ${userEmail}: ${JSON.stringify(returnUser)}`);
logAttempt("getUser", true, ipAddress);
return returnUser;
};

// Gets owns user data from User DynamoDB table
export const main = handler(async (event) => {
const userItem = (await getUser(event.queryStringParameters.email)) ?? {};
const userItem =
(await getUser(
event.queryStringParameters.email,
event.requestContext.identity.sourceIp
)) ?? {};
userItem.validRoutes = getUserRoleObj(userItem.roleList).getAccesses();

return userItem;
});
6 changes: 6 additions & 0 deletions services/app-api/updateUserStatus.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { saveEmail } from "./utils/saveEmail";
import { getUser } from "./getUser";
import { changeUserStatus } from "./utils/changeUserStatus";
import { getMyApprovers } from "./getMyApprovers";
import { logAttempt } from "./utils/logAttempt";

const statusLabels = {
[USER_STATUS.ACTIVE]: "granted",
Expand Down Expand Up @@ -84,10 +85,12 @@ export const doUpdate = async (body, doneBy, doneTo) => {
export const updateUserStatus = async (event) => {
let body;
const rightNowNormalized = Date.now();
const ipAddress = event.requestContext.identity.sourceIp;

try {
body = JSON.parse(event.body);
} catch (e) {
logAttempt("updateUserStatus", false, ipAddress);
console.error("Failed to parse body", e);
return RESPONSE_CODE.USER_SUBMISSION_FAILED;
}
Expand All @@ -100,6 +103,7 @@ export const updateUserStatus = async (event) => {
getUser(body.email),
]);
} catch (e) {
logAttempt("updateUserStatus", false, ipAddress);
console.error("Could not fetch relevant user info", e);
return RESPONSE_CODE.USER_NOT_FOUND;
}
Expand Down Expand Up @@ -155,9 +159,11 @@ export const updateUserStatus = async (event) => {
);
}
} catch (e) {
logAttempt("updateUserStatus", false, ipAddress, body.user);
console.log("failed to send email: ", e);
}

logAttempt("updateUserStatus", true, ipAddress, body.user);
return RESPONSE_CODE.USER_SUBMITTED;
};

Expand Down
8 changes: 8 additions & 0 deletions services/app-api/utils/logAttempt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const logAttempt = (functionName, attemptType, ipAddress, user) => {
const currentTimeStamp = new Date().toISOString();
ipAddress = ipAddress ?? "Not found";
const attemptStatus = attemptType ? "success" : "failure";
const role = user ? user.role : "Not found";
const message = `attempts ${functionName}: ${attemptStatus} | request timestamp: ${currentTimeStamp} | userRole: ${role} | IP address: ${ipAddress}`;
console.log(message);
};

0 comments on commit 05ac99d

Please sign in to comment.