google / gcp_scanner

A comprehensive scanner for Google Cloud
Apache License 2.0
311 stars 97 forks source link

Add timestamp to filename while saving output of gcp scan #84

Closed 3V3RYONE closed 1 year ago

3V3RYONE commented 1 year ago

This PR fixes #79 .

Reason of Bug

The current src/gcp_scanner/scanner.py file in the codebase, creates a JSON file with the name of the project to store the output of the scan. If the file already exists, it opens the same file in the append mode and appends the output of the scan to the same file.

Approach to fix the bug

I added the timestamp of the scan to the name of the output file while writing the scan results. In this way, for every scan, there will be different output files where the output would be written into.

Before the changes

The output gets appended to same file in case of multiple scans.

In the picture shown below, I use gcp_scan three times and I have shown the size of the output file after each scan. before_changes The wc command outputs the number of lines in the file to terminal. It can be clearly seen after each scan the number of lines in the output file gets increased by 2484. This indicates the output is appended to the same file.

After the changes

The output gets written to different files with unique timestamps of the scan.

In the picture shown below, I used gcp_scan three times and I have shown the list of files present in the output directory after each scan. after_changes It can be clearly seen that after each scan, the number of files increases based on the timestamp of the scan. Thus, the output is written to new files.

Note

Feel free to let me know if any changes are required in the current PR. Thanks a ton! :)

Checklist

google-cla[bot] commented 1 year ago

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

ZetaTwo commented 1 year ago

I like this change but one small thing that I notice immediately. Please don't use ":" in output filenames as this will not work well on Windows and MacOS.

3V3RYONE commented 1 year ago

@ZetaTwo Thank you for your comment. Fair point really. I just used colon to distinguish between the date and time in the filename. In that case we might try out other combinations. I am listing a few below:

Feel free to let me know the best convention that we should follow! Thank you :)

ZetaTwo commented 1 year ago

I think filename-YYYY-MM-DD_HH-MM-SS.json is the best.

3V3RYONE commented 1 year ago

@ZetaTwo Worked on your suggestion with the latest commit. The new format is filename-YYYY-MM-DD_HH-MM-SS.json!

new_format

peb-peb commented 1 year ago

LGTM

3V3RYONE commented 1 year ago

@ZetaTwo any updates on this?

mshudrak commented 1 year ago

Fixed all comments in #134, thanks for the PR. closing this PR now.