diffend 0.2.26 → 0.2.31

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -42,16 +42,21 @@ module Diffend
42
42
  class << self
43
43
  # Execute request
44
44
  #
45
- # @param config [OpenStruct] diffend config
46
- # @param endpoint_url [String]
47
- # @param payload [Hash]
45
+ # @param request_object [Diffend::RequestObject]
48
46
  #
49
47
  # @return [Net::HTTPResponse] response from Diffend
50
- def call(config, endpoint_url, payload)
48
+ def call(request_object)
51
49
  retry_count ||= -1
52
50
 
53
- build_http(endpoint_url) do |http, uri|
54
- response = http.request(build_request(uri, config, payload))
51
+ build_http(request_object.url) do |http, uri|
52
+ response = http.request(
53
+ build_request(
54
+ uri,
55
+ request_object.request_method,
56
+ request_object.config,
57
+ request_object.payload
58
+ )
59
+ )
55
60
 
56
61
  if SERVER_ERRORS.include?(response.code.to_i)
57
62
  raise Diffend::Errors::RequestServerError, response.code.to_i
@@ -66,8 +71,8 @@ module Diffend
66
71
 
67
72
  Diffend::HandleErrors::Report.call(
68
73
  exception: e,
69
- payload: payload,
70
- config: config,
74
+ payload: request_object.payload,
75
+ config: request_object.config,
71
76
  message: :request_error
72
77
  )
73
78
  rescue *CONNECTION_EXCEPTIONS => e
@@ -77,8 +82,8 @@ module Diffend
77
82
 
78
83
  Diffend::HandleErrors::Report.call(
79
84
  exception: e,
80
- payload: payload,
81
- config: config,
85
+ payload: request_object.payload,
86
+ config: request_object.config,
82
87
  message: :request_error
83
88
  )
84
89
  rescue *TIMEOUT_EXCEPTIONS => e
@@ -88,8 +93,8 @@ module Diffend
88
93
 
89
94
  Diffend::HandleErrors::Report.call(
90
95
  exception: e,
91
- payload: payload,
92
- config: config,
96
+ payload: request_object.payload,
97
+ config: request_object.config,
93
98
  message: :request_error
94
99
  )
95
100
  end
@@ -101,7 +106,7 @@ module Diffend
101
106
  def handle_retry(message, retry_count)
102
107
  return false if retry_count == RETRIES
103
108
 
104
- Bundler.ui.error(message)
109
+ Bundler.ui.warn(message)
105
110
  sleep(exponential_backoff(retry_count))
106
111
 
107
112
  retry_count < RETRIES
@@ -118,25 +123,40 @@ module Diffend
118
123
  uri.port,
119
124
  use_ssl: uri.scheme == 'https',
120
125
  verify_mode: OpenSSL::SSL::VERIFY_NONE,
121
- open_timeout: 5,
122
- read_timeout: 5
126
+ open_timeout: 15,
127
+ read_timeout: 15
123
128
  ) { |http| yield(http, uri) }
124
129
  end
125
130
 
126
131
  # Build http post request and assigns headers and payload
127
132
  #
128
133
  # @param uri [URI::HTTPS]
134
+ # @param request_method [Symbol]
129
135
  # @param config [OpenStruct] Diffend config
130
136
  # @param payload [Hash] with versions to check
131
137
  #
132
- # @return [Net::HTTP::Post]
133
- def build_request(uri, config, payload)
134
- Net::HTTP::Post
138
+ # @return [Net::HTTP::Post, Net::HTTP::Put]
139
+ def build_request(uri, request_method, config, payload)
140
+ pick_request_method(request_method)
135
141
  .new(uri.request_uri, HEADERS)
136
142
  .tap { |request| assign_auth(request, config) }
137
143
  .tap { |request| assign_payload(request, payload) }
138
144
  end
139
145
 
146
+ # Pick request method
147
+ #
148
+ # @param request_method [Symbol]
149
+ #
150
+ # @return [Net::HTTP::Post, Net::HTTP::Put]
151
+ def pick_request_method(request_method)
152
+ case request_method
153
+ when :post
154
+ Net::HTTP::Post
155
+ when :put
156
+ Net::HTTP::Put
157
+ end
158
+ end
159
+
140
160
  # Assigns basic authorization if provided in the config
141
161
  #
