paraxial 0.1.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1bd3d81eda937e486869f0796af505440ba3268ef365a59170ac0545dcf06a10
4
- data.tar.gz: '099afef44bbc41beb2fdad948610864fa4592837428701c77d0f965d047e7d68'
3
+ metadata.gz: ea5f2156685b5204495019d6a72ad0f433326438abfef4e70e26b39ab62bf58b
4
+ data.tar.gz: 4dd855ada83c429df5422b42177af07863041ef94feb14912375df5f8d18e807
5
5
  SHA512:
6
- metadata.gz: 59db9199f831f0d4b65d8aa56bfca1ed976da91c03799b237c5b2d4fe62d5df1fd76c9de6dbf4969c90c6a0c50d6cfedfe57762f89a36f63997f1df7f811178c
7
- data.tar.gz: 5efcc6c94e05db287a6679e06188f86b354ec4c098c8f2f4276e10a73385895cbbf3b2c6bc2a7364e11c26d3ae4e953ff18109193d533eec6e41e0356895c9b8
6
+ metadata.gz: 5f458988de37e4a6c9c0a8f8e1489b613925faf48d11dc59c1addbcca49acc740292cddae12bd675c25c8c640a2f45133e5ed6d9232c8319f74fe868c2e2396e
7
+ data.tar.gz: 81504558ae7231a7f5f5397f6ca640faf63532c15686d61942d54a306c0d28b865f3e11e8c17973729a1b200c74ad3246f149ca46a53635e91dfa229fe57a998
data/lib/paraxial/cli.rb CHANGED
@@ -4,30 +4,101 @@ require 'net/http'
4
4
  require 'uri'
5
5
  require 'json'
6
6
  require 'time'
7
+ require 'yaml'
8
+ require_relative 'helpers'
7
9
 
8
10
  module Paraxial
9
11
  class CLI < Thor
10
- desc "scan", "Run scan"
12
+ desc 'scan', 'Run scan'
13
+ option :github_app, type: :boolean, default: false, desc: 'Use GitHub app'
14
+ option :install_id, type: :numeric, desc: 'GitHub App installation ID'
15
+ option :repo_owner, type: :string, desc: 'Repository owner'
16
+ option :repo_name, type: :string, desc: 'Repository name'
17
+ option :pr_number, type: :numeric, desc: 'Pull request number'
18
+
11
19
  def scan
