macaron package
This module initializes the necessary components for the macaron package.
Subpackages
- macaron.build_spec_generator package
- macaron.code_analyzer package
- macaron.config package
- macaron.database package
- macaron.dependency_analyzer package
- macaron.malware_analyzer package
- macaron.output_reporter package
- macaron.parsers package
- macaron.path_utils package
- macaron.policy_engine package
- macaron.provenance package
- macaron.repo_finder package
- macaron.repo_verifier package
- macaron.slsa_analyzer package
- macaron.vsa package
Submodules
macaron.console module
This module implements a rich console handler for logging.
- class macaron.console.RichConsoleHandler(*args, verbose=False, **kwargs)
Bases:
RichHandler
A rich console handler for logging with rich formatting and live updates.
- __init__(*args, verbose=False, **kwargs)
Initialize the RichConsoleHandler.
- emit(record)
Emit a log record with rich formatting.
- Parameters:
record (logging.LogRecord) – The log record to be emitted.
- Return type:
- add_description_table_content(key, value)
Add or update a key-value pair in the description table.
- no_of_checks(value)
Initialize the progress bar with the total number of checks.
- update_checks(check_id, status='RUNNING')
Update the status of a specific check and refresh the progress table.
- update_checks_summary(checks_summary, total_checks)
Update the summary tables with the results of the checks.
- update_report_table(report_type, report_path)
Update the report table with the path of a generated report.
- generate_policy_summary_table()
Generate the policy summary table based on the current policy summary data.
- Return type:
- update_policy_report(report_path)
Update the policy report path in the policy summary.
- update_vsa(vsa_path)
Update the verification summary attestation path.
- update_policy_engine(results)
Update the policy engine results including components that violate or satisfy policies.
- update_find_source_table(key, value)
Add or update a key-value pair in the find source table.
- update_dump_defaults(value)
Update the dump defaults value.
- update_gen_build_spec(key, value)
Add or update a key-value pair in the generate build spec table.
- make_layout()
Create the layout for the live console display.
- Returns:
A rich Group object containing the layout for the live console display.
- Return type:
Group
- error(message)
Handle error logging.
- start(command)
Start the live console display.
- class macaron.console.AccessHandler
Bases:
object
A class to manage access to the RichConsoleHandler instance.
- __init__()
Initialize the AccessHandler with a default RichConsoleHandler instance.
- set_handler(verbose)
Set a new RichConsoleHandler instance with the specified verbosity.
- Parameters:
verbose (bool) – if True, enables verbose logging
- Returns:
The new RichConsoleHandler instance.
- Return type:
- get_handler()
Get the current RichConsoleHandler instance.
- Returns:
The current RichConsoleHandler instance.
- Return type:
macaron.environment_variables module
Helper functions related to environment variables.
- macaron.environment_variables.get_patched_env(patch, _env=None)
Return a dictionary whose elements copied from
os.environ
and are updated according topatch
.This function does not modify
os.environ
.- Parameters:
patch (Mapping[str, str | None]) – A mapping (immutable) in which: - each key is an environment variable. - each value is the value to set to the corresponding environment variable. If value is
None
, the environment variable is “unset”._env (dict[str, str] | None) – The environment being updated. This is
None
by default, in which caseos.environ
is being updated.
- Returns:
The the dictionary contains the patched env variables.
- Return type:
macaron.errors module
This module contains error classes for Macaron.
- exception macaron.errors.InvalidExpectationError
Bases:
MacaronError
Happens when the provenance expectation is invalid.
- exception macaron.errors.ExpectationRuntimeError
Bases:
MacaronError
Happens if there are errors while validating the expectation against a target.
- exception macaron.errors.CUEExpectationError
Bases:
MacaronError
Happens when the CUE expectation is invalid.
- exception macaron.errors.CUERuntimeError
Bases:
MacaronError
Happens when there are errors in CUE expectation validation.
- exception macaron.errors.ConfigurationError
Bases:
MacaronError
Happens when there is an error in the configuration (.ini) file.
- exception macaron.errors.CloneError
Bases:
MacaronError
Happens when cannot clone a git repository.
- exception macaron.errors.GitTagError
Bases:
MacaronError
Happens when there is a Git tag related error.
- exception macaron.errors.RepoCheckOutError
Bases:
MacaronError
Happens when there is an error when checking out the correct revision of a git repository.
- exception macaron.errors.RepoNotFoundError
Bases:
MacaronError
Happens if a repository is not found.
- exception macaron.errors.PURLNotFoundError
Bases:
MacaronError
Happens if the PURL identifier for a software component is not found.
- exception macaron.errors.InvalidPURLError
Bases:
MacaronError
Happens when the input PURL string is invalid.
- exception macaron.errors.DuplicateError
Bases:
MacaronError
The class for errors for duplicated data.
- exception macaron.errors.InvalidHTTPResponseError
Bases:
MacaronError
Happens when the HTTP response is invalid or unexpected.
- exception macaron.errors.APIAccessError
Bases:
MacaronError
Happens when a service API cannot be accessed.
- Reasons can include:
misconfiguration issues
invalid API request
network errors
unexpected response returned by the API
- exception macaron.errors.CheckRegistryError
Bases:
MacaronError
The Check Registry Error class.
- exception macaron.errors.ProvenanceError
Bases:
MacaronError
When there is an error while extracting from provenance.
- exception macaron.errors.InvalidAnalysisTargetError
Bases:
MacaronError
When a valid Analysis Target cannot be constructed.
- exception macaron.errors.ParseError
Bases:
MacaronError
The errors related to parsers.
- exception macaron.errors.CallGraphError
Bases:
MacaronError
The errors related to callgraphs.
- exception macaron.errors.GitHubActionsValueError
Bases:
MacaronError
The errors related to GitHub Actions value errors.
- exception macaron.errors.CycloneDXParserError
Bases:
MacaronError
The CycloneDX error class.
- exception macaron.errors.DependencyAnalyzerError
Bases:
MacaronError
The DependencyAnalyzer error class.
- exception macaron.errors.HeuristicAnalyzerValueError
Bases:
MacaronError
Error class for BaseHeuristicAnalyzer errors when parsing data.
- exception macaron.errors.LocalArtifactFinderError
Bases:
MacaronError
Happens when there is an error looking for local artifacts.
- exception macaron.errors.SourceCodeError
Bases:
MacaronError
Error for operations on package source code.
- exception macaron.errors.CommandLineParseError
Bases:
Exception
Raised if an error is encountered while parsing a CLI Command.
- exception macaron.errors.PatchBuildCommandError
Bases:
Exception
Raised if an error is encountered while patching a Maven CLI Command.
macaron.json_tools module
This module provides utility functions for JSON data.
- macaron.json_tools.json_extract(entry, keys, type_)
Return the value found by following the list of depth-sequential keys inside the passed JSON dictionary.
The value must be of the passed type.
- Parameters:
- Returns:
The found value as the type of the type parameter.
- Return type:
T | None
macaron.util module
This module includes utilities functions for Macaron.
- macaron.util.send_get_http(url, headers)
Send the GET HTTP request with the given url and headers.
This method also handle logging when the server return error status code.
- macaron.util.send_head_http_raw(url, headers=None, timeout=None, allow_redirects=True)
Send the HEAD HTTP request with the given url and headers.
This method also handle logging when the API server return error status code.
- Parameters:
- Returns:
If a Response object is returned and
allow_redirects
isTrue
(the default) it will have a status code of 200 (OK). Ifallow_redirects
isFalse
the response can instead have a status code of 302. Otherwise, the request has failed andNone
will be returned.- Return type:
Response | None
- macaron.util.send_get_http_raw(url, headers=None, timeout=None, allow_redirects=True, check_response_fails=True, stream=False)
Send the GET HTTP request with the given url and headers.
This method also handle logging when the API server return error status code.
- Parameters:
url (str) – The url of the request.
headers (dict | None) – The dict that describes the headers of the request.
timeout (int | None) – The request timeout (optional).
allow_redirects (bool) – Whether to allow redirects. Default: True.
check_response_fails (bool) – When True, check if the response fails. Otherwise, return the response.
stream (bool) – Indicates whether the response should be immediately downloaded (False) or streamed (True). Default: False.
- Returns:
If a Response object is returned and
allow_redirects
isTrue
(the default) it will have a status code of 200 (OK). Ifallow_redirects
isFalse
the response can instead have a status code of 302. Otherwise, the request has failed andNone
will be returned. Ifcheck_response_fails
is False, the response will be returned regardless of its status code.- Return type:
Response | None
- macaron.util.send_post_http_raw(url, json_data=None, headers=None, timeout=None, allow_redirects=True)
Send a POST HTTP request with the given url, data, and headers.
This method also handle logging when the API server returns error status code.
- Parameters:
- Returns:
If a Response object is returned and
allow_redirects
isTrue
(the default) it will have a status code of 200 (OK). Ifallow_redirects
isFalse
the response can instead have a status code of 302. Otherwise, the request has failed andNone
will be returned.- Return type:
Response | None
- class macaron.util.StreamWriteDownloader(file)
Bases:
object
A class to handle writing a streamed download to a file.
- __init__(file)
Initialise the class with the file path.
- macaron.util.can_download_file(url, size_limit, timeout=None)
Send a head request to check if the file provided at url can be downloaded within the size limit.
It expects a URL to a file, and checks the “Content-Length” field of the response.
- macaron.util.download_file_with_size_limit(url, headers, file_path, timeout=40, size_limit=0)
Download a file with a size limit that will abort the operation if exceeded.
- Parameters:
url (str) – The target of the request.
headers (dict) – The headers to use in the request.
file_path (str) – The path to download the file to.
timeout (int) – The timeout in seconds for the request.
size_limit (int) – The size limit in bytes of the downloaded file. A download will terminate if it reaches beyond this amount.
- Returns:
True if the operation succeeded, False otherwise.
- Return type:
- macaron.util.stream_file_with_size_limit(url, headers, chunk_function, timeout=40, size_limit=0)
Stream a file download and perform the passed function on the chunks of its data.
If data in excess of the size limit is received, this operation will be aborted.
- Parameters:
url (str) – The target of the request.
headers (dict) – The headers to use in the request.
chunk_function (Callable[[bytes], None]) – The function to use with each downloaded chunk.
timeout (int) – The timeout in seconds for the request.
size_limit (int) – The size limit in bytes of the downloaded file. A download will terminate if it reaches beyond this amount. The default value of zero disables the limit.
- Returns:
True if the operation succeeded, False otherwise.
- Return type:
- macaron.util.check_rate_limit(response)
Check the remaining calls limit to GitHub API and wait accordingly.
- Parameters:
response (Response) – The latest response from GitHub API.
- Return type:
- macaron.util.construct_query(params)
Construct a URL query from the provided keywords params.
- Parameters:
params (dict) – The dictionary of parameters for the search.
- Returns:
The constructed query as string.
- Return type:
Examples
>>> construct_query({"bar":1,"foo":2}) 'bar=1&foo=2'
- macaron.util.download_github_build_log(url, headers)
Download and return the build log from a GitHub API build log url.
- macaron.util.copy_file(src, dest_dir)
Copy a file using shutil.copy2.
This copy operation will preserve the permission of the src file.
- macaron.util.copy_file_bulk(file_list, src_path, target_path)
Copy multiple files using the
copy_file
method.Files in
file_list
will be copied from src_path to target_path.If a file in
file_list
exists intarget_path
, it will be ignored. This method will handle creating intermediate dirs to store files accordingly.- Parameters:
- Returns:
True if succeed else False.
- Return type:
See also
copy_file
Copy a single file.
Examples
file.txt
will be copied fromsrc/foo/bar/file.txt
totarget/foo/bar/file.txt
copy_file_bulk(["foo/bar/file.txt"], "src", "target")
- class macaron.util.BytesDecoder
Bases:
object
This class aims to decode some non-UTF8 bytes to a valid string.
The aim is not to ‘correctly’ parse the passed data. Only to successfully do so. It is assumed that an attempt to decode using UTF8 has already failed. The top 10 most common encodings (after UTF-8) are tried.
- COMMON_ENCODINGS = ['ISO-8859-1', 'cp1252', 'cp1251', 'euc-jp', 'euc-kr', 'shift_jis', 'gb2312', 'cp1250', 'ISO-8859-2', 'big5']
- macaron.util.html_is_js_challenge(html)
Check if this HTML is the JavaScript Challenge response.
The JavaScript Challenge is generally returned to a GET request when a CDN serves some JavaScript code to be rendered for the page. This usually means the HTML page isn’t obtained when using request libraries that cannot render JavaScript, and this page is returned instead.