class Chelsea::Bom

Class to convert dependencies to SBOM xml

Public Class Methods

new(dependencies) click to toggle source
# File lib/chelsea/bom.rb, line 25
def initialize(dependencies)
  @dependencies = dependencies
end

Public Instance Methods

collect() click to toggle source
# File lib/chelsea/bom.rb, line 29
def collect
  xml
  to_s
end
random_urn_uuid() click to toggle source
# File lib/chelsea/bom.rb, line 42
def random_urn_uuid
  "urn:uuid:#{SecureRandom.uuid}"
end
to_s() click to toggle source
# File lib/chelsea/bom.rb, line 38
def to_s
  Ox.dump(@xml)
end
xml() click to toggle source
# File lib/chelsea/bom.rb, line 34
def xml
  @xml ||= _get_xml
end

Private Instance Methods

_bom_xml() click to toggle source
# File lib/chelsea/bom.rb, line 61
def _bom_xml
  bom = Ox::Element.new('bom')
  bom[:xmlns] = 'http://cyclonedx.org/schema/bom/1.1'
  bom[:version] = '1'
  bom[:serialNumber] = random_urn_uuid
  bom
end
_component_xml(name, version) click to toggle source
# File lib/chelsea/bom.rb, line 77
def _component_xml(name, version)
  component = Ox::Element.new('component')
  component[:type] = 'library'
  n = Ox::Element.new('name')
  n << name
  v = Ox::Element.new('version')
  v << version.version
  purl = Ox::Element.new('purl')
  purl << Chelsea.to_purl(name, version.version)
  component << n << v << purl
  component
end
_get_xml() click to toggle source
# File lib/chelsea/bom.rb, line 48
def _get_xml
  doc = Ox::Document.new
  doc << _root_xml
  bom = _bom_xml
  doc << bom
  components = Ox::Element.new('components')
  @dependencies.each do |_, (name, version)|
    components << _component_xml(name, version)
  end
  bom << components
  doc
end
_root_xml() click to toggle source
# File lib/chelsea/bom.rb, line 69
def _root_xml
  instruct = Ox::Instruct.new(:xml)
  instruct[:version] = '1.0'
  instruct[:encoding] = 'UTF-8'
  instruct[:standalone] = 'yes'
  instruct
end