Skip to content
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

SUP-1602 Snyk task API migration from deprecated version to REST API version #549

Open
wants to merge 44 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
c4bf96f
Snyk Api base url updated
jesutorrr May 22, 2024
96cd4f0
Update snyk_v2_client.rb
jesutorrr May 22, 2024
050ce49
Update rexml
jesutorrr May 22, 2024
0cfa808
Update snyk_v2_task.rb
jesutorrr May 22, 2024
f9d6725
Update snyk_v2_task.rb
jesutorrr May 22, 2024
9916ac1
Update snyk_v2_task.rb
jesutorrr May 22, 2024
5385bbb
Update snyk_v2_task.rb
jesutorrr May 22, 2024
414dd59
Updating for REST
jesutorrr May 24, 2024
3b854a2
Pagination update
jesutorrr May 28, 2024
06ec270
Update snyk_v2_client.rb
jesutorrr May 28, 2024
5a31595
Update snyk_v2_task.rb
jesutorrr May 28, 2024
54da0fc
Update snyk_v2_task.rb
jesutorrr May 30, 2024
86840d6
Update snyk_v2_task.rb
jesutorrr May 30, 2024
b696573
Update snyk_v2_task.rb
jesutorrr May 30, 2024
061c322
Update snyk_v2_task.rb
jesutorrr May 30, 2024
4d86339
Update snyk_v2_task.rb
jesutorrr Jun 1, 2024
295e1cc
For VCR tests
jesutorrr Jun 3, 2024
ba8f5b1
VCR Test2
jesutorrr Jun 3, 2024
f7b77ae
Updating fixtures files
jesutorrr Jun 5, 2024
581eff4
fixtures update
jesutorrr Jun 5, 2024
3237494
fixtures pass
jesutorrr Jun 5, 2024
9b9608c
to pass specs test
jesutorrr Jun 5, 2024
511f818
To pass stubs test
jesutorrr Jun 5, 2024
da9ee2a
Update snyk_v2_stubs.rb
jesutorrr Jun 5, 2024
6244d9e
Update snyk_v2_task_spec.rb
jesutorrr Jun 5, 2024
47bfb17
Update snyk_v2_task.rb
jesutorrr Jun 14, 2024
bf2bda0
Update snyk_v2_task_spec.rb
jesutorrr Jun 14, 2024
00f0ec8
Update snyk_v2_stubs.rb
jesutorrr Jun 14, 2024
ee43623
Update snyk_v2_stubs.rb
jesutorrr Jun 14, 2024
b3ef7aa
Update snyk_v2_task_spec.rb
jesutorrr Jun 14, 2024
f25c783
Deleting empty issues
jesutorrr Jun 14, 2024
05d9912
Update snyk_v2_stubs.rb
jesutorrr Jun 27, 2024
c4ca99f
Update snyk_v2_task_spec.rb
jesutorrr Jun 27, 2024
e312e6f
Update snyk_v2_task_spec.rb
jesutorrr Jun 27, 2024
0da2d2d
Update snyk_v2_task_spec.rb
jesutorrr Jun 27, 2024
c1d1694
Adding VCRs
jesutorrr Jun 27, 2024
19bc860
Update snyk_v2_task_spec.rb
jesutorrr Jun 27, 2024
3628f6a
One example test
jesutorrr Jun 27, 2024
8849867
Update snyk_v2_stubs.rb
jesutorrr Jul 16, 2024
ab7b5fe
Update Gemfile.lock
jesutorrr Jul 16, 2024
69169b3
Update snyk_v2_task_spec.rb
jesutorrr Jul 16, 2024
386a7cb
Update snyk_v2_task.rb
jesutorrr Jul 18, 2024
139fa65
Fixing vuln
jesutorrr Jul 18, 2024
1d053ec
Update snyk_v2_task.rb
jesutorrr Jul 18, 2024
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
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ gem "aws-sdk-inspector2"
gem "httparty"
gem "ipaddress"
gem "rest-client"
gem "rexml", ">= 3.2.5"
gem "rexml", ">= 3.2.7"
gem "ruby-limiter"
gem "sanitize"
gem "strscan"
Expand Down
9 changes: 5 additions & 4 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ GEM
netrc (~> 0.8)
reverse_markdown (2.1.1)
nokogiri
rexml (3.2.5)
rexml (3.2.8)
strscan (>= 3.0.9)
rspec (3.12.0)
rspec-core (~> 3.12.0)
rspec-expectations (~> 3.12.0)
Expand Down Expand Up @@ -145,7 +146,7 @@ GEM
unicode-display_width (>= 1.5, < 3.0)
unicode_utils (~> 1.4)
strings-ansi (0.2.0)
strscan (3.0.6)
strscan (3.1.0)
thor (1.2.1)
tilt (2.0.11)
timecop (0.9.6)
Expand Down Expand Up @@ -185,7 +186,7 @@ DEPENDENCIES
pry
pry-byebug
rest-client
rexml (>= 3.2.5)
rexml (>= 3.2.7)
rspec
rspec-github
rubocop
Expand All @@ -203,4 +204,4 @@ RUBY VERSION
ruby 3.2.2p53

BUNDLED WITH
2.4.10
2.5.9
52 changes: 33 additions & 19 deletions tasks/connectors/snyk_v2/lib/snyk_v2_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,58 @@ module SnykV2
class SnykV2Client
class ApiError < StandardError; end

HOST = "https://snyk.io"

def initialize(token)
def initialize(token, snyk_api_base)
@token = token
@api_base_url = "https://#{snyk_api_base}/rest"
@headers = {
"content-type" => "application/json",
"accept" => "application/json",
"Authorization" => "token #{token}"
"Content-Type" => "application/json",
"Accept" => "application/json",
"Authorization" => "Token #{@token}"
}
end