142
162
  # @param request [Net::HTTP::Post] prepared http post
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Diffend
4
+ # Class responsible for preparing diffend request object
5
+ RequestObject = Struct.new(:config, :url, :payload, :request_method, keyword_init: true)
6
+ end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+
5
+ module Diffend
6
+ # Module responsible for fetching diffend verdict on local context
7
+ module RequestVerdict
8
+ class << self
9
+ # @param command [String] either install or update
10
+ # @param definition [Bundler::Definition] definition for your source
11
+ # @param config [OpenStruct] diffend config
12
+ def call(command, config, definition)
13
+ payload = Diffend::LocalContext.call(command, config.project_id, definition)
14
+
15
+ response = Diffend::Request.call(
16
+ build_request_object(command, config, payload)
17
+ )
18
+
19
+ JSON.parse(response.body)
20
+ rescue Bundler::GemNotFound
21
+ raise ::Diffend::Errors::DependenciesResolveException
22
+ rescue StandardError => e
23
+ Diffend::HandleErrors::Report.call(
24
+ exception: e,
25
+ payload: payload || {},
26
+ config: config,
27
+ message: :unhandled_exception,
28
+ report: true
29
+ )
30
+ end
31
+
32
+ # @param command [String] either install or update
33
+ # @param config [OpenStruct] diffend config
34
+ # @param payload [Hash]
35
+ #
36
+ # @return [Diffend::RequestObject]
37
+ def build_request_object(command, config, payload)
38
+ Diffend::RequestObject.new(
39
+ config: config,
40
+ url: commands_url(command, config.project_id),
41
+ payload: payload,
42
+ request_method: :post
43
+ )
44
+ end
45
+
46
+ # Provides diffend command endpoint url
47
+ #
48
+ # @param command [String] either install or update
49
+ # @param project_id [String] diffend project_id
50
+ #
51
+ # @return [String] diffend endpoint
52
+ def commands_url(command, project_id)
53
+ return ENV['DIFFEND_COMMAND_URL'] if ENV.key?('DIFFEND_COMMAND_URL')
54
+
55
+ "https://my.diffend.io/api/projects/#{project_id}/bundle/#{command}"
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,85 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Diffend
4
+ # Track what is run in production
5
+ class Track
6
+ # Time that we want to wait between track requests
7
+ TRACK_SLEEP = 15
8
+ # Time that we want to wait before we retry
9
+ RETRY_SLEEP = 15
10
+
11
+ # Initialize tracking
12
+ def initialize
13
+ @mutex = Mutex.new
14
+ @config = Diffend::Config.call
15
+ end
16
+
17
+ # Start tracking
18
+ def start
19
+ response = exec_request
20
+
21
+ perform(response['id'])
22
+ rescue Diffend::Errors::HandledException
23
+ sleep(RETRY_SLEEP)
24
+
25
+ retry
26
+ rescue StandardError => e
27
+ Diffend::HandleErrors::Report.call(
28
+ exception: e,
29
+ config: @config,
30
+ message: :unhandled_exception,
31
+ report: true,
32
+ raise_exception: false
33
+ )
34
+
35
+ sleep(RETRY_SLEEP)
36
+
37
+ retry
38
+ end
39
+
40
+ # @param request_id [String]
41
+ def perform(request_id)
42
+ loop do
43
+ @mutex.synchronize do
44
+ track_request(request_id)
45
+ end
46
+
47
+ sleep(TRACK_SLEEP)
48
+ end
49
+ end
50
+
51
+ # Perform an exec request
52
+ def exec_request
53
+ Diffend::Execute.call(Diffend::Commands::EXEC, @config)
54
+ end
55
+
56
+ # Perform a track request
57
+ #
58
+ # @param request_id [String]
59
+ def track_request(request_id)
60
+ Diffend::Request.call(
61
+ build_request_object(request_id)
62
+ )
63
+ end
64
+
65
+ # @param request_id [String]
66
+ #
67
+ # @return [Diffend::RequestObject]
68
+ def build_request_object(request_id)
69
+ Diffend::RequestObject.new(
70
+ config: @config,
71
+ url: track_url(@config.project_id, request_id),
72
+ payload: { id: request_id }.freeze,
73
+ request_method: :put
74
+ ).freeze
75
+ end
76
+
77
+ # @param project_id [String] diffend project_id
78
+ # @param request_id [String]
79
+ #
80
+ # @return [String]
81
+ def track_url(project_id, request_id)
82
+ "https://my.diffend.io/api/projects/#{project_id}/bundle/#{request_id}/track"
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Diffend
4
+ # Current version
5
+ VERSION = '0.2.31'
6
+ end
data/plugins.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'diffend'
3
+ require 'diffend/plugin'
4
4
 
