Skip to content

Commit

Permalink
IMN-740 get import presigned url (#910)
Browse files Browse the repository at this point in the history
  • Loading branch information
Viktor-K authored Sep 12, 2024
1 parent fdd9255 commit 2244046
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 6 deletions.
6 changes: 6 additions & 0 deletions packages/bff/.env
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,9 @@ RATE_LIMITER_TIMEOUT="300"
EXPORT_ESERVICE_CONTAINER="interop-application-import-export-local"
EXPORT_ESERVICE_PATH="local/eservices-export"
PRESIGNED_URL_GET_DURATION_MINUTES=5000

IMPORT_ESERVICE_CONTAINER= "interop-application-import-export-local"
IMPORT_ESERVICE_PATH= "local/eservices-import"
PRESIGNED_URL_PUT_DURATION_MINUTES= 5000

RISK_ANALYSIS_DOCUMENTS_PATH="risk-analysis/docs"
20 changes: 17 additions & 3 deletions packages/bff/src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,20 @@ export const ExportFileConfig = z
}));
export type ExportFileConfig = z.infer<typeof ExportFileConfig>;

export const S3RiskAnalysisConfig = z
export const ImportFileConfig = z
.object({
IMPORT_ESERVICE_CONTAINER: z.string(),
IMPORT_ESERVICE_PATH: z.string(),
PRESIGNED_URL_PUT_DURATION_MINUTES: z.coerce.number(),
})
.transform((c) => ({
importEserviceContainer: c.IMPORT_ESERVICE_CONTAINER,
importEservicePath: c.IMPORT_ESERVICE_PATH,
presignedUrlPutDurationMinutes: c.PRESIGNED_URL_PUT_DURATION_MINUTES,
}));
export type ImportFileConfig = z.infer<typeof ImportFileConfig>;

export const RiskAnalysisDocumentConfig = z
.object({
RISK_ANALYSIS_DOCUMENTS_PATH: z.string(),
})
Expand All @@ -139,10 +152,11 @@ const BffProcessConfig = CommonHTTPServiceConfig.and(TenantProcessServerConfig)
.and(TokenGenerationConfig)
.and(SessionTokenGenerationConfig)
.and(FileManagerConfig)
.and(S3RiskAnalysisConfig)
.and(AllowedListConfig)
.and(S3Config)
.and(ExportFileConfig);
.and(RiskAnalysisDocumentConfig)
.and(ExportFileConfig)
.and(ImportFileConfig);

export type BffProcessConfig = z.infer<typeof BffProcessConfig>;
export const config: BffProcessConfig = BffProcessConfig.parse(process.env);
22 changes: 19 additions & 3 deletions packages/bff/src/routers/catalogRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -580,9 +580,25 @@ const catalogRouter = (
}
}
)
.get("/import/eservices/presignedUrl", async (_req, res) =>
res.status(501).send()
)
.get("/import/eservices/presignedUrl", async (req, res) => {
const ctx = fromBffAppContext(req.ctx, req.headers);
try {
const response = await catalogService.generatePutPresignedUrl(
req.query.fileName,
ctx
);

return res.status(200).json(response).send();
} catch (error) {
const errorRes = makeApiProblem(
error,
bffGetCatalogErrorMapper,
ctx.logger,
"Error getting eservice import presigned url"
);
return res.status(errorRes.status).json(errorRes).end();
}
})
.post("/import/eservices", async (_req, res) => res.status(501).send());

return catalogRouter;
Expand Down
17 changes: 17 additions & 0 deletions packages/bff/src/services/catalogService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -927,5 +927,22 @@ export function catalogServiceBuilder(
url,
};
},
generatePutPresignedUrl: async (
filename: string,
{ authData }: WithLogger<BffAppContext>
): Promise<bffApi.FileResource> => {
const path = `${bffConfig.importEservicePath}/${authData.organizationId}`;
const url = await fileManager.generatePutPresignedUrl(
bffConfig.importEserviceContainer,
path,
filename,
bffConfig.presignedUrlPutDurationMinutes
);

return {
filename,
url,
};
},
};
}
16 changes: 16 additions & 0 deletions packages/commons/src/file-manager/fileManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ export type FileManager = {
fileName: string,
durationInMinutes: number
) => Promise<string>;
generatePutPresignedUrl: (
bucketName: string,
path: string,
fileName: string,
durationInMinutes: number
) => Promise<string>;
};

export function initFileManager(
Expand Down Expand Up @@ -195,5 +201,15 @@ export function initFileManager(
const command = new GetObjectCommand({ Bucket: bucketName, Key: key });
return getSignedUrl(client, command, { expiresIn: durationInMinutes });
},
generatePutPresignedUrl: async (
bucketName: string,
path: string,
fileName: string,
durationInMinutes: number
): Promise<string> => {
const key: string = buildS3Key(path, undefined, fileName);
const command = new PutObjectCommand({ Bucket: bucketName, Key: key });
return getSignedUrl(client, command, { expiresIn: durationInMinutes });
},
};
}

0 comments on commit 2244046

Please sign in to comment.