Merge pull request #1443 from akhilmhdh/fix/service-token-email-telemetry

fix(telemetry): added back email for telemetry when using service token
This commit is contained in:
Maidul Islam
2024-02-22 16:23:00 -05:00
committed by GitHub
6 changed files with 31 additions and 7 deletions

View File

@@ -5,7 +5,7 @@ import { verifyAuth } from "@app/server/plugins/auth/verify-auth";
import { AuthMode } from "@app/services/auth/auth-type";
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 {
const strBody = body instanceof Buffer ? body.toString() : body;

View File

@@ -177,6 +177,8 @@ export const permissionServiceFactory = ({
const getServiceTokenProjectPermission = async (serviceTokenId: string, projectId: string) => {
const serviceToken = await serviceTokenDAL.findById(serviceTokenId);
if (!serviceToken) throw new BadRequestError({ message: "Service token not found" });
if (serviceToken.projectId !== projectId)
throw new UnauthorizedError({
message: "Failed to find service authorization for given project"

View File

@@ -27,7 +27,7 @@ export type TAuthMode =
}
| {
authMode: AuthMode.SERVICE_TOKEN;
serviceToken: TServiceTokens;
serviceToken: TServiceTokens & { createdByEmail: string };
actor: ActorType.SERVICE;
serviceTokenId: string;
}

View File

@@ -28,7 +28,7 @@ const getDistinctId = (req: FastifyRequest) => {
return `identity-${req.auth.identityId}`;
}
if (req.auth.actor === ActorType.SERVICE) {
return `service-token-${req.auth.serviceToken.id}`;
return req.auth.serviceToken.createdByEmail || `service-token-null-creator-${req.auth.serviceTokenId}`; // when user gets removed from system
}
return "unknown-auth-data";
};

View File

@@ -1,10 +1,32 @@
import { Knex } from "knex";
import { TDbClient } from "@app/db";
import { TableName } from "@app/db/schemas";
import { ormify } from "@app/lib/knex";
import { TableName, TUsers } from "@app/db/schemas";
import { DatabaseError } from "@app/lib/errors";
import { ormify, selectAllTableCols } from "@app/lib/knex";
export type TServiceTokenDALFactory = ReturnType<typeof serviceTokenDALFactory>;
export const serviceTokenDALFactory = (db: TDbClient) => {
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 };
};

View File

@@ -142,7 +142,7 @@ export const serviceTokenServiceFactory = ({
const updatedToken = await serviceTokenDAL.updateById(serviceToken.id, {
lastUsed: new Date()
});
return updatedToken;
return { ...serviceToken, lastUsed: updatedToken.lastUsed };
};
return {