Skip to content
Merged
Changes from all 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
25 changes: 17 additions & 8 deletions lib/quayio/scanner/image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Quayio
module Scanner
class Image < Struct.new(:name, :quayio_token, :whitelist)
RELEVANT_SEVERITIES = %w(High Critical)
MAX_ATTEMPTS = 5

def vulnerable?
quayio? && image_exists? && scanned? && high_vulnerabilities_present?
Expand Down Expand Up @@ -44,14 +45,22 @@ def tag
def raw_image
return @raw_image if defined? @raw_image

@raw_image = begin
JSON.parse(
RestClient.get("https://quay.io/api/v1/repository/#{repo}/tag/#{tag}/images",
authorization: "Bearer #{quayio_token}", accept: :json)
)['images'].first
rescue RestClient::ExceptionWithResponse => err
return nil if err.http_code == 404 # ignore unknown repos
raise err
(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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warum versucht ihr es nur bei 520? Das ist ja sogar ein Spezialfall von Cloudflare. Warum nehmt ihr nicht einfach alle 500?

Suggested change
if err.http_code == 520 and attempt < MAX_ATTEMPTS
if err.http_code >= 500 and attempt < MAX_ATTEMPTS

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, gute Frage, ich bin unentschlossen. Uns nervt gerade der 520, weil wir wissen, dass das ein temporäres Problem ist und das weggeht, wenn zu einem späteren Zeitpunkt ein erneuter Request kommt. Andere 500 haben wir nicht gesehen und wir wissen auch nicht, wie dann die Situation aussieht. Daher würde ich für den Moment abwarten, bis wir in eine solche Situation kommen und dann bewerten, ob wir alle 500 abfangen.

sleep(rand(10))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warum hier ein Zufall beim warten auf den nächsten Versuch? Ich kenne sonst das Pattern, dass mit jedem weiteren Versuch länger gewartet wird (als Optimierung der Gesamtwartezeit). Ich würde daher schreiben

Suggested change
sleep(rand(10))
sleep(attempt ** 2)

Das ** steht für Potenz.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Das Problem scheint daher zu kommen, dass bei einem Check-Request alle Docker-Nodes gleichzeitig zu quay.io rennen. Das würde sich nicht ändern, wenn alle Nodes deterministisch warten, um dann erneut gleichzeitig bei quay.io anzufragen. Ich will hier bewusst für eine zufällige zeitliche Streuung der Requests über die einzelnen Nodes sorgen.

next
end
raise err
end
@raw_image = JSON.parse(response)['images'].first
return @raw_image
end
end

Expand Down