Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 22 additions & 39 deletions lib/quayio/scanner/image.rb
Original file line number Diff line number Diff line change
@@ -1,78 +1,61 @@
require 'json'
require 'rest-client'
require 'quayio/scanner/repository'

module Quayio
module Scanner
class Image < Struct.new(:name, :quayio_token, :whitelist)
class Image
RELEVANT_SEVERITIES = %w(High Critical)
MAX_ATTEMPTS = 5

def initialize(name, quayio_token, whitelist)
@name = name
@whitelist = whitelist

repo = name.split(':').first.gsub(%r{quay.io\/}, '')
Comment thread
stefan-as marked this conversation as resolved.
Outdated
@repository = Repository.new(quayio_token, repo)
end

def vulnerable?
quayio? && image_exists? && scanned? && high_vulnerabilities_present?
quayio? && image_exists? && scanned? && vulnerabilities_present?
Comment thread
stefan-as marked this conversation as resolved.
Outdated
end

private

def quayio?
name.match(%r{^quay.io\/})
@name.match(%r{^quay.io\/})
end

def image_exists?
raw_image
raw_id
Comment thread
stefan-as marked this conversation as resolved.
Outdated
end

def scanned?
raw_scan['status'] == 'scanned'
end

def high_vulnerabilities_present?
def vulnerabilities_present?
raw_scan['data']['Layer']['Features'].detect do |f|
f['Vulnerabilities'] && f['Vulnerabilities'].detect do |v|
RELEVANT_SEVERITIES.include?(v['Severity']) &&
!whitelist.include?(v['Name'])
!@whitelist.include?(v['Name'])
end
end
end

def repo
name.split(':').first.gsub(%r{quay.io\/}, '')
end

def tag
name.split(':').last
@name.split(':').last
end

def raw_image
return @raw_image if defined? @raw_image
def raw_id
return @raw_id if defined? @raw_id

(1..MAX_ATTEMPTS).each do |attempt|
begin
response = RestClient.get(
"https://quay.io/api/v1/repository/#{repo}/tag/#{tag}/images",
authorization: "Bearer #{quayio_token}",
accept: :json)
rescue RestClient::ExceptionWithResponse => err
return nil if err.http_code == 404 # ignore unknown repos
if err.http_code == 520 and attempt < MAX_ATTEMPTS
sleep(rand(10))
next
end
raise err
end
@raw_image = JSON.parse(response)['images'].first
return @raw_image
end
@raw_id = @repository.id(tag)
return @raw_id
end
Comment thread
stefan-as marked this conversation as resolved.
Outdated

def raw_scan
return @raw_scan if defined? @raw_scan

@raw_scan = begin
JSON.parse(
RestClient.get("https://quay.io/api/v1/repository/#{repo}/image/#{raw_image['id']}/security?vulnerabilities=true",
authorization: "Bearer #{quayio_token}", accept: :json)
)
end
@raw_scan = @repository.scan(raw_id)
return @raw_scan
end
Comment thread
stefan-as marked this conversation as resolved.
end
end
Expand Down
36 changes: 36 additions & 0 deletions lib/quayio/scanner/repository.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require 'rest-client'
require 'json'

module Quayio
module Scanner
class Repository < Struct.new(:quayio_token, :repo)
MAX_ATTEMPTS = 5

def id(tag)
result = api_call("/tag/#{tag}/images")
return (result['images'].first)['id']
Comment thread
stefan-as marked this conversation as resolved.
Outdated
end

def scan(id)
return api_call("/image/#{id}/security?vulnerabilities=true")
Comment thread
stefan-as marked this conversation as resolved.
Outdated
end

private

def api_call(uri)
(1..Float::INFINITY).each do |attempt|
Comment thread
stefan-as marked this conversation as resolved.
begin
response = RestClient.get(
"https://quay.io/api/v1/repository/#{repo}#{uri}",
authorization: "Bearer #{quayio_token}",
accept: :json)
return JSON.parse(response)
Comment thread
stefan-as marked this conversation as resolved.
rescue RestClient::ExceptionWithResponse => err
raise err if err.http_code != 520 or attempt >= MAX_ATTEMPTS
sleep(rand(10))
end
end
end
end
end
end