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.
Defined Under Namespace
Classes: DownloadFailed, UpdateFailed
Constant Summary collapse
- URL =
Git URL of the ruby-advisory-db.
'https://github.com/rubysec/ruby-advisory-db.git'
- USER_PATH =
Path to the user's copy of the ruby-advisory-db.
File.(File.join(Gem.user_home,'.local','share','ruby-advisory-db'))
- DEFAULT_PATH =
Default path to the ruby-advisory-db.
ENV.fetch('BUNDLER_AUDIT_DB',USER_PATH)
Instance Attribute Summary collapse
-
#path ⇒ String
readonly
The path to the advisory database.
Class Method Summary collapse
-
.download(options = {}) ⇒ Dataase
Downloads the ruby-advisory-db.
-
.exists?(path = DEFAULT_PATH) ⇒ Boolean
Tests whether the database exists.
-
.path ⇒ String
The default path for the database.
-
.update!(options = {}) ⇒ Boolean
deprecated
Deprecated.
Use #update! instead.
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.
-
#commit_id ⇒ String?
The last commit ID of the repository.
-
#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.
-
#git? ⇒ Boolean
Determines if the database is a git repository.
-
#initialize(path = self.class.path) ⇒ Database
constructor
Initializes the Advisory Database.
-
#inspect ⇒ String
Inspects the database.
-
#last_updated_at ⇒ Time
Determines the time 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!(options = {}) ⇒ true?
Updates the ruby-advisory-db.
Constructor Details
#initialize(path = self.class.path) ⇒ Database
Initializes the Advisory Database.
62 63 64 65 66 67 68 |
# File 'lib/bundler/audit/database.rb', line 62 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 ⇒ String (readonly)
The path to the advisory database.
51 52 53 |
# File 'lib/bundler/audit/database.rb', line 51 def path @path end |
Class Method Details
.download(options = {}) ⇒ Dataase
Requires network access.
Downloads the ruby-advisory-db.
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/bundler/audit/database.rb', line 117 def self.download(={}) unless (.keys - [:path, :quiet]).empty? raise(ArgumentError,"Invalid option(s)") end path = .fetch(:path,DEFAULT_PATH) command = %w[git clone] command << '--quiet' if [:quiet] command << URL << path unless system(*command) raise(DownloadFailed,"failed to download #{URL} to #{path.inspect}") end return new(path) end |
.exists?(path = DEFAULT_PATH) ⇒ Boolean
Tests whether the database exists.
90 91 92 |
# File 'lib/bundler/audit/database.rb', line 90 def self.exists?(path=DEFAULT_PATH) File.directory?(path) && !(Dir.entries(path) - %w[. ..]).empty? end |
.path ⇒ String
The default path for the database.
76 77 78 |
# File 'lib/bundler/audit/database.rb', line 76 def self.path DEFAULT_PATH end |
.update!(options = {}) ⇒ Boolean
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/bundler/audit/database.rb', line 157 def self.update!(={}) raise "Invalid option(s)" unless (.keys - [:quiet]).empty? if File.directory?(DEFAULT_PATH) begin new(DEFAULT_PATH).update!() rescue UpdateFailed then false end else begin download(.merge(path: DEFAULT_PATH)) rescue DownloadFailed then false end end end |
Instance Method Details
#advisories {|advisory| ... } ⇒ Enumerator
Enumerates over every advisory in the database.
265 266 267 268 269 270 271 |
# File 'lib/bundler/audit/database.rb', line 265 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.
288 289 290 291 292 293 294 |
# File 'lib/bundler/audit/database.rb', line 288 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.
312 313 314 315 316 317 318 319 320 |
# File 'lib/bundler/audit/database.rb', line 312 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 |
#commit_id ⇒ String?
The last commit ID of the repository.
228 229 230 231 232 233 234 |
# File 'lib/bundler/audit/database.rb', line 228 def commit_id if git? Dir.chdir(@path) do `git rev-parse HEAD`.chomp end end end |
#each_advisory_path {|path| ... } ⇒ Object (protected)
Enumerates over every advisory path in the database.
363 364 365 |
# File 'lib/bundler/audit/database.rb', line 363 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.
379 380 381 |
# File 'lib/bundler/audit/database.rb', line 379 def each_advisory_path_for(name,&block) Dir.glob(File.join(@path,'gems',name,'*.yml'),&block) end |
#git? ⇒ Boolean
Determines if the database is a git repository.
180 181 182 |
# File 'lib/bundler/audit/database.rb', line 180 def git? File.directory?(File.join(@path,'.git')) end |
#inspect ⇒ String
Inspects the database.
348 349 350 |
# File 'lib/bundler/audit/database.rb', line 348 def inspect "#<#{self.class}:#{self}>" end |
#last_updated_at ⇒ Time
Determines the time when the database was last updated.
243 244 245 246 247 248 249 250 251 |
# File 'lib/bundler/audit/database.rb', line 243 def last_updated_at if git? Dir.chdir(@path) do Time.parse(`git log --date=iso8601 --pretty="%cd" -1`) end else File.mtime(@path) end end |
#size ⇒ Integer
The number of advisories within the database.
328 329 330 |
# File 'lib/bundler/audit/database.rb', line 328 def size each_advisory_path.count end |
#to_s ⇒ String
Converts the database to a String.
338 339 340 |
# File 'lib/bundler/audit/database.rb', line 338 def to_s @path end |
#update!(options = {}) ⇒ true?
Updates the ruby-advisory-db.
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/bundler/audit/database.rb', line 204 def update!(={}) if git? Dir.chdir(@path) do command = %w[git pull] command << '--quiet' if [:quiet] command << 'origin' << 'master' unless system(*command) raise(UpdateFailed,"failed to update #{@path.inspect}") end return true end end end |