12
- puts "[Paraxial] Scan NOW"
13
- cops = "Paraxial,Security/Eval,Security/IoMethods,Security/JSONLoad,Security/MarshalLoad,Security/Open,Security/YAMLLoad"
14
- rubocop = `rubocop --only #{cops} --format json`
15
- lockfile = File.read("./Gemfile.lock")
16
- api_key = ENV['PARAXIAL_API_KEY']
17
- uri = URI.parse(ENV['PARAXIAL_URL'] + "/api/ruby_scan")
18
- headers = { 'Content-Type': 'application/json' }
19
-
20
- body = { rubocop: rubocop, lockfile: lockfile, api_key: api_key, timestamp: Paraxial.get_timestamp() }
21
- response = Net::HTTP.post(uri, body.to_json, headers)
22
- puts response.body
23
-
24
- if ENV['PARAXIAL_API_KEY'] == nil
25
- puts "[Paraxial] Environment variable PARAXIAL_API_KEY not found, set with: "
26
- puts "[Paraxial] export PARAXIAL_API_KEY=your_site_api_key_here"
27
- puts "[Paraxial] Exiting"
28
- exit()
20
+ puts '[Paraxial] Scan starting...'
21
+ if check_rubocop_configuration
22
+ puts '[Paraxial] .rubocop.yml contains the required paraxial configuration.'
23
+ else
24
+ puts '[Paraxial] .rubocop.yml does not contain the required paraxial configuration.'
25
+ puts '[Paraxial] How to configure: TODO_URL'
26
+ exit
27
+ end
28
+
29
+ if ENV['PARAXIAL_API_KEY'].nil?
30
+ puts '[Paraxial] Environment variable PARAXIAL_API_KEY not found'
31
+ else
32
+ github_app = options[:github_app]
33
+ install_id = options[:install_id]
34
+ repo_owner = options[:repo_owner]
35
+ repo_name = options[:repo_name]
36
+ pr_number = options[:pr_number]
37
+
38
+ cops = 'Paraxial,Security/Eval,Security/IoMethods,Security/JSONLoad,Security/MarshalLoad,Security/Open,Security/YAMLLoad'
39
+ rubocop = `rubocop --only #{cops} --format json`
40
+ lockfile = File.read('./Gemfile.lock')
41
+ api_key = ENV['PARAXIAL_API_KEY']
42
+ uri = URI.parse(Paraxial::Helpers.get_paraxial_url + '/api/ruby_scan')
43
+ headers = { 'Content-Type': 'application/json' }
44
+
45
+ body = { rubocop:, lockfile:, api_key:, timestamp: Paraxial.get_timestamp }
46
+ response = Net::HTTP.post(uri, body.to_json, headers)
47
+ puts "[Paraxial] scan result: #{response.body}"
48
+ github_valid = (!!github_app and !!install_id and !!repo_owner and !!repo_name and !!pr_number)
49
+
50
+ if github_app and github_valid == false
51
+ puts '[Paraxial] --github_app missing arguments'
52
+ puts '[Paraxial] Required: --github_app, --install_id, --repo_owner, --repo_name, --pr_number'
53
+ elsif github_app and github_valid
54
+ uuid_regex = /UUID\s+(\S+)/
55
+ match = response.body.match(uuid_regex)
56
+ uuid = match[1] if match
57
+ if uuid
58
+ final_uuid = uuid.chomp('.')
59
+ censored_backend_map = {
60
+ 'installation_id' => install_id,
61
+ 'repository_owner' => repo_owner,
62
+ 'repository_name' => repo_name,
63
+ 'pull_request_number' => pr_number,
64
+ 'scan_uuid' => final_uuid,
65
+ 'api_key' => 'REDACTED'
66
+ }
67
+ cbms = JSON.pretty_generate(censored_backend_map)
68
+ puts "[Paraxial] GitHub hash: #{cbms}"
69
+
70
+ censored_backend_map['api_key'] = api_key
71
+ backend_map = censored_backend_map
72
+ parax_uri = URI.parse(Paraxial::Helpers.get_paraxial_url + '/api/github_app')
73
+ github_pr_url = "https://github.com/#{repo_owner}/#{repo_name}/pull/#{pr_number}"
74
+
75
+ rr = Net::HTTP.post(parax_uri, backend_map.to_json, headers)
76
+ puts "[Paraxial] parax_uri response: #{rr.body}"
77
+ puts "[Paraxial] #{github_pr_url}"
78
+ else
79
+ puts '[Paraxial] No scan UUID found'
80
+ end
81
+
82
+ else
83
+ :ok
84
+ end
85
+ end
86
+ end
87
+
88
+ private
89
+
90
+ def check_rubocop_configuration
91
+ rubocop_file = File.join(Dir.pwd, '.rubocop.yml')
92
+
93
+ return false unless File.exist?(rubocop_file)
94
+
95
+ config = YAML.load_file(rubocop_file)
96
+ required_key = 'require'
97
+
98
+ if config.is_a?(Hash) && config[required_key].is_a?(Array)
99
+ config[required_key].include?('paraxial')
29
100
  else
30
- puts "[Paraxial] Scan result here"
101
+ false
31
102
  end
32
103
  end
33
104
  end
@@ -0,0 +1,7 @@
1
+ module Paraxial
2
+ module Helpers
3
+ def self.get_paraxial_url
4
+ @paraxial_url ||= ENV['PARAXIAL_URL'] || 'https://app.paraxial.io/'
5
+ end
6
+ end
7
+ end
@@ -1,25 +1,66 @@
1
1
  require 'bundler'
2
2
  require 'paraxial'
3
+ require 'rpatricia'
4
+ require_relative '../helpers'
5
+
3
6
  Bundler.setup
4
7
 
5
8
  Rails.application.config.to_prepare do
6
- # Your code here
7
- puts "[Paraxial] Runtime start"
8
-
9
- deps_and_licenses = []
10
- Bundler.load.specs.each do |spec|
11
- # Print the gem name and license
12
- h = { name: spec.name, version: spec.version.to_s, description: Paraxial.trim_dep(spec.description), license: spec.license || 'None' }
13
- deps_and_licenses << h
14
- end
15
- deps_and_licenses << { name: "ruby", version: RUBY_VERSION, description: "The Ruby Programming Language", license: "Ruby"}
9
+ puts '[Paraxial] Init start'
16
10
  api_key = ENV['PARAXIAL_API_KEY']
17
- uri = URI.parse(ENV['PARAXIAL_URL'] + "/api/ruby_app_lic")
18
- headers = { 'Content-Type': 'application/json' }
19
11
 
