ibm_cloud_sdk_core 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b18d557fd57fed55fa59dc6be40502ed3cd5cb5dd38927b1e0086191056412a1
4
- data.tar.gz: 5d4a983f01f4d9eeaa10dc6970683048ef7b3a0996f9f6de43c1832f04618a4f
3
+ metadata.gz: 98e7c5e7a5e78cc4f561a8b7fddbe026b201d5c18365354236f8b7cd185e2e2e
4
+ data.tar.gz: 97490e5521edbc772cd9a1932099ac4243ed70a2b712813e9cd4d1ef65e77073
5
5
  SHA512:
6
- metadata.gz: 95eb85a507326b86317e5a3209e3086ce1e27f6a849d0d19148b83847da61d71227f12f66268f540b55d0c90c55e052739dc29eb8a86e331a9b783c43062e4ee
7
- data.tar.gz: ca617c999050ed7e2dca0583e1d62ae018b898ec46feb021875c916433d390a351f1448fbb9d310baa57dc2dfda95ad889c205663ffe4619913a771305d46735
6
+ metadata.gz: e6bb9065154371294ed94a910c3e3400f8b41723f4fcee4f6656f8ab8e015c5a2817ff9503e23176ab59ee9d7e92e530274c94f4a8cb376e1293aec6ac681a45
7
+ data.tar.gz: 177559d12ccfe34ad297774558e1c6d2ac2139a97fd66f71ece0c736b61a1806b12f4a60ca9fc3008957ada9a2b81e0b050e7cf01f8aeb8362e795b291ef18bc
@@ -243,6 +243,8 @@ module IBMCloudSdkCore
243
243
  ssl_context = OpenSSL::SSL::SSLContext.new
244
244
  ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE
245
245
  @conn.default_options = { ssl_context: ssl_context }
246
+
247
+ @token_manager&.ssl_verification(true)
246
248
  end
247
249
  add_proxy(proxy) unless proxy.empty? || !proxy.dig(:address).is_a?(String) || !proxy.dig(:port).is_a?(Integer)
248
250
  add_timeout(timeout) unless timeout.empty? || (!timeout.key?(:per_operation) && !timeout.key?(:global))
@@ -17,6 +17,7 @@ module IBMCloudSdkCore
17
17
  DEFAULT_CLIENT_SECRET = "bx"
18
18
  REQUEST_TOKEN_GRANT_TYPE = "urn:ibm:params:oauth:grant-type:apikey"
19
19
  REQUEST_TOKEN_RESPONSE_TYPE = "cloud_iam"
20
+ TOKEN_NAME = "access_token"
20
21
 
21
22
  attr_accessor :token_info, :user_access_token