def snyk_get_orgs
print "Getting list of orgs"

response = http_get("#{HOST}/api/v1/orgs", @headers)
raise ApiError, "Unable to retrieve submissions, please check credentials." unless response
response = http_get("#{@api_base_url}/orgs?version=2024-04-29", @headers)
raise ApiError, "Unable to retrieve organizations, please check credentials." unless response

JSON.parse(response)["orgs"]
JSON.parse(response)["data"]
end

def snyk_get_projects(org)
print "Getting list of projects"

response = http_get("#{HOST}/api/v1/org/#{org}/projects", @headers)
raise ApiError, "Unable to retrieve submissions, please check credentials." unless response
response = http_get("#{@api_base_url}/orgs/#{org}/projects?version=2024-04-29&limit=100", @headers)
raise ApiError, "Unable to retrieve projects, please check credentials." unless response

JSON.parse(response)["projects"]
JSON.parse(response)["data"]
end

def snyk_get_issues(per_page, search_json, page_num, from_date, to_date)
print "Getting issues"
snyk_query_api = "https://snyk.io/api/v1/reporting/issues?perPage=#{per_page}&page=#{page_num}&from=#{from_date}&to=#{to_date}"
print_debug("Get issues query: #{snyk_query_api}")
def snyk_get_issues(per_page, page_num, from_date, to_date, org)
print "Getting list of issues"
pages = page_num

all_issues = []
next_url = "#{@api_base_url}/orgs/#{org}/issues?version=2024-04-29&limit=#{per_page}&created_after=#{from_date}&created_before=#{to_date}"

pages.times do
print_debug("Fetching data from URL: #{next_url}")

response = http_get(next_url, @headers)
raise ApiError, "Unable to retrieve issues, please check credentials." unless response

data = JSON.parse(response)
page_issues = data["data"]
all_issues << page_issues

next_url = data.dig("links", "next")
break unless next_url

response = http_post(snyk_query_api, @headers, search_json)
raise ApiError, "Unable to retrieve submissions, please check credentials." unless response
next_url = URI.join(@api_base_url, next_url).to_s
end

JSON.parse(response)["results"]
all_issues
end
end
end
Expand Down
36 changes: 17 additions & 19 deletions tasks/connectors/snyk_v2/readme.md
Original file line number Diff line number Diff line change
@@ -1,39 +1,37 @@
## Running the Snyk V2 task

This toolkit brings in data from Snyk V2
This toolkit brings in data from Snyk V2.

To run this task you need the following information from Snyk V2:
To run this task, you need the following information from Snyk V2:

1. Snyk API Token
2. Snyk environment API base URL without prefix e.g. api.eu.snyk.io, api.snyk.io or api.au.snyk.io

## Command Line

See the main Toolkit for instructions on running tasks. For this task, if you leave off the Kenna API Key and Kenna Connector ID, the task will create a json file in the default or specified output directory. You can review the file before attempting to upload to the Kenna directly.
See the main Toolkit for instructions on running tasks. For this task, if you leave off the Kenna API Key and Kenna Connector ID, the task will create a JSON file in the default or specified output directory. You can review the file before attempting to upload to Kenna directly.

Recommended Steps:

1. Run with Snyk V2 Keys only to ensure you are able to get data properly from the scanner
1. Review output for expected data
1. Create Kenna Data Importer connector in Kenna (example name: Snyk V2 KDI)
1. Manually run the connector with the json from step 1
1. Click on the name of the connector to get the connector id
1. Run the task with Snyk V2 Keys and Kenna Key/connector id
1. Run with Snyk V2 Keys only to ensure you are able to get data properly from the scanner.
2. Review output for expected data.
3. Create Kenna Data Importer connector in Kenna (example name: Snyk V2 KDI).
4. Manually run the connector with the JSON from step 1.
5. Click on the name of the connector to get the connector ID.
6. Run the task with Snyk V2 Keys and Kenna Key/connector ID.

Complete list of Options:

| Option | Required | Description | default |
| Option | Required | Description | Default |
| --- | --- | --- | --- |
| snyk_api_token | true | Snyk API Token | n/a |
| import_type | false | what to import "vulns" or "findings". By default "vulns" | vulns |
| retrieve_from | false | default will be 90 days before today | 90 |
| include_license | false | retrieve license issues. | n/a |
| projectName_strip_colon | false | strip colon and following data from Project Name - used as application identifier | n/a |
| packageManager_strip_colon | false | strip colon and following data from packageManager - used in asset file locator | n/a |
| package_strip_colon | false | strip colon and following data from package - used in asset file locator | n/a |
| application_locator_mapping | false | indicates which field should be used in application locator. Valid options are application and organization. Default is application. | application |
| page_size | false | The number of objects per page (currently limited from 1 to 1000). | 1000 |
| batch_size | false | The maximum number of issues to submit to Kenna in each batch. | 500 |
| retrieve_from | false | Default will be 60 days before today | 60 |
| include_license | false | Retrieve license issues | false |
| page_size | false | The number of objects per page (Min 10┃Max 100┃multiple of 10) | 100 |
| batch_size | false | The maximum number of issues to submit to Kenna in each batch | 500 |
| page_num | false | Max pagination number | 5000 |
| kenna_connector_id | false | If set, we'll try to upload to this connector | n/a |
| kenna_api_key | false | Kenna API Key | n/a |
| kenna_api_host | false | Kenna API Hostname | api.kennasecurity.com |
| snyk_api_base | true | Snyk environment API base URL without prefix e.g. api.eu.snyk.io, api.snyk.io or api.au.snyk.io | n/a |
| output_directory | false | If set, will write a file upon completion. Path is relative to toolkit root directory | output/snyk |
Loading
Loading