abiraj 0.1.0 → 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: d96e91447aac465771ed31896d6f99e959f61cca70f4bce689a48b15410feef6
4
- data.tar.gz: c3bbd736acae1d901bbe6a7185f57aabca7a454a57686a3f08607edf72a05fae
3
+ metadata.gz: 07dd79f7c92f15bf9aa690977178a66a52dfc9d7bb050721d3beee183b8b2de3
4
+ data.tar.gz: 64d625886da84d225c3ccdc8cf2e38af4835cb7fd55c7c7e02699fc54b7c2a0f
5
5
  SHA512:
6
- metadata.gz: c46a230e1a113a33e2103ca84a1bcb12782934b98f5968dc2ac826e8036ef020aaca4b7afc57f1cbf17463e7223ba81f3593dfaf4ec61a2407f7d80006e8562d
7
- data.tar.gz: 23be80da0ad17c335482ea27fdb147f83fe19d3a3d4dd8815fffa830e618d67488df0088a4233b727c8f60954c592acda65e958da4ce987d3cff7272650b5d28
6
+ metadata.gz: f75573ba26f415d651d21538fb32e1341d3c9ad417d3bd8e0c7fa63b2603866dd4c00817592e54811256e0f49f800392dc13dbdce9aa264588ead0366517350e
7
+ data.tar.gz: d8824ca8b7af97b590b80cff3d353373e11d013d9109f904f5f1b52f3a6353b6fd3556c59c1e3f8520abf6397eb9cdea319fc10c611da8f1bde431e146ac4ac2
data/lib/abiraj.rb CHANGED
@@ -1,122 +1,286 @@
1
- require "net/http"
2
- require "json"
3
- require 'openssl'
4
-
5
- module Abiraj
6
- class Error < StandardError; end
7
-
8
- def self.logger(message:, level: 'info')
9
- level = 'warn' if level == 'error'
10
- prefix = "[ABIRAJ] "
11
- if defined?(Chef) && Chef.const_defined?(:Log)
12
- case level.downcase
13
- when "error"
14
- Chef::Log.error(prefix + message)
15
- when "debug"
16
- Chef::Log.debug(prefix + message)
17
- when "warn"
18
- Chef::Log.warn(prefix + message)
19
- else
20
- Chef::Log.info(prefix + message)
21
- end
22
- end
23
- end
24
-
25
- class Account
26
- def initialize(server_url:, authtoken:, org: nil, certificate: nil)
27
- @init = false
28
-
29
- # Validate inputs
30
- if server_url.nil? || authtoken.nil? || server_url.strip.empty? || authtoken.strip.empty?
31
- Abiraj.logger(message: "Server URL and authtoken are required to initialize Abiraj.", level: "error")
32
- return
33
- end
34
-
35
- unless server_url.start_with?('http://', 'https://')
36
- Abiraj.logger(message: "Invalid server URL. It must start with http:// or https://", level: "error")
37
- return
38
- end
39
-
40
- # Load certificate if provided
41
- # if server_url.start_with?('https://') && certificate
42
- # begin
43
- # cert_content = File.read(certificate)
44
- # Abiraj.logger(message: "Loaded certificate content successfully.")
45
- # @certificate = OpenSSL::X509::Certificate.new(cert_content)
46
- # rescue StandardError => e
47
- # Abiraj.logger(message: "Failed to read or parse certificate: #{e.message}", level: "error")
48
- # return
49
- # end
50
- # end
51
-
52
- @server_url = server_url.strip
53
- @authtoken = authtoken.strip
54
- @org = org&.strip
55
- @certificate = certificate if certificate
56
- @init = true
57
- Abiraj.logger(message: "Abiraj initialized successfully.")
58
- end
59
-
60
- def get(account_id: nil, account_name: nil, account_title: nil)
61
- return log_and_return_nil("Abiraj is not initialized.") unless @init
62
-
63
- Abiraj.logger(message: "Fetching account data.")
64
-
65
- # Prepare request parameters
66
- params = {}
67
- params['account_id'] = account_id.to_s if account_id
68
- params['account_name'] = account_name unless account_name.to_s.strip.empty?
69
- params['account_title'] = account_title unless account_title.to_s.strip.empty?
70
-
71
- # Send GET request
72
- account_data = get_request(params, "/api/get_account_details_dict")
73
- if account_data
74
- Abiraj.logger(message: "Account data fetched successfully.")
75
- return account_data
76
- else
77
- log_and_return_nil("Unable to fetch account data.")
78
- end
79
- end
80
-
81
- private
82
-
83
- def log_and_return_nil(message)
84
- Abiraj.logger(message: message, level: "error")
85
- nil
86
- end
87
-
88
- def get_request(params, endpoint)
89
- Abiraj.logger(message: "Raising API request to Abiraj server.")
90
-
91
- uri = URI(@server_url + endpoint)
92
- uri.query = URI.encode_www_form(params) unless params.empty?
93
-
94
- http = Net::HTTP.new(uri.host, uri.port)
95
- http.use_ssl = uri.scheme == 'https'
96
- http.verify_mode = OpenSSL::SSL::VERIFY_PEER
97
-
98
- if http.use_ssl? && @certificate
99
- http.ca_file = @certificate
100
- end
101
-
102
- request = Net::HTTP::Get.new(uri)
103
- request['authtoken'] = @authtoken
104
- request['org'] = @org if @org
105
-
106
- begin
107
- response = http.request(request)
108
- Abiraj.logger(message: "HTTP response code: #{response.code}", level: "debug")
109
-
110
- if response.code.to_i == 200
111
- JSON.parse(response.body)
112
- else
113
- log_and_return_nil("Unexpected response code #{response.code}: #{response.body}")
114
- end
115
- rescue JSON::ParserError => e
116
- log_and_return_nil("Failed to parse JSON response: #{e.message}")
117
- rescue StandardError => e
118
- log_and_return_nil("HTTP request failed: #{e.message}")
119
- end
120
- end
121
- end
122
- end
1
+ require "net/http"
2
+ require "json"
3
+ require "openssl"
4
+
5
+ module Abiraj
6
+ class Error < StandardError; end
7
+
8
+ class << self
9
+ attr_accessor :server_url, :authtoken, :org, :certificate
10
+ def log(message:, level: "info")
11
+ prefix = "[Abiraj] "
12
+ message = message.to_s
13
+ case level.downcase
14
+ when "error"
15
+ Chef::Log.error(prefix + message)
16
+ nil
17
+ when "debug"
18
+ Chef::Log.debug(prefix + message)
19
+ when "warn"
20
+ Chef::Log.warn(prefix + message)
21
+ when "info"
22
+ Chef::Log.info(prefix + message)
23
+ end
24
+ end
25
+ end
26
+
27
+ class Init
28
+ def initialize(server_url:, authtoken:, org: nil, certificate: nil)
29
+ if server_url.nil? || authtoken.nil? || server_url.strip.empty? || authtoken.strip.empty?
30
+ Abiraj.log(message: "Server URL and authtoken are required to initialize Abiraj", level: "error")
31
+ return nil
32
+ end
33
+
34
+ unless server_url.start_with?("http://", "https://")
35
+ Abiraj.log(message: "Invalid server URL. It must start with http:// or https://", level: "error")
36
+ return nil
37
+ end
38
+
39
+ Abiraj.server_url = server_url.strip
40
+ Abiraj.authtoken = authtoken.strip
41
+ Abiraj.org = org unless org.to_s.strip.empty? || org.nil?
42
+ Abiraj.certificate = certificate
43
+ Abiraj.log(message: "Abiraj initialized successfully")
44
+ end
45
+ end
46
+
47
+ class Account
48
+ def self.get(account_id: nil, account_name: nil, account_title: nil)
49
+ begin
50
+ unless Abiraj.server_url && Abiraj.authtoken
51
+ Abiraj.log(message: "Abiraj is not initialized. Please initialize before using.", level: "error")
52
+ return nil
53
+ end
54
+
55
+ Abiraj.log(message: "Fetching account data")
56
+ params = {}
57
+ params["account_id"] = account_id.to_s if account_id
58
+ params["account_name"] = account_name unless account_name.to_s.strip.empty?
59
+ params["account_title"] = account_title unless account_title.to_s.strip.empty?
60
+ account = self.new.send(:get_request, params, "/api/get_account_details_dict")
61
+ if account
62
+ Abiraj.log(message: "Account data fetched successfully")
63
+ return account
64
+ else
65
+ Abiraj.log(message: "Unable to fetch account data.", level: "error")
66
+ return nil
67
+ end
68
+
69
+ rescue StandardError => e
70
+ Abiraj.log(message: "Failed to fetch account data: #{e.message}", level: "error")
71
+ return nil
72
+ end
73
+ end
74
+
75
+ protected
76
+
77
+ def get_request(params, endpoint)
78
+ Abiraj.log(message: "Raising API request to Abiraj server.")
79
+ uri = URI(Abiraj.server_url + endpoint)
80
+ uri.query = URI.encode_www_form(params) unless params.empty?
81
+ http = set_http(uri)
82
+ request = Net::HTTP::Get.new(uri)
83
+ request["authtoken"] = Abiraj.authtoken
84
+ if Abiraj.org && !Abiraj.org.strip.empty?
85
+ request["org"] = Abiraj.org
86
+ end
87
+ begin
88
+ response = http.request(request)
89
+ response = JSON.parse(response.body)
90
+ if response['status_code'] == 200
91
+ return response
92
+ else
93
+ Abiraj.log(message: "#{response['status_code']}: #{response['message']}", level: "error")
94
+ end
95
+ rescue JSON::ParserError => e
96
+ Abiraj.log(message: "Failed to parse JSON response: #{e.message}", level: "error")
97
+ return nil
98
+ rescue StandardError => e
99
+ Abiraj.log(message: "HTTP request failed: #{e.message}", level: "error")
100
+ return nil
101
+ end
102
+ end
103
+
104
+ def set_http(uri)
105
+ begin
106
+ Abiraj.log(message: "Setting up HTTP connection.")
107
+
108
+ http = Net::HTTP.new(uri.host, uri.port)
109
+ http.use_ssl = uri.scheme == "https"
110
+
111
+ if uri.scheme == "https"
112
+ http.verify_mode = OpenSSL::SSL::VERIFY_PEER
113
+ configure_ssl(http, uri)
114
+ end
115
+ http
116
+ rescue StandardError => e
117
+ Abiraj.log(message: "Failed to set HTTP connection: #{e.message}", level: "error")
118
+ return nil
119
+ end
120
+ end
121
+
122
+ def configure_ssl(http, uri)
123
+ begin
124
+ if Abiraj.server_url.start_with?("https://")
125
+ if Abiraj.certificate
126
+ handle_ssl_certificate(http)
127
+ else
128
+ Abiraj.log(message: "No SSL certificate provided. Attempting to fetch from server.")
129
+ fetch_and_set_server_certificate(http, uri)
130
+ end
131
+ end
132
+ rescue StandardError => e
133
+ Abiraj.log(message: "Failed to configure SSL: #{e.message}", level: "error")
134
+ return nil
135
+ end
136
+ end
137
+
138
+ def handle_ssl_certificate(http)
139
+ begin
140
+ Abiraj.log(message: "Adding SSL certificate.")
141
+ if Abiraj.certificate.start_with?("-----BEGIN CERTIFICATE-----")
142
+ cert_object = OpenSSL::X509::Certificate.new(Abiraj.certificate)
143
+ http.cert_store = OpenSSL::X509::Store.new
144
+ http.cert_store.add_cert(cert_object)
145
+ elsif File.exist?(Abiraj.certificate)
146
+ http.ca_file = Abiraj.certificate
147
+ else
148
+ Abiraj.log(message: "Invalid certificate value or file path", level: "error")
149
+ return nil
150
+ end
151
+ http
152
+ rescue OpenSSL::X509::CertificateError => e
153
+ Abiraj.log(message: "Invalid certificate format: #{e.message}", level: "error")
154
+ return nil
155
+ end
156
+ end
157
+
158
+ def fetch_and_set_server_certificate(http, uri)
159
+ begin
160
+ tcp_socket = TCPSocket.new(uri.host, uri.port || 443)
161
+ ssl_socket = OpenSSL::SSL::SSLSocket.new(tcp_socket)
162
+ ssl_socket.connect
163
+ server_cert = ssl_socket.peer_cert
164
+ ssl_socket.close
165
+ tcp_socket.close
166
+ http.cert_store = OpenSSL::X509::Store.new
167
+ http.cert_store.add_cert(server_cert)
168
+ Abiraj.log(message: "Certificate fetched and added to HTTP connection.")
169
+ rescue StandardError => e
170
+ Abiraj.log(message: "Failed to fetch SSL certificate: #{e.message}. Disabling SSL verification.", level: "warn")
171
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
172
+ end
173
+ end
174
+ end
175
+
176
+ class Accounts
177
+ def self.get(account_ids: [])
178
+ if Abiraj.server_url.nil? || Abiraj.authtoken.nil? || Abiraj.server_url.strip.empty? || Abiraj.authtoken.strip.empty?
179
+ Abiraj.log(message: "Abiraj is not initialized. Please initialize before using.", level: "error")
180
+ return nil
181
+ end
182
+ securden_accounts = self.new.send(:post_request, account_ids, "/api/get_multiple_accounts_details")
183
+ end
184
+
185
+ protected
186
+
187
+ def post_request(data, request_path)
188
+ Abiraj.log(message: "Raising API request to Abiraj server.")
189
+ payload = { "account_ids": data }
190
+ uri = URI(Abiraj.server_url + request_path)
191
+ http = set_http(uri)
192
+ request = Net::HTTP::Post.new(uri)
193
+ request["authtoken"] = Abiraj.authtoken
194
+ request["Content-Type"] = "application/json" # Set the content type for JSON data
195
+ request["org"] = Abiraj.org if Abiraj.org && !Abiraj.org.strip.empty?
196
+ request.body = payload.to_json unless payload.nil? || payload.empty?
197
+
198
+ begin
199
+ response = http.request(request)
200
+ response = JSON.parse(response.body)
201
+ if response['status_code'] == 200
202
+ return response
203
+ else
204
+ Abiraj.log(message: "#{response['status_code']}: #{response['message']}", level: "error")
205
+ end
206
+ rescue JSON::ParserError => e
207
+ Abiraj.log(message: "Failed to parse JSON response: #{e.message}", level: "error")
208
+ return nil
209
+ rescue StandardError => e
210
+ Abiraj.log(message: "HTTP request failed: #{e.message}", level: "error")
211
+ return nil
212
+ end
213
+ end
214
+
215
+ def set_http(uri)
216
+ begin
217
+ Abiraj.log(message: "Setting up HTTP connection.")
218
+
219
+ http = Net::HTTP.new(uri.host, uri.port)
220
+ http.use_ssl = uri.scheme == "https"
221
+
222
+ if uri.scheme == "https"
223
+ http.verify_mode = OpenSSL::SSL::VERIFY_PEER
224
+ configure_ssl(http, uri)
225
+ end
226
+ http
227
+ rescue StandardError => e
228
+ Abiraj.log(message: "Failed to set HTTP connection: #{e.message}", level: "error")
229
+ return nil
230
+ end
231
+ end
232
+
233
+ def configure_ssl(http, uri)
234
+ begin
235
+ if Abiraj.server_url.start_with?("https://")
236
+ if Abiraj.certificate
237
+ handle_ssl_certificate(http)
238
+ else
239
+ Abiraj.log(message: "No SSL certificate provided. Attempting to fetch from server.")
240
+ fetch_and_set_server_certificate(http, uri)
241
+ end
242
+ end
243
+ rescue StandardError => e
244
+ Abiraj.log(message: "Failed to configure SSL: #{e.message}", level: "error")
245
+ return nil
246
+ end
247
+ end
248
+
249
+ def handle_ssl_certificate(http)
250
+ begin
251
+ Abiraj.log(message: "Adding SSL certificate.")
252
+ if Abiraj.certificate.start_with?("-----BEGIN CERTIFICATE-----")
253
+ cert_object = OpenSSL::X509::Certificate.new(Abiraj.certificate)
254
+ http.cert_store = OpenSSL::X509::Store.new
255
+ http.cert_store.add_cert(cert_object)
256
+ elsif File.exist?(Abiraj.certificate)
257
+ http.ca_file = Abiraj.certificate
258
+ else
259
+ Abiraj.log(message: "Invalid certificate value or file path", level: "error")
260
+ return nil
261
+ end
262
+ http
263
+ rescue OpenSSL::X509::CertificateError => e
264
+ Abiraj.log(message: "Invalid certificate format: #{e.message}", level: "error")
265
+ return nil
266
+ end
267
+ end
268
+
269
+ def fetch_and_set_server_certificate(http, uri)
270
+ begin
271
+ tcp_socket = TCPSocket.new(uri.host, uri.port || 443)
272
+ ssl_socket = OpenSSL::SSL::SSLSocket.new(tcp_socket)
273
+ ssl_socket.connect
274
+ server_cert = ssl_socket.peer_cert
275
+ ssl_socket.close
276
+ tcp_socket.close
277
+ http.cert_store = OpenSSL::X509::Store.new
278
+ http.cert_store.add_cert(server_cert)
279
+ Abiraj.log(message: "Certificate fetched and added to HTTP connection.")
280
+ rescue StandardError => e
281
+ Abiraj.log(message: "Failed to fetch SSL certificate: #{e.message}. Disabling SSL verification.", level: "warn")
282
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
283
+ end
284
+ end
285
+ end
286
+ end
metadata CHANGED
@@ -1,25 +1,25 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: abiraj
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Abiraj
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-12-27 00:00:00.000000000 Z
11
+ date: 2025-01-01 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: This plugin sends a GET request to an API to fetch account data (passwords,
14
14
  etc.) based on the account ID, name, and title.
15
15
  email:
16
- - contact@abiraj.com
16
+ - devops-support@abiraj.com
17
17
  executables: []
18
18
  extensions: []
19
19
  extra_rdoc_files: []
20
20
  files:
21
21
  - lib/abiraj.rb
22
- - lib/securden/version.rb
22
+ - lib/abiraj/version.rb
23
23
  homepage:
24
24
  licenses: []
25
25
  metadata: {}
File without changes