22
23
  def initialize(iam_apikey: nil, iam_access_token: nil, iam_url: nil,
@@ -24,7 +25,7 @@ module IBMCloudSdkCore
24
25
  @iam_apikey = iam_apikey
25
26
  @user_access_token = iam_access_token
26
27
  @iam_url = iam_url.nil? ? DEFAULT_IAM_URL : iam_url
27
- super(url: iam_url, access_token: iam_access_token)
28
+ super(url: iam_url, access_token: iam_access_token, token_name: TOKEN_NAME)
28
29
 
29
30
  # Both the client id and secret should be provided or neither should be provided.
30
31
  if !iam_client_id.nil? && !iam_client_secret.nil?
@@ -9,13 +9,14 @@ require_relative("./jwt_token_manager")
9
9
  module IBMCloudSdkCore
10
10
  # Class to manage ICP4D Token Authentication
11
11
  class ICP4DTokenManager < JWTTokenManager
12
+ TOKEN_NAME = "accessToken"
12
13
  def initialize(url: nil, username: nil, password: nil, access_token: nil)
13
14
  raise ArgumentError.new("The url is mandatory for ICP4D.") if url.nil? && access_token.nil?
14
15
 
15
16
  url += "/v1/preauth/validateAuth"
16
17
  @username = username
17
18
  @password = password
18
- super(url: url, user_access_token: access_token)
19
+ super(url: url, user_access_token: access_token, token_name: TOKEN_NAME)
19
20
  end
20
21
 
21
22
  def request_token
@@ -6,8 +6,6 @@ require("jwt")
6
6
  require("rbconfig")
7
7
  require_relative("./version.rb")
8
8
 
9
- TOKEN_NAME = "access_token"
10
-
11
9
  module IBMCloudSdkCore
12
10
  # Class to manage JWT Token Authentication
13
11
  class JWTTokenManager
@@ -22,6 +20,7 @@ module IBMCloudSdkCore
22
20
  @url = vars[:url]
23
21
  @token_info = vars[:token_info]
24
22
  @user_access_token = vars[:access_token]
23
+ @token_name = vars[:token_name]
25
24
  @time_to_live = nil
26
25
  @expire_time = nil
27
26
  @disable_ssl_verification = false
@@ -33,9 +32,9 @@ module IBMCloudSdkCore
33
32
  elsif @token_info.nil? || token_expired?
34
33
  token_info = request_token
35
34
  save_token_info(token_info: token_info)
36
- @token_info[TOKEN_NAME]
35
+ @token_info[@token_name]
37
36
  elsif !@token_info.nil?
38
- @token_info[TOKEN_NAME]
37
+ @token_info[@token_name]
39
38
  end
40
39
  end
41
40
 
@@ -43,6 +42,10 @@ module IBMCloudSdkCore
43
42
  @user_access_token = access_token
44
43
  end
45
44
 
45
+ def ssl_verification(disable_ssl_verification)
46
+ @disable_ssl_verification = disable_ssl_verification
47
+ end
48
+
46
49
  private
47
50
 
48
51
  # Check if currently stored token is expired.
@@ -59,7 +62,7 @@ module IBMCloudSdkCore
59
62
  end
60
63
 
61
64
  def save_token_info(token_info: nil)
62
- access_token = token_info[TOKEN_NAME]
65
+ access_token = token_info[@token_name]
63
66
  decoded_response = JWT.decode access_token, nil, false, {}
64
67
  exp = decoded_response[0]["exp"]
65
68
  iat = decoded_response[0]["iat"]
@@ -69,13 +72,26 @@ module IBMCloudSdkCore
69
72
  end
70
73
 
71
74
  def request(method:, url:, headers: nil, params: nil, data: nil, username: nil, password: nil)
72
- response = HTTP.basic_auth(user: username, pass: password).request(
73
- method,
74
- url,
75
- body: data,
76
- headers: headers,
77
- params: params
78
- )
75
+ if @disable_ssl_verification
76
+ ssl_context = OpenSSL::SSL::SSLContext.new
77
+ ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE
78
+ response = HTTP.basic_auth(user: username, pass: password).request(
79
+ method,
80
+ url,
81
+ body: data,
82
+ headers: headers,
83
+ params: params,
84
+ ssl_context: ssl_context
85
+ )
86
+ else
87
+ response = HTTP.basic_auth(user: username, pass: password).request(
88
+ method,
89
+ url,
90
+ body: data,
91
+ headers: headers,
92
+ params: params
93
+ )
94
+ end
79
95
  return JSON.parse(response.body.to_s) if (200..299).cover?(response.code)
80
96
 
81
97
  require_relative("./api_exception.rb")
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module IBMCloudSdkCore
4
- VERSION = "0.3.0"
4
+ VERSION = "0.3.1"
5
5
  end
@@ -351,4 +351,27 @@ class BaseServiceTest < Minitest::Test
351
351
  token_manager = service.instance_variable_get(:@token_manager)
352
352
  assert_equal(token_manager.instance_variable_get(:@user_access_token), "new_token")
353
353
  end
354
+
355
+ def test_icp4d_disable_ssl
356
+ service = IBMCloudSdkCore::BaseService.new(
357
+ authentication_type: "icp4d",
358
+ icp4d_url: "https://the.sixth.one",
359
+ icp4d_access_token: "token",
360
+ url: "http://the.com"
361
+ )
362
+ stub_request(:get, "http://the.com/music")
363
+ .with(
364
+ headers: {
365
+ "Authorization" => "Basic Og==",
366
+ "Host" => "the.com"
367
+ }
368
+ ).to_return(status: 200, body: {}.to_json, headers: {})
369
+ assert_equal(service.instance_variable_get(:@icp4d_access_token), "token")
370
+ service.send :icp4d_token_manager, icp4d_access_token: "new_token", icp4d_url: "the.url"
371
+ token_manager = service.instance_variable_get(:@token_manager)
372
+ service.configure_http_client(disable_ssl_verification: true)
373
+ assert_equal(token_manager.instance_variable_get(:@disable_ssl_verification), true)
374
+ service_response = token_manager.send :request, method: "GET", url: "http://the.com/music", headers: {}
375
+ assert_equal({}, service_response)
376
+ end
354
377
  end
@@ -85,7 +85,7 @@ class JWTTokenManagerTest < Minitest::Test
85
85
  access_token = JWT.encode(access_token_layout, "secret", "HS256", "kid": "230498151c214b788dd97f22b85410a5")
86
86
 
87
87
  token = {
88
- "access_token" => access_token,
88
+ "accessToken" => access_token,
89
89
  "token_type" => "Bearer",
90
90
  "expires_in" => 3600,
91
91
  "expiration" => Time.now.to_i + (6 * 3600),
@@ -95,7 +95,8 @@ class JWTTokenManagerTest < Minitest::Test
95
95
  token_manager = IBMCloudSdkCore::JWTTokenManager.new(
96
96
  icp4d_url: "https://the.sixth.one",
97
97
  username: "you",
98
- password: "me"
98
+ password: "me",
99
+ token_name: "accessToken"
99
100
  )
100
101
  token_manager.send(:save_token_info, token_info: token)
101
102
  token_response = token_manager.send(:token)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ibm_cloud_sdk_core
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mamoon Raja