5
- Diffend.register
5
+ Diffend::Plugin.register
@@ -3,7 +3,6 @@
3
3
  require 'byebug'
4
4
  require 'diffend'
5
5
 
6
-
7
6
  command = 'install'
8
7
  project_id = nil
9
8
 
@@ -12,4 +11,4 @@ lockfile = ARGV[1]
12
11
 
13
12
  definition = Diffend::BuildBundlerDefinition.call(command, gemfile lockfile)
14
13
 
15
- pp Diffend::Voting::Versions::Remote.payload(command, project_id, definition)
14
+ pp Diffend::LocalContext.call(command, project_id, definition)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: diffend
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.26
4
+ version: 0.2.31
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tomasz Pajor
@@ -34,7 +34,7 @@ cert_chain:
34
34
  9MmF6uCQa1EjK2p8tYT0MnbHrFkoehxdX4VO9y99GAkhZyJNKPYPtyAUFV27sT2V
35
35
  LfCJRk4ifKIN/FUCwDSn8Cz0m6oH265q0p6wdzI6qrWOjP8tGOMBTA==
36
36
  -----END CERTIFICATE-----
37
- date: 2020-09-10 00:00:00.000000000 Z
37
+ date: 2020-09-24 00:00:00.000000000 Z
38
38
  dependencies:
39
39
  - !ruby/object:Gem::Dependency
40
40
  name: bundler
@@ -94,18 +94,28 @@ files:
94
94
  - lib/diffend.rb
95
95
  - lib/diffend/build_bundler_definition.rb
96
96
  - lib/diffend/commands.rb
97
+ - lib/diffend/config.rb
97
98
  - lib/diffend/config/fetcher.rb
98
99
  - lib/diffend/config/file_finder.rb
99
100
  - lib/diffend/config/validator.rb
100
101
  - lib/diffend/errors.rb
102
+ - lib/diffend/execute.rb
101
103
  - lib/diffend/handle_errors/build_exception_payload.rb
102
104
  - lib/diffend/handle_errors/display_to_stdout.rb
103
105
  - lib/diffend/handle_errors/messages.rb
104
106
  - lib/diffend/handle_errors/report.rb
107
+ - lib/diffend/local_context.rb
108
+ - lib/diffend/local_context/diffend.rb
109
+ - lib/diffend/local_context/host.rb
110
+ - lib/diffend/local_context/packages.rb
111
+ - lib/diffend/local_context/platform.rb
112
+ - lib/diffend/monitor.rb
113
+ - lib/diffend/plugin.rb
105
114
  - lib/diffend/request.rb
106
- - lib/diffend/voting.rb
107
- - lib/diffend/voting/versions/local.rb
108
- - lib/diffend/voting/versions/remote.rb
115
+ - lib/diffend/request_object.rb
116
+ - lib/diffend/request_verdict.rb
117
+ - lib/diffend/track.rb
118
+ - lib/diffend/version.rb
109
119
  - plugins.rb
110
120
  - scripts/generate_payload_for_file.rb
111
121
  homepage: https://diffend.io
@@ -130,5 +140,5 @@ requirements: []
130
140
  rubygems_version: 3.1.2
131
141
  signing_key:
132
142
  specification_version: 4
133
- summary: OSS supply chain security and management platform.
143
+ summary: OSS supply chain security and management platform
134
144
  test_files: []
