sophos_central_api 0.1.1 → 0.2.0

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: 4001f1778a4cbba03903acb2007aeca8c0840b351baf9654e2e238efb5b1650a
4
- data.tar.gz: fd8467571f6574c9647faab25aa1fc3016a6e1321a179ba5a94de32911cb4fc2
3
+ metadata.gz: de42982e7073acef13887732efb277b3f24e31fcf34552f7857100edc20b99a2
4
+ data.tar.gz: 40425c2083e372ea6434f86723209ddd1673a1055ca8fc26e9bac997fb961305
5
5
  SHA512:
6
- metadata.gz: df69465b988d9cdc9460ea1e3220eb5e8516386b1c1b6b54d44885c23724c6eac20084053eb7002bc0b1d7bca600e181000879bac27192f0fc2aa4fea14311d1
7
- data.tar.gz: 5766c1061673b8fc638573094111ca4c0023c25d02c58480cc824874187c981e39dce26aeaf93c6ccbacde44b0c862589f973db52789de5d3bb26023dff3ac98
6
+ metadata.gz: 4c3655439d962b32baef1e85a1ec31e4442c103472a82d0a2388d271fccde35c2fadb43088fd07db5ef3676d99331cea568f4019f61705b54a88577a7f97e073
7
+ data.tar.gz: 47cc68a252b5ad954f6bb6b48aae862b27e48ab35e0bacdb8c1fb605dd4d322d2af1325ec5457bef5ad6c970d20ac5f01c23e30705bd41f50c58a0c0e55c208b
data/.env.template CHANGED
@@ -1,2 +1,3 @@
1
1
  SOPHOS_CLIENT_ID=<SOPHOS_CLIENT_ID>
2
2
  SOPHOS_CLIENT_SECRET=<SOPHOS_CLIENT_SECRET>
3
+ TEST_TENANT=<TENANT ID>
data/CHANGELOG.md CHANGED
@@ -6,3 +6,8 @@
6
6
  ## [0.1.1] - 2024-02-14
7
7
  - Convienience method added to get client for a teannt api host
8
8
 
