sophos_central_api 0.2.2 → 0.2.3
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 +4 -4
- data/CHANGELOG.md +3 -0
- data/README.md +3 -4
- data/lib/sophos/authentication.rb +5 -6
- data/lib/sophos/client/endpoint.rb +0 -1
- data/lib/sophos/client/helper.rb +104 -26
- data/lib/sophos/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 7b18fa53d284ea012c44050984a138d3c8c4241b8dcce505b541a92c10951d1c
|
|
4
|
+
data.tar.gz: 940bcfba27107629b2788cd150aaa6f88687d095bd5653c0a415d47aa073085c
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 73aedd595b2ba56026dbe7a871764f2e2636fde292d2bf58b4e5ed07d131b722767e6f3a6587c6dbf0b70fd033441a7b41a27603e4fd253bedcd333ec9dae314
|
|
7
|
+
data.tar.gz: 2daf707681ea5acb32340c5cacac75d763096eca80d806c1b0d2f468324e6e32b2292353a9fd587b1fafa9cdc958196dc67c459e28643617f71120d997509f8d
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
|
@@ -66,7 +66,6 @@ client.login
|
|
|
66
66
|
|:--|:--|:--|
|
|
67
67
|
|.login|||
|
|
68
68
|
|
|
69
|
-
|
|
70
69
|
### Partner
|
|
71
70
|
|
|
72
71
|
Endpoint for partner related requests
|
|
@@ -82,8 +81,8 @@ roles = client.roles
|
|
|
82
81
|
|.admins, .admin(id) |/partner/v1/admins/{id}|
|
|
83
82
|
|.admin_role_assignments(admin_id)|/partner/v1/admins/{admin_id}/role-assignments|
|
|
84
83
|
|.admin_role_assignment(admin_id,assignment_id)|/partner/v1/admins/{admin_id}/role-assignments/{assignment_id}|
|
|
85
|
-
|.permission_sets
|
|
86
|
-
|.billing_usage(year, month)
|
|
84
|
+
|.permission_sets |/partner/v1/roles/permission-sets|
|
|
85
|
+
|.billing_usage(year, month) |/partner/v1/billing-usage/{year}/{month}|
|
|
87
86
|
|
|
88
87
|
### Common
|
|
89
88
|
|
|
@@ -102,7 +101,7 @@ tenant = @client.tenant(id)
|
|
|
102
101
|
|Resource|API endpoint|
|
|
103
102
|
|:--|:--|
|
|
104
103
|
|.alerts, .alert(id) |.../alerts/|
|
|
105
|
-
|.directory_user_groups, .directory_user_groups(id)
|
|
104
|
+
|.directory_user_groups, .directory_user_groups(id) |.../directory/user-groups|
|
|
106
105
|
|.directory_user_group_users(id) |.../directory/user-groups/{id}/users|
|
|
107
106
|
|.directory_users, .directory_user(id) |.../directory/users|
|
|
108
107
|
|.directory_user_groups(id) |.../directory/users/{id}/groups|
|
|
@@ -12,7 +12,6 @@ module Sophos
|
|
|
12
12
|
# @note Ensure that `client_id` and `client_secret` are configured before calling authentication methods.
|
|
13
13
|
# @see https://developer.sophos.com/getting-started
|
|
14
14
|
module Authentication
|
|
15
|
-
|
|
16
15
|
# Authorizes with the Sophos portal and retrieves an access token.
|
|
17
16
|
# This method performs an OAuth2 client credentials flow, returning a valid token.
|
|
18
17
|
#
|
|
@@ -25,7 +24,7 @@ module Sophos
|
|
|
25
24
|
# api.auth_token
|
|
26
25
|
#
|
|
27
26
|
def auth_token(_options = {})
|
|
28
|
-
raise ConfigurationError
|
|
27
|
+
raise ConfigurationError, 'Client id and/or secret not configured' unless client_id && client_secret
|
|
29
28
|
|
|
30
29
|
# POST request to obtain the OAuth2 token using client credentials
|
|
31
30
|
response = connection.post("#{id_endpoint}/api/v2/oauth2/token") do |request|
|
|
@@ -39,7 +38,7 @@ module Sophos
|
|
|
39
38
|
# Returns the generated access token
|
|
40
39
|
self.access_token
|
|
41
40
|
rescue Faraday::UnauthorizedError => e
|
|
42
|
-
raise AuthenticationError
|
|
41
|
+
raise AuthenticationError, "Unauthorized; response #{e}"
|
|
43
42
|
end
|
|
44
43
|
alias login auth_token
|
|
45
44
|
|
|
@@ -63,12 +62,12 @@ module Sophos
|
|
|
63
62
|
# @raise [AuthenticationError] If `access_token` is missing or invalid.
|
|
64
63
|
def api_process_token(response)
|
|
65
64
|
self.access_token = response['access_token']
|
|
66
|
-
self.token_type = response['token_type']
|
|
65
|
+
self.token_type = response['token_type'] unless self.token_type.downcase.eql? response['token_type']
|
|
67
66
|
self.refresh_token = response['refresh_token']
|
|
68
67
|
self.token_expires = response['expires_in']
|
|
69
68
|
|
|
70
69
|
if self.access_token.nil? || self.access_token.empty?
|
|
71
|
-
raise AuthenticationError
|
|
70
|
+
raise AuthenticationError, "Could not find valid access_token; response #{response}"
|
|
72
71
|
end
|
|
73
72
|
end
|
|
74
73
|
|
|
@@ -85,7 +84,7 @@ module Sophos
|
|
|
85
84
|
self.endpoint = partner.apiHosts.global
|
|
86
85
|
self.connection_options = { headers: { 'X-partner-id': self.partner_id } }
|
|
87
86
|
else
|
|
88
|
-
raise AuthenticationError
|
|
87
|
+
raise AuthenticationError, "Partner id not returned; response #{partner}"
|
|
89
88
|
end
|
|
90
89
|
end
|
|
91
90
|
end
|
data/lib/sophos/client/helper.rb
CHANGED
|
@@ -1,49 +1,97 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
module Sophos
|
|
4
|
-
#
|
|
4
|
+
# This module provides helper methods to dynamically define API calls for the Sophos Client.
|
|
5
|
+
# It supports paginated and non-paginated API methods, and allows generating
|
|
6
|
+
# URLs for different API types (`common`, `endpoint`, `partner`).
|
|
7
|
+
#
|
|
8
|
+
# == Example:
|
|
9
|
+
#
|
|
10
|
+
# # Define paginated API methods for fetching alerts:
|
|
11
|
+
# Helper::def_api_call(:alerts, Helper::common_url(:alerts), :alert)
|
|
12
|
+
#
|
|
13
|
+
# # Generated methods:
|
|
14
|
+
# client.alerts # Fetches paginated alerts
|
|
15
|
+
# client.alert(12345) # Fetches a specific alert by ID (singular)
|
|
16
|
+
#
|
|
17
|
+
# @see https://developer.sophos.com/ Sophos API Documentation
|
|
5
18
|
class Client
|
|
6
19
|
module Helper
|
|
7
|
-
|
|
8
|
-
#
|
|
9
|
-
# @param method_name [Symbol, String]
|
|
10
|
-
# @return [String]
|
|
20
|
+
# Converts method names to a URL-safe format by replacing underscores with hyphens.
|
|
21
|
+
#
|
|
22
|
+
# @param method_name [Symbol, String] The method name to sanitize.
|
|
23
|
+
# @return [String] The sanitized method name (e.g., 'user_groups' => 'user-groups').
|
|
24
|
+
#
|
|
25
|
+
# == Example:
|
|
26
|
+
# sanitize(:user_groups) # => "user-groups"
|
|
11
27
|
def self.sanitize(method_name)
|
|
12
28
|
method_name.to_s.tr('_', '-')
|
|
13
29
|
end
|
|
14
30
|
|
|
15
|
-
#
|
|
16
|
-
#
|
|
17
|
-
# @
|
|
31
|
+
# Generates a URL path for the common API.
|
|
32
|
+
#
|
|
33
|
+
# @param method [Symbol, String] The endpoint method name.
|
|
34
|
+
# @return [String] Full path for the common API (e.g., `/common/v1/alerts`).
|
|
35
|
+
#
|
|
36
|
+
# == Example:
|
|
37
|
+
# common_url(:alerts) # => "/common/v1/alerts"
|
|
18
38
|
def self.common_url(method)
|
|
19
39
|
url('common', method)
|
|
20
40
|
end
|
|
21
41
|
|
|
22
|
-
#
|
|
42
|
+
# Generates a URL path for the endpoint API.
|
|
43
|
+
#
|
|
44
|
+
# @param method [Symbol, String] The endpoint method name.
|
|
45
|
+
# @return [String] Full path for the endpoint API.
|
|
46
|
+
#
|
|
47
|
+
# == Example:
|
|
48
|
+
# endpoint_url(:downloads) # => "/endpoint/v1/downloads"
|
|
23
49
|
def self.endpoint_url(method)
|
|
24
50
|
url('endpoint', method)
|
|
25
51
|
end
|
|
26
52
|
|
|
27
|
-
#
|
|
53
|
+
# Generates a URL path for the partner API.
|
|
54
|
+
#
|
|
55
|
+
# @param method [Symbol, String] The endpoint method name.
|
|
56
|
+
# @return [String] Full path for the partner API.
|
|
57
|
+
#
|
|
58
|
+
# == Example:
|
|
59
|
+
# partner_url(:tenants) # => "/partner/v1/tenants"
|
|
28
60
|
def self.partner_url(method)
|
|
29
61
|
url('partner', method)
|
|
30
62
|
end
|
|
31
63
|
|
|
32
|
-
#
|
|
33
|
-
#
|
|
34
|
-
# @param
|
|
35
|
-
# @
|
|
64
|
+
# Constructs the full API URL path.
|
|
65
|
+
#
|
|
66
|
+
# @param api [String] API type (e.g., 'common', 'endpoint', 'partner').
|
|
67
|
+
# @param method [Symbol, String] The sanitized endpoint method name.
|
|
68
|
+
# @return [String] Full API URL path.
|
|
69
|
+
#
|
|
70
|
+
# == Example:
|
|
71
|
+
# url('common', :alerts) # => "/common/v1/alerts"
|
|
36
72
|
def self.url(api, method)
|
|
37
73
|
"/#{api}/v1/#{sanitize(method)}"
|
|
38
74
|
end
|
|
39
75
|
|
|
40
|
-
# Dynamically
|
|
41
|
-
#
|
|
76
|
+
# Dynamically defines API methods for singular and plural endpoints.
|
|
77
|
+
#
|
|
78
|
+
# If `singular_method` is provided, it generates:
|
|
79
|
+
# - A plural method for fetching all resources (e.g., `alerts`).
|
|
80
|
+
# - A singular method for fetching a specific resource by ID (e.g., `alert(id)`).
|
|
81
|
+
#
|
|
82
|
+
# @param method [Symbol] The plural method name (e.g., `:alerts`).
|
|
83
|
+
# @param url [String] The API endpoint URL path.
|
|
84
|
+
# @param singular_method [Symbol, nil] The singular method name (optional).
|
|
85
|
+
# @param paged [Boolean] Whether the method should support pagination (default: true).
|
|
42
86
|
#
|
|
43
|
-
#
|
|
44
|
-
#
|
|
45
|
-
#
|
|
46
|
-
#
|
|
87
|
+
# == Example:
|
|
88
|
+
# def_api_call(:alerts, "/common/v1/alerts", :alert)
|
|
89
|
+
#
|
|
90
|
+
# # Generates:
|
|
91
|
+
# # - alerts(params = {}) – Fetches paginated alerts.
|
|
92
|
+
# # - alert(id, params = {}) – Fetches a specific alert by ID.
|
|
93
|
+
#
|
|
94
|
+
# @see define_singular_and_plural_methods, define_paged_method, define_plain_method
|
|
47
95
|
def self.def_api_call(method, url, singular_method = nil, paged = true)
|
|
48
96
|
if singular_method
|
|
49
97
|
define_singular_and_plural_methods(method, url, singular_method)
|
|
@@ -52,10 +100,22 @@ module Sophos
|
|
|
52
100
|
end
|
|
53
101
|
end
|
|
54
102
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
#
|
|
58
|
-
#
|
|
103
|
+
# Defines both singular and plural methods for API endpoints.
|
|
104
|
+
#
|
|
105
|
+
# - The plural method fetches either paginated resources or a single resource if an ID is passed.
|
|
106
|
+
# - The singular method explicitly fetches a single resource by ID.
|
|
107
|
+
#
|
|
108
|
+
# @param method [Symbol] The plural method name (e.g., `:alerts`).
|
|
109
|
+
# @param url [String] The API endpoint URL path.
|
|
110
|
+
# @param singular_method [Symbol] The singular method name (e.g., `:alert`).
|
|
111
|
+
#
|
|
112
|
+
# == Example:
|
|
113
|
+
# define_singular_and_plural_methods(:alerts, "/common/v1/alerts", :alert)
|
|
114
|
+
#
|
|
115
|
+
# # Generates:
|
|
116
|
+
# # - alerts(params = {}) – Fetches paginated alerts.
|
|
117
|
+
# # - alerts(id, params = {}) – Fetches a single alert by ID.
|
|
118
|
+
# # - alert(id, params = {}) – Fetches a single alert explicitly by ID.
|
|
59
119
|
def self.define_singular_and_plural_methods(method, url, singular_method)
|
|
60
120
|
# Define plural method: paginated call if no ID, otherwise fetch singular resource.
|
|
61
121
|
define_method(method) do |id = nil, params = {}|
|
|
@@ -68,14 +128,32 @@ module Sophos
|
|
|
68
128
|
end
|
|
69
129
|
end
|
|
70
130
|
|
|
71
|
-
#
|
|
131
|
+
# Defines a paginated method for list-based API endpoints.
|
|
132
|
+
#
|
|
133
|
+
# @param method [Symbol] The method name (e.g., `:alerts`).
|
|
134
|
+
# @param url [String] The API endpoint URL path.
|
|
135
|
+
#
|
|
136
|
+
# == Example:
|
|
137
|
+
# define_paged_method(:alerts, "/common/v1/alerts")
|
|
138
|
+
#
|
|
139
|
+
# # Generates:
|
|
140
|
+
# # - alerts(params = {}) – Fetches paginated alerts.
|
|
72
141
|
def self.define_paged_method(method, url)
|
|
73
142
|
define_method(method) do |params = {}|
|
|
74
143
|
get_paged(url, params)
|
|
75
144
|
end
|
|
76
145
|
end
|
|
77
146
|
|
|
78
|
-
#
|
|
147
|
+
# Defines a simple, non-paginated API method.
|
|
148
|
+
#
|
|
149
|
+
# @param method [Symbol] The method name (e.g., `:roles`).
|
|
150
|
+
# @param url [String] The API endpoint URL path.
|
|
151
|
+
#
|
|
152
|
+
# == Example:
|
|
153
|
+
# define_plain_method(:roles, "/partner/v1/roles")
|
|
154
|
+
#
|
|
155
|
+
# # Generates:
|
|
156
|
+
# # - roles(params = {}) – Fetches roles without pagination.
|
|
79
157
|
def self.define_plain_method(method, url)
|
|
80
158
|
define_method(method) do |params = {}|
|
|
81
159
|
get(url, params)
|
data/lib/sophos/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: sophos_central_api
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.2.
|
|
4
|
+
version: 0.2.3
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Janco Tanis
|
|
8
8
|
bindir: exe
|
|
9
9
|
cert_chain: []
|
|
10
|
-
date: 2025-
|
|
10
|
+
date: 2025-11-04 00:00:00.000000000 Z
|
|
11
11
|
dependencies:
|
|
12
12
|
- !ruby/object:Gem::Dependency
|
|
13
13
|
name: faraday
|