Coverage for main.py : 100%
Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1#!/usr/bin/env python3
2"""
3Integrate with Veracode's SAST APIs to allow the submission of artifacts for
4scanning and checking an app for compliance against the configured policy
5"""
7# built-ins
8import logging
9import sys
10import json
12# custom
13from veracode.check_compliance import check_compliance
14from veracode.submit_artifacts import submit_artifacts
15from veracode.api import ResultsAPI, UploadAPI, SandboxAPI
16from veracode.utils import configure_environment
17from veracode.config import get_config, apply_config
18from veracode import __project_name__
21def main() -> None:
22 """
23 Integration with Veracode Static Analysis
24 """
25 ## Setup logging
26 # Format the logs as JSON for simplicity
27 formatting = json.dumps(
28 {
29 "timestamp": "%(asctime)s",
30 "namespace": "%(name)s",
31 "loglevel": "%(levelname)s",
32 "message": "%(message)s",
33 }
34 )
35 # Default to a log level of WARNING until the config is parsed
36 logging.basicConfig(level="WARNING", format=formatting)
37 log = logging.getLogger(__project_name__)
39 # Get the effective config
40 try:
41 config = get_config()
42 except ValueError:
43 log.error("Unable to create a valid configuration")
44 sys.exit(1)
46 # Update the log level to whatever was set in the config
47 logging.getLogger().setLevel(config["loglevel"])
49 # Create the API objects and apply the config
50 try:
51 results_api = apply_config(
52 api=ResultsAPI(app_name=config["apis"]["results"]["app_name"]),
53 config=config,
54 )
55 upload_api = apply_config(
56 api=UploadAPI(app_name=config["apis"]["upload"]["app_name"]), config=config
57 )
58 if "sandbox_name" in config["apis"]["sandbox"]:
59 sandbox_api = apply_config(
60 api=SandboxAPI(
61 app_name=config["apis"]["sandbox"]["app_name"],
62 sandbox_name=config["apis"]["sandbox"]["sandbox_name"],
63 ),
64 config=config,
65 )
66 else:
67 sandbox_api = None
68 except (TypeError, NameError):
69 log.error("Unable to create valid API objects")
70 sys.exit(1)
72 # Configure the environment
73 for step in config["workflow"]:
74 if step == "submit_artifacts":
75 configure_environment(
76 api_key_id=config["api_key_id"], api_key_secret=config["api_key_secret"]
77 )
78 success = submit_artifacts(upload_api=upload_api, sandbox_api=sandbox_api)
80 if success:
81 log.info("Successfully submit build artifacts for scanning")
82 else:
83 log.error("Failed to submit build artifacts for scanning")
84 sys.exit(1)
85 elif step == "check_compliance":
86 configure_environment(
87 api_key_id=config["api_key_id"], api_key_secret=config["api_key_secret"]
88 )
89 if not check_compliance(results_api=results_api):
90 sys.exit(1)
93if __name__ == "__main__":
94 main()