Vendor SBOM schema
This commit is contained in:
parent
90a90b3076
commit
040372fafa
7816
Library/Homebrew/data/schemas/sbom.spdx.schema.3.json
Normal file
7816
Library/Homebrew/data/schemas/sbom.spdx.schema.3.json
Normal file
File diff suppressed because it is too large
Load Diff
@ -1240,8 +1240,6 @@ on_request: installed_on_request?, options:)
|
|||||||
def fetch
|
def fetch
|
||||||
return if previously_fetched_formula
|
return if previously_fetched_formula
|
||||||
|
|
||||||
SBOM.fetch_schema! if Homebrew::EnvConfig.developer?
|
|
||||||
|
|
||||||
fetch_dependencies
|
fetch_dependencies
|
||||||
|
|
||||||
return if only_deps?
|
return if only_deps?
|
||||||
|
|||||||
@ -10,9 +10,7 @@ require "utils/curl"
|
|||||||
# Rather than calling `new` directly, use one of the class methods like {SBOM.create}.
|
# Rather than calling `new` directly, use one of the class methods like {SBOM.create}.
|
||||||
class SBOM
|
class SBOM
|
||||||
FILENAME = "sbom.spdx.json"
|
FILENAME = "sbom.spdx.json"
|
||||||
SCHEMA_URL = "https://spdx.github.io/spdx-3-model/model.jsonld"
|
SCHEMA_FILE = (HOMEBREW_LIBRARY_PATH/"data/schemas/sbom.spdx.schema.3.json").freeze
|
||||||
SCHEMA_FILENAME = "sbom.spdx.schema.3.json"
|
|
||||||
SCHEMA_CACHE_TARGET = (HOMEBREW_CACHE/"sbom/#{SCHEMA_FILENAME}").freeze
|
|
||||||
|
|
||||||
# Instantiates a {SBOM} for a new installation of a formula.
|
# Instantiates a {SBOM} for a new installation of a formula.
|
||||||
sig { params(formula: Formula, tab: Tab).returns(T.attached_class) }
|
sig { params(formula: Formula, tab: Tab).returns(T.attached_class) }
|
||||||
@ -89,43 +87,9 @@ class SBOM
|
|||||||
spdxfile(formula).exist?
|
spdxfile(formula).exist?
|
||||||
end
|
end
|
||||||
|
|
||||||
sig { returns(T::Hash[String, String]) }
|
sig { returns(T::Hash[String, T.untyped]) }
|
||||||
def self.fetch_schema!
|
def self.schema
|
||||||
return @schema if @schema.present?
|
@schema ||= JSON.parse(SCHEMA_FILE.read, freeze: true)
|
||||||
|
|
||||||
url = SCHEMA_URL
|
|
||||||
target = SCHEMA_CACHE_TARGET
|
|
||||||
quieter = target.exist? && !target.empty?
|
|
||||||
|
|
||||||
curl_args = Utils::Curl.curl_args(retries: 0)
|
|
||||||
curl_args += ["--silent", "--time-cond", target.to_s] if quieter
|
|
||||||
|
|
||||||
begin
|
|
||||||
unless quieter
|
|
||||||
oh1 "Fetching SBOM schema"
|
|
||||||
ohai "Downloading #{url}"
|
|
||||||
end
|
|
||||||
Utils::Curl.curl_download(*curl_args, url, to: target, retries: 0)
|
|
||||||
FileUtils.touch(target, mtime: Time.now)
|
|
||||||
rescue ErrorDuringExecution
|
|
||||||
target.unlink if target.exist? && target.empty?
|
|
||||||
|
|
||||||
if target.exist?
|
|
||||||
opoo "SBOM schema update failed, falling back to cached version."
|
|
||||||
else
|
|
||||||
opoo "Failed to fetch SBOM schema, cannot perform SBOM validation!"
|
|
||||||
|
|
||||||
return {}
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
@schema = begin
|
|
||||||
JSON.parse(target.read, freeze: true)
|
|
||||||
rescue JSON::ParserError
|
|
||||||
target.unlink
|
|
||||||
opoo "Failed to fetch SBOM schema, cached version corrupted, cannot perform SBOM validation!"
|
|
||||||
{}
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
sig { params(bottling: T::Boolean).returns(T::Boolean) }
|
sig { params(bottling: T::Boolean).returns(T::Boolean) }
|
||||||
@ -136,14 +100,7 @@ class SBOM
|
|||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
schema = SBOM.fetch_schema!
|
schemer = JSONSchemer.schema(SBOM.schema)
|
||||||
if schema.blank?
|
|
||||||
error_message = "Could not fetch JSON schema to validate SBOM!"
|
|
||||||
ENV["HOMEBREW_ENFORCE_SBOM"] ? odie(error_message) : opoo(error_message)
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
|
|
||||||
schemer = JSONSchemer.schema(schema)
|
|
||||||
data = to_spdx_sbom(bottling:)
|
data = to_spdx_sbom(bottling:)
|
||||||
return true if schemer.valid?(data)
|
return true if schemer.valid?(data)
|
||||||
|
|
||||||
|
|||||||
@ -443,7 +443,6 @@ RSpec.describe FormulaInstaller do
|
|||||||
|
|
||||||
it "shows audit problems if HOMEBREW_DEVELOPER is set" do
|
it "shows audit problems if HOMEBREW_DEVELOPER is set" do
|
||||||
ENV["HOMEBREW_DEVELOPER"] = "1"
|
ENV["HOMEBREW_DEVELOPER"] = "1"
|
||||||
expect(SBOM).to receive(:fetch_schema!).and_return({})
|
|
||||||
formula_installer.fetch
|
formula_installer.fetch
|
||||||
formula_installer.install
|
formula_installer.install
|
||||||
expect(formula_installer).to receive(:audit_installed).and_call_original
|
expect(formula_installer).to receive(:audit_installed).and_call_original
|
||||||
|
|||||||
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
require "sbom"
|
require "sbom"
|
||||||
|
|
||||||
RSpec.describe SBOM, :needs_network do
|
RSpec.describe SBOM do
|
||||||
describe "#valid?" do
|
describe "#valid?" do
|
||||||
it "returns true if a minimal SBOM is valid" do
|
it "returns true if a minimal SBOM is valid" do
|
||||||
f = formula { url "foo-1.0" }
|
f = formula { url "foo-1.0" }
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user