Skip to content

Commit 82249db

Browse files
author
Vitalie D
committed
Active Scanning and Replacing (AWS) Tokens (#1265)
1 parent a92e03a commit 82249db

File tree

25 files changed

+809
-14
lines changed

25 files changed

+809
-14
lines changed

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ GEM
325325
rb-fsevent (0.9.8)
326326
rb-inotify (0.10.1)
327327
ffi (~> 1.0)
328-
rbtrace (0.4.12)
328+
rbtrace (0.4.14)
329329
ffi (>= 1.0.6)
330330
msgpack (>= 0.4.3)
331331
optimist (>= 3.0.0)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
module Travis::API::V3
3+
class Models::ScanResult
4+
attr_reader :id, :log_id, :job_id, :owner_id, :owner_type, :created_at, :formatted_content, :issues_found, :archived, :purged_at, :token, :token_created_at,
5+
:job_number, :build_id, :build_number, :job_finished_at, :commit_sha, :commit_compare_url, :commit_branch, :repository_id
6+
7+
def initialize(attributes = {})
8+
@id = attributes.fetch('id')
9+
@log_id = attributes.fetch('log_id')
10+
@job_id = attributes.fetch('job_id')
11+
@owner_id = attributes.fetch('owner_id')
12+
@owner_type = attributes.fetch('owner_type')
13+
@created_at = attributes.fetch('created_at')
14+
@formatted_content = attributes.fetch('formatted_content')
15+
@issues_found = attributes.fetch('issues_found')
16+
@archived = attributes.fetch('archived')
17+
@purged_at = attributes.fetch('purged_at')
18+
@token = attributes.fetch('token')
19+
@token_created_at = attributes.fetch('token_created_at')
20+
@job_number = attributes.fetch('job_number')
21+
@build_id = attributes.fetch('build_id')
22+
@build_number = attributes.fetch('build_number')
23+
@job_finished_at = attributes.fetch('job_finished_at')
24+
@commit_sha = attributes.fetch('commit_sha')
25+
@commit_compare_url = attributes.fetch('commit_compare_url')
26+
@commit_branch = attributes.fetch('commit_branch')
27+
@repository_id = attributes.fetch('repository_id')
28+
end
29+
end
30+
end
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# frozen_string_literal: true
2+
3+
module Travis::API::V3
4+
class Models::ScannerCollection
5+
def initialize(collection, total_count)
6+
@collection = collection
7+
@total_count = total_count
8+
end
9+
10+
def count(*)
11+
@total_count
12+
end
13+
14+
def limit(*)
15+
self
16+
end
17+
18+
def offset(*)
19+
self
20+
end
21+
22+
def map
23+
return @collection.map unless block_given?
24+
25+
@collection.map { |x| yield x }
26+
end
27+
28+
def to_sql
29+
"scanner_query:#{Time.now.to_i}"
30+
end
31+
end
32+
end

lib/travis/api/v3/permissions/repository.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ def create_request?
4242
write?
4343
end
4444

45+
def check_scan_results?
46+
write?
47+
end
48+
4549
def admin?
4650
access_control.adminable? object
4751
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module Travis::API::V3
2+
class Queries::ScanResult < RemoteQuery
3+
params :id
4+
5+
def find
6+
scanner_client.get_scan_result(id)
7+
end
8+
9+
private
10+
11+
def scanner_client
12+
@_scanner_client ||= ScannerClient.new(nil)
13+
end
14+
end
15+
end
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module Travis::API::V3
2+
class Queries::ScanResults < Query
3+
params :repository_id, :offset, :limit
4+
5+
def all
6+
# Reset the scan status on viewing the reports
7+
Repository.find(repository_id).update!(scan_failed_at: nil)
8+
9+
page = (offset.to_i / limit.to_i) + 1
10+
scanner_client(repository_id).scan_results(
11+
page.to_s,
12+
limit
13+
)
14+
end
15+
16+
private
17+
18+
def scanner_client(repository_id)
19+
@_scanner_client ||= ScannerClient.new(repository_id)
20+
end
21+
end
22+
end

lib/travis/api/v3/renderer/repository.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
module Travis::API::V3
22
class Renderer::Repository < ModelRenderer
33
representation(:minimal, :id, :name, :slug)
4-
representation(:standard, :id, :name, :slug, :description, :github_id, :vcs_id, :vcs_type, :github_language, :active, :private, :owner, :owner_name, :vcs_name, :default_branch, :starred, :managed_by_installation, :active_on_org, :migration_status, :history_migration_status, :shared, :config_validation, :server_type)
5-
representation(:experimental, :id, :name, :slug, :description, :vcs_id, :vcs_type, :github_id, :github_language, :active, :private, :owner, :default_branch, :starred, :current_build, :last_started_build, :next_build_number, :server_type)
6-
representation(:internal, :id, :name, :slug, :github_id, :vcs_id, :vcs_type, :active, :private, :owner, :default_branch, :private_key, :token, :user_settings, :server_type)
4+
representation(:standard, :id, :name, :slug, :description, :github_id, :vcs_id, :vcs_type, :github_language, :active, :private, :owner, :owner_name, :vcs_name, :default_branch, :starred, :managed_by_installation, :active_on_org, :migration_status, :history_migration_status, :shared, :config_validation, :server_type, :scan_failed_at)
5+
representation(:experimental, :id, :name, :slug, :description, :vcs_id, :vcs_type, :github_id, :github_language, :active, :private, :owner, :default_branch, :starred, :current_build, :last_started_build, :next_build_number, :server_type, :scan_failed_at)
6+
representation(:internal, :id, :name, :slug, :github_id, :vcs_id, :vcs_type, :active, :private, :owner, :default_branch, :private_key, :token, :user_settings, :server_type, :scan_failed_at)
77
representation(:additional, :allow_migration)
88

99
hidden_representations(:experimental, :internal)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
module Travis::API::V3
2+
class Renderer::ScanResult < ModelRenderer
3+
representation(:minimal, :id, :created_at, :formatted_content, :issues_found, :job_id, :build_id, :job_number, :build_number, :job_finished_at,
4+
:commit_sha, :commit_compare_url, :commit_branch, :build_created_by)
5+
representation(:standard, *representations[:minimal])
6+
7+
def build_created_by
8+
job = Travis::API::V3::Models::Job.find(model.job_id)
9+
build = Travis::API::V3::Models::Build.find(job.source_id)
10+
return nil unless creator = build.sender
11+
{
12+
'@type' => build.sender_type.downcase,
13+
'@href' => created_by_href(creator),
14+
'@representation' => 'minimal'.freeze,
15+
'id' => creator.id,
16+
'login' => creator.login
17+
}
18+
end
19+
20+
private def created_by_href(creator)
21+
case creator
22+
when V3::Models::Organization then Renderer.href(:organization, script_name: script_name, id: creator.id)
23+
when V3::Models::User then Renderer.href(:user, script_name: script_name, id: creator.id)
24+
end
25+
end
26+
end
27+
end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module Travis::API::V3
2+
class Renderer::ScanResults < CollectionRenderer
3+
type :scan_results
4+
collection_key :scan_results
5+
end
6+
end

lib/travis/api/v3/routes.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,16 @@ module Routes
283283
end
284284
end
285285

286+
resource :scan_results do
287+
route '/scan_results'
288+
get :all
289+
end
290+
291+
resource :scan_result do
292+
route '/scan_result/{scan_result.id}'
293+
get :find
294+
end
295+
286296
resource :user do
287297
capture id: :digit
288298
route '/user/{user.id}'

0 commit comments

Comments
 (0)