aboutsummaryrefslogtreecommitdiff
path: root/rules/sbom.bzl
blob: aaa4fa49e7f8579818b9818a162096d0251ce431 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# Copyright 2022 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.
"""SBOM generation"""

load(
    "@rules_license//rules:gather_metadata.bzl",
    "gather_metadata_info",
    "gather_metadata_info_and_write",
    "write_metadata_info",
)
load(
    "@rules_license//rules:private/gathering_providers.bzl",
    "TransitiveLicensesInfo",
)

# This rule is proof of concept, and may not represent the final
# form of a rule for compliance validation.
def _generate_sbom_impl(ctx):
    # Gather all licenses and write information to one place

    licenses_file = ctx.actions.declare_file("_%s_licenses_info.json" % ctx.label.name)
    write_metadata_info(ctx, ctx.attr.deps, licenses_file)

    license_files = []
    # if ctx.outputs.license_texts:
    #     license_files = get_licenses_mapping(ctx.attr.deps).keys()

    # Now turn the big blob of data into something consumable.
    inputs = [licenses_file]
    outputs = [ctx.outputs.out]
    args = ctx.actions.args()
    args.add("--licenses_info", licenses_file.path)
    args.add("--out", ctx.outputs.out.path)
    ctx.actions.run(
        mnemonic = "CreateSBOM",
        progress_message = "Creating SBOM for %s" % ctx.label,
        inputs = inputs,
        outputs = outputs,
        executable = ctx.executable._sbom_generator,
        arguments = [args],
    )
    outputs.append(licenses_file)  # also make the json file available.
    return [DefaultInfo(files = depset(outputs))]

_generate_sbom = rule(
    implementation = _generate_sbom_impl,
    attrs = {
        "deps": attr.label_list(
            aspects = [gather_metadata_info],
        ),
        "out": attr.output(mandatory = True),
        "_sbom_generator": attr.label(
            default = Label("@rules_license//tools:write_sbom"),
            executable = True,
            allow_files = True,
            cfg = "exec",
        ),
    },
)

def generate_sbom(**kwargs):
    _generate_sbom(**kwargs)

def _manifest_impl(ctx):
    # Gather all licenses and make it available as deps for downstream rules
    # Additionally write the list of license filenames to a file that can
    # also be used as an input to downstream rules.
    licenses_file = ctx.actions.declare_file(ctx.attr.out.name)
    mappings = get_licenses_mapping(ctx.attr.deps, ctx.attr.warn_on_legacy_licenses)
    ctx.actions.write(
        output = licenses_file,
        content = "\n".join([",".join([f.path, p]) for (f, p) in mappings.items()]),
    )
    return [DefaultInfo(files = depset(mappings.keys()))]

_manifest = rule(
    implementation = _manifest_impl,
    doc = """Internal tmplementation method for manifest().""",
    attrs = {
        "deps": attr.label_list(
            doc = """List of targets to collect license files for.""",
            aspects = [gather_metadata_info],
        ),
        "out": attr.output(
            doc = """Output file.""",
            mandatory = True,
        ),
        "warn_on_legacy_licenses": attr.bool(default = False),
    },
)

def manifest(name, deps, out = None, **kwargs):
    if not out:
        out = name + ".manifest"

    _manifest(name = name, deps = deps, out = out, **kwargs)

def _licenses_used_impl(ctx):
    # Gather all licenses and make it available as JSON
    write_metadata_info(ctx, ctx.attr.deps, ctx.outputs.out)
    return [DefaultInfo(files = depset([ctx.outputs.out]))]

_licenses_used = rule(
    implementation = _licenses_used_impl,
    doc = """Internal tmplementation method for licenses_used().""",
    attrs = {
        "deps": attr.label_list(
            doc = """List of targets to collect LicenseInfo for.""",
            aspects = [gather_metadata_info_and_write],
        ),
        "out": attr.output(
            doc = """Output file.""",
            mandatory = True,
        ),
    },
)

def get_licenses_mapping(deps, warn = False):
    """Creates list of entries representing all licenses for the deps.

    Args:

      deps: a list of deps which should have TransitiveLicensesInfo providers.
            This requires that you have run the gather_licenses_info
            aspect over them

      warn: boolean, if true, display output about legacy targets that need
            update

    Returns:
      {File:package_name}
    """
    tls = []
    for dep in deps:
        lds = dep[TransitiveLicensesInfo].licenses
        tls.append(lds)

    ds = depset(transitive = tls)

    # Ignore any legacy licenses that may be in the report
    mappings = {}
    for lic in ds.to_list():
        if type(lic.license_text) == "File":
            mappings[lic.license_text] = lic.package_name
        elif warn:
            print("Legacy license %s not included, rule needs updating" % lic.license_text)

    return mappings