-
Notifications
You must be signed in to change notification settings - Fork 234
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1593 from zendesk/grosser/clair
Covert clair support to a plugin
- Loading branch information
Showing
12 changed files
with
211 additions
and
202 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 was deleted.
Oops, something went wrong.
This file contains 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,6 @@ | ||
Hyperclair will pull the image from registry and run scan with Clair scanner. | ||
This is supposed to use a forked version with `ENV` var and `/` support. | ||
https://github.com/zendesk/hyperclair | ||
(discussion on why we use a fork / when we can switch see https://github.com/wemanity-belgium/hyperclair/pull/90) | ||
|
||
Set `HYPERCLAIR_PATH` ENV variable in samson to enable. |
This file contains 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,61 @@ | ||
# frozen_string_literal: true | ||
# TODO: should check based on docker_repo_digest not tag | ||
module SamsonHyperclair | ||
class Engine < Rails::Engine | ||
end | ||
|
||
class << self | ||
def append_job_with_scan(job, docker_tag) | ||
return unless clair = ENV['HYPERCLAIR_PATH'] | ||
|
||
append_output job, "### Clair scan: started\n" | ||
|
||
Thread.new do | ||
ActiveRecord::Base.connection_pool.with_connection do | ||
sleep 0.1 if Rails.env.test? # in test we reuse the same connection, so we cannot use it at the same time | ||
success, output, time = scan(clair, job.project, docker_tag) | ||
status = (success ? "success" : "errored or vulnerabilities found") | ||
output = "### Clair scan: #{status} in #{time}s\n#{output}" | ||
append_output job, output | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def append_output(job, output) | ||
job.reload | ||
job.update_column(:output, job.output + output) | ||
end | ||
|
||
def scan(executable, project, docker_ref) | ||
with_time do | ||
Samson::CommandExecutor.execute( | ||
executable, | ||
*project.docker_repo(registry: :default).split('/', 2), | ||
docker_ref, | ||
whitelist_env: [ | ||
'DOCKER_REGISTRY_USER', | ||
'DOCKER_REGISTRY_PASS', | ||
'AWS_ACCESS_KEY_ID', | ||
'AWS_SECRET_ACCESS_KEY', | ||
'PATH' | ||
], | ||
timeout: 60 * 60 | ||
) | ||
end | ||
end | ||
|
||
def with_time | ||
result = [] | ||
time = Benchmark.realtime { result = yield } | ||
result << time | ||
end | ||
end | ||
end | ||
|
||
Samson::Hooks.callback :after_docker_build do |build| | ||
if build.docker_build_job.succeeded? | ||
SamsonHyperclair.append_job_with_scan(build.docker_build_job, build.docker_ref) | ||
end | ||
end |
This file contains 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,6 @@ | ||
# frozen_string_literal: true | ||
Gem::Specification.new "samson_hyperclair", "0.0.0" do |s| | ||
s.summary = "Samson Hyperclair integration" | ||
s.authors = ["Michael Grosser"] | ||
s.email = "[email protected]" | ||
end |
79 changes: 79 additions & 0 deletions
79
plugins/hyperclair/test/samson_hyperclair/samson_plugin_test.rb
This file contains 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,79 @@ | ||
# frozen_string_literal: true | ||
require_relative '../test_helper' | ||
|
||
SingleCov.covered! unless defined?(Rake) # rake preloads all plugins | ||
|
||
describe SamsonHyperclair do | ||
describe :after_docker_build do | ||
let(:build) { builds(:docker_build) } | ||
|
||
before { build.docker_build_job = jobs(:succeeded_test) } | ||
|
||
it "runs clair" do | ||
SamsonHyperclair.expects(:append_job_with_scan) | ||
Samson::Hooks.fire(:after_docker_build, build) | ||
end | ||
|
||
it "does not run clair when build failed" do | ||
build.docker_build_job.status = 'errored' | ||
SamsonHyperclair.expects(:append_job_with_scan).never | ||
Samson::Hooks.fire(:after_docker_build, build) | ||
end | ||
end | ||
|
||
describe '.append_job_with_scan' do | ||
share_database_connection_in_all_threads | ||
with_registries ["docker-registry.example.com"] | ||
|
||
def execute! | ||
SamsonHyperclair.append_job_with_scan(job, 'latest') | ||
end | ||
|
||
let(:job) { jobs(:succeeded_test) } | ||
|
||
around do |t| | ||
Tempfile.open('clair') do |f| | ||
f.write("#!/bin/bash\necho HELLO\nexit 0") | ||
f.close | ||
File.chmod 0o755, f.path | ||
with_env(HYPERCLAIR_PATH: f.path, DOCKER_REGISTRY: 'my.registry', &t) | ||
end | ||
end | ||
|
||
it "runs clair and reports success to the database" do | ||
execute! | ||
|
||
job.reload | ||
job.output.must_include "Clair scan: started" | ||
|
||
wait_for_threads | ||
|
||
job.output.must_include "Clair scan: success" | ||
job.output.must_include "HELLO" | ||
end | ||
|
||
it "runs clair and reports missing script to the database" do | ||
File.unlink ENV['HYPERCLAIR_PATH'] | ||
|
||
execute! | ||
|
||
wait_for_threads | ||
|
||
job.reload | ||
job.output.must_include "Clair scan: errored" | ||
job.output.must_include "No such file or directory" | ||
end | ||
|
||
it "runs clair and reports failed script to the database" do | ||
File.write ENV['HYPERCLAIR_PATH'], "#!/bin/bash\necho WORLD\nexit 1" | ||
|
||
execute! | ||
|
||
wait_for_threads | ||
|
||
job.reload | ||
job.output.must_include "Clair scan: errored" | ||
job.output.must_include "WORLD" | ||
end | ||
end | ||
end |
This file contains 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,2 @@ | ||
# frozen_string_literal: true | ||
require_relative '../../../test/test_helper' |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.