Class: LogStash::DependencyReport

Inherits:
Clamp::Command
  • Object
show all
Defined in:
lib/logstash/dependency_report.rb

Defined Under Namespace

Modules: SPDX

Instance Method Summary collapse

Instance Method Details

#executeObject



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
# File 'lib/logstash/dependency_report.rb', line 16

def execute
  require "csv"

  tmp_dir = java.lang.System.getProperty("java.io.tmpdir")
  ruby_output_path = File.join(tmp_dir, SecureRandom.uuid)
  # Write a CSV with just the ruby stuff
  CSV.open(ruby_output_path, "wb", :headers => [ "name", "version", "url", "license" ], :write_headers => true) do |csv|
    puts "Finding gem dependencies"
    gems.each { |d| csv << d }
    puts "Finding gem embedded java/jar dependencies"
    jars.each { |d| csv << d }
  end
  puts "Wrote temporary ruby deps CSV to #{ruby_output_path}"

  # Use gradle to find the rest and add to the ruby CSV
  puts "Find gradle jar dependencies #{Dir.pwd}"
  command = ["./gradlew", "generateLicenseReport", "-PlicenseReportInputCSV=#{ruby_output_path}", "-PlicenseReportOutputCSV=#{output_path}"]
  puts "Executing #{command}"
  system(*command)
  if $?.exitstatus != 0
    raise "Could not run gradle java deps! Exit status #{$?.exitstatus}"
  end

  nil
ensure
  FileUtils.rm(ruby_output_path) if ruby_output_path
end

#gemsObject



44
45
46
47
48
49
50
51
# File 'lib/logstash/dependency_report.rb', line 44

def gems
  # @mgreau requested `logstash-*` dependencies be removed from this list:
  # https://github.com/elastic/logstash/pull/8837#issuecomment-351859433
  Gem::Specification.reject { |g| g.name =~ /^logstash-/ }.collect do |gem|
    licenses = ("UNKNOWN" if gem.licenses.empty?) || (gem.licenses.map { |l| SPDX.map(l) }.join("|") if !gem.licenses.empty?)
    [gem.name, gem.version.to_s, gem.homepage, licenses]
  end
end

#jarsObject



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
# File 'lib/logstash/dependency_report.rb', line 53

def jars
  jars = []
  # For any gems with jar dependencies,
  #   Look at META-INF/MANIFEST.MF for any jars in each gem
  #   Note any important details.
  Gem::Specification.select { |g| g.requirements && g.requirements.any? { |r| r =~ /^jar / } }.collect do |gem|

    # Where is the gem installed
    root = gem.full_gem_path

    Dir.glob(File.join(root, "**", "*.jar")).collect do |path|
      jar = java.util.jar.JarFile.new(path)
      manifest = jar.getManifest

      pom_entries = jar.entries.select { |t| t.getName.start_with?("META-INF/maven/") && t.getName.end_with?("/pom.properties") }

      # Some jar files have multiple maven pom.properties files. It is unclear how to know what is correct?
      # TODO(sissel): Maybe we should use all pom.properties files? None of the pom.properties/pom.xml files have license information, though.
      # TODO(sissel): In some cases, there are META-INF/COPYING and
      #   META-INF/NOTICE.txt files? Can we use these somehow? There is no
      #   common syntax for parsing these files, though...
      pom_map = if pom_entries.count == 1
        pom_in = jar.getInputStream(pom_entries.first)
        pom_content = pom_in.available.times.collect { pom_in.read }.pack("C*")
        # Split non-comment lines by `key=val` into a map { key => val }
        Hash[pom_content.split(/\r?\n/).grep(/^[^#]/).map { |line| line.split("=", 2) }]
      else
        {}
      end

      next if manifest.nil?
      # convert manifest attributes to a map w/ keys .to_s
      # without this, the attribute keys will be `Object#inspect` values
      # like #<Java::JavaUtilJar::Attributes::Name0xabcdef0>
      attributes = Hash[manifest.getMainAttributes.map { |k,v| [k.to_s, v] }]

      begin
        # Prefer the maven/pom groupId when it is available.
        artifact = pom_map.fetch("artifactId", attributes.fetch("Implementation-Title"))
        group = pom_map.fetch("groupId", attributes.fetch("Implementation-Vendor-Id"))
        jars << [
          group + ":" + artifact,
          attributes.fetch("Bundle-Version"),
          attributes.fetch("Bundle-DocURL"),
          SPDX.map(attributes.fetch("Bundle-License")),
        ]
      rescue KeyError => e
        # The jar is missing a required manifest field, it may not have any useful manifest data.
        # Ignore it and move on.
      end
    end
  end
  jars.uniq.sort
end