abiraj 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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/abiraj/version.rb +1 -1
  3. data/lib/abiraj.rb +230 -94
  4. metadata +8 -8
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b5894ed4fd9f1f42f60427ef95452ad54a5933cd32c3454ea11a4674300946ea
4
- data.tar.gz: e1db6072dc4c4ce90613e11502b3f138ae36f061bbc3e05fea49bdbcd20ef7de
3
+ metadata.gz: 07dd79f7c92f15bf9aa690977178a66a52dfc9d7bb050721d3beee183b8b2de3
4
+ data.tar.gz: 64d625886da84d225c3ccdc8cf2e38af4835cb7fd55c7c7e02699fc54b7c2a0f
5
5
  SHA512:
6
- metadata.gz: fc1147d7ee3a4dec5bb05076b055d1460a30301d851c6f5d35bb8415bf28776d7405b3da36da58e45806b2e3cbd63fcf019a266b551b67c621caf354496f3fad
7
- data.tar.gz: a6e2dacfe433ad8c92bffe69645fa59af42f599abf0ac1d4ca240a065d2e982562af47568709381c2fc0df2ca28138991dff219161daee0d6f780f5532e150f7
6
+ metadata.gz: f75573ba26f415d651d21538fb32e1341d3c9ad417d3bd8e0c7fa63b2603866dd4c00817592e54811256e0f49f800392dc13dbdce9aa264588ead0366517350e
7
+ data.tar.gz: d8824ca8b7af97b590b80cff3d353373e11d013d9109f904f5f1b52f3a6353b6fd3556c59c1e3f8520abf6397eb9cdea319fc10c611da8f1bde431e146ac4ac2
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Abiraj
4
- VERSION = "0.1.1"
4
+ VERSION = "0.1.0"
5
5
  end
6
6
 
data/lib/abiraj.rb CHANGED
@@ -4,147 +4,283 @@ require "openssl"
4
4
 
5
5
  module Abiraj
6
6
  class Error < StandardError; end
7
-
8
- def self.logger(message:, level: "info")
9
- prefix = "[Abiraj] "
10
- message = message.to_s
11
- puts prefix + message
12
- case level.downcase
13
- when "error"
14
- Chef::Log.error(prefix + message)
15
- nil
16
- when "debug"
17
- Chef::Log.debug(prefix + message)
18
- when "warn"
19
- Chef::Log.warn(prefix + message)
20
- when "info"
21
- Chef::Log.info(prefix + message)
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
22
24
  end
23
25
  end
24
-
25
- class Account
26
+
27
+ class Init
26
28
  def initialize(server_url:, authtoken:, org: nil, certificate: nil)
27
- @init = false
28
29
  if server_url.nil? || authtoken.nil? || server_url.strip.empty? || authtoken.strip.empty?
29
- logger("Server URL and authtoken are required to initialize Abiraj.", "error")
30
+ Abiraj.log(message: "Server URL and authtoken are required to initialize Abiraj", level: "error")
31
+ return nil
30
32
  end
31
33
 
32
34
  unless server_url.start_with?("http://", "https://")
33
- logger("Invalid server URL. It must start with http:// or https://", "error")
34
- return
35
- end
36
- @server_url = server_url.strip
37
- @authtoken = authtoken.strip
38
- @org = org&.strip
39
- if certificate
40
- @certificate = certificate
35
+ Abiraj.log(message: "Invalid server URL. It must start with http:// or https://", level: "error")
36
+ return nil
41
37
  end
42
- @init = true
43
- logger("Abiraj initialized successfully.")
44
- end
45
38
 
46
- def get(account_id: nil, account_name: nil, account_title: nil)
47
- if !@init
48
- logger("Abiraj is not initialized.", "error")
49
- return
50
- end
51
- logger("Fetching account data.")
52
- params = {}
53
- params["account_id"] = account_id.to_s if account_id
54
- params["account_name"] = account_name unless account_name.to_s.strip.empty?
55
- params["account_title"] = account_title unless account_title.to_s.strip.empty?
56
- account_data = get_request(params, "/api/get_account_details_dict")
57
- if account_data
58
- logger("Account data fetched successfully.")
59
- return account_data
60
- else
61
- logger("Unable to fetch account data.", "error")
62
- end
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")
63
44
  end
45
+ end
64
46
 
65
- private
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
66
68
 
67
- def logger(message, level = "info")
68
- Abiraj.logger(message: message, level: level)
69
- if level == "error"
69
+ rescue StandardError => e
70
+ Abiraj.log(message: "Failed to fetch account data: #{e.message}", level: "error")
70
71
  return nil
71
72
  end
72
73
  end
73
74
 
75
+ protected
76
+
74
77
  def get_request(params, endpoint)
75
- logger("Raising API request to Abiraj server.")
76
-
77
- uri = URI(@server_url + endpoint)
78
+ Abiraj.log(message: "Raising API request to Abiraj server.")
79
+ uri = URI(Abiraj.server_url + endpoint)
78
80
  uri.query = URI.encode_www_form(params) unless params.empty?
