Skip to content

Commit

Permalink
fix(telemetry): added back email for telemetry when using service token
Browse files Browse the repository at this point in the history
  • Loading branch information
akhilmhdh committed Feb 22, 2024
1 parent acfa89b commit be49de5
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 7 deletions.
2 changes: 1 addition & 1 deletion backend/src/ee/routes/v1/scim-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 2 additions & 0 deletions backend/src/ee/services/permission/permission-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion backend/src/server/plugins/auth/inject-identity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export type TAuthMode =
}
| {
authMode: AuthMode.SERVICE_TOKEN;
serviceToken: TServiceTokens;
serviceToken: TServiceTokens & { createdByEmail: string };
actor: ActorType.SERVICE;
serviceTokenId: string;
}
Expand Down
2 changes: 1 addition & 1 deletion backend/src/server/routes/v3/secret-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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-${req.auth.serviceTokenId}`; // when user gets removed from system
}
return "unknown-auth-data";
};
Expand Down
28 changes: 25 additions & 3 deletions backend/src/services/service-token/service-token-dal.ts
Original file line number Diff line number Diff line change
@@ -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 };
};
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit be49de5

Please sign in to comment.