Skip to content

Instantly share code, notes, and snippets.

@omerlh
Created May 30, 2018 18:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save omerlh/d07f230f6857621b100304e33231c081 to your computer and use it in GitHub Desktop.
Save omerlh/d07f230f6857621b100304e33231c081 to your computer and use it in GitHub Desktop.
Snyk's Check command
#!/usr/bin/env ruby
#generated with https://jhawthorn.github.io/curl-to-ruby/
require 'net/http'
require 'uri'
require 'json'
require 'optparse'
options = {}
begin
OptionParser.new do |opts|
opts.banner = "Usage: check.rb [options]"
opts.on("-k", "--api-key KEY", "Snyk's API key") do |key|
options[:api_key] = key
end
opts.on("-o", "--organizaton ORGANIZATION", "The organization containing the targert project") do |organization|
options[:organization] = organization
end
opts.on("-p", "--project PROJECT", "The target project") do |project|
options[:project] = project
end
end.parse!
rescue OptionParser::InvalidOption => e
p "failed to parse options: #{e}"
exit 3
end
# source: https://stackoverflow.com/a/2149183/4792970
mandatory = [:api_key, :organization, :project]
missing = mandatory.select{ |param| options[param].nil? }
unless missing.empty?
p "missing one or more required params: #{missing}"
exit 3
end
uri = URI.parse("https://snyk.io/api/v1/org/#{options[:organization]}/project/#{options[:project]}/issues")
request = Net::HTTP::Post.new(uri)
request["Authorization"] = "token #{options[:api_key]}"
req_options = {
use_ssl: uri.scheme == "https",
}
begin
response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
http.request(request)
end
rescue SocketError => e
p "Snyk query failed due to network error: #{e}"
exit 3
rescue Exception => e
p "Snyk query failed due to unknwon error #{e}"
exit 3
end
if (response.code.to_i > 299)
p "Snyk query failed for unknown reason, status code: #{response.code}"
exit 3
end
project = JSON.parse(response.body)
if (project["ok"])
p "OK Project has no dependeinces with known vulnerabilities"
exit 0
else
p "WARNING Project has one or more depndencies with known vulnerabilities, checkout Snyk's website for more details"
exit 1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment