Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#3869 - Federal Restrictions - Unzip ZIP files coming from SFTP #4163

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ export class FedRestrictionProcessingService {
*/
async process(processSummary: ProcessSummary): Promise<void> {
const auditUser = this.systemUsersService.systemUser;
// Get the list of all files from SFTP ordered by file name.
// Get the list of all ZIP files from SFTP ordered by file name.
const fileSearch = new RegExp(
`^${this.esdcConfig.environmentCode}CSLS\\.PBC\\.RESTR\\.LIST\\.D[\\w]*\\.[\\d]*$`,
`^${this.esdcConfig.environmentCode}CSLS\\.PBC\\.RESTR\\.LIST\\.D[\\w]*\\.[\\d]*\\.(zip|ZIP)$`,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not enforcing the zip extension would allow the process to download the file either way, compressed or not.
I do not see an AC requesting either one or not and I do not see a reason to have it restricted.
Not a blocker.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not enforcing the .zip will also lead to not enforcing the end of file name by removing the existing $. I see the benefit of it working either way, but should e remove the $. ?

Let me know if I am missing something here.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we talked, IMO, we do need to enforce the precise ending of the string but I am ok either way.

"i",
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
getFileNameAsExtendedCurrentTimestamp,
convertToASCII,
FILE_DEFAULT_ENCODING,
readFirstExtractedFile,
} from "@sims/utilities";
import {
LINE_BREAK_SPLIT_REGEX,
Expand Down Expand Up @@ -154,10 +155,29 @@ export abstract class SFTPIntegrationBase<DownloadType> {
return false;
}
}
// Read all the file content and create a buffer with 'ascii' encoding.
const fileContent = await client.get(remoteFilePath, undefined, {
readStreamOptions: { encoding: FILE_DEFAULT_ENCODING },
});
let fileContent: string;
const fileExtension = path.extname(remoteFilePath).toLowerCase();
const isZIPFile = fileExtension === ".zip";
if (isZIPFile) {
// Read the zip file content with null encoding to avoid data corruption.
const compressedFileContent = (await client.get(
remoteFilePath,
undefined,
{ readStreamOptions: { encoding: null } },
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

)) as Buffer;
// Read the first file content with 'ascii' encoding.
const { fileName, data } = readFirstExtractedFile(
compressedFileContent,
{ encoding: FILE_DEFAULT_ENCODING },
);
this.logger.log(`Extracted the first file ${fileName}.`);
fileContent = data;
} else {
// Read all the file content and create a buffer with 'ascii' encoding.
fileContent = (await client.get(remoteFilePath, undefined, {
readStreamOptions: { encoding: FILE_DEFAULT_ENCODING },
})) as string;
}
// Convert the file content to an array of text lines and remove possible blank lines.
return fileContent
.toString()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import * as AdmZip from "adm-zip";

/**
* Reads the first extracted file from a compressed archive file.
* @param compressedFileBuffer compressed file buffer.
* @param options options.
* - `encoding`: encoding to read the file.
* @returns first extracted file name and data.
*/
export function readFirstExtractedFile(
compressedFileBuffer: Buffer,
options?: { encoding: string },
): {
fileName: string;
data: string;
} {
const zipFile = new AdmZip(compressedFileBuffer);
const [firstExtractedFile] = zipFile.getEntries();
if (!firstExtractedFile) {
throw new Error("No files found in zip file.");
}
// Read the first extracted file with the specified encoding.
const data = zipFile.readAsText(firstExtractedFile, options?.encoding);
andrewsignori-aot marked this conversation as resolved.
Show resolved Hide resolved
return {
fileName: firstExtractedFile.name,
data,
};
}
1 change: 1 addition & 0 deletions sources/packages/backend/libs/utilities/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ export * from "./math-utils";
export * from "./specialized-string-builder";
export * from "./string-utils";
export * from "./address-utils";
export * from "./compressed-file-utils";
19 changes: 19 additions & 0 deletions sources/packages/backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion sources/packages/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"@nestjs/terminus": "^10.2.3",
"@nestjs/typeorm": "^10.0.2",
"@types/ssh2-sftp-client": "^9.0.3",
"adm-zip": "^0.5.16",
"axios": "^1.7.4",
"bull": "^4.12.2",
"clamscan": "^2.2.1",
Expand Down Expand Up @@ -99,6 +100,7 @@
"@suites/di.nestjs": "^3.0.0",
"@suites/doubles.jest": "^3.0.0",
"@suites/unit": "^3.0.0",
"@types/adm-zip": "^0.5.7",
"@types/clamscan": "^2.0.8",
"@types/express": "^4.17.8",
"@types/faker": "^5.1.5",
Expand Down Expand Up @@ -177,4 +179,4 @@
"^@sims/auth(|/.*)$": "<rootDir>/libs/auth/src/$1"
}
}
}
}
Loading