ibm_cloud_sdk_core 1.0.0.rc2 → 1.0.0.rc3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/ibm_cloud_sdk_core/authenticators/config_based_authenticator_factory.rb +5 -5
- data/lib/ibm_cloud_sdk_core/authenticators/cp4d_authenticator.rb +4 -4
- data/lib/ibm_cloud_sdk_core/authenticators/iam_authenticator.rb +2 -11
- data/lib/ibm_cloud_sdk_core/base_service.rb +11 -6
- data/lib/ibm_cloud_sdk_core/token_managers/iam_token_manager.rb +1 -1
- data/lib/ibm_cloud_sdk_core/token_managers/jwt_token_manager.rb +6 -9
- data/lib/ibm_cloud_sdk_core/version.rb +1 -1
- data/test/unit/test_base_service.rb +31 -82
- data/test/unit/test_configure_http_proxy.rb +35 -14
- data/test/unit/test_cp4d_authenticator.rb +97 -0
- data/test/unit/{test_icp4d_token_manager.rb → test_cp4d_token_manager.rb} +0 -0
- data/test/unit/test_iam_authenticator.rb +143 -0
- data/test/unit/test_iam_token_manager.rb +0 -1
- data/test/unit/test_jwt_token_manager.rb +51 -2
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 552a7ea5d30afc2a3e9764b365e6a1ea1666852e7eefe54e01e63dc949cfdf02
|
4
|
+
data.tar.gz: 5acfc4a1cfd2f358096bf4c6153282ad39761ed9535ee283dfb28a3f499c7858
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 967664c0155975c23714884c142259824a74e363df6134bbfaff7236701f61ef3ec5b37950509cd78fcbb37b185e75eda451891fdc88f02f5f9766b9e7c040ed
|
7
|
+
data.tar.gz: e9005fe810d53199e88963ff13a4099c09c624e7f7664243aae7be405bd5bf77d71639cf8a0ace8f61a55beb569eb14343f430bbc8e689abdd8ab42c86003c96
|
@@ -26,11 +26,11 @@ module IBMCloudSdkCore
|
|
26
26
|
else
|
27
27
|
auth_type = config[:auth_type]
|
28
28
|
end
|
29
|
-
return BasicAuthenticator.new(config) if auth_type
|
30
|
-
return BearerTokenAuthenticator.new(config) if auth_type
|
31
|
-
return CloudPakForDataAuthenticator.new(config) if auth_type
|
32
|
-
return IamAuthenticator.new(config) if auth_type
|
33
|
-
return
|
29
|
+
return BasicAuthenticator.new(config) if auth_type.casecmp(AUTH_TYPE_BASIC).zero?
|
30
|
+
return BearerTokenAuthenticator.new(config) if auth_type.casecmp(AUTH_TYPE_BEARER_TOKEN).zero?
|
31
|
+
return CloudPakForDataAuthenticator.new(config) if auth_type.casecmp(AUTH_TYPE_CP4D).zero?
|
32
|
+
return IamAuthenticator.new(config) if auth_type.casecmp(AUTH_TYPE_IAM).zero?
|
33
|
+
return NoAuthAuthenticator.new if auth_type.casecmp(AUTH_TYPE_NO_AUTH).zero?
|
34
34
|
end
|
35
35
|
end
|
36
36
|
end
|
@@ -8,7 +8,7 @@ require_relative("../utils.rb")
|
|
8
8
|
module IBMCloudSdkCore
|
9
9
|
# Basic Authenticator
|
10
10
|
class CloudPakForDataAuthenticator < Authenticator
|
11
|
-
attr_accessor :authentication_type
|
11
|
+
attr_accessor :authentication_type, :disable_ssl_verification
|
12
12
|
def initialize(vars)
|
13
13
|
defaults = {
|
14
14
|
username: nil,
|
@@ -33,14 +33,14 @@ module IBMCloudSdkCore
|
|
33
33
|
end
|
34
34
|
|
35
35
|
# Adds the Authorization header, if possible
|
36
|
-
def authenticate(
|
37
|
-
|
36
|
+
def authenticate(headers)
|
37
|
+
headers["Authorization"] = "Bearer #{@token_manager.access_token}"
|
38
38
|
end
|
39
39
|
|
40
40
|
# Checks if all the inputs needed are present
|
41
41
|
def validate
|
42
42
|
raise ArgumentError.new("The username or password shouldn\'t be None.") if @username.nil? || @password.nil?
|
43
|
-
raise ArgumentError.new("The url
|
43
|
+
raise ArgumentError.new("The url shouldn\'t be None.") if @url.nil?
|
44
44
|
raise ArgumentError.new('The username shouldn\'t start or end with curly brackets or quotes. Be sure to remove any {} and \" characters surrounding your username') if check_bad_first_or_last_char(@username)
|
45
45
|
raise ArgumentError.new('The password shouldn\'t start or end with curly brackets or quotes. Be sure to remove any {} and \" characters surrounding your password') if check_bad_first_or_last_char(@password)
|
46
46
|
raise ArgumentError.new('The url shouldn\'t start or end with curly brackets or quotes. Be sure to remove any {} and \" characters surrounding your url') if check_bad_first_or_last_char(@url)
|
@@ -8,10 +8,7 @@ require_relative("../utils.rb")
|
|
8
8
|
module IBMCloudSdkCore
|
9
9
|
# Basic Authenticator
|
10
10
|
class IamAuthenticator < Authenticator
|
11
|
-
|
12
|
-
DEFAULT_CLIENT_SECRET = "bx"
|
13
|
-
|
14
|
-
attr_accessor :authentication_type
|
11
|
+
attr_accessor :authentication_type, :disable_ssl_verification, :client_id, :client_secret
|
15
12
|
def initialize(vars)
|
16
13
|
defaults = {
|
17
14
|
url: nil,
|
@@ -39,7 +36,6 @@ module IBMCloudSdkCore
|
|
39
36
|
|
40
37
|
def authenticate(headers)
|
41
38
|
headers["Authorization"] = "Bearer #{@token_manager.access_token}"
|
42
|
-
headers
|
43
39
|
end
|
44
40
|
|
45
41
|
def validate
|
@@ -48,12 +44,7 @@ module IBMCloudSdkCore
|
|
48
44
|
raise ArgumentError.new('The apikey shouldn\'t start or end with curly brackets or quotes. Be sure to remove any {} and \" characters surrounding your apikey') if check_bad_first_or_last_char(@apikey)
|
49
45
|
|
50
46
|
# Both the client id and secret should be provided or neither should be provided.
|
51
|
-
if @client_id.nil? && @client_secret.nil?
|
52
|
-
@client_id = DEFAULT_CLIENT_ID
|
53
|
-
@client_secret = DEFAULT_CLIENT_SECRET
|
54
|
-
elsif @client_id.nil? || client_secret.nil?
|
55
|
-
raise ArgumentError.new("Only one of 'client_id' or 'client_secret' were specified, but both parameters should be specified together.")
|
56
|
-
end
|
47
|
+
raise ArgumentError.new("Only one of 'client_id' or 'client_secret' were specified, but both parameters should be specified together.") if (@client_id.nil? && !@client_secret.nil?) || (!@client_id.nil? && @client_secret.nil?)
|
57
48
|
end
|
58
49
|
end
|
59
50
|
end
|
@@ -23,7 +23,7 @@ end
|
|
23
23
|
module IBMCloudSdkCore
|
24
24
|
# Class for interacting with the API
|
25
25
|
class BaseService
|
26
|
-
attr_accessor :display_name
|
26
|
+
attr_accessor :display_name, :service_url, :disable_ssl_verification
|
27
27
|
attr_reader :conn, :authenticator
|
28
28
|
def initialize(vars)
|
29
29
|
defaults = {
|
@@ -32,15 +32,16 @@ module IBMCloudSdkCore
|
|
32
32
|
display_name: nil
|
33
33
|
}
|
34
34
|
vars = defaults.merge(vars)
|
35
|
-
@
|
35
|
+
@service_url = vars[:service_url]
|
36
36
|
@authenticator = vars[:authenticator]
|
37
37
|
@disable_ssl_verification = vars[:disable_ssl_verification]
|
38
38
|
@display_name = vars[:display_name]
|
39
39
|
@service_name = @display_name.tr(" ", "_").downcase unless @display_name.nil?
|
40
|
+
@authenticator = IBMCloudSdkCore::ConfigBasedAuthenticatorFactory.new.get_authenticator(service_name: @service_name) if @authenticator.nil?
|
40
41
|
|
41
|
-
if @service_name && !@
|
42
|
+
if @service_name && !@service_url
|
42
43
|
config = get_service_properties(@service_name)
|
43
|
-
@
|
44
|
+
@service_url = config[:url] unless config.nil?
|
44
45
|
end
|
45
46
|
|
46
47
|
@temp_headers = {}
|
@@ -74,13 +75,17 @@ module IBMCloudSdkCore
|
|
74
75
|
|
75
76
|
conn = @conn
|
76
77
|
|
78
|
+
@authenticator.authenticate(@temp_headers)
|
77
79
|
args[:headers] = args[:headers].merge(@temp_headers) unless @temp_headers.nil?
|
78
80
|
@temp_headers = nil unless @temp_headers.nil?
|
79
81
|
|
82
|
+
raise ArgumentError.new("service_url must be provided") if @service_url.nil?
|
83
|
+
raise ArgumentError.new('The service_url shouldn\'t start or end with curly brackets or quotes. Be sure to remove any {} and \" characters surrounding your username') if check_bad_first_or_last_char(@service_url)
|
84
|
+
|
80
85
|
if args.key?(:form)
|
81
86
|
response = conn.follow.request(
|
82
87
|
args[:method],
|
83
|
-
HTTP::URI.parse(@
|
88
|
+
HTTP::URI.parse(@service_url + args[:url]),
|
84
89
|
headers: conn.default_options.headers.merge(HTTP::Headers.coerce(args[:headers])),
|
85
90
|
params: args[:params],
|
86
91
|
form: args[:form]
|
@@ -88,7 +93,7 @@ module IBMCloudSdkCore
|
|
88
93
|
else
|
89
94
|
response = conn.follow.request(
|
90
95
|
args[:method],
|
91
|
-
HTTP::URI.parse(@
|
96
|
+
HTTP::URI.parse(@service_url + args[:url]),
|
92
97
|
headers: conn.default_options.headers.merge(HTTP::Headers.coerce(args[:headers])),
|
93
98
|
body: args[:json],
|
94
99
|
params: args[:params]
|
@@ -16,7 +16,7 @@ module IBMCloudSdkCore
|
|
16
16
|
REQUEST_TOKEN_RESPONSE_TYPE = "cloud_iam"
|
17
17
|
TOKEN_NAME = "access_token"
|
18
18
|
|
19
|
-
attr_accessor :token_info, :token_name
|
19
|
+
attr_accessor :token_info, :token_name, :client_id, :client_secret
|
20
20
|
def initialize(
|
21
21
|
apikey: nil,
|
22
22
|
url: nil,
|
@@ -35,10 +35,6 @@ module IBMCloudSdkCore
|
|
35
35
|
end
|
36
36
|
end
|
37
37
|
|
38
|
-
def ssl_verification(disable_ssl_verification)
|
39
|
-
@disable_ssl_verification = disable_ssl_verification
|
40
|
-
end
|
41
|
-
|
42
38
|
private
|
43
39
|
|
44
40
|
# Check if currently stored token is expired.
|
@@ -65,10 +61,10 @@ module IBMCloudSdkCore
|
|
65
61
|
end
|
66
62
|
|
67
63
|
def request(method:, url:, headers: nil, params: nil, data: nil, username: nil, password: nil)
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
response = HTTP.
|
64
|
+
ssl_context = OpenSSL::SSL::SSLContext.new
|
65
|
+
ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE if @disable_ssl_verification
|
66
|
+
if username.nil? && password.nil?
|
67
|
+
response = HTTP.request(
|
72
68
|
method,
|
73
69
|
url,
|
74
70
|
body: data,
|
@@ -82,7 +78,8 @@ module IBMCloudSdkCore
|
|
82
78
|
url,
|
83
79
|
body: data,
|
84
80
|
headers: headers,
|
85
|
-
params: params
|
81
|
+
params: params,
|
82
|
+
ssl_context: ssl_context
|
86
83
|
)
|
87
84
|
end
|
88
85
|
return JSON.parse(response.body.to_s) if (200..299).cover?(response.code)
|
@@ -21,10 +21,10 @@ class BaseServiceTest < Minitest::Test
|
|
21
21
|
end
|
22
22
|
|
23
23
|
def test_wrong_apikey
|
24
|
+
file_path = File.join(File.dirname(__FILE__), "../../resources/ibm-credentials.env")
|
25
|
+
ENV["IBM_CREDENTIALS_FILE"] = file_path
|
24
26
|
assert_raises do
|
25
|
-
IBMCloudSdkCore::
|
26
|
-
apikey: "{apikey"
|
27
|
-
)
|
27
|
+
IBMCloudSdkCore::ConfigBasedAuthenticatorFactory.new.get_authenticator(service_name: "wrong")
|
28
28
|
end
|
29
29
|
end
|
30
30
|
|
@@ -37,6 +37,23 @@ class BaseServiceTest < Minitest::Test
|
|
37
37
|
end
|
38
38
|
end
|
39
39
|
|
40
|
+
def test_iam_client_id_only
|
41
|
+
assert_raises ArgumentError do
|
42
|
+
IBMCloudSdkCore::IamAuthenticator.new(apikey: "apikey", client_id: "Salah")
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_no_auth_authenticator
|
47
|
+
file_path = File.join(File.dirname(__FILE__), "../../resources/ibm-credentials.env")
|
48
|
+
ENV["IBM_CREDENTIALS_FILE"] = file_path
|
49
|
+
authenticator = IBMCloudSdkCore::ConfigBasedAuthenticatorFactory.new.get_authenticator(service_name: "red_sox")
|
50
|
+
service = IBMCloudSdkCore::BaseService.new(
|
51
|
+
display_name: "Assistant",
|
52
|
+
authenticator: authenticator
|
53
|
+
)
|
54
|
+
refute_nil(service)
|
55
|
+
end
|
56
|
+
|
40
57
|
def test_correct_creds_and_headers
|
41
58
|
authenticator = IBMCloudSdkCore::BasicAuthenticator.new(
|
42
59
|
username: "username",
|
@@ -56,21 +73,11 @@ class BaseServiceTest < Minitest::Test
|
|
56
73
|
refute_nil(service)
|
57
74
|
end
|
58
75
|
|
59
|
-
def test_set_credentials_from_path_in_env
|
60
|
-
file_path = File.join(File.dirname(__FILE__), "../../resources/ibm-credentials.env")
|
61
|
-
ENV["IBM_CREDENTIALS_FILE"] = file_path
|
62
|
-
service = IBMCloudSdkCore::BaseService.new(display_name: "Visual Recognition")
|
63
|
-
assert_equal(service.instance_variable_get(:@url), "https://gateway.ronaldo.com")
|
64
|
-
refute_nil(service)
|
65
|
-
ENV.delete("IBM_CREDENTIALS_FILE")
|
66
|
-
end
|
67
|
-
|
68
76
|
def test_set_credentials_from_path_in_env_nlu
|
69
77
|
file_path = File.join(File.dirname(__FILE__), "../../resources/ibm-credentials.env")
|
70
78
|
ENV["IBM_CREDENTIALS_FILE"] = file_path
|
71
|
-
|
72
|
-
assert_equal(
|
73
|
-
refute_nil(service)
|
79
|
+
authenticator = IBMCloudSdkCore::ConfigBasedAuthenticatorFactory.new.get_authenticator(service_name: "natural_language_understanding")
|
80
|
+
assert_equal(authenticator.authentication_type, "bearerToken")
|
74
81
|
ENV.delete("IBM_CREDENTIALS_FILE")
|
75
82
|
end
|
76
83
|
|
@@ -81,7 +88,6 @@ class BaseServiceTest < Minitest::Test
|
|
81
88
|
service = IBMCloudSdkCore::BaseService.new(display_name: "Leo Messi", url: "some.url", authenticator: authenticator)
|
82
89
|
assert_equal(authenticator.authentication_type, "bearerToken")
|
83
90
|
refute_nil(service)
|
84
|
-
ENV.delete("IBM_CREDENTIALS_FILE")
|
85
91
|
end
|
86
92
|
|
87
93
|
def test_vcap_services
|
@@ -100,18 +106,18 @@ class BaseServiceTest < Minitest::Test
|
|
100
106
|
"Host" => "we.the.best"
|
101
107
|
}
|
102
108
|
).to_return(status: 200, body: "", headers: {})
|
103
|
-
authenticator = IBMCloudSdkCore::ConfigBasedAuthenticatorFactory.new.get_authenticator(service_name: "
|
104
|
-
service = IBMCloudSdkCore::BaseService.new(display_name: "Salah", authenticator: authenticator,
|
109
|
+
authenticator = IBMCloudSdkCore::ConfigBasedAuthenticatorFactory.new.get_authenticator(service_name: "salah")
|
110
|
+
service = IBMCloudSdkCore::BaseService.new(display_name: "Salah", authenticator: authenticator, service_url: "https://we.the.best")
|
105
111
|
service_response = service.request(method: "GET", url: "/music", headers: {})
|
106
112
|
assert_equal("", service_response.result)
|
107
113
|
end
|
108
114
|
|
109
115
|
def test_dummy_request_form_data
|
116
|
+
authenticator = IBMCloudSdkCore::BearerTokenAuthenticator.new(bearer_token: "token")
|
110
117
|
service = IBMCloudSdkCore::BaseService.new(
|
111
118
|
display_name: "Assistant",
|
112
|
-
|
113
|
-
|
114
|
-
url: "https://gateway.watsonplatform.net/"
|
119
|
+
authenticator: authenticator,
|
120
|
+
service_url: "https://gateway.watsonplatform.net/"
|
115
121
|
)
|
116
122
|
form_data = {}
|
117
123
|
file = File.open(Dir.getwd + "/resources/cnc_test.pdf")
|
@@ -143,9 +149,9 @@ class BaseServiceTest < Minitest::Test
|
|
143
149
|
"Host" => "we.the.best"
|
144
150
|
}
|
145
151
|
).to_return(status: 500, body: response.to_json, headers: {})
|
146
|
-
authenticator = IBMCloudSdkCore::ConfigBasedAuthenticatorFactory.new.get_authenticator(service_name: "
|
147
|
-
service = IBMCloudSdkCore::BaseService.new(display_name: "Salah", authenticator: authenticator,
|
148
|
-
assert_raises do
|
152
|
+
authenticator = IBMCloudSdkCore::ConfigBasedAuthenticatorFactory.new.get_authenticator(service_name: "salah")
|
153
|
+
service = IBMCloudSdkCore::BaseService.new(display_name: "Salah", authenticator: authenticator, service_url: "https://we.the.best")
|
154
|
+
assert_raises IBMCloudSdkCore::ApiException do
|
149
155
|
service.request(method: "GET", url: "/music", headers: {})
|
150
156
|
end
|
151
157
|
end
|
@@ -172,66 +178,9 @@ class BaseServiceTest < Minitest::Test
|
|
172
178
|
service = IBMCloudSdkCore::BaseService.new(
|
173
179
|
display_name: "Assistant",
|
174
180
|
authenticator: authenticator,
|
175
|
-
|
181
|
+
service_url: "https://we.the.best"
|
176
182
|
)
|
177
183
|
service_response = service.request(method: "GET", url: "/music", headers: {})
|
178
184
|
assert_equal(response, service_response.result)
|
179
185
|
end
|
180
|
-
|
181
|
-
def test_for_cp4d_authenticator
|
182
|
-
token_layout = {
|
183
|
-
"username": "dummy",
|
184
|
-
"role": "Admin",
|
185
|
-
"permissions": %w[administrator manage_catalog],
|
186
|
-
"sub": "admin",
|
187
|
-
"iss": "sss",
|
188
|
-
"aud": "sss",
|
189
|
-
"uid": "sss",
|
190
|
-
"iat": Time.now.to_i + 3600,
|
191
|
-
"exp": Time.now.to_i
|
192
|
-
}
|
193
|
-
token = JWT.encode token_layout, "secret", "HS256"
|
194
|
-
response = {
|
195
|
-
"accessToken" => token,
|
196
|
-
"token_type" => "Bearer",
|
197
|
-
"expires_in" => 3600,
|
198
|
-
"expiration" => 1_524_167_011,
|
199
|
-
"refresh_token" => "jy4gl91BQ"
|
200
|
-
}
|
201
|
-
stub_request(:get, "https://hello.world/v1/preauth/validateAuth")
|
202
|
-
.with(
|
203
|
-
headers: {
|
204
|
-
"Authorization" => "Basic aGVsbG86d29ybGQ=",
|
205
|
-
"Connection" => "close",
|
206
|
-
"Host" => "hello.world"
|
207
|
-
}
|
208
|
-
)
|
209
|
-
.to_return(status: 200, body: response.to_json, headers: {})
|
210
|
-
authenticator = IBMCloudSdkCore::CloudPakForDataAuthenticator.new(
|
211
|
-
username: "hello",
|
212
|
-
password: "world",
|
213
|
-
url: "https://hello.world"
|
214
|
-
)
|
215
|
-
refute_nil(authenticator)
|
216
|
-
end
|
217
|
-
|
218
|
-
# def test_CP4D_disable_ssl
|
219
|
-
# authenticator = IBMCloudSdkCore::CloudPakForDataAuthenticator.new(
|
220
|
-
# username: "username",
|
221
|
-
# password: "password"
|
222
|
-
# )
|
223
|
-
# IBMCloudSdkCore::BaseService.new(
|
224
|
-
# display_name: "Assistant",
|
225
|
-
# url: "http://the.com",
|
226
|
-
# authenticator: authenticator
|
227
|
-
# )
|
228
|
-
# stub_request(:get, "http://the.com/music")
|
229
|
-
# .with(
|
230
|
-
# headers: {
|
231
|
-
# "Authorization" => "Basic Og==",
|
232
|
-
# "Host" => "the.com"
|
233
|
-
# }
|
234
|
-
# ).to_return(status: 200, body: {}.to_json, headers: {})
|
235
|
-
# assert_equal(authenticator.instance_variable_get(:@access_token), "token")
|
236
|
-
# end
|
237
186
|
end
|
@@ -9,11 +9,14 @@ WebMock.disable_net_connect!(allow_localhost: true)
|
|
9
9
|
# Unit tests for the configure_http_client customizations, such as proxies and timeouts
|
10
10
|
class HTTPConfigTest < Minitest::Test
|
11
11
|
def test_proxy_address_port
|
12
|
-
|
13
|
-
version: "2018-03-16",
|
12
|
+
authenticator = IBMCloudSdkCore::BasicAuthenticator.new(
|
14
13
|
username: "username",
|
15
14
|
password: "password"
|
16
15
|
)
|
16
|
+
service = IBMCloudSdkCore::BaseService.new(
|
17
|
+
version: "2018-03-16",
|
18
|
+
authenticator: authenticator
|
19
|
+
)
|
17
20
|
service.configure_http_client(
|
18
21
|
proxy: {
|
19
22
|
address: "bogus_address.com",
|
@@ -26,11 +29,14 @@ class HTTPConfigTest < Minitest::Test
|
|
26
29
|
end
|
27
30
|
|
28
31
|
def test_proxy_username_password
|
29
|
-
|
30
|
-
version: "2018-03-16",
|
32
|
+
authenticator = IBMCloudSdkCore::BasicAuthenticator.new(
|
31
33
|
username: "username",
|
32
34
|
password: "password"
|
33
35
|
)
|
36
|
+
service = IBMCloudSdkCore::BaseService.new(
|
37
|
+
version: "2018-03-16",
|
38
|
+
authenticator: authenticator
|
39
|
+
)
|
34
40
|
service.configure_http_client(
|
35
41
|
proxy: {
|
36
42
|
address: "bogus_address.com",
|
@@ -47,11 +53,14 @@ class HTTPConfigTest < Minitest::Test
|
|
47
53
|
end
|
48
54
|
|
49
55
|
def test_proxy_headers
|
50
|
-
|
51
|
-
version: "2018-03-16",
|
56
|
+
authenticator = IBMCloudSdkCore::BasicAuthenticator.new(
|
52
57
|
username: "username",
|
53
58
|
password: "password"
|
54
59
|
)
|
60
|
+
service = IBMCloudSdkCore::BaseService.new(
|
61
|
+
version: "2018-03-16",
|
62
|
+
authenticator: authenticator
|
63
|
+
)
|
55
64
|
service.configure_http_client(
|
56
65
|
proxy: {
|
57
66
|
address: "bogus_address.com",
|
@@ -68,11 +77,14 @@ class HTTPConfigTest < Minitest::Test
|
|
68
77
|
end
|
69
78
|
|
70
79
|
def test_proxy_username_password_headers
|
71
|
-
|
72
|
-
version: "2018-03-16",
|
80
|
+
authenticator = IBMCloudSdkCore::BasicAuthenticator.new(
|
73
81
|
username: "username",
|
74
82
|
password: "password"
|
75
83
|
)
|
84
|
+
service = IBMCloudSdkCore::BaseService.new(
|
85
|
+
version: "2018-03-16",
|
86
|
+
authenticator: authenticator
|
87
|
+
)
|
76
88
|
service.configure_http_client(
|
77
89
|
proxy: {
|
78
90
|
address: "bogus_address.com",
|
@@ -93,11 +105,14 @@ class HTTPConfigTest < Minitest::Test
|
|
93
105
|
end
|
94
106
|
|
95
107
|
def test_timeout_per_operation
|
96
|
-
|
97
|
-
version: "2018-03-16",
|
108
|
+
authenticator = IBMCloudSdkCore::BasicAuthenticator.new(
|
98
109
|
username: "username",
|
99
110
|
password: "password"
|
100
111
|
)
|
112
|
+
service = IBMCloudSdkCore::BaseService.new(
|
113
|
+
version: "2018-03-16",
|
114
|
+
authenticator: authenticator
|
115
|
+
)
|
101
116
|
service.configure_http_client(
|
102
117
|
timeout: {
|
103
118
|
per_operation: {
|
@@ -120,11 +135,14 @@ class HTTPConfigTest < Minitest::Test
|
|
120
135
|
end
|
121
136
|
|
122
137
|
def test_timeout_global
|
123
|
-
|
124
|
-
version: "2018-03-16",
|
138
|
+
authenticator = IBMCloudSdkCore::BasicAuthenticator.new(
|
125
139
|
username: "username",
|
126
140
|
password: "password"
|
127
141
|
)
|
142
|
+
service = IBMCloudSdkCore::BaseService.new(
|
143
|
+
version: "2018-03-16",
|
144
|
+
authenticator: authenticator
|
145
|
+
)
|
128
146
|
service.configure_http_client(
|
129
147
|
timeout: {
|
130
148
|
global: 20
|
@@ -141,11 +159,14 @@ class HTTPConfigTest < Minitest::Test
|
|
141
159
|
end
|
142
160
|
|
143
161
|
def test_disable_ssl_verification
|
144
|
-
|
145
|
-
version: "2018-03-16",
|
162
|
+
authenticator = IBMCloudSdkCore::BasicAuthenticator.new(
|
146
163
|
username: "username",
|
147
164
|
password: "password"
|
148
165
|
)
|
166
|
+
service = IBMCloudSdkCore::BaseService.new(
|
167
|
+
version: "2018-03-16",
|
168
|
+
authenticator: authenticator
|
169
|
+
)
|
149
170
|
service.configure_http_client(disable_ssl_verification: true)
|
150
171
|
refute_nil(service.conn.default_options.ssl_context)
|
151
172
|
end
|
@@ -0,0 +1,97 @@
|
|
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 base service
|
13
|
+
class Cp4dAuthenticatorTest < Minitest::Test
|
14
|
+
def test_cp4d_authenticator
|
15
|
+
token_layout = {
|
16
|
+
"username": "dummy",
|
17
|
+
"role": "Admin",
|
18
|
+
"permissions": %w[administrator manage_catalog],
|
19
|
+
"sub": "admin",
|
20
|
+
"iss": "sss",
|
21
|
+
"aud": "sss",
|
22
|
+
"uid": "sss",
|
23
|
+
"iat": Time.now.to_i + 3600,
|
24
|
+
"exp": Time.now.to_i
|
25
|
+
}
|
26
|
+
token = JWT.encode token_layout, "secret", "HS256"
|
27
|
+
response = {
|
28
|
+
"accessToken" => token,
|
29
|
+
"token_type" => "Bearer",
|
30
|
+
"expires_in" => 3600,
|
31
|
+
"expiration" => 1_524_167_011,
|
32
|
+
"refresh_token" => "jy4gl91BQ"
|
33
|
+
}
|
34
|
+
stub_request(:get, "https://icp.com/v1/preauth/validateAuth")
|
35
|
+
.with(
|
36
|
+
headers: {
|
37
|
+
"Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
|
38
|
+
"Connection" => "close",
|
39
|
+
"Host" => "icp.com",
|
40
|
+
"User-Agent" => "http.rb/4.1.1"
|
41
|
+
}
|
42
|
+
)
|
43
|
+
.to_return(status: 200, body: response.to_json, headers: {})
|
44
|
+
authenticator = IBMCloudSdkCore::CloudPakForDataAuthenticator.new(
|
45
|
+
username: "username",
|
46
|
+
password: "password",
|
47
|
+
url: "https://icp.com",
|
48
|
+
disable_ssl_verification: true
|
49
|
+
)
|
50
|
+
refute_nil(authenticator)
|
51
|
+
assert_equal(authenticator.instance_variable_get(:@token_manager).access_token, token)
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_cp4d_authenticator_authenticate
|
55
|
+
token_layout = {
|
56
|
+
"username": "dummy",
|
57
|
+
"role": "Admin",
|
58
|
+
"permissions": %w[administrator manage_catalog],
|
59
|
+
"sub": "admin",
|
60
|
+
"iss": "sss",
|
61
|
+
"aud": "sss",
|
62
|
+
"uid": "sss",
|
63
|
+
"iat": Time.now.to_i + 3600,
|
64
|
+
"exp": Time.now.to_i
|
65
|
+
}
|
66
|
+
token = JWT.encode token_layout, "secret", "HS256"
|
67
|
+
response = {
|
68
|
+
"accessToken" => token,
|
69
|
+
"token_type" => "Bearer",
|
70
|
+
"expires_in" => 3600,
|
71
|
+
"expiration" => 1_524_167_011,
|
72
|
+
"refresh_token" => "jy4gl91BQ"
|
73
|
+
}
|
74
|
+
stub_request(:get, "https://icp.com/v1/preauth/validateAuth")
|
75
|
+
.with(
|
76
|
+
headers: {
|
77
|
+
"Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
|
78
|
+
"Connection" => "close",
|
79
|
+
"Host" => "icp.com",
|
80
|
+
"User-Agent" => "http.rb/4.1.1"
|
81
|
+
}
|
82
|
+
)
|
83
|
+
.to_return(status: 200, body: response.to_json, headers: {})
|
84
|
+
authenticator = IBMCloudSdkCore::CloudPakForDataAuthenticator.new(
|
85
|
+
username: "username",
|
86
|
+
password: "password",
|
87
|
+
url: "https://icp.com",
|
88
|
+
disable_ssl_verification: true
|
89
|
+
)
|
90
|
+
refute_nil(authenticator)
|
91
|
+
assert_equal(authenticator.instance_variable_get(:@token_manager).access_token, token)
|
92
|
+
headers = {}
|
93
|
+
authenticated_headers = { "Authorization" => "Bearer " + token }
|
94
|
+
authenticator.authenticate(headers)
|
95
|
+
assert_equal(headers, authenticated_headers)
|
96
|
+
end
|
97
|
+
end
|
File without changes
|
@@ -0,0 +1,143 @@
|
|
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 base service
|
13
|
+
class IamAuthenticatorTest < Minitest::Test
|
14
|
+
def test_iam_authenticator
|
15
|
+
token_layout = {
|
16
|
+
"username": "dummy",
|
17
|
+
"role": "Admin",
|
18
|
+
"permissions": %w[administrator manage_catalog],
|
19
|
+
"sub": "admin",
|
20
|
+
"iss": "sss",
|
21
|
+
"aud": "sss",
|
22
|
+
"uid": "sss",
|
23
|
+
"iat": Time.now.to_i + 3600,
|
24
|
+
"exp": Time.now.to_i
|
25
|
+
}
|
26
|
+
token = JWT.encode token_layout, "secret", "HS256"
|
27
|
+
response = {
|
28
|
+
"access_token" => token,
|
29
|
+
"token_type" => "Bearer",
|
30
|
+
"expires_in" => 3600,
|
31
|
+
"expiration" => 1_524_167_011,
|
32
|
+
"refresh_token" => "jy4gl91BQ"
|
33
|
+
}
|
34
|
+
stub_request(:post, "https://iam.cloud.ibm.com/identity/token")
|
35
|
+
.with(
|
36
|
+
body: {
|
37
|
+
"apikey" => "apikey",
|
38
|
+
"grant_type" => "urn:ibm:params:oauth:grant-type:apikey",
|
39
|
+
"response_type" => "cloud_iam"
|
40
|
+
},
|
41
|
+
headers: {
|
42
|
+
"Connection" => "close",
|
43
|
+
"Host" => "iam.cloud.ibm.com",
|
44
|
+
"User-Agent" => "http.rb/4.1.1"
|
45
|
+
}
|
46
|
+
)
|
47
|
+
.to_return(status: 200, body: response.to_json, headers: {})
|
48
|
+
authenticator = IBMCloudSdkCore::IamAuthenticator.new(
|
49
|
+
apikey: "apikey"
|
50
|
+
)
|
51
|
+
refute_nil(authenticator)
|
52
|
+
assert_equal(authenticator.instance_variable_get(:@token_manager).access_token, token)
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_iam_authenticator_client_id_client_secret
|
56
|
+
token_layout = {
|
57
|
+
"username": "dummy",
|
58
|
+
"role": "Admin",
|
59
|
+
"permissions": %w[administrator manage_catalog],
|
60
|
+
"sub": "admin",
|
61
|
+
"iss": "sss",
|
62
|
+
"aud": "sss",
|
63
|
+
"uid": "sss",
|
64
|
+
"iat": Time.now.to_i + 3600,
|
65
|
+
"exp": Time.now.to_i
|
66
|
+
}
|
67
|
+
token = JWT.encode token_layout, "secret", "HS256"
|
68
|
+
response = {
|
69
|
+
"access_token" => token,
|
70
|
+
"token_type" => "Bearer",
|
71
|
+
"expires_in" => 3600,
|
72
|
+
"expiration" => 1_524_167_011,
|
73
|
+
"refresh_token" => "jy4gl91BQ"
|
74
|
+
}
|
75
|
+
stub_request(:post, "https://iam.cloud.ibm.com/identity/token")
|
76
|
+
.with(
|
77
|
+
body: {
|
78
|
+
"apikey" => "apikey",
|
79
|
+
"grant_type" => "urn:ibm:params:oauth:grant-type:apikey",
|
80
|
+
"response_type" => "cloud_iam"
|
81
|
+
},
|
82
|
+
headers: {
|
83
|
+
"Connection" => "close",
|
84
|
+
"Authorization" => "Basic Yng6Yng=",
|
85
|
+
"Host" => "iam.cloud.ibm.com",
|
86
|
+
"User-Agent" => "http.rb/4.1.1"
|
87
|
+
}
|
88
|
+
)
|
89
|
+
.to_return(status: 200, body: response.to_json, headers: {})
|
90
|
+
authenticator = IBMCloudSdkCore::IamAuthenticator.new(
|
91
|
+
apikey: "apikey",
|
92
|
+
client_id: "bx",
|
93
|
+
client_secret: "bx"
|
94
|
+
)
|
95
|
+
refute_nil(authenticator)
|
96
|
+
assert_equal(authenticator.instance_variable_get(:@token_manager).access_token, token)
|
97
|
+
end
|
98
|
+
|
99
|
+
def test_cp4d_authenticator_authenticate
|
100
|
+
token_layout = {
|
101
|
+
"username": "dummy",
|
102
|
+
"role": "Admin",
|
103
|
+
"permissions": %w[administrator manage_catalog],
|
104
|
+
"sub": "admin",
|
105
|
+
"iss": "sss",
|
106
|
+
"aud": "sss",
|
107
|
+
"uid": "sss",
|
108
|
+
"iat": Time.now.to_i + 3600,
|
109
|
+
"exp": Time.now.to_i
|
110
|
+
}
|
111
|
+
token = JWT.encode token_layout, "secret", "HS256"
|
112
|
+
response = {
|
113
|
+
"access_token" => token,
|
114
|
+
"token_type" => "Bearer",
|
115
|
+
"expires_in" => 3600,
|
116
|
+
"expiration" => 1_524_167_011,
|
117
|
+
"refresh_token" => "jy4gl91BQ"
|
118
|
+
}
|
119
|
+
stub_request(:post, "https://iam.cloud.ibm.com/identity/token")
|
120
|
+
.with(
|
121
|
+
body: {
|
122
|
+
"apikey" => "apikey",
|
123
|
+
"grant_type" => "urn:ibm:params:oauth:grant-type:apikey",
|
124
|
+
"response_type" => "cloud_iam"
|
125
|
+
},
|
126
|
+
headers: {
|
127
|
+
"Connection" => "close",
|
128
|
+
"Host" => "iam.cloud.ibm.com",
|
129
|
+
"User-Agent" => "http.rb/4.1.1"
|
130
|
+
}
|
131
|
+
)
|
132
|
+
.to_return(status: 200, body: response.to_json, headers: {})
|
133
|
+
authenticator = IBMCloudSdkCore::IamAuthenticator.new(
|
134
|
+
apikey: "apikey"
|
135
|
+
)
|
136
|
+
refute_nil(authenticator)
|
137
|
+
assert_equal(authenticator.instance_variable_get(:@token_manager).access_token, token)
|
138
|
+
headers = {}
|
139
|
+
authenticated_headers = { "Authorization" => "Bearer " + token }
|
140
|
+
authenticator.authenticate(headers)
|
141
|
+
assert_equal(headers, authenticated_headers)
|
142
|
+
end
|
143
|
+
end
|
@@ -60,7 +60,6 @@ class IAMTokenManagerTest < Minitest::Test
|
|
60
60
|
body: { "apikey" => "apikey", "grant_type" => "urn:ibm:params:oauth:grant-type:apikey", "response_type" => "cloud_iam" },
|
61
61
|
headers: {
|
62
62
|
"Accept" => "application/json",
|
63
|
-
"Authorization" => "Basic Og==",
|
64
63
|
"Content-Type" => "application/x-www-form-urlencoded",
|
65
64
|
"Host" => "iam.cloud.ibm.com"
|
66
65
|
}
|
@@ -25,7 +25,6 @@ class JWTTokenManagerTest < Minitest::Test
|
|
25
25
|
stub_request(:get, "https://the.sixth.one")
|
26
26
|
.with(
|
27
27
|
headers: {
|
28
|
-
"Authorization" => "Basic Og==",
|
29
28
|
"Host" => "the.sixth.one"
|
30
29
|
}
|
31
30
|
).to_return(status: 200, body: response.to_json, headers: {})
|
@@ -46,7 +45,6 @@ class JWTTokenManagerTest < Minitest::Test
|
|
46
45
|
stub_request(:get, "https://the.sixth.one/")
|
47
46
|
.with(
|
48
47
|
headers: {
|
49
|
-
"Authorization" => "Basic Og==",
|
50
48
|
"Host" => "the.sixth.one"
|
51
49
|
}
|
52
50
|
).to_return(status: 500, body: response.to_json, headers: {})
|
@@ -89,4 +87,55 @@ class JWTTokenManagerTest < Minitest::Test
|
|
89
87
|
token_response = token_manager.send(:token)
|
90
88
|
assert_equal(access_token, token_response)
|
91
89
|
end
|
90
|
+
|
91
|
+
def test_cp4d_disable_ssl
|
92
|
+
token_layout = {
|
93
|
+
"username": "dummy",
|
94
|
+
"role": "Admin",
|
95
|
+
"permissions": %w[administrator manage_catalog],
|
96
|
+
"sub": "admin",
|
97
|
+
"iss": "sss",
|
98
|
+
"aud": "sss",
|
99
|
+
"uid": "sss",
|
100
|
+
"iat": Time.now.to_i + 3600,
|
101
|
+
"exp": Time.now.to_i
|
102
|
+
}
|
103
|
+
token = JWT.encode token_layout, "secret", "HS256"
|
104
|
+
response = {
|
105
|
+
"accessToken" => token,
|
106
|
+
"token_type" => "Bearer",
|
107
|
+
"expires_in" => 3600,
|
108
|
+
"expiration" => 1_524_167_011,
|
109
|
+
"refresh_token" => "jy4gl91BQ"
|
110
|
+
}
|
111
|
+
stub_request(:get, "https://icp.com/v1/preauth/validateAuth")
|
112
|
+
.with(
|
113
|
+
headers: {
|
114
|
+
"Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
|
115
|
+
"Connection" => "close",
|
116
|
+
"Host" => "icp.com",
|
117
|
+
"User-Agent" => "http.rb/4.1.1"
|
118
|
+
}
|
119
|
+
)
|
120
|
+
.to_return(status: 200, body: response.to_json, headers: {})
|
121
|
+
authenticator = IBMCloudSdkCore::CloudPakForDataAuthenticator.new(
|
122
|
+
username: "username",
|
123
|
+
password: "password",
|
124
|
+
url: "https://icp.com",
|
125
|
+
disable_ssl_verification: true
|
126
|
+
)
|
127
|
+
IBMCloudSdkCore::BaseService.new(
|
128
|
+
display_name: "Assistant",
|
129
|
+
service_url: "http://the.com",
|
130
|
+
authenticator: authenticator
|
131
|
+
)
|
132
|
+
stub_request(:get, "http://the.com/music")
|
133
|
+
.with(
|
134
|
+
headers: {
|
135
|
+
"Authorization" => "Basic Og==",
|
136
|
+
"Host" => "the.com"
|
137
|
+
}
|
138
|
+
).to_return(status: 200, body: {}.to_json, headers: {})
|
139
|
+
assert_equal(authenticator.instance_variable_get(:@token_manager).access_token, token)
|
140
|
+
end
|
92
141
|
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.0.
|
4
|
+
version: 1.0.0.rc3
|
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-09-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: concurrent-ruby
|
@@ -247,9 +247,11 @@ files:
|
|
247
247
|
- test/test_helper.rb
|
248
248
|
- test/unit/test_base_service.rb
|
249
249
|
- test/unit/test_configure_http_proxy.rb
|
250
|
+
- test/unit/test_cp4d_authenticator.rb
|
251
|
+
- test/unit/test_cp4d_token_manager.rb
|
250
252
|
- test/unit/test_detailed_response.rb
|
253
|
+
- test/unit/test_iam_authenticator.rb
|
251
254
|
- test/unit/test_iam_token_manager.rb
|
252
|
-
- test/unit/test_icp4d_token_manager.rb
|
253
255
|
- test/unit/test_jwt_token_manager.rb
|
254
256
|
homepage: https://www.github.com/IBM
|
255
257
|
licenses:
|