-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tools/write_workspce_sbom: Implement a tool to generate a SBOM for
Bazel's workspace and check it in.
- Loading branch information
1 parent
875707f
commit d46d50c
Showing
3 changed files
with
94 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#!/usr/bin/env python3 | ||
# Copyright 2020 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Proof of a WORKSPACE SBOM generator. | ||
This is only a demonstration. It will be replaced with other tools. | ||
""" | ||
|
||
import argparse | ||
import codecs | ||
import json | ||
import sbom | ||
import subprocess | ||
import os | ||
|
||
TOOL = 'https//github.com/bazelbuild/rules_license/tools:write_workspace_sbom' | ||
|
||
def main(): | ||
parser = argparse.ArgumentParser( | ||
description='Demonstraton license compliance checker') | ||
|
||
parser.add_argument('--out', default='sbom.out', help='SBOM output') | ||
args = parser.parse_args() | ||
|
||
if "BUILD_WORKING_DIRECTORY" in os.environ: | ||
os.chdir(os.environ["BUILD_WORKING_DIRECTORY"]) | ||
|
||
external_query_process = subprocess.run( | ||
['bazel', 'query', '--output', 'streamed_jsonproto', '//external:*'], | ||
stdout=subprocess.PIPE, | ||
) | ||
sbom_packages = [] | ||
for dep_string in external_query_process.stdout.decode('utf-8').splitlines(): | ||
dep = json.loads(dep_string) | ||
if dep["type"] != "RULE": | ||
continue | ||
|
||
rule = dep["rule"] | ||
if rule["ruleClass"] == "http_archive": | ||
sbom_package = {} | ||
sbom_packages.append(sbom_package) | ||
|
||
if "attribute" not in rule: | ||
continue | ||
|
||
attributes = {attribute["name"]: attribute for attribute in rule["attribute"]} | ||
|
||
if "name" in attributes: | ||
sbom_package["package_name"] = attributes["name"]["stringValue"] | ||
|
||
if "url" in attributes: | ||
sbom_package["package_url"] = attributes["url"]["stringValue"] | ||
elif "urls" in attributes: | ||
urls = attributes["urls"]["stringListValue"] | ||
if urls and len(urls) > 0: | ||
sbom_package["package_url"] = attributes["urls"]["stringListValue"][0] | ||
|
||
with codecs.open(args.out, mode='w', encoding='utf-8') as out: | ||
sbom_writer = sbom.SBOMWriter(TOOL, out) | ||
sbom_writer.write_header(package="Bazel's Workspace SBOM") | ||
sbom_writer.write_packages(packages=sbom_packages) | ||
|
||
if __name__ == '__main__': | ||
main() |