Skip to content

Commit

Permalink
fix secrets from filestash errors
Browse files Browse the repository at this point in the history
  • Loading branch information
brnovasco committed Aug 5, 2024
1 parent 72d769e commit f3d7dd9
Showing 1 changed file with 43 additions and 11 deletions.
54 changes: 43 additions & 11 deletions apps/deepsirius-ui/src/server/api/routers/ssh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ const unzippedImage = z.object({
src: z.string(),
});

const apiErrorSchema = z
.object({
status: z.string(),
message: z.string(),
})
.transform((data) => {
return {
status: data.status,
message: data.message.replace(env.STORAGE_API_KEY, '***'),
};
});

export const sshRouter = createTRPCRouter({
ls: protectedProcedure
.input(
Expand Down Expand Up @@ -48,12 +60,7 @@ export const sshRouter = createTRPCRouter({
const data: unknown = await res.json();

if (!res.ok) {
const error = z
.object({
status: z.string(),
message: z.string(),
})
.parse(data);
const error = apiErrorSchema.parse(data);

throw new TRPCError({
code: 'INTERNAL_SERVER_ERROR',
Expand Down Expand Up @@ -110,16 +117,24 @@ export const sshRouter = createTRPCRouter({
Cookie: cookie,
},
});
if (!res.ok) {
const data: unknown = await res.json();
const error = apiErrorSchema.parse(data);

throw new TRPCError({
code: 'INTERNAL_SERVER_ERROR',
message: error.message,
});
}
const data = await res.text();
if (!res.ok) {
throw new TRPCError({
code: 'INTERNAL_SERVER_ERROR',
message: data,
message: data.replace(env.STORAGE_API_KEY, '***'),
});
}
return { content: data };
}),
}),
catImage: protectedProcedure
.input(
z
Expand Down Expand Up @@ -147,6 +162,15 @@ export const sshRouter = createTRPCRouter({
Cookie: cookie,
},
});
if (!res.ok) {
const data: unknown = await res.json();
const error = apiErrorSchema.parse(data);

throw new TRPCError({
code: 'INTERNAL_SERVER_ERROR',
message: error.message,
});
}
const data = await res.arrayBuffer();
const base64data = Buffer.from(data).toString('base64');
return { src: `data:image/png;base64,${base64data}` };
Expand Down Expand Up @@ -179,10 +203,16 @@ export const sshRouter = createTRPCRouter({
},
});
if (!res.ok) {
throw new Error(`Failed to fetch: ${res.statusText}`);
const data: unknown = await res.json();
const error = apiErrorSchema.parse(data);

throw new TRPCError({
code: 'INTERNAL_SERVER_ERROR',
message: error.message,
});
}
const buffer = await res.arrayBuffer().catch((err: Error) => {
throw new Error(`Failed to fetch buffer file: ${err.message}`);
throw new Error(`Failed to fetch buffer file: ${err.message.replace(env.STORAGE_API_KEY, '***')}`);
});
const zip = await JSZip.loadAsync(buffer).catch((err: Error) => {
throw new Error(`Failed to load ZIP file: ${err.message}`);
Expand Down Expand Up @@ -347,7 +377,9 @@ export const sshRouter = createTRPCRouter({
.query(async ({ ctx, input }) => {
const connection = ctx.connection;

const command = `head ${input.lines ? `-n ${input.lines} ` : ''}${input.path}`;
const command = `head ${input.lines ? `-n ${input.lines} ` : ''}${
input.path
}`;
const { stdout, stderr } = await connection.execCommand(command);

if (!!stderr) {
Expand Down

0 comments on commit f3d7dd9

Please sign in to comment.