fix(telemetry): added back email for telemetry when using service token

This commit is contained in:
Akhil Mohan
2024-02-22 17:09:33 +05:30
parent acfa89ba8b
commit be49de5f34
6 changed files with 31 additions and 7 deletions
+1 -1
View File
@@ -5,7 +5,7 @@ import { verifyAuth } from "@app/server/plugins/auth/verify-auth";
import { AuthMode } from "@app/services/auth/auth-type"; import { AuthMode } from "@app/services/auth/auth-type";
export const registerScimRouter = async (server: FastifyZodProvider) => { export const registerScimRouter = async (server: FastifyZodProvider) => {
server.addContentTypeParser("application/scim+json", { parseAs: "string" }, function (req, body, done) { server.addContentTypeParser("application/scim+json", { parseAs: "string" }, (_, body, done) => {
try { try {
const strBody = body instanceof Buffer ? body.toString() : body; const strBody = body instanceof Buffer ? body.toString() : body;
@@ -177,6 +177,8 @@ export const permissionServiceFactory = ({
const getServiceTokenProjectPermission = async (serviceTokenId: string, projectId: string) => { const getServiceTokenProjectPermission = async (serviceTokenId: string, projectId: string) => {
const serviceToken = await serviceTokenDAL.findById(serviceTokenId); const serviceToken = await serviceTokenDAL.findById(serviceTokenId);
if (!serviceToken) throw new BadRequestError({ message: "Service token not found" });
if (serviceToken.projectId !== projectId) if (serviceToken.projectId !== projectId)
throw new UnauthorizedError({ throw new UnauthorizedError({
message: "Failed to find service authorization for given project" message: "Failed to find service authorization for given project"
@@ -27,7 +27,7 @@ export type TAuthMode =
} }
| { | {
authMode: AuthMode.SERVICE_TOKEN; authMode: AuthMode.SERVICE_TOKEN;
serviceToken: TServiceTokens; serviceToken: TServiceTokens & { createdByEmail: string };
actor: ActorType.SERVICE; actor: ActorType.SERVICE;
serviceTokenId: string; serviceTokenId: string;
} }
@@ -28,7 +28,7 @@ const getDistinctId = (req: FastifyRequest) => {
return `identity-${req.auth.identityId}`; return `identity-${req.auth.identityId}`;
} }
if (req.auth.actor === ActorType.SERVICE) { if (req.auth.actor === ActorType.SERVICE) {
return `service-token-${req.auth.serviceToken.id}`; return req.auth.serviceToken.createdByEmail || `service-token-${req.auth.serviceTokenId}`; // when user gets removed from system
} }
return "unknown-auth-data"; return "unknown-auth-data";
}; };
@@ -1,10 +1,32 @@
import { Knex } from "knex";
import { TDbClient } from "@app/db"; import { TDbClient } from "@app/db";
import { TableName } from "@app/db/schemas"; import { TableName, TUsers } from "@app/db/schemas";
import { ormify } from "@app/lib/knex"; import { DatabaseError } from "@app/lib/errors";
import { ormify, selectAllTableCols } from "@app/lib/knex";
export type TServiceTokenDALFactory = ReturnType<typeof serviceTokenDALFactory>; export type TServiceTokenDALFactory = ReturnType<typeof serviceTokenDALFactory>;
export const serviceTokenDALFactory = (db: TDbClient) => { export const serviceTokenDALFactory = (db: TDbClient) => {
const stOrm = ormify(db, TableName.ServiceToken); const stOrm = ormify(db, TableName.ServiceToken);
return stOrm;
const findById = async (id: string, tx?: Knex) => {
try {
const doc = await (tx || db)(TableName.ServiceToken)
.leftJoin<TUsers>(
TableName.Users,
`${TableName.Users}.id`,
db.raw(`${TableName.ServiceToken}."createdBy"::uuid`)
)
.where(`${TableName.ServiceToken}.id`, id)
.select(selectAllTableCols(TableName.ServiceToken))
.select(db.ref("email").withSchema(TableName.Users).as("createdByEmail"))
.first();
return doc;
} catch (err) {
throw new DatabaseError({ error: err, name: "FindById" });
}
};
return { ...stOrm, findById };
}; };
@@ -142,7 +142,7 @@ export const serviceTokenServiceFactory = ({
const updatedToken = await serviceTokenDAL.updateById(serviceToken.id, { const updatedToken = await serviceTokenDAL.updateById(serviceToken.id, {
lastUsed: new Date() lastUsed: new Date()
}); });
return updatedToken; return { ...serviceToken, lastUsed: updatedToken.lastUsed };
}; };
return { return {