sophos_central_api 0.1.2 → 0.2.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ed2be4896987431b40e0fc3b01f61addc9b58485aa3f1157493dadb070a792e2
4
- data.tar.gz: 848756dcc5fd520b1f5584acad7a9a318caf569cfd4bbef377b9e646fbc6c5f6
3
+ metadata.gz: a26247b4f61c367498ff21c935dcd319058384b1684f10c220f6f7700eaa0095
4
+ data.tar.gz: b6975aedaa003656add82f3fbddc4c15c8f66048faebbc16987ae23e7102a5ba
5
5
  SHA512:
6
- metadata.gz: b0e6aff68a24b83ae099fbb6d4273dc3cfa918b36223fefca96951b51f352081c50bfeef5f246205daf0ec98281a837f32390d151704340e2f048193479a2bf8
7
- data.tar.gz: 5aec805aaf3ab8988af7dc8adc50fb4e41564e94bf1cfdfa1fd05b6251bd7ac6a0af0a87fc94b1e619e408b5e1e8266d8ddfa3d86fd2a92bb4dd39c2112731d1
6
+ metadata.gz: afdda664f2c0982cc6ae3be738da6f8df8156a8a4287e73c3fc64058e6b6639b31066b17b0f6ea263613c753267902f3a91121f5478eab13ce5ef9e6f2f2bc59
7
+ data.tar.gz: b77d24174658993217761df3341585db908e973710e4d91eb63f8cad023e295ae83be4c9158bb810397d9ae8bd8f69ab31019afc2d1d4ae6f652c1b5654fe38d
data/CHANGELOG.md CHANGED
@@ -8,3 +8,9 @@
8
8
 
9
9
  ## [0.1.2] - 2024-02-15
10
10
  - Fix initialization issue
11
+
12
+ ## [0.2.0] - 2024-02-20
13
+ - Configuration and authentication exceptions changed
14
+
15
+ ## [0.2.1] - 2024-07-17
16
+ - Fix issue with paging for endpoints
data/Gemfile CHANGED
@@ -7,4 +7,5 @@ gemspec
7
7
 
8
8
  gem 'rake', '~> 13.0'
9
9
  gem 'rubocop', '~> 1.7'
10
+ gem 'simplecov', require: false, group: :test
10
11
  gem 'wrapi'
data/README.md CHANGED
@@ -1,4 +1,7 @@
1
1
  # Sophos Central Partner API
