ibm_cloud_sdk_core 1.0.1 → 1.1.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 +4 -4
- data/lib/ibm_cloud_sdk_core/base_service.rb +11 -9
- data/lib/ibm_cloud_sdk_core/utils.rb +20 -3
- data/lib/ibm_cloud_sdk_core/version.rb +1 -1
- data/test/unit/test_base_service.rb +108 -2
- data/test/unit/test_utils.rb +26 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 632fa6196faf266c004867a8002a4dc159aa652d9dd26144975a987cb9b0e7c0
|
4
|
+
data.tar.gz: 4d555fd04a96095a72e01dd78be693c66bb0e04e36f4307ee211042256960fe8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bea320e17520b61c97bd2b48cb230a401fed62476749250779622598d5a40242f1b186fafee50e4829257d356b528b536c78010f389073e731619589ab985f22
|
7
|
+
data.tar.gz: 106b79e7a30871c74c84ad47ae8d17cbaa306dd98dc1174f06a23a471865cea42954d06030fd301174cac7ab868fdd76abd19db54bb4840eca47103b1b3e58f8
|
@@ -39,16 +39,11 @@ module IBMCloudSdkCore
|
|
39
39
|
|
40
40
|
raise ArgumentError.new("authenticator must be provided") if @authenticator.nil?
|
41
41
|
|
42
|
-
if @service_name && !@service_url
|
43
|
-
config = get_service_properties(@service_name)
|
44
|
-
@service_url = config[:url] unless config.nil?
|
45
|
-
end
|
46
|
-
|
47
|
-
configure_http_client(disable_ssl_verification: @disable_ssl_verification)
|
48
|
-
@temp_headers = {}
|
49
42
|
@conn = HTTP::Client.new(
|
50
43
|
headers: {}
|
51
44
|
).use normalize_uri: { normalizer: NORMALIZER }
|
45
|
+
configure_service(@service_name)
|
46
|
+
@temp_headers = {}
|
52
47
|
end
|
53
48
|
|
54
49
|
def disable_ssl_verification=(disable_ssl_verification)
|
@@ -61,6 +56,15 @@ module IBMCloudSdkCore
|
|
61
56
|
headers.each_pair { |k, v| @conn.default_options.headers.add(k, v) }
|
62
57
|
end
|
63
58
|
|
59
|
+
def configure_service(service_name)
|
60
|
+
config = get_service_properties(service_name) if service_name
|
61
|
+
|
62
|
+
@service_url = config[:url] unless config.nil? || config[:url].nil?
|
63
|
+
disable_ssl_verification = explicitly_true(config[:disable_ssl]) unless config.nil? || config[:disable_ssl].nil?
|
64
|
+
# configure the http client if ssl is disabled
|
65
|
+
configure_http_client(disable_ssl_verification: disable_ssl_verification) if disable_ssl_verification
|
66
|
+
end
|
67
|
+
|
64
68
|
# @return [DetailedResponse]
|
65
69
|
def request(args)
|
66
70
|
defaults = { method: nil, url: nil, accept_json: false, headers: nil, params: nil, json: {}, data: nil }
|
@@ -143,8 +147,6 @@ module IBMCloudSdkCore
|
|
143
147
|
ssl_context = OpenSSL::SSL::SSLContext.new
|
144
148
|
ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
145
149
|
@conn.default_options = { ssl_context: ssl_context }
|
146
|
-
|
147
|
-
@token_manager&.ssl_verification(true)
|
148
150
|
end
|
149
151
|
add_proxy(proxy) unless proxy.empty? || !proxy.dig(:address).is_a?(String) || !proxy.dig(:port).is_a?(Integer)
|
150
152
|
add_timeout(timeout) unless timeout.empty? || (!timeout.key?(:per_operation) && !timeout.key?(:global))
|
@@ -16,6 +16,13 @@ def check_bad_first_or_last_char(str)
|
|
16
16
|
return str.start_with?("{", "\"") || str.end_with?("}", "\"") unless str.nil?
|
17
17
|
end
|
18
18
|
|
19
|
+
# checks if the provided value is truthy
|
20
|
+
def explicitly_true(value)
|
21
|
+
return value.to_s.casecmp("true").zero? unless value.nil?
|
22
|
+
|
23
|
+
false
|
24
|
+
end
|
25
|
+
|
19
26
|
# Initiates the credentials based on the credential file
|
20
27
|
def load_from_credential_file(service_name, separator = "=")
|
21
28
|
credential_file_path = ENV["IBM_CREDENTIALS_FILE"]
|
@@ -63,10 +70,20 @@ def load_from_vcap_services(service_name)
|
|
63
70
|
vcap_services = ENV["VCAP_SERVICES"]
|
64
71
|
unless vcap_services.nil?
|
65
72
|
services = JSON.parse(vcap_services)
|
73
|
+
credentials = ""
|
74
|
+
# search for matching inner name value
|
75
|
+
services.each do |_key, val|
|
76
|
+
service = val.detect { |item| item["name"] == service_name }
|
77
|
+
credentials = service["credentials"] unless service.nil?
|
78
|
+
break unless credentials.nil? || credentials.empty?
|
79
|
+
end
|
66
80
|
config = {}
|
67
|
-
|
68
|
-
|
69
|
-
|
81
|
+
# if no matching inner key is found, then search outer keys
|
82
|
+
if credentials.nil? || credentials.empty?
|
83
|
+
credentials = services[service_name][0]["credentials"] if services.key?(service_name) && !services[service_name].empty?
|
84
|
+
return config if credentials.nil? || credentials.empty?
|
85
|
+
end
|
86
|
+
# store credentials
|
70
87
|
credentials.each do |key, val|
|
71
88
|
config.store(key.to_sym, val)
|
72
89
|
end
|
@@ -26,6 +26,7 @@ class BaseServiceTest < Minitest::Test
|
|
26
26
|
assert_raises do
|
27
27
|
IBMCloudSdkCore::ConfigBasedAuthenticatorFactory.new.get_authenticator(service_name: "wrong")
|
28
28
|
end
|
29
|
+
ENV.delete("IBM_CREDENTIALS_FILE")
|
29
30
|
end
|
30
31
|
|
31
32
|
def test_wrong_url
|
@@ -130,6 +131,7 @@ class BaseServiceTest < Minitest::Test
|
|
130
131
|
service = IBMCloudSdkCore::BaseService.new(service_name: "leo_messi", url: "some.url", authenticator: authenticator)
|
131
132
|
assert_equal(authenticator.authentication_type, "bearerToken")
|
132
133
|
refute_nil(service)
|
134
|
+
ENV.delete("IBM_CREDENTIALS_FILE")
|
133
135
|
end
|
134
136
|
|
135
137
|
def test_vcap_services
|
@@ -138,6 +140,7 @@ class BaseServiceTest < Minitest::Test
|
|
138
140
|
service = IBMCloudSdkCore::BaseService.new(service_name: "salah", authenticator: authenticator)
|
139
141
|
assert_equal(authenticator.username, "mo")
|
140
142
|
assert_equal(service.service_name, "salah")
|
143
|
+
ENV.delete("VCAP_SERVICES")
|
141
144
|
end
|
142
145
|
|
143
146
|
def test_dummy_request
|
@@ -149,9 +152,11 @@ class BaseServiceTest < Minitest::Test
|
|
149
152
|
}
|
150
153
|
).to_return(status: 200, body: "", headers: {})
|
151
154
|
authenticator = IBMCloudSdkCore::ConfigBasedAuthenticatorFactory.new.get_authenticator(service_name: "salah")
|
152
|
-
service = IBMCloudSdkCore::BaseService.new(service_name: "salah", authenticator: authenticator
|
155
|
+
service = IBMCloudSdkCore::BaseService.new(service_name: "salah", authenticator: authenticator)
|
156
|
+
service.service_url = "https://we.the.best"
|
153
157
|
service_response = service.request(method: "GET", url: "/music", headers: {})
|
154
158
|
assert_equal("", service_response.result)
|
159
|
+
ENV.delete("VCAP_SERVICES")
|
155
160
|
end
|
156
161
|
|
157
162
|
def test_dummy_request_form_data
|
@@ -192,10 +197,12 @@ class BaseServiceTest < Minitest::Test
|
|
192
197
|
}
|
193
198
|
).to_return(status: 500, body: response.to_json, headers: {})
|
194
199
|
authenticator = IBMCloudSdkCore::ConfigBasedAuthenticatorFactory.new.get_authenticator(service_name: "salah")
|
195
|
-
service = IBMCloudSdkCore::BaseService.new(service_name: "salah", authenticator: authenticator
|
200
|
+
service = IBMCloudSdkCore::BaseService.new(service_name: "salah", authenticator: authenticator)
|
201
|
+
service.service_url = "https://we.the.best"
|
196
202
|
assert_raises IBMCloudSdkCore::ApiException do
|
197
203
|
service.request(method: "GET", url: "/music", headers: {})
|
198
204
|
end
|
205
|
+
ENV.delete("VCAP_SERVICES")
|
199
206
|
end
|
200
207
|
|
201
208
|
def test_dummy_request_icp
|
@@ -225,4 +232,103 @@ class BaseServiceTest < Minitest::Test
|
|
225
232
|
service_response = service.request(method: "GET", url: "/music", headers: {})
|
226
233
|
assert_equal(response, service_response.result)
|
227
234
|
end
|
235
|
+
|
236
|
+
def test_configure_service_using_credentials_in_env
|
237
|
+
file_path = File.join(File.dirname(__FILE__), "../../resources/ibm-credentials.env")
|
238
|
+
ENV["IBM_CREDENTIALS_FILE"] = file_path
|
239
|
+
|
240
|
+
authenticator = IBMCloudSdkCore::BasicAuthenticator.new(
|
241
|
+
username: "apikey",
|
242
|
+
password: "icp-xyz"
|
243
|
+
)
|
244
|
+
service = IBMCloudSdkCore::BaseService.new(
|
245
|
+
authenticator: authenticator,
|
246
|
+
service_name: "visual_recognition"
|
247
|
+
)
|
248
|
+
service.configure_service("visual_recognition")
|
249
|
+
|
250
|
+
assert_equal("https://gateway.ronaldo.com", service.service_url)
|
251
|
+
assert(service.disable_ssl_verification)
|
252
|
+
ENV.delete("IBM_CREDENTIALS_FILE")
|
253
|
+
end
|
254
|
+
|
255
|
+
def test_configure_service_using_credentials_in_vcap_match_outer_key
|
256
|
+
authenticator = IBMCloudSdkCore::BasicAuthenticator.new(
|
257
|
+
username: "apikey",
|
258
|
+
password: "icp-xyz"
|
259
|
+
)
|
260
|
+
service = IBMCloudSdkCore::BaseService.new(
|
261
|
+
authenticator: authenticator,
|
262
|
+
service_name: "key_to_service_entry_1"
|
263
|
+
)
|
264
|
+
ENV["VCAP_SERVICES"] = JSON.parse(File.read(Dir.getwd + "/resources/vcap-testing.json")).to_json
|
265
|
+
service.configure_service("key_to_service_entry_1")
|
266
|
+
|
267
|
+
assert_equal("https://on.the.toolchainplatform.net/devops-insights/api", service.service_url)
|
268
|
+
ENV.delete("VCAP_SERVICES")
|
269
|
+
end
|
270
|
+
|
271
|
+
def test_configure_service_using_credentials_in_vcap_match_inner_key
|
272
|
+
authenticator = IBMCloudSdkCore::BasicAuthenticator.new(
|
273
|
+
username: "apikey",
|
274
|
+
password: "icp-xyz"
|
275
|
+
)
|
276
|
+
service = IBMCloudSdkCore::BaseService.new(
|
277
|
+
authenticator: authenticator,
|
278
|
+
service_name: "devops_insights"
|
279
|
+
)
|
280
|
+
ENV["VCAP_SERVICES"] = JSON.parse(File.read(Dir.getwd + "/resources/vcap-testing.json")).to_json
|
281
|
+
service.configure_service("devops_insights")
|
282
|
+
|
283
|
+
assert_equal("https://ibmtesturl.net/devops_insights/api", service.service_url)
|
284
|
+
ENV.delete("VCAP_SERVICES")
|
285
|
+
end
|
286
|
+
|
287
|
+
def test_configure_service_using_credentials_in_vcap_no_matching_key
|
288
|
+
authenticator = IBMCloudSdkCore::BasicAuthenticator.new(
|
289
|
+
username: "apikey",
|
290
|
+
password: "icp-xyz"
|
291
|
+
)
|
292
|
+
service = IBMCloudSdkCore::BaseService.new(
|
293
|
+
authenticator: authenticator,
|
294
|
+
service_name: "different_name_two"
|
295
|
+
)
|
296
|
+
ENV["VCAP_SERVICES"] = JSON.parse(File.read(Dir.getwd + "/resources/vcap-testing.json")).to_json
|
297
|
+
service.configure_service("different_name_two")
|
298
|
+
|
299
|
+
assert_equal("https://gateway.watsonplatform.net/different-name-two/api", service.service_url)
|
300
|
+
ENV.delete("VCAP_SERVICES")
|
301
|
+
end
|
302
|
+
|
303
|
+
def test_configure_service_using_credentials_in_vcap_empty_entry
|
304
|
+
authenticator = IBMCloudSdkCore::BasicAuthenticator.new(
|
305
|
+
username: "apikey",
|
306
|
+
password: "icp-xyz"
|
307
|
+
)
|
308
|
+
service = IBMCloudSdkCore::BaseService.new(
|
309
|
+
authenticator: authenticator,
|
310
|
+
service_name: "empty_service"
|
311
|
+
)
|
312
|
+
ENV["VCAP_SERVICES"] = JSON.parse(File.read(Dir.getwd + "/resources/vcap-testing.json")).to_json
|
313
|
+
service.configure_service("empty_service")
|
314
|
+
|
315
|
+
assert_nil(service.service_url)
|
316
|
+
ENV.delete("VCAP_SERVICES")
|
317
|
+
end
|
318
|
+
|
319
|
+
def test_configure_service_using_credentials_in_vcap_no_creds
|
320
|
+
authenticator = IBMCloudSdkCore::BasicAuthenticator.new(
|
321
|
+
username: "apikey",
|
322
|
+
password: "icp-xyz"
|
323
|
+
)
|
324
|
+
service = IBMCloudSdkCore::BaseService.new(
|
325
|
+
authenticator: authenticator,
|
326
|
+
service_name: "no-creds-service-two"
|
327
|
+
)
|
328
|
+
ENV["VCAP_SERVICES"] = JSON.parse(File.read(Dir.getwd + "/resources/vcap-testing.json")).to_json
|
329
|
+
service.configure_service("no-creds-service-two")
|
330
|
+
|
331
|
+
assert_nil(service.service_url)
|
332
|
+
ENV.delete("VCAP_SERVICES")
|
333
|
+
end
|
228
334
|
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require("json")
|
4
|
+
require("jwt")
|
5
|
+
require_relative("./../test_helper.rb")
|
6
|
+
require_relative("./../../lib/ibm_cloud_sdk_core/authenticators/basic_authenticator")
|
7
|
+
require_relative("./../../lib/ibm_cloud_sdk_core/authenticators/config_based_authenticator_factory")
|
8
|
+
require("webmock/minitest")
|
9
|
+
|
10
|
+
WebMock.disable_net_connect!(allow_localhost: true)
|
11
|
+
|
12
|
+
# Unit tests for the utility methods
|
13
|
+
class UtilsTest < Minitest::Test
|
14
|
+
def test_explicitly_true
|
15
|
+
assert(explicitly_true("true"))
|
16
|
+
assert(explicitly_true("True"))
|
17
|
+
assert(explicitly_true(true))
|
18
|
+
|
19
|
+
assert_equal(explicitly_true("false"), false)
|
20
|
+
assert_equal(explicitly_true("False"), false)
|
21
|
+
assert_equal(explicitly_true("someothervalue"), false)
|
22
|
+
assert_equal(explicitly_true(""), false)
|
23
|
+
assert_equal(explicitly_true(false), false)
|
24
|
+
assert_equal(explicitly_true(nil), false)
|
25
|
+
end
|
26
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ibm_cloud_sdk_core
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mamoon Raja
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-11-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: concurrent-ruby
|
@@ -253,6 +253,7 @@ files:
|
|
253
253
|
- test/unit/test_iam_authenticator.rb
|
254
254
|
- test/unit/test_iam_token_manager.rb
|
255
255
|
- test/unit/test_jwt_token_manager.rb
|
256
|
+
- test/unit/test_utils.rb
|
256
257
|
homepage: https://www.github.com/IBM
|
257
258
|
licenses:
|
258
259
|
- Apache-2.0
|