From 0209621f813fc86b3385add00a38f2477240a367 Mon Sep 17 00:00:00 2001 From: pyth0n1c <87383215+pyth0n1c@users.noreply.github.com> Date: Mon, 4 Apr 2022 15:29:28 -0700 Subject: [PATCH 1/3] Code to generate and upload pass rate badge after completion of a scheduled run. Also uploads the summary_test_results.json file for use on research.splunk.com --- .github/workflows/detection-testing.yml | 29 ++++++++- .../generate_detection_coverage_badge.py | 65 +++++++++++++++++++ 2 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 bin/docker_detection_tester/generate_detection_coverage_badge.py diff --git a/.github/workflows/detection-testing.yml b/.github/workflows/detection-testing.yml index 80062fe601..2d0bc6ca44 100644 --- a/.github/workflows/detection-testing.yml +++ b/.github/workflows/detection-testing.yml @@ -287,8 +287,8 @@ jobs: name: DetectionFailureManifest path: | bin/docker_detection_tester/detection_failure_manifest.json - + #Always clean these up, they make the output messy - name: Clean up intermediate Files uses: geekyeggo/delete-artifact@v1 @@ -305,7 +305,34 @@ jobs: config_tests_7.json.results config_tests_8.json.results config_tests_9.json.results + + - name: Upload S3 Badge and Summary Artifacts for Nightly Scheduled Run + if: ${{ github.event_name == 'schedule' }} + uses: aws-actions/configure-aws-credentials@v1 + with: + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + + aws-region: us-west-2 + run: | + cd bin/docker_detection_tester + python generate_detection_coverage_badge.py --input_summary_file summary_test_results.json --output_badge_file detection_coverage.svg --badge_string "Pass Rate" + + + #Upload artifact (summary test results) + aws s3 cp summary_test_results.json s3://security_content/reporting/summary_test_results.json + + # make the file public since it is not by default + aws s3api put-object-acl --bucket security-content --key summary_test_results.json --acl public-read + + + #Upload artifact (test results coverage badge) + aws s3 cp detection_coverage.svg s3://security_content/reporting/detection_coverage.svg + + # make the file public since it is not by default + aws s3api put-object-acl --bucket security-content --key detection_coverage.svg --acl public-read + diff --git a/bin/docker_detection_tester/generate_detection_coverage_badge.py b/bin/docker_detection_tester/generate_detection_coverage_badge.py new file mode 100644 index 0000000000..142e5b488c --- /dev/null +++ b/bin/docker_detection_tester/generate_detection_coverage_badge.py @@ -0,0 +1,65 @@ +import argparse +import json +import sys + +RAW_BADGE_SVG = ''' + + + + + + + + + + + + + + {} + {} + +''' + + +parser = argparse.ArgumentParser(description='Use a summary.json file to generate a test coverage badge') +parser.add_argument('-i', "--input_summary_file", type=argparse.FileType('r'), required = True, + help='Summary file to use to generate the pass percentage badge') +parser.add_argument('-o', "--output_badge_file", type=argparse.FileType('w'), required = True, + help='Name of the badge to output') +parser.add_argument('-s', "--badge_string", type=str, required = True, + help='Name of the badge to output') + + + +try: + results = parser.parse_args() +except Exception as e: + print(f"Error parsing arguments: {str(e)}") + exit(1) + +try: + summary_info = json.loads(results.input_summary_file.read()) +except Exception as e: + print(f"Error loading {results.input_summary_file.name} JSON file: {str(e)}") + sys.exit(1) + +if 'summary' not in summary_info: + print("Missing 'summary' key in {results.input_summary_file.name}") + sys.exit(1) +elif 'PASS_RATE' not in summary_info['summary'] or 'TESTS_PASSED' not in summary_info['summary']: + print(f"Missing PASS_RATE in 'summary' section of {results.input_summary_file.name}") + sys.exit(1) +pass_percent = 100 * summary_info['summary']['PASS_RATE'] + + +try: + results.output_badge_file.write(RAW_BADGE_SVG.format(results.badge_string, "{:2.1f}%".format(pass_percent))) +except Exception as e: + print(f"Error generating badge: {str(e)}") + sys.exit(1) + + +print("Badge {results.output_badge_file.name} successfully generated!") +sys.exit(0) + From 8abf0592fce76e70c3d091bb901efb10026539c0 Mon Sep 17 00:00:00 2001 From: pyth0n1c <87383215+pyth0n1c@users.noreply.github.com> Date: Mon, 4 Apr 2022 15:32:09 -0700 Subject: [PATCH 2/3] Converting string to fstring --- .../generate_detection_coverage_badge.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/docker_detection_tester/generate_detection_coverage_badge.py b/bin/docker_detection_tester/generate_detection_coverage_badge.py index 142e5b488c..9962a4ef6a 100644 --- a/bin/docker_detection_tester/generate_detection_coverage_badge.py +++ b/bin/docker_detection_tester/generate_detection_coverage_badge.py @@ -60,6 +60,6 @@ except Exception as e: sys.exit(1) -print("Badge {results.output_badge_file.name} successfully generated!") +print(f"Badge {results.output_badge_file.name} successfully generated!") sys.exit(0) From 93176ca83b85b6de32bc54b77ae2763b7219e3f0 Mon Sep 17 00:00:00 2001 From: pyth0n1c <87383215+pyth0n1c@users.noreply.github.com> Date: Mon, 4 Apr 2022 15:52:49 -0700 Subject: [PATCH 3/3] Updated S3 upload code to proper bucket path --- .github/workflows/detection-testing.yml | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/.github/workflows/detection-testing.yml b/.github/workflows/detection-testing.yml index 2d0bc6ca44..ef6af6d8fd 100644 --- a/.github/workflows/detection-testing.yml +++ b/.github/workflows/detection-testing.yml @@ -320,17 +320,19 @@ jobs: #Upload artifact (summary test results) - aws s3 cp summary_test_results.json s3://security_content/reporting/summary_test_results.json + aws s3 cp summary_test_results.json s3://security-content/reporting/summary_test_results.json + #Since these reside in a public bucket, no need to explicitly mark as public # make the file public since it is not by default - aws s3api put-object-acl --bucket security-content --key summary_test_results.json --acl public-read + #aws s3api put-object-acl --bucket security-content --key reporting/summary_test_results.json --acl public-read #Upload artifact (test results coverage badge) - aws s3 cp detection_coverage.svg s3://security_content/reporting/detection_coverage.svg - + aws s3 cp detection_coverage.svg s3://security-content/reporting/detection_coverage.svg + + #Since these reside in a public bucket, no need to explicitly mark as public # make the file public since it is not by default - aws s3api put-object-acl --bucket security-content --key detection_coverage.svg --acl public-read + #aws s3api put-object-acl --bucket security-content --key reporting/detection_coverage.svg --acl public-read