metadata.gz.sig CHANGED
Binary file
@@ -1,304 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Diffend
4
- module Voting
5
- # Module responsible for handling both local and remote gem versions
6
- module Versions
7
- # Module responsible for preparing current or current/new versions of gems
8
- class Local
9
- # Definition of a local path, if it matches it means that we are the source
10
- ME_PATH = '.'
11
- # Sources that we expect to match ourselves too
12
- ME_SOURCES = [
13
- Bundler::Source::Gemspec,
14
- Bundler::Source::Path
15
- ].freeze
16
- # List of dependency types
17
- DEPENDENCIES_TYPES = {
18
- direct: 0,
19
- dependency: 1
20
- }.freeze
21
- # List of sources types
22
- SOURCES_TYPES = {
23
- valid: 0,
24
- multiple_primary: 1
25
- }.freeze
26
- # List of gem sources types
27
- GEM_SOURCES_TYPES = {
28
- local: 0,
29
- gemfile_source: 1,
30
- gemfile_git: 2,
31
- gemfile_path: 3
32
- }.freeze
33
-
34
- class << self
35
- # @param command [String] either install or update
36
- # @param definition [Bundler::Definition] definition for your source
37
- def call(command, definition)
38
- Bundler.ui.silence { definition.resolve_remotely! }
39
-
40
- instance = new(definition)
41
-
42
- case command
43
- when Commands::INSTALL then instance.build_install
44
- when Commands::UPDATE then instance.build_update
45
- else
46
- raise ArgumentError, "invalid command: #{command}"
47
- end
48
- end
49
- end
50
-
51
- # @param definition [Bundler::Definition] definition for your source
52
- #
53
- # @return [Hash] local dependencies
54
- def initialize(definition)
55
- @definition = definition
56
- @direct_dependencies = Hash[definition.dependencies.map { |val| [val.name, val] }]
57
- # Support case without Gemfile.lock
58
- @locked_specs = @definition.locked_gems ? @definition.locked_gems.specs : []
59
- end
60
-
61
- # Build install specification
62
- #
63
- # @return [Hash]
64
- def build_install
65
- hash = build_main
66
-
67
- @definition.specs.each do |spec|
68
- next if skip?(spec.source)
69
-
70
- locked_spec = @locked_specs.find { |s| s.name == spec.name }
71
-
72
- hash['dependencies'][spec.name] = {
73
- 'platform' => build_spec_platform(spec, locked_spec),
74
- 'source' => build_spec_source(spec),
75
- 'type' => build_dependency_type(spec.name),
76
- 'versions' => build_versions(spec, locked_spec)
77
- }
78
- end
79
-
80
- hash
81
- end
82
-
83
- # Build update specification
84
- #
85
- # @return [Hash]
86
- def build_update
87
- hash = build_main
88
-
89
- @definition.specs.each do |spec|
90
- next if skip?(spec.source)
91
-
92
- locked_spec = @locked_specs.find { |s| s.name == spec.name }
93
-
94
- hash['dependencies'][spec.name] = {
95
- 'platform' => build_spec_platform(spec, locked_spec),
96
- 'source' => build_spec_source(spec),
97
- 'type' => build_dependency_type(spec.name),
98
- 'versions' => build_versions(spec, locked_spec)
99
- }
100
- end
101
-
102
- hash
103
- end
104
-
105
- private
106
-
107
- # Build default specification
108
- #
109
- # @return [Hash]
110
- def build_main
111
- {
112
- 'dependencies' => {},
113
- 'sources' => build_sources,
114
- 'plugins' => {},
115
- 'platforms' => @definition.platforms.map(&:to_s)
116
- }
117
- end
118
-
119
- # Build gem versions
120
- #
121
- # @param spec [Bundler::StubSpecification, Bundler::LazySpecification, Gem::Specification]
122
- # @param locked_spec [Bundler::LazySpecification, Gem::Specification, NilClass]
123
- #
124
- # @return [Array<String>]
125
- def build_versions(spec, locked_spec = nil)
126
- if locked_spec && locked_spec.version.to_s != spec.version.to_s
127
- [locked_spec.version.to_s, spec.version.to_s]
128
- else
129
- [spec.version.to_s]
130
- end
131
- end
132
-
133
- # @param specs [Array] specs that are direct dependencies
134
- # @param name [String] spec name
135
- #
136
- # @return [Boolean] dependency type
137
- def build_dependency_type(name)
138
- if @direct_dependencies.key?(name)
139
- DEPENDENCIES_TYPES[:direct]
140
- else
141
- DEPENDENCIES_TYPES[:dependency]
142
- end
143
- end
144
-
145
- # Build gem platform
146
- #
147
- # @param spec [Bundler::StubSpecification, Bundler::LazySpecification, Gem::Specification]
148
- # @param locked_spec [Bundler::LazySpecification, Gem::Specification, NilClass]
149
- #
150
- # @return [String]
151
- def build_spec_platform(spec, locked_spec)
152
- parse_platform(
153
- spec.platform || locked_spec&.platform || spec.send(:generic_local_platform)
154
- )
155
- end
156
-
157
- # Parse gem platform
158
- #
159
- # @param platform [String, Gem::Platform]
160
- #
161
- # @return [String]
162
- def parse_platform(platform)
163
- case platform
164
- when String then platform
165
- when Gem::Platform then platform.os
166
- end
167
- end
168
-
169
- # Build gem source type
170
- #
171
- # @param source [Bundler::Source] gem source type
172
- #
173
- # @return [Integer] internal gem source type
174
- def build_spec_gem_source_type(source)
175
- case source
176
- when Bundler::Source::Metadata
177
- GEM_SOURCES_TYPES[:local]
178
- when Bundler::Source::Rubygems, Bundler::Source::Rubygems::Remote
179
- GEM_SOURCES_TYPES[:gemfile_source]
180
- when Bundler::Source::Git
181
- GEM_SOURCES_TYPES[:gemfile_git]
182
- when Bundler::Source::Path
183
- GEM_SOURCES_TYPES[:gemfile_path]
184
- else
185
- raise ArgumentError, "unknown source #{source.class}"
186
- end
187
- end
188
-
189
- # Build gem source
190
- #
191
- # @param spec [Bundler::StubSpecification, Bundler::LazySpecification, Gem::Specification]
192
- #
193
- # @return [Hash]
194
- def build_spec_source(spec)
195
- source = source_for_spec(spec)
196
-
197
- {
198
- 'type' => build_spec_gem_source_type(source),
199
- 'value' => source_name_from_source(source)
200
- }
201
- end
202
-
203
- # Figure out source for gem
204
- #
205
- # @param spec [Bundler::StubSpecification, Bundler::LazySpecification, Gem::Specification]
206
- #
207
- # @return [Bundler::Source] gem source type
208
- def source_for_spec(spec)
209
- return spec.remote if spec.remote
210
-
211
- case spec.source
212
- when Bundler::Source::Rubygems
213
- spec
214
- .source
215
- .send(:remote_specs)
216
- .search(Bundler::Dependency.new(spec.name, spec.version))
217
- .last
218
- .remote
219
- when Bundler::Source::Metadata, Bundler::Source::Git, Bundler::Source::Path
220
- spec.source
221
- else
222
- raise ArgumentError, "unknown source #{spec.source.class}"
223
- end
224
- end
225
-
226
- # Build gem source name
227
- #
228
- # @param source [Bundler::Source] gem source type
229
- #
230
- # @return [String]
231
- def source_name_from_source(source)
232
- case source
233
- when Bundler::Source::Metadata
234
- ''
235
- when Bundler::Source::Rubygems::Remote
236
- source_name(source.anonymized_uri)
237
- when Bundler::Source::Git
238
- source.instance_variable_get(:@safe_uri)
239
- when Bundler::Source::Path
240
- source.path
241
- else
242
- raise ArgumentError, "unknown source #{source.class}"
243
- end
244
- end
245
-
246
- # @param uri [Bundler::URI]
247
- #
248
- # @return [String]
249
- def source_name(uri)
250
- uri.to_s[0...-1]
251
- end
252
-
253
- # Build sources used in the Gemfile
254
- #
255
- # @return [Array<Hash>]
256
- def build_sources
257
- sources = @definition.send(:sources).rubygems_sources
258
- hash = {}
259
-
260
- sources.each do |source|
261
- type = build_source_type(source.remotes)
262
-
263
- source.remotes.each do |src|
264
- hash[source_name(src)] = type
265
- end
266
- end
267
-
268
- hash.map { |name, type| { 'name' => name, 'type' => type } }
269
- end
270
-
271
- # Build gem source type
272
- #
273
- # @param remotes [Array<Bundler::URI>]
274
- #
275
- # @return [Integer] internal source type
276
- def build_source_type(remotes)
277
- remotes.count > 1 ? SOURCES_TYPES[:multiple_primary] : SOURCES_TYPES[:valid]
278
- end
279
-
280
- # Checks if we should skip a source
281
- #
282
- # @param source [Bundler::Source] gem source type
283
- #
284
- # @return [Boolean] true if we should skip this source, false otherwise
285
- def skip?(source)
286
- return true if me?(source)
287
-
288
- false
289
- end
290
-
291
- # Checks if it's a self source, this happens for repositories that are a gem
292
- #
293
- # @param source [Bundler::Source] gem source type
294
- #
295
- # @return [Boolean] true if it's a self source, false otherwise
296
- def me?(source)
297
- return false unless ME_SOURCES.include?(source.class)
298
-
299
- source.path.to_s == ME_PATH
300
- end
301
- end
302
- end
303
- end
304
- end