79
81
  http = set_http(uri)
80
82
  request = Net::HTTP::Get.new(uri)
81
- request["authtoken"] = @authtoken
82
- if @org && !@org.strip.empty?
83
- request["org"] = @org
83
+ request["authtoken"] = Abiraj.authtoken
84
+ if Abiraj.org && !Abiraj.org.strip.empty?
85
+ request["org"] = Abiraj.org
84
86
  end
85
87
  begin
86
88
  response = http.request(request)
87
- if response.code.to_i == 200
88
- JSON.parse(response.body)
89
+ response = JSON.parse(response.body)
90
+ if response['status_code'] == 200
91
+ return response
89
92
  else
90
- logger("Unexpected response code #{response.code}: #{response.body}", "error")
93
+ Abiraj.log(message: "#{response['status_code']}: #{response['message']}", level: "error")
91
94
  end
92
95
  rescue JSON::ParserError => e
93
- logger("Failed to parse JSON response: #{e.message}", "error")
96
+ Abiraj.log(message: "Failed to parse JSON response: #{e.message}", level: "error")
97
+ return nil
94
98
  rescue StandardError => e
95
- logger("HTTP request failed: #{e.message}", "error")
99
+ Abiraj.log(message: "HTTP request failed: #{e.message}", level: "error")
100
+ return nil
96
101
  end
97
102
  end
98
103
 
99
104
  def set_http(uri)
100
105
  begin
101
- logger("Setting up HTTP connection.")
106
+ Abiraj.log(message: "Setting up HTTP connection.")
107
+
102
108
  http = Net::HTTP.new(uri.host, uri.port)
103
109
  http.use_ssl = uri.scheme == "https"
104
- http.verify_mode = OpenSSL::SSL::VERIFY_PEER
105
- http = set_ssl(http)
106
- return http
110
+
111
+ if uri.scheme == "https"
112
+ http.verify_mode = OpenSSL::SSL::VERIFY_PEER
113
+ configure_ssl(http, uri)
114
+ end
115
+ http
107
116
  rescue StandardError => e
108
- logger("Failed to set HTTP connection: #{e.message}", "error")
117
+ Abiraj.log(message: "Failed to set HTTP connection: #{e.message}", level: "error")
109
118
  return nil
110
119
  end
111
120
  end
112
121
 
113
- def set_ssl(http)
122
+ def configure_ssl(http, uri)
114
123
  begin
115
- if @server_url.start_with?("https://") && @certificate
116
- logger("Setting SSL connection with certificate.")
117
-
118
- if @certificate.start_with?("-----BEGIN CERTIFICATE-----")
119
- logger("Binding certificate value.")
120
-
121
- cert_object = OpenSSL::X509::Certificate.new(@certificate)
122
- http.cert_store = OpenSSL::X509::Store.new
123
- http.cert_store.add_cert(cert_object)
124
-
125
- elsif File.exist?(@certificate)
126
- logger("Binding certificate file.")
127
- http.ca_file = @certificate
128
-
124
+ if Abiraj.server_url.start_with?("https://")
125
+ if Abiraj.certificate
126
+ handle_ssl_certificate(http)
129
127
  else
130
- logger("Invalid certificate content or file path.", "error")
131
- return nil
128
+ Abiraj.log(message: "No SSL certificate provided. Attempting to fetch from server.")
129
+ fetch_and_set_server_certificate(http, uri)
132
130
  end
133
-
134
- elsif @server_url.start_with?("https://") && !@certificate
135
- logger("Disabling SSL verification. Certificate required for SSL secure connection", "warn")
136
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
137
131
  end
138
-
139
- return http
140
-
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
141
152
  rescue OpenSSL::X509::CertificateError => e
142
- logger("Invalid certificate format: #{e.message}", "error")
153
+ Abiraj.log(message: "Invalid certificate format: #{e.message}", level: "error")
143
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.")
144
169
  rescue StandardError => e
145
- logger("Failed to set SSL connection: #{e.message}", "error")
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")
146
180
  return nil
147
181
  end
148
- 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
149
285
  end
150
286
  end
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: abiraj
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
  - Abiraj
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-12-30 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
22
  - lib/abiraj/version.rb
23
- homepage:
23
+ homepage:
24
24
  licenses: []
25
25
  metadata: {}
26
- post_install_message:
26
+ post_install_message:
27
27
  rdoc_options: []
28
28
  require_paths:
29
29
  - lib
@@ -38,8 +38,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
38
38
  - !ruby/object:Gem::Version
39
39
  version: '0'
40
40
  requirements: []
41
- rubygems_version: 3.5.23
42
- signing_key:
41
+ rubygems_version: 3.0.3
42
+ signing_key:
43
43
  specification_version: 4
44
44
  summary: Abiraj Chef plugin tool to extend and securely manage secrets and credential.
45
45
  test_files: []