20
- body = { app_lic: deps_and_licenses, api_key: api_key, timestamp: Paraxial.get_timestamp() }
21
- Thread.new do
22
- response = Net::HTTP.post(uri, body.to_json, headers)
23
- end
12
+ if api_key.nil?
13
+ puts '[Paraxial] Init PARAXIAL_API_KEY key not set, agent not started'
14
+ elsif Rails.env.test?
15
+ puts '[Paraxial] Init Test environment detected, agent not started'
16
+ else
17
+ begin
18
+ puts '[Paraxial] Init config valid, agent starting'
19
+ deps_and_licenses = []
20
+ Bundler.load.specs.each do |spec|
21
+ # Print the gem name and license
22
+ h = { name: spec.name, version: spec.version.to_s, description: Paraxial.trim_dep(spec.description),
23
+ license: spec.license || 'None' }
24
+ deps_and_licenses << h
25
+ end
26
+ deps_and_licenses << { name: 'ruby', version: RUBY_VERSION, description: 'The Ruby Programming Language',
27
+ license: 'Ruby' }
28
+ uri = URI.parse(Paraxial::Helpers.get_paraxial_url + '/api/ruby_app_lic')
29
+ headers = { 'Content-Type': 'application/json' }
24
30
 
31
+ body = { app_lic: deps_and_licenses, api_key:, timestamp: Paraxial.get_timestamp }
32
+ cloud_uri = URI.parse(Paraxial::Helpers.get_paraxial_url + '/api/cloud_ip_list')
33
+ response = Net::HTTP.get(cloud_uri)
34
+
35
+ Thread.new do
36
+ Net::HTTP.post(uri, body.to_json, headers)
37
+ end
38
+
39
+ # https://github.com/jkitching/rpatricia
40
+ pt_v4 = Patricia.new
41
+ pt_v6 = Patricia.new(:AF_INET6)
42
+ cloud_list = JSON.parse(response)
43
+ cloud_list.each do |k, v|
44
+ if k.include?('::')
45
+ pt_v6.add(k, v)
46
+ else
47
+ pt_v4.add(k, v)
48
+ end
49
+ end
50
+ # puts '[Paraxial] pt_v4.num_nodes'
51
+ # puts pt_v4.num_nodes
52
+ # puts 'pt_v6.num_nodes'
53
+ # puts pt_v6.num_nodes
54
+ PARAXIAL_IPV4 = pt_v4
55
+ PARAXIAL_IPV6 = pt_v6
56
+ rescue Errno::ECONNREFUSED => _e
57
+ puts '[Paraxial] Init HTTP request failed, check configuration'
58
+ PARAXIAL_IPV4 = Patricia.new
59
+ PARAXIAL_IPV6 = Patricia.new(:AF_INET6)
60
+ rescue StandardError => _e
61
+ puts '[Paraxial] Init error, check configuration'
62
+ PARAXIAL_IPV4 = Patricia.new
63
+ PARAXIAL_IPV6 = Patricia.new(:AF_INET6)
64
+ end
65
+ end
25
66
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Paraxial
4
- VERSION = "0.1.0"
4
+ VERSION = '0.3.0'
5
5
  end
data/lib/paraxial.rb CHANGED
@@ -17,11 +17,33 @@ module Paraxial
17
17
  class Error < StandardError; end
18
18
  # Your code goes here...
19
19
 
20
+ class Defense
21
+ def initialize(app)
22
+ @app = app
23
+ end
24
+
25
+ def call(env)
26
+ request_path = env['PATH_INFO']
27
+
28
+ if request_path.end_with?('.php')
29
+ # Return a 404 response if the request path ends with '.php'
30
+ [404, { 'Content-Type' => 'text/plain' }, ["Not Found from Paraxial.io"]]
31
+ else
32
+ # Pass the request to the next middleware or the application
33
+ @app.call(env)
34
+ end
35
+ end
36
+ end
37
+
20
38
  def self.get_timestamp
21
39
  utc_time = Time.now.utc
22
40
  utc_time.strftime("%Y-%m-%d %H:%M:%S.%6N") + "Z"
23
41
  end
24
42
 
43
+ def self.cloud_ip?(ip)
44
+ !!(PARAXIAL_IPV4.search_best(ip) or PARAXIAL_IPV6.search_best(ip))
45
+ end
46
+
25
47
  def self.trim_dep(input)
26
48
  if input == nil
27
49
  nil
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: paraxial
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Lubas
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-07-24 00:00:00.000000000 Z
11
+ date: 2024-08-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -24,6 +24,34 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '3.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rpatricia
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rubocop
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
27
55
  - !ruby/object:Gem::Dependency
28
56
  name: thor
29
57
  requirement: !ruby/object:Gem::Requirement
@@ -54,6 +82,7 @@ files:
54
82
  - lib/paraxial.rb
55
83
  - lib/paraxial/cli.rb
56
84
  - lib/paraxial/engine.rb
85
+ - lib/paraxial/helpers.rb
57
86
  - lib/paraxial/initializers/startup.rb
58
87
  - lib/paraxial/version.rb
59
88
  - lib/rubocop/cop/paraxial/constantize.rb