Class: Bundler::Audit::Database
- Inherits:
-
Object
- Object
- Bundler::Audit::Database
- Defined in:
- lib/bundler/audit/database.rb
Overview
Represents the directory of advisories, grouped by gem name and CVE number.
Constant Summary collapse
- URL =
Git URL of the ruby-advisory-db
'https://github.com/rubysec/ruby-advisory-db.git'
- PATH =
Path to the user's copy of the ruby-advisory-db
File.(File.join(ENV['HOME'],'.local','share','ruby-advisory-db'))
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
The path to the advisory database.
Class Method Summary collapse
-
.install! ⇒ Boolean
Downloads the database.
-
.path ⇒ String
The default path for the database.
-
.path=(new_path) ⇒ String
Sets the default path for the database.
-
.update! ⇒ Boolean
Updates the user's ruby-advisory-db.
Instance Method Summary collapse
-
#advisories {|advisory| ... } ⇒ Enumerator
Enumerates over every advisory in the database.
-
#advisories_for(name) {|advisory| ... } ⇒ Enumerator
Enumerates over advisories for the given gem.
-
#check_gem(gem) {|advisory| ... } ⇒ Enumerator
Verifies whether the gem is effected by any advisories.
-
#each_advisory_path {|path| ... } ⇒ Object
protected
Enumerates over every advisory path in the database.
-
#each_advisory_path_for(name) {|path| ... } ⇒ Object
protected
Enumerates over the advisories for the given gem.
-
#initialize(path = self.class.path) ⇒ Database
constructor
Initializes the Advisory Database.
-
#inspect ⇒ String
Inspects the database.
-
#last_updated ⇒ Time
Determines when the database was last updated.
-
#size ⇒ Integer
The number of advisories within the database.
-
#to_s ⇒ String
Converts the database to a String.
-
#update! ⇒ Boolean
Updates the database.
Constructor Details
#initialize(path = self.class.path) ⇒ Database
Initializes the Advisory Database.
51 52 53 54 55 56 57 |
# File 'lib/bundler/audit/database.rb', line 51 def initialize(path=self.class.path) unless File.directory?(path) raise(ArgumentError,"#{path.dump} is not a directory") end @path = path end |
Instance Attribute Details
#path ⇒ Object (readonly)
The path to the advisory database
40 41 42 |
# File 'lib/bundler/audit/database.rb', line 40 def path @path end |
Class Method Details
.install! ⇒ Boolean
Downloads the database.
87 88 89 |
# File 'lib/bundler/audit/database.rb', line 87 def self.install! system 'git', 'clone', URL, path end |
.path ⇒ String
The default path for the database.
65 66 67 |
# File 'lib/bundler/audit/database.rb', line 65 def self.path @@path ||= PATH end |
.path=(new_path) ⇒ String
Sets the default path for the database.
75 76 77 |
# File 'lib/bundler/audit/database.rb', line 75 def self.path=(new_path) @@path = new_path end |
.update! ⇒ Boolean
Updates the user's ruby-advisory-db.
101 102 103 104 105 106 107 |
# File 'lib/bundler/audit/database.rb', line 101 def self.update! if File.directory?(File.join(path, '.git')) new(path).update! else install! end end |
Instance Method Details
#advisories {|advisory| ... } ⇒ Enumerator
Enumerates over every advisory in the database.
150 151 152 153 154 155 156 |
# File 'lib/bundler/audit/database.rb', line 150 def advisories(&block) return enum_for(__method__) unless block_given? each_advisory_path do |path| yield Advisory.load(path) end end |
#advisories_for(name) {|advisory| ... } ⇒ Enumerator
Enumerates over advisories for the given gem.
173 174 175 176 177 178 179 |
# File 'lib/bundler/audit/database.rb', line 173 def advisories_for(name) return enum_for(__method__,name) unless block_given? each_advisory_path_for(name) do |path| yield Advisory.load(path) end end |
#check_gem(gem) {|advisory| ... } ⇒ Enumerator
Verifies whether the gem is effected by any advisories.
197 198 199 200 201 202 203 204 205 |
# File 'lib/bundler/audit/database.rb', line 197 def check_gem(gem) return enum_for(__method__,gem) unless block_given? advisories_for(gem.name) do |advisory| if advisory.vulnerable?(gem.version) yield advisory end end end |
#each_advisory_path {|path| ... } ⇒ Object (protected)
Enumerates over every advisory path in the database.
248 249 250 |
# File 'lib/bundler/audit/database.rb', line 248 def each_advisory_path(&block) Dir.glob(File.join(@path,'gems','*','*.yml'),&block) end |
#each_advisory_path_for(name) {|path| ... } ⇒ Object (protected)
Enumerates over the advisories for the given gem.
264 265 266 |
# File 'lib/bundler/audit/database.rb', line 264 def each_advisory_path_for(name,&block) Dir.glob(File.join(@path,'gems',name,'*.yml'),&block) end |
#inspect ⇒ String
Inspects the database.
233 234 235 |
# File 'lib/bundler/audit/database.rb', line 233 def inspect "#<#{self.class}:#{self}>" end |
#last_updated ⇒ Time
Determines when the database was last updated.
134 135 136 |
# File 'lib/bundler/audit/database.rb', line 134 def last_updated Dir.chdir(@path) { Time.parse(`git log -1 --format=%ad`) } end |
#size ⇒ Integer
The number of advisories within the database.
213 214 215 |
# File 'lib/bundler/audit/database.rb', line 213 def size each_advisory_path.count end |
#to_s ⇒ String
Converts the database to a String.
223 224 225 |
# File 'lib/bundler/audit/database.rb', line 223 def to_s @path end |
#update! ⇒ Boolean
Requires network access.
Updates the database.
120 121 122 123 124 |
# File 'lib/bundler/audit/database.rb', line 120 def update! Dir.chdir(@path) do system 'git', 'pull', 'origin', 'master' end end |