-
Notifications
You must be signed in to change notification settings - Fork 0
Fix rate limiting on scan #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c1cc79f
fix 520 on api call for scans
stefan-as b7fd0ac
refactor raise condition
stefan-as a11e589
do not allow 404 errors on quay.io API calls
stefan-as 2c4b93c
lots of code style refactoring
stefan-as 1833d1b
pass RuboCop coding style
stefan-as 7ebf9a3
do not trust QUAY_IO_REPO_NAME regex match, better safe than sorry
stefan-as 0dce8fc
check methods should return booleans
stefan-as File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| Style/FrozenStringLiteralComment: | ||
| Enabled: false | ||
|
|
||
| Style/Documentation: | ||
| Enabled: false | ||
|
|
||
| Metrics/MethodLength: | ||
| Max: 50 | ||
|
|
||
| Metrics/BlockLength: | ||
| Max: 200 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,26 +1,33 @@ | ||
| require 'quayio/scanner/image' | ||
| require 'docker' | ||
|
|
||
| module Quayio | ||
| module Scanner | ||
| class Check < Struct.new(:docker_url, :quayio_token, :whitelist) | ||
| Check = Struct.new(:docker_url, :quayio_token, :whitelist) do | ||
| def run | ||
| Docker.url = docker_url | ||
| containers = Docker::Container.all | ||
| .map { |dc| dc.json['Config']['Image'] } | ||
| .uniq | ||
|
|
||
| vulnerable_images = containers | ||
| .map { |container| Image.new(container, quayio_token, whitelist) } | ||
| .select(&:vulnerable?) | ||
| .map(&:name) | ||
|
|
||
| if vulnerable_images.empty? | ||
| [:ok, "#{containers.size} Containers are ok"] | ||
| else | ||
| [:critical, "The images are insecure: #{vulnerable_images.join(', ')}"] | ||
| end | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def containers | ||
| Docker::Container | ||
| .all | ||
| .map { |dc| dc.json['Config']['Image'] } | ||
| .uniq | ||
| end | ||
|
|
||
| def vulnerable_images | ||
| containers | ||
| .map { |container| Image.new(container, quayio_token, whitelist) } | ||
| .select(&:vulnerable?) | ||
| .map(&:name) | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| require 'rest-client' | ||
| require 'json' | ||
|
|
||
| module Quayio | ||
| module Scanner | ||
| Repository = Struct.new(:quayio_token, :org, :repo, :tag) do | ||
| MAX_ATTEMPTS = 5 | ||
|
|
||
| def id | ||
| @id ||= fetch_id | ||
| end | ||
|
|
||
| def scan | ||
| api_call("/image/#{id}/security?vulnerabilities=true") | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def fetch_id | ||
| result = api_call("/tag/#{tag}/images") | ||
| (result['images'].first)['id'] | ||
| end | ||
|
|
||
| def api_call(uri) | ||
| (1..Float::INFINITY).each do |attempt| | ||
| begin | ||
| response = RestClient.get( | ||
| "https://quay.io/api/v1/repository/#{org}/#{repo}#{uri}", | ||
| authorization: "Bearer #{quayio_token}", | ||
| accept: :json | ||
| ) | ||
| return JSON.parse(response) | ||
|
stefan-as marked this conversation as resolved.
|
||
| rescue RestClient::ExceptionWithResponse => e | ||
| raise e if e.http_code != 520 || attempt >= MAX_ATTEMPTS | ||
|
|
||
| sleep(rand(10)) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.