ibm_cloud_sdk_core 0.1.2 → 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: 2f1a58243a7408312fa23e3d7628a6fea40359e2565ce9966da37b0655663b9c
4
- data.tar.gz: 5f51521d8185cefa9f6101debb7da0214de20338d9c8d71d1997c9565fab8cd3
3
+ metadata.gz: 9807ace911b05d47e8d13b72e7623c1ed2a68693cebec40f3d68f2f7d3e437ae
4
+ data.tar.gz: db29d041a1ab924b050e357ed6dfc64ed9f806a231fc82d4f48fc8342103adfe
5
5
  SHA512:
6
- metadata.gz: f5675f25b359c9109b57e40a12f187a5c859f6737f8f40ce53e4104c03e9d7d2f2ce16b19afde24a314469a2766615b591772a72e53704c0656b6c0e2e7b19b5
7
- data.tar.gz: 2c6bf48c17328e76044178406ab364a5bb01ef23b10ff416e85972452bc0ac6130dbc4212f42110797de80bee6f390d67e96b36714e9ebc777900139832b89e7
6
+ metadata.gz: 7cd90962533f73d125c1ca06346fe4e49d775c43ef1a0b3cce2a974c389942b793301e98b4be532eaf3e566c2ef7f5737127c865736e322ac2a4f1fa2be92765
7
+ data.tar.gz: c271ba5ee551a6121af1f4c4a68b9c2a14ca91f5839cd15ed370b004836b1891fe75a67aed8caa9b4182c5c0bf7ff29024fcd105f6a5f3f688b34e7973dcf9e4
data/README.md CHANGED
@@ -1,9 +1,10 @@
1
1
  [![Build Status](https://travis-ci.com/IBM/ruby-sdk-core.svg?branch=master)](https://travis-ci.com/IBM/ruby-sdk-core)
2
2
  [![codecov](https://codecov.io/gh/IBM/ruby-sdk-core/branch/master/graph/badge.svg)](https://codecov.io/gh/IBM/ruby-sdk-core)
3
- [![Gem Version](https://badge.fury.io/rb/ibm_watson.svg)](https://rubygems.org/gems/ibm_cloud_sdk_core)
3
+ [![Gem Version](https://badge.fury.io/rb/ibm_cloud_sdk_core.svg)](https://rubygems.org/gems/ibm_cloud_sdk_core)
4
4
 
5
5
  # ruby-sdk-core
6
- This project contains the core functionality used by Ruby SDK's generated by the IBM OpenAPI 3 SDK Generator (openapi-sdkgen).
6
+ This project contains the core functionality used by Ruby SDK's generated by the
7
+ [IBM OpenAPI SDK Generator](https://github.ibm.com/CloudEngineering/openapi-sdkgen).
7
8
  Ruby code generated by openapi-sdkgen will depend on the function contained in this project.
8
9
 
9
10
  ## Installation
@@ -32,4 +33,4 @@ Find more open source projects on the [IBM Github Page](http://github.com/IBM)
32
33
  ## License
33
34
 
34
35
  This library is licensed under Apache 2.0. Full license text is
35
- available in [LICENSE](LICENSE).
36
+ available in [LICENSE](LICENSE).
@@ -28,6 +28,8 @@ module IBMCloudSdkCore
28
28
  iam_apikey: nil,
29
29
  iam_access_token: nil,
30
30
  iam_url: nil,
31
+ iam_client_id: nil,
32
+ iam_client_secret: nil,
31
33
  display_name: nil
32
34
  }
33
35
  vars = defaults.merge(vars)
@@ -42,7 +44,9 @@ module IBMCloudSdkCore
42
44
  @display_name = vars[:display_name]
43
45
 
44
46
  if (!vars[:iam_access_token].nil? || !vars[:iam_apikey].nil?) && !@icp_prefix
45
- set_token_manager(iam_apikey: vars[:iam_apikey], iam_access_token: vars[:iam_access_token], iam_url: vars[:iam_url])
47
+ set_token_manager(iam_apikey: vars[:iam_apikey], iam_access_token: vars[:iam_access_token],
48
+ iam_url: vars[:iam_url], iam_client_id: vars[:iam_client_id],
49
+ iam_client_secret: vars[:iam_client_secret])
46
50
  elsif !vars[:iam_apikey].nil? && @icp_prefix
47
51
  @username = "apikey"
48
52
  @password = vars[:iam_apikey]
@@ -244,11 +248,16 @@ module IBMCloudSdkCore
244
248
  return str.start_with?("{", "\"") || str.end_with?("}", "\"") unless str.nil?
245
249
  end
246
250
 
247
- def set_token_manager(iam_apikey: nil, iam_access_token: nil, iam_url: nil)
251
+ def set_token_manager(iam_apikey: nil, iam_access_token: nil, iam_url: nil,
252
+ iam_client_id: nil, iam_client_secret: nil)
248
253
  @iam_apikey = iam_apikey
249
254
  @iam_access_token = iam_access_token
250
255
  @iam_url = iam_url
251
- @token_manager = IAMTokenManager.new(iam_apikey: iam_apikey, iam_access_token: iam_access_token, iam_url: iam_url)
256
+ @iam_client_id = iam_client_id
257
+ @iam_client_secret = iam_client_secret
258
+ @token_manager =
259
+ IAMTokenManager.new(iam_apikey: iam_apikey, iam_access_token: iam_access_token,
260
+ iam_url: iam_url, iam_client_id: iam_client_id, iam_client_secret: iam_client_secret)
252
261
  end
253
262
 
254
263
  def add_timeout(timeout)
@@ -8,16 +8,19 @@ require_relative("./version.rb")
8
8
  module IBMCloudSdkCore
9
9
  # Class to manage IAM Token Authentication
10
10
  class IAMTokenManager
11
- DEFAULT_IAM_URL = "https://iam.bluemix.net/identity/token"
11
+ DEFAULT_IAM_URL = "https://iam.cloud.ibm.com/identity/token"
12
12
  CONTENT_TYPE = "application/x-www-form-urlencoded"
13
13
  ACCEPT = "application/json"
14
14
  DEFAULT_AUTHORIZATION = "Basic Yng6Yng="
15
+ DEFAULT_CLIENT_ID = "bx"
16
+ DEFAULT_CLIENT_SECRET = "bx"
15
17
  REQUEST_TOKEN_GRANT_TYPE = "urn:ibm:params:oauth:grant-type:apikey"
16
18
  REQUEST_TOKEN_RESPONSE_TYPE = "cloud_iam"
17
19
  REFRESH_TOKEN_GRANT_TYPE = "refresh_token"
18
20
 
19
21
  attr_accessor :token_info, :user_access_token
20
- def initialize(iam_apikey: nil, iam_access_token: nil, iam_url: nil)
22
+ def initialize(iam_apikey: nil, iam_access_token: nil, iam_url: nil,
23
+ iam_client_id: nil, iam_client_secret: nil)
21
24
  @iam_apikey = iam_apikey
22
25
  @user_access_token = iam_access_token
23
26
  @iam_url = iam_url.nil? ? DEFAULT_IAM_URL : iam_url
@@ -28,12 +31,22 @@ module IBMCloudSdkCore
28
31
  "expires_in" => nil,
29
32
  "expiration" => nil
30
33
  }
34
+ # Both the client id and secret should be provided or neither should be provided.
35
+ if !iam_client_id.nil? && !iam_client_secret.nil?
36
+ @iam_client_id = iam_client_id
37
+ @iam_client_secret = iam_client_secret
38
+ elsif iam_client_id.nil? && iam_client_secret.nil?
39
+ @iam_client_id = DEFAULT_CLIENT_ID
40
+ @iam_client_secret = DEFAULT_CLIENT_SECRET
41
+ else
42
+ raise ArgumentError.new("Only one of 'iam_client_id' or 'iam_client_secret' were specified, but both parameters should be specified together.")
43
+ end
31
44
  end
32
45
 
33
46
  def request(method:, url:, headers: nil, params: nil, data: nil)
34
47
  response = nil
35
48
  if headers.key?("Content-Type") && headers["Content-Type"] == CONTENT_TYPE
36
- response = HTTP.request(
49
+ response = HTTP.basic_auth(user: @iam_client_id, pass: @iam_client_secret).request(
37
50
  method,
38
51
  url,
39
52
  body: HTTP::URI.form_encode(data),
@@ -78,7 +91,6 @@ module IBMCloudSdkCore
78
91
  def request_token
79
92
  headers = {
80
93
  "Content-Type" => CONTENT_TYPE,
81
- "Authorization" => DEFAULT_AUTHORIZATION,
82
94
  "Accept" => ACCEPT
83
95
  }
84
96
  data = {
@@ -99,7 +111,6 @@ module IBMCloudSdkCore
99
111
  def refresh_token
100
112
  headers = {
101
113
  "Content-Type" => CONTENT_TYPE,
102
- "Authorization" => DEFAULT_AUTHORIZATION,
103
114
  "accept" => ACCEPT
104
115
  }
105
116
  data = {
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module IBMCloudSdkCore
4
- VERSION = "0.1.2"
4
+ VERSION = "0.2.0"
5
5
  end
@@ -220,7 +220,7 @@ class BaseServiceTest < Minitest::Test
220
220
  headers = {
221
221
  "Content-Type" => "application/json"
222
222
  }
223
- stub_request(:post, "https://iam.bluemix.net/identity/token")
223
+ stub_request(:post, "https://iam.cloud.ibm.com/identity/token")
224
224
  .with(
225
225
  body: {
226
226
  "apikey" => "xyz",
@@ -232,7 +232,7 @@ class BaseServiceTest < Minitest::Test
232
232
  "Authorization" => "Basic Yng6Yng=",
233
233
  "Connection" => "close",
234
234
  "Content-Type" => "application/x-www-form-urlencoded",
235
- "Host" => "iam.bluemix.net",
235
+ "Host" => "iam.cloud.ibm.com",
236
236
  "User-Agent" => "http.rb/4.1.1"
237
237
  }
238
238
  ).to_return(status: 200, body: token_response.to_json, headers: {})
@@ -269,7 +269,7 @@ class BaseServiceTest < Minitest::Test
269
269
  headers = {
270
270
  "Content-Type" => "application/json"
271
271
  }
272
- stub_request(:post, "https://iam.bluemix.net/identity/token")
272
+ stub_request(:post, "https://iam.cloud.ibm.com/identity/token")
273
273
  .with(
274
274
  body: {
275
275
  "apikey" => "xyz",
@@ -281,7 +281,7 @@ class BaseServiceTest < Minitest::Test
281
281
  "Authorization" => "Basic Yng6Yng=",
282
282
  "Connection" => "close",
283
283
  "Content-Type" => "application/x-www-form-urlencoded",
284
- "Host" => "iam.bluemix.net",
284
+ "Host" => "iam.cloud.ibm.com",
285
285
  "User-Agent" => "http.rb/4.1.1"
286
286
  }
287
287
  ).to_return(status: 200, body: token_response.to_json, headers: {})
@@ -8,7 +8,6 @@ WebMock.disable_net_connect!(allow_localhost: true)
8
8
  # Unit tests for the IAM Token Manager
9
9
  class IAMTokenManagerTest < Minitest::Test
10
10
  def test_request_token
11
- iam_url = "https://iam.bluemix.net/identity/token"
12
11
  response = {
13
12
  "access_token" => "oAeisG8yqPY7sFR_x66Z15",
14
13
  "token_type" => "Bearer",
@@ -17,19 +16,19 @@ class IAMTokenManagerTest < Minitest::Test
17
16
  "refresh_token" => "jy4gl91BQ"
18
17
  }
19
18
 
19
+ # Use default iam_url, client id/secret
20
20
  token_manager = IBMCloudSdkCore::IAMTokenManager.new(
21
21
  iam_apikey: "iam_apikey",
22
- iam_access_token: "iam_access_token",
23
- iam_url: iam_url
22
+ iam_access_token: "iam_access_token"
24
23
  )
25
- stub_request(:post, "https://iam.bluemix.net/identity/token")
24
+ stub_request(:post, "https://iam.cloud.ibm.com/identity/token")
26
25
  .with(
27
26
  body: { "apikey" => "iam_apikey", "grant_type" => "urn:ibm:params:oauth:grant-type:apikey", "response_type" => "cloud_iam" },
28
27
  headers: {
29
28
  "Accept" => "application/json",
30
29
  "Authorization" => "Basic Yng6Yng=",
31
30
  "Content-Type" => "application/x-www-form-urlencoded",
32
- "Host" => "iam.bluemix.net"
31
+ "Host" => "iam.cloud.ibm.com"
33
32
  }
34
33
  ).to_return(status: 200, body: response.to_json, headers: {})
35
34
  token_response = token_manager.send(:request_token)
@@ -63,24 +62,22 @@ class IAMTokenManagerTest < Minitest::Test
63
62
  end
64
63
 
65
64
  def test_request_token_fails_catch_exception
66
- iam_url = "https://iam.bluemix.net/identity/token"
67
65
  token_manager = IBMCloudSdkCore::IAMTokenManager.new(
68
66
  iam_apikey: "iam_apikey",
69
- iam_access_token: "iam_access_token",
70
- iam_url: iam_url
67
+ iam_access_token: "iam_access_token"
71
68
  )
72
69
  response = {
73
70
  "code" => "500",
74
71
  "error" => "Oh no"
75
72
  }
76
- stub_request(:post, "https://iam.bluemix.net/identity/token")
73
+ stub_request(:post, "https://iam.cloud.ibm.com/identity/token")
77
74
  .with(
78
75
  body: { "apikey" => "iam_apikey", "grant_type" => "urn:ibm:params:oauth:grant-type:apikey", "response_type" => "cloud_iam" },
79
76
  headers: {
80
77
  "Accept" => "application/json",
81
78
  "Authorization" => "Basic Yng6Yng=",
82
79
  "Content-Type" => "application/x-www-form-urlencoded",
83
- "Host" => "iam.bluemix.net"
80
+ "Host" => "iam.cloud.ibm.com"
84
81
  }
85
82
  ).to_return(status: 401, body: response.to_json, headers: {})
86
83
  begin
@@ -91,7 +88,7 @@ class IAMTokenManagerTest < Minitest::Test
91
88
  end
92
89
 
93
90
  def test_refresh_token
94
- iam_url = "https://iam.bluemix.net/identity/token"
91
+ iam_url = "https://iam.cloud.ibm.com/identity/token"
95
92
  response = {
96
93
  "access_token" => "oAeisG8yqPY7sFR_x66Z15",
97
94
  "token_type" => "Bearer",
@@ -104,14 +101,14 @@ class IAMTokenManagerTest < Minitest::Test
104
101
  iam_access_token: "iam_access_token",
105
102
  iam_url: iam_url
106
103
  )
107
- stub_request(:post, "https://iam.bluemix.net/identity/token")
104
+ stub_request(:post, "https://iam.cloud.ibm.com/identity/token")
108
105
  .with(
109
106
  body: { "grant_type" => "refresh_token", "refresh_token" => "" },
110
107
  headers: {
111
108
  "Accept" => "application/json",
112
109
  "Authorization" => "Basic Yng6Yng=",
113
110
  "Content-Type" => "application/x-www-form-urlencoded",
114
- "Host" => "iam.bluemix.net"
111
+ "Host" => "iam.cloud.ibm.com"
115
112
  }
116
113
  ).to_return(status: 200, body: response.to_json, headers: {})
117
114
  token_response = token_manager.send(:refresh_token)
@@ -157,7 +154,7 @@ class IAMTokenManagerTest < Minitest::Test
157
154
  end
158
155
 
159
156
  def test_get_token
160
- iam_url = "https://iam.bluemix.net/identity/token"
157
+ iam_url = "https://iam.cloud.ibm.com/identity/token"
161
158
  token_manager = IBMCloudSdkCore::IAMTokenManager.new(
162
159
  iam_apikey: "iam_apikey",
163
160
  iam_url: iam_url
@@ -174,14 +171,14 @@ class IAMTokenManagerTest < Minitest::Test
174
171
  "expiration" => 1_524_167_011,
175
172
  "refresh_token" => "jy4gl91BQ"
176
173
  }
177
- stub_request(:post, "https://iam.bluemix.net/identity/token")
174
+ stub_request(:post, "https://iam.cloud.ibm.com/identity/token")
178
175
  .with(
179
176
  body: { "apikey" => "iam_apikey", "grant_type" => "urn:ibm:params:oauth:grant-type:apikey", "response_type" => "cloud_iam" },
180
177
  headers: {
181
178
  "Accept" => "application/json",
182
179
  "Authorization" => "Basic Yng6Yng=",
183
180
  "Content-Type" => "application/x-www-form-urlencoded",
184
- "Host" => "iam.bluemix.net"
181
+ "Host" => "iam.cloud.ibm.com"
185
182
  }
186
183
  ).to_return(status: 200, body: response.to_json, headers: {})
187
184
  token_manager.user_access_token = ""
@@ -192,13 +189,13 @@ class IAMTokenManagerTest < Minitest::Test
192
189
  token = token_manager.token
193
190
  assert_equal("hellohello", token)
194
191
 
195
- stub_request(:post, "https://iam.bluemix.net/identity/token")
192
+ stub_request(:post, "https://iam.cloud.ibm.com/identity/token")
196
193
  .with(
197
194
  headers: {
198
195
  "Accept" => "application/json",
199
196
  "Authorization" => "Basic Yng6Yng=",
200
197
  "Content-Type" => "application/x-www-form-urlencoded",
201
- "Host" => "iam.bluemix.net"
198
+ "Host" => "iam.cloud.ibm.com"
202
199
  }
203
200
  ).to_return(status: 200, body: response.to_json, headers: {})
204
201
  token_manager.token_info["expiration"] = Time.now.to_i - 4000
@@ -216,11 +213,68 @@ class IAMTokenManagerTest < Minitest::Test
216
213
  assert_equal("dummy", token)
217
214
  end
218
215
 
216
+ def test_client_id_only
217
+ assert_raises do
218
+ IBMCloudSdkCore::IAMTokenManager.new(
219
+ iam_apikey: "iam_apikey",
220
+ iam_access_token: "iam_access_token",
221
+ iam_client_id: "client_id"
222
+ )
223
+ end
224
+ end
225
+
226
+ def test_client_secret_only
227
+ assert_raises do
228
+ IBMCloudSdkCore::IAMTokenManager.new(
229
+ iam_apikey: "iam_apikey",
230
+ iam_access_token: "iam_access_token",
231
+ iam_client_secret: "client_secret"
232
+ )
233
+ end
234
+ end
235
+
236
+ def test_request_token_nondefault_client_id_secret
237
+ response = {
238
+ "access_token" => "oAeisG8yqPY7sFR_x66Z15",
239
+ "token_type" => "Bearer",
240
+ "expires_in" => 3600,
241
+ "expiration" => 1_524_167_011,
242
+ "refresh_token" => "jy4gl91BQ"
243
+ }
244
+
245
+ token_manager = IBMCloudSdkCore::IAMTokenManager.new(
246
+ iam_apikey: "iam_apikey",
247
+ iam_client_id: "foo",
248
+ iam_client_secret: "bar"
249
+ )
250
+ stub_request(:post, "https://iam.cloud.ibm.com/identity/token")
251
+ .with(basic_auth: %w[foo bar]).to_return(status: 200, body: response.to_json, headers: {})
252
+ token_response = token_manager.send(:request_token)
253
+ assert_equal(response, token_response)
254
+ end
255
+
256
+ def test_request_token_default_client_id_secret
257
+ response = {
258
+ "access_token" => "oAeisG8yqPY7sFR_x66Z15",
259
+ "token_type" => "Bearer",
260
+ "expires_in" => 3600,
261
+ "expiration" => 1_524_167_011,
262
+ "refresh_token" => "jy4gl91BQ"
263
+ }
264
+
265
+ token_manager = IBMCloudSdkCore::IAMTokenManager.new(
266
+ iam_apikey: "iam_apikey"
267
+ )
268
+ stub_request(:post, "https://iam.cloud.ibm.com/identity/token")
269
+ .with(basic_auth: %w[bx bx]).to_return(status: 200, body: response.to_json, headers: {})
270
+ token_response = token_manager.send(:request_token)
271
+ assert_equal(response, token_response)
272
+ end
273
+
219
274
  def test_dont_leak_constants
220
275
  assert_nil(defined? DEFAULT_IAM_URL)
221
276
  assert_nil(defined? CONTENT_TYPE)
222
277
  assert_nil(defined? ACCEPT)
223
- assert_nil(defined? DEFAULT_AUTHORIZATION)
224
278
  assert_nil(defined? REQUEST_TOKEN_GRANT_TYPE)
225
279
  assert_nil(defined? REQUEST_TOKEN_RESPONSE_TYPE)
226
280
  assert_nil(defined? REFRESH_TOKEN_GRANT_TYPE)
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: 0.1.2
4
+ version: 0.2.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-04-05 00:00:00.000000000 Z
11
+ date: 2019-05-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concurrent-ruby