9
+ ## [0.1.2] - 2024-02-15
10
+ - Fix initialization issue
11
+
12
+ ## [0.2.0] - 2024-02-20
13
+ - Configuration and authentication exceptions changed
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
30
  ```
28
- require 'sophos_partner_api'
31
+ require 'sophos_central_api'
29
32
 
30
33
  Sophos.configure do |config|
31
34
  config.client_id = ENV["SOPHOS_CLIENT_ID"]
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,6 +10,7 @@ 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'
@@ -22,8 +25,10 @@ module Sophos
22
25
  self.endpoint = partner.apiHosts.global
23
26
  self.connection_options = { headers: { 'X-partner-id': self.partner_id } }
24
27
  else
25
- raise raise StandardError.new 'Partner id not returned; response ' + response.to_s
28
+ raise AuthenticationError.new 'Partner id not returned; response ' + response.to_s
26
29
  end
30
+ rescue Faraday::UnauthorizedError => e
31
+ raise AuthenticationError.new 'Unauthorized; response ' + e.to_s
27
32
  end
28
33
  alias login auth_token
29
34
 
@@ -43,7 +48,7 @@ module Sophos
43
48
  self.token_type = response['token_type']
44
49
  self.refresh_token = response['refresh_token']
45
50
  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?
51
+ raise AuthenticationError.new 'Could not find valid access_token; response ' + response.to_s if at.nil? || at.empty?
47
52
 
48
53
  at
49
54
  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
@@ -7,36 +7,14 @@ module Sophos
7
7
  # @see https://developer.sophos.com/docs/partner-v1/1/overview
8
8
  module Partner
9
9
 
10
- def self.__def_partner_call(method, id_field = nil)
11
- if id_field
12
- self.send(:define_method, method) do |id = nil|
13
- url = partner_url(method)
14
- if id
15
- get(partner_url("#{method}/#{id}"))
16
- else
17
- get_paged(url)
18
- end
19
- end
20
- # strip trailing 's'
21
- singular = method.to_s.chop.to_sym
22
- self.send(:define_method, singular) do |id, params = nil|
23
- get(partner_url("#{method}/#{id}", params))
24
- end
25
- else
26
- self.send(:define_method, method) do
27
- get_paged(partner_url(method))
28
- end
29
- end
30
- end
31
-
32
10
  # @see https://developer.sophos.com/docs/partner-v1/1/routes/tenants/get
33
- Helper::def_api_call :tenants, Helper::partner_url(:tenants), true
11
+ Helper::def_api_call :tenants, Helper::partner_url(:tenants), :tenant
34
12
 
35
13
  # @see https://developer.sophos.com/docs/partner-v1/1/routes/roles/get
36
- Helper::def_api_call :roles, Helper::partner_url(:roles), true
14
+ Helper::def_api_call :roles, Helper::partner_url(:roles), :role
37
15
 
38
16
  # @see https://developer.sophos.com/docs/partner-v1/1/routes/admins/get
39
- Helper::def_api_call :admins, Helper::partner_url(:admins), true
17
+ Helper::def_api_call :admins, Helper::partner_url(:admins), :admin
40
18
 
41
19
  # Get the list of role assignments for given admin.
42
20
  # @see https://developer.sophos.com/docs/partner-v1/1/routes/admins/%7BadminId%7D/role-assignments/get
@@ -61,11 +39,6 @@ module Sophos
61
39
  get_paged(Helper::partner_url("billing/usage/#{year}/#{month}"), params)
62
40
  end
63
41
 
64
- private
65
-
66
- def __partner_url(method, id = nil)
67
- "/partner/v1/#{method}"
68
- end
69
42
  end
70
43
  end
71
44
  end
@@ -1,20 +1,47 @@
1
1
  require 'wrapi'
2
+ require File.expand_path('version', __dir__)
3
+ require File.expand_path('pagination', __dir__)
2
4
 
3
5
  module Sophos
4
6
  # Defines constants and methods related to configuration
5
7
  module Configuration
6
8
  include WrAPI::Configuration
7
9
 
8
- # 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]
9
11
  VALID_OPTIONS_KEYS = (WrAPI::Configuration::VALID_OPTIONS_KEYS + [:partner_id, :tenant_id, :id_endpoint]).freeze
10
12
 
11
13
  # @private
12
14
  attr_accessor *VALID_OPTIONS_KEYS
13
15
 
16
+ DEFAULT_ENDPOINT = 'https://api.central.sophos.com'.freeze
17
+ DEFAULT_ID_ENDPOINT = 'https://id.sophos.com'.freeze
18
+ DEFAULT_UA = "Sophos Ruby API wrapper #{Sophos::VERSION}".freeze
19
+ DEFAULT_PAGINATION = Sophos::RequestPagination::PagesPagination
20
+ DEFAULT_PAGE_SIZE = 100
21
+
22
+ # When this module is extended, set all configuration options to their default values
23
+ def self.extended(base)
24
+ base.reset
25
+ end
26
+
27
+ # Create a hash of options and their values
28
+ def options
29
+ VALID_OPTIONS_KEYS.inject({}) do |option, key|
30
+ option.merge!(key => send(key))
31
+ end
32
+ end
33
+
14
34
  # Reset all configuration options to defaults
15
35
  def reset
16
36
  super
17
37
  self.partner_id = nil
38
+ self.tenant_id = nil
39
+
40
+ self.endpoint = DEFAULT_ENDPOINT
41
+ self.id_endpoint = DEFAULT_ID_ENDPOINT
42
+ self.user_agent = DEFAULT_UA
43
+ self.page_size = DEFAULT_PAGE_SIZE
44
+ self.pagination_class = DEFAULT_PAGINATION
18
45
  end
19
46
  end
20
47
  end
@@ -0,0 +1,11 @@
1
+ module Sophos
2
+
3
+ # Generic error to be able to rescue all Zabbix errors
4
+ class SophosError < StandardError; end
5
+
6
+ # Raised when Zabbix not configured correctly
7
+ class ConfigurationError < SophosError; end
8
+
9
+ # Error when authentication fails
10
+ class AuthenticationError < SophosError; end
11
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Sophos
4
- VERSION = '0.1.1'
4
+ VERSION = '0.2.0'
5
5
  end
@@ -1,37 +1,14 @@
1
1
  require "wrapi"
2
2
  require File.expand_path('sophos/api', __dir__)
3
3
  require File.expand_path('sophos/client', __dir__)
4
- require File.expand_path('sophos/pagination', __dir__)
5
- require File.expand_path('sophos/version', __dir__)
6
4
 
7
5
  module Sophos
8
6
  extend Configuration
9
7
  extend WrAPI::RespondTo
10
8
 
11
- DEFAULT_ENDPOINT = 'https://api.central.sophos.com'.freeze
12
- DEFAULT_ID_ENDPOINT = 'https://id.sophos.com'.freeze
13
- DEFAULT_UA = "Sophos Ruby API wrapper #{Sophos::VERSION}".freeze
14
- DEFAULT_PAGINATION = Sophos::RequestPagination::PagesPagination
15
- DEFAULT_PAGE_SIZE = 100
16
-
17
9
  #
18
10
  # @return [Sophos::Client]
19
11
  def self.client(options = {})
20
- Sophos::Client.new({
21
- endpoint: DEFAULT_ENDPOINT,
22
- id_endpoint: DEFAULT_ID_ENDPOINT,
23
- user_agent: DEFAULT_UA,
24
- page_size: DEFAULT_PAGE_SIZE,
25
- pagination_class: DEFAULT_PAGINATION
26
- }.merge(options))
27
- end
28
-
29
- def self.reset
30
- super
31
- self.endpoint = DEFAULT_ENDPOINT
32
- self.id_endpoint = DEFAULT_ID_ENDPOINT
33
- self.user_agent = DEFAULT_UA
34
- self.page_size = DEFAULT_PAGE_SIZE
35
- self.pagination_class = DEFAULT_PAGINATION
12
+ Sophos::Client.new(options)
36
13
  end
37
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.1
4
+ version: 0.2.0
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-14 00:00:00.000000000 Z
11
+ date: 2024-02-20 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