2
+ [![Version](https://img.shields.io/gem/v/sophos_central_api.svg)](https://rubygems.org/gems/sophos_central_api)
3
+ [![Maintainability](https://api.codeclimate.com/v1/badges/0e0c212559aad49a915c/maintainability)](https://codeclimate.com/github/jancotanis/sophos/maintainability)
4
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/0e0c212559aad49a915c/test_coverage)](https://codeclimate.com/github/jancotanis/sophos/test_coverage)
2
5
 
3
6
  This is a wrapper for the Sophos Central Partner API. You can see the API endpoints here https://developer.sophos.com/getting-started
4
7
 
@@ -9,7 +12,7 @@ Currently only the GET requests to customers, endpoints and alerts are implement
9
12
  Add this line to your application's Gemfile:
10
13
 
11
14
  ```ruby
12
- gem 'sophos_partner_api'
15
+ gem 'sophos_central_api'
13
16
  ```
14
17
 
15
18
  And then execute:
@@ -18,14 +21,14 @@ And then execute:
18
21
 
19
22
  Or install it yourself as:
20
23
 
21
- $ gem install sophos_partner_api
24
+ $ gem install sophos_central_api
22
25
 
23
26
  ## Usage
24
27
 
25
28
  Before you start making the requests to API provide the client id and client secret and email/password using the configuration wrapping.
26
29
 
27
- ```
28
- require 'sophos_partner_api'
30
+ ```ruby
31
+ require 'sophos_central_api'
29
32
 
30
33
  Sophos.configure do |config|
31
34
  config.client_id = ENV["SOPHOS_CLIENT_ID"]
@@ -43,7 +46,7 @@ end
43
46
 
44
47
  ## Resources
45
48
  ### Authentication
46
- ```
49
+ ```ruby
47
50
  # setup configuration
48
51
  #
49
52
  client.login
@@ -55,7 +58,7 @@ client.login
55
58
 
56
59
  ### Partner
57
60
  Endpoint for partner related requests
58
- ```
61
+ ```ruby
59
62
  roles = client.roles
60
63
  ```
61
64
 
@@ -73,7 +76,7 @@ roles = client.roles
73
76
  ### Common
74
77
  This is the OAS 3.0 specification for the Common API in Sophos Central.
75
78
 
76
- ```
79
+ ```ruby
77
80
  @client = Sophos.client()
78
81
  @client.login
79
82
  :
@@ -93,7 +96,7 @@ tenant = @client.tenant(id)
93
96
 
94
97
  ### Endpoints
95
98
  Returns endpoint for a provided tenant
96
- ```
99
+ ```ruby
97
100
  @client = Sophos.client()
98
101
  @client.login
99
102
  :
data/Rakefile CHANGED
@@ -1,12 +1,20 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'bundler/gem_tasks'
4
+ require 'dotenv'
4
5
  require 'rake/testtask'
5
6
 
7
+ Dotenv.load
8
+
9
+ system './bin/cc-test-reporter before-build'
10
+
6
11
  Rake::TestTask.new(:test) do |t|
7
12
  t.libs << 'test'
8
13
  t.libs << 'lib'
9
14
  t.test_files = FileList['test/**/*_test.rb']
15
+ at_exit do
16
+ system './bin/cc-test-reporter after-build'
17
+ end
10
18
  end
11
19
 
12
20
  require 'rubocop/rake_task'
Binary file
@@ -1,5 +1,7 @@
1
+ require 'faraday'
1
2
  require 'json'
2
3
  require 'uri'
4
+ require File.expand_path('error', __dir__)
3
5
 
4
6
  module Sophos
5
7
  # Deals with authentication flow and stores it within global configuration
@@ -8,22 +10,20 @@ module Sophos
8
10
  # Authorize to the Sophos portal and return access_token
9
11
  # @see https://developer.sophos.com/getting-started
10
12
  def auth_token(options = {})
13
+ raise ConfigurationError.new 'Client id and/or secret not configured' unless client_id && client_secret
11
14
  # use id endpoint instead of global api
12
15
  response = connection.post(id_endpoint+'/api/v2/oauth2/token') do |request|
13
16
  request.headers['Content-Type'] = 'application/x-www-form-urlencoded'
14
17
  request.body = URI.encode_www_form( api_access_token_params )
15
18
  end
16
19
  api_process_token(response.body)
17
-
20
+ setup_connection
18
21
  # use default endpoint and replace it with global endpoint
19
- partner = self.get( "/whoami/v1" )
20
- if 'partner'.eql?(partner.idType) && partner.id
21
- self.partner_id = partner.id
22
- self.endpoint = partner.apiHosts.global
23
- self.connection_options = { headers: { 'X-partner-id': self.partner_id } }
24
- else
25
- raise raise StandardError.new 'Partner id not returned; response ' + response.to_s
26
- end
22
+ self.access_token
23
+
24
+ rescue Faraday::UnauthorizedError => e
25
+
26
+ raise AuthenticationError.new 'Unauthorized; response ' + e.to_s
27
27
  end
28
28
  alias login auth_token
29
29
 
@@ -39,13 +39,23 @@ module Sophos
39
39
  end
40
40
 
41
41
  def api_process_token(response)
42
- at = self.access_token = response['access_token']
43
- self.token_type = response['token_type']
44
- self.refresh_token = response['refresh_token']
45
- self.token_expires = response['expires_in']
46
- raise StandardError.new 'Could not find valid access_token; response ' + response.to_s if at.nil? || at.empty?
42
+ self.access_token = response['access_token']
43
+ self.token_type = response['token_type']
44
+ self.refresh_token = response['refresh_token']
45
+ self.token_expires = response['expires_in']
47
46
 
48
- at
47
+ raise AuthenticationError.new 'Could not find valid access_token; response ' + response.to_s if self.access_token.nil? || self.access_token.empty?
48
+ end
49
+
50
+ def setup_connection
51
+ partner = self.get( "/whoami/v1" )
52
+ if 'partner'.eql?(partner.idType) && partner.id
53
+ self.partner_id = partner.id
54
+ self.endpoint = partner.apiHosts.global
55
+ self.connection_options = { headers: { 'X-partner-id': self.partner_id } }
56
+ else
57
+ raise AuthenticationError.new 'Partner id not returned; response ' + partner.to_s
58
+ end
49
59
  end
50
60
  end
51
61
  end
@@ -11,23 +11,23 @@ module Sophos
11
11
  Helper::def_api_call :downloads, Helper::endpoint_url(:downloads)
12
12
 
13
13
  # @see https://developer.sophos.com/docs/endpoint-v1/1/routes/endpoint-groups/get
14
- Helper::def_api_call :endpoint_groups, Helper::endpoint_url(:endpoint_groups), true
14
+ Helper::def_api_call :endpoint_groups, Helper::endpoint_url(:endpoint_groups), :endpoint_group
15
15
  def endpoint_group_endpoints(group_id)
16
16
  get_paged Helper::endpoint_url("endpoint-groups/#{group_id}/endpoints")
17
17
  end
18
18
 
19
19
  # @see https://developer.sophos.com/docs/endpoint-v1/1/routes/migrations/get
20
- Helper::def_api_call :migrations, Helper::endpoint_url(:migrations), true
20
+ Helper::def_api_call :migrations, Helper::endpoint_url(:migrations), :migration
21
21
  def migration_endpoints(migration_id)
22
22
  get_paged Helper::endpoint_url("migrations/#{migration_id}/endpoints")
23
23
  end
24
24
 
25
25
  # @see https://developer.sophos.com/docs/endpoint-v1/1/routes/policies/get
26
- Helper::def_api_call :policies, Helper::endpoint_url(:policies), true
26
+ Helper::def_api_call :policies, Helper::endpoint_url(:policies), :policy
27
27
 
28
28
  # Get all the endpoints for the specified tenant. No endpoint method defined as this clashes with api endpoint method
29
29
  # @see https://developer.sophos.com/docs/endpoint-v1/1/routes/endpoints/get
30
- Helper::def_api_call :endpoints, Helper::endpoint_url(:endpoints), false
30
+ Helper::def_api_call :endpoints, Helper::endpoint_url(:endpoints)
31
31
  def endpoint_isolation(endpoint_id)
32
32
  get Helper::endpoint_url("endpoints/#{endpoint_id}/isolation")
33
33
  end
@@ -8,26 +8,17 @@ module Sophos
8
8
  method_name.to_s.gsub('_', '-')
9
9
  end
10
10
 
11
- def self.singular method_name
12
- method_name = method_name.to_s.gsub(/s$/, '')
13
- method_name.gsub(/ie$/, 'y').to_sym
14
- end
15
-
16
- def self.common_url(method)
17
- "/common/v1/#{sanitize(method)}"
18
- end
19
-
20
- def self.endpoint_url(method)
21
- "/endpoint/v1/#{sanitize(method)}"
22
- end
23
-
24
- def self.partner_url(method)
25
- "/partner/v1/#{sanitize(method)}"
11
+ def self.common_url(method) self.url('common', method); end
12
+ def self.endpoint_url(method) self.url('endpoint', method); end
13
+ def self.partner_url(method) self.url('partner', method); end
14
+
15
+ def self.url(api,method)
16
+ "/#{api}/v1/#{sanitize(method)}"
26
17
  end
27
18
 
28
19
  # generate end point for 'endpoint' and 'endpoints'
29
- def self.def_api_call(method, url, id_field = false, paged = true)
30
- if id_field
20
+ def self.def_api_call(method, url, singular_method = nil, paged = true)
21
+ if singular_method
31
22
  self.send(:define_method, method) do |id = nil, params = {}|
32
23
  if id
33
24
  get("#{url}/#{id}", params)
@@ -36,8 +27,7 @@ module Sophos
36
27
  end
37
28
  end
38
29
  # strip trailing 's'
39
- singlr = self.singular(method) #method.to_s.chop.to_sym
40
- self.send(:define_method, singlr) do |id, params = {}|
30
+ self.send(:define_method, singular_method) do |id, params = {}|
41
31
  get("#{url}/#{id}", params)
42
32
  end
43
33
  else
@@ -8,13 +8,13 @@ module Sophos
8
8
  module Partner
9
9
 
10
10
  # @see https://developer.sophos.com/docs/partner-v1/1/routes/tenants/get
11
- Helper::def_api_call :tenants, Helper::partner_url(:tenants), true
11
+ Helper::def_api_call :tenants, Helper::partner_url(:tenants), :tenant
12
12
 
13
13
  # @see https://developer.sophos.com/docs/partner-v1/1/routes/roles/get
14
- Helper::def_api_call :roles, Helper::partner_url(:roles), true
14
+ Helper::def_api_call :roles, Helper::partner_url(:roles), :role
15
15
 
16
16
  # @see https://developer.sophos.com/docs/partner-v1/1/routes/admins/get
17
- Helper::def_api_call :admins, Helper::partner_url(:admins), true
17
+ Helper::def_api_call :admins, Helper::partner_url(:admins), :admin
18
18
 
19
19
  # Get the list of role assignments for given admin.
20
20
  # @see https://developer.sophos.com/docs/partner-v1/1/routes/admins/%7BadminId%7D/role-assignments/get
@@ -7,7 +7,7 @@ module Sophos
7
7
  module Configuration
8
8
  include WrAPI::Configuration
9
9
 
10
- # An array of additional valid keys in the options hash when configuring a {Sophos::API}
10
+ # An array of additional valid keys in the options hash when configuring a [Sophos::API]
11
11
  VALID_OPTIONS_KEYS = (WrAPI::Configuration::VALID_OPTIONS_KEYS + [:partner_id, :tenant_id, :id_endpoint]).freeze
12
12
 
13
13
  # @private
@@ -0,0 +1,11 @@
1
+ module Sophos
2
+
3
+ # Generic error to be able to rescue all Sophos errors
4
+ class SophosError < StandardError; end
5
+
6
+ # Raised when Sophos not configured correctly
7
+ class ConfigurationError < SophosError; end
8
+
9
+ # Error when authentication fails
10
+ class AuthenticationError < SophosError; end
11
+ end
@@ -24,10 +24,13 @@ module Sophos
24
24
  # ignore page size
25
25
  @page_size = page_size
26
26
  @total = @current = 1
27
+ @nextKey = nil
27
28
  end
28
29
 
29
30
  def page_options
30
- { 'page': @current, 'pageSize': @page_size, 'pageTotal': true }
31
+ options = { 'page': @current, 'pageSize': @page_size, 'pageTotal': true }
32
+ options = options.merge({ 'pageFromKey': @nextKey }) if @current > 1
33
+ options
31
34
  end
32
35
 
33
36
  def next_page!(data)
@@ -40,6 +43,7 @@ module Sophos
40
43
  else
41
44
  @current += 1
42
45
  end
46
+ @nextKey = pages['nextKey'] if pages['nextKey']
43
47
  else
44
48
  # no page info so assume single page request
45
49
  @total = 0
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Sophos
4
- VERSION = '0.1.2'
4
+ VERSION = '0.2.1'
5
5
  end
@@ -9,6 +9,6 @@ module Sophos
9
9
  #
10
10
  # @return [Sophos::Client]
11
11
  def self.client(options = {})
12
- Sophos::Client.new
12
+ Sophos::Client.new(options)
13
13
  end
14
14
  end
@@ -32,5 +32,6 @@ Gem::Specification.new do |s|
32
32
  s.add_runtime_dependency 'wrapi', ">= 0.3.0"
33
33
  s.add_development_dependency 'dotenv'
34
34
  s.add_development_dependency 'minitest'
35
+ s.add_development_dependency 'simplecov'
35
36
  s.add_development_dependency 'rubocop'
36
37
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sophos_central_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Janco Tanis
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-02-15 00:00:00.000000000 Z
11
+ date: 2024-07-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: rubocop
71
85
  requirement: !ruby/object:Gem::Requirement
@@ -92,6 +106,7 @@ files:
92
106
  - Gemfile
93
107
  - README.md
94
108
  - Rakefile
109
+ - bin/cc-test-reporter.exe
95
110
  - lib/sophos/api.rb
96
111
  - lib/sophos/authentication.rb
97
112
  - lib/sophos/client.rb
@@ -100,6 +115,7 @@ files:
100
115
  - lib/sophos/client/helper.rb
101
116
  - lib/sophos/client/partner.rb
102
117
  - lib/sophos/configuration.rb
118
+ - lib/sophos/error.rb
103
119
  - lib/sophos/pagination.rb
104
120
  - lib/sophos/version.rb
105
121
  - lib/sophos_central_api.rb
@@ -125,7 +141,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
125
141
  - !ruby/object:Gem::Version
126
142
  version: '0'
127
143
  requirements: []
128
- rubygems_version: 3.2.12
144
+ rubygems_version: 3.2.3
129
145
  signing_key:
130
146
  specification_version: 4
131
147
  summary: A Ruby wrapper for the Sophos Central REST APIs (readonly)