ibm_cloud_sdk_core 1.0.0.rc1 → 1.0.0.rc2
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/basic_authenticator.rb +5 -3
- data/lib/ibm_cloud_sdk_core/authenticators/bearer_token_authenticator.rb +3 -2
- data/lib/ibm_cloud_sdk_core/authenticators/cp4d_authenticator.rb +1 -1
- data/lib/ibm_cloud_sdk_core/authenticators/iam_authenticator.rb +10 -11
- data/lib/ibm_cloud_sdk_core/base_service.rb +7 -0
- data/lib/ibm_cloud_sdk_core/token_managers/cp4d_token_manager.rb +5 -0
- data/lib/ibm_cloud_sdk_core/token_managers/iam_token_manager.rb +7 -2
- data/lib/ibm_cloud_sdk_core/token_managers/jwt_token_manager.rb +0 -4
- data/lib/ibm_cloud_sdk_core/version.rb +1 -1
- data/test/unit/test_base_service.rb +29 -1
- data/test/unit/test_iam_token_manager.rb +38 -47
- data/test/unit/test_icp4d_token_manager.rb +23 -12
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 24cf43bb93090b3c2cdb627369b7645ca17eadc7262f16fbb18b40e76f1285b2
|
4
|
+
data.tar.gz: 2d8be9a330480c90327a5b52d6d2121476aaaf85ecb5ef3e3574945b4ceb75f1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1294cd526ded8bccfd89556c9450e7111fcff4ae31162a9f4ea5004d6c718ca360a0b6a09a6f83bc1b3910993f8593d76add495736d361b1c876115df5f35ac9
|
7
|
+
data.tar.gz: de99be13ca0a0ce0e0a4d73852bb7c2f25171717bc654e51d7dca70a834f26c1c05a8cac4bf98b2f14f908e0467ad9cd06e8c3799da8e4dbad0c6457e3752189
|
@@ -7,7 +7,7 @@ require_relative("../utils.rb")
|
|
7
7
|
module IBMCloudSdkCore
|
8
8
|
# Basic Authenticator
|
9
9
|
class BasicAuthenticator < Authenticator
|
10
|
-
attr_accessor :username, :password
|
10
|
+
attr_accessor :username, :password, :authentication_type
|
11
11
|
def initialize(vars)
|
12
12
|
defaults = {
|
13
13
|
username: nil,
|
@@ -21,8 +21,10 @@ module IBMCloudSdkCore
|
|
21
21
|
end
|
22
22
|
|
23
23
|
# Adds the Authorization header, if possible
|
24
|
-
def authenticate(
|
25
|
-
|
24
|
+
def authenticate(headers)
|
25
|
+
base64_authentication = Base64.strict_encode64("#{@username}:#{@password}")
|
26
|
+
headers["Authorization"] = "Basic #{base64_authentication}"
|
27
|
+
headers
|
26
28
|
end
|
27
29
|
|
28
30
|
# Checks if all the inputs needed are present
|
@@ -19,8 +19,9 @@ module IBMCloudSdkCore
|
|
19
19
|
end
|
20
20
|
|
21
21
|
# Adds the Authorization header, if possible
|
22
|
-
def authenticate(
|
23
|
-
|
22
|
+
def authenticate(headers)
|
23
|
+
headers["Authorization"] = "Bearer #{@bearer_token}"
|
24
|
+
headers
|
24
25
|
end
|
25
26
|
|
26
27
|
# Checks if all the inputs needed are present
|
@@ -26,8 +26,9 @@ module IBMCloudSdkCore
|
|
26
26
|
@client_secret = vars[:client_secret]
|
27
27
|
@disable_ssl_verification = vars[:disable_ssl_verification]
|
28
28
|
@authentication_type = AUTH_TYPE_IAM
|
29
|
+
|
29
30
|
validate
|
30
|
-
@token_manager =
|
31
|
+
@token_manager = IAMTokenManager.new(
|
31
32
|
apikey: @apikey,
|
32
33
|
url: @url,
|
33
34
|
client_id: @client_id,
|
@@ -36,8 +37,9 @@ module IBMCloudSdkCore
|
|
36
37
|
)
|
37
38
|
end
|
38
39
|
|
39
|
-
def authenticate(
|
40
|
-
|
40
|
+
def authenticate(headers)
|
41
|
+
headers["Authorization"] = "Bearer #{@token_manager.access_token}"
|
42
|
+
headers
|
41
43
|
end
|
42
44
|
|
43
45
|
def validate
|
@@ -46,14 +48,11 @@ module IBMCloudSdkCore
|
|
46
48
|
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)
|
47
49
|
|
48
50
|
# Both the client id and secret should be provided or neither should be provided.
|
49
|
-
if
|
50
|
-
@
|
51
|
-
@
|
52
|
-
elsif
|
53
|
-
|
54
|
-
@iam_client_secret = DEFAULT_CLIENT_SECRET
|
55
|
-
else
|
56
|
-
raise ArgumentError.new("Only one of 'iam_client_id' or 'iam_client_secret' were specified, but both parameters should be specified together.")
|
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.")
|
57
56
|
end
|
58
57
|
end
|
59
58
|
end
|
@@ -9,6 +9,12 @@ require_relative("./detailed_response.rb")
|
|
9
9
|
require_relative("./api_exception.rb")
|
10
10
|
require_relative("./utils.rb")
|
11
11
|
require_relative("./authenticators/authenticator")
|
12
|
+
require_relative("./authenticators/basic_authenticator")
|
13
|
+
require_relative("./authenticators/bearer_token_authenticator")
|
14
|
+
require_relative("./authenticators/config_based_authenticator_factory")
|
15
|
+
require_relative("./authenticators/iam_authenticator")
|
16
|
+
require_relative("./authenticators/cp4d_authenticator")
|
17
|
+
require_relative("./authenticators/no_auth_authenticator")
|
12
18
|
|
13
19
|
NORMALIZER = lambda do |uri| # Custom URI normalizer when using HTTP Client
|
14
20
|
HTTP::URI.parse uri
|
@@ -37,6 +43,7 @@ module IBMCloudSdkCore
|
|
37
43
|
@url = config[:url] unless config.nil?
|
38
44
|
end
|
39
45
|
|
46
|
+
@temp_headers = {}
|
40
47
|
@conn = HTTP::Client.new(
|
41
48
|
headers: {}
|
42
49
|
).use normalize_uri: { normalizer: NORMALIZER }
|
@@ -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, :
|
19
|
+
attr_accessor :token_info, :token_name
|
20
20
|
def initialize(
|
21
21
|
apikey: nil,
|
22
22
|
url: nil,
|
@@ -25,11 +25,16 @@ module IBMCloudSdkCore
|
|
25
25
|
disable_ssl_verification: nil
|
26
26
|
)
|
27
27
|
@apikey = apikey
|
28
|
-
|
28
|
+
url = DEFAULT_IAM_URL if url.nil?
|
29
29
|
@client_id = client_id
|
30
30
|
@client_secret = client_secret
|
31
31
|
@disable_ssl_verification = disable_ssl_verification
|
32
32
|
super(url: url, token_name: TOKEN_NAME)
|
33
|
+
token
|
34
|
+
end
|
35
|
+
|
36
|
+
def access_token
|
37
|
+
@token_info[TOKEN_NAME]
|
33
38
|
end
|
34
39
|
|
35
40
|
private
|
@@ -179,10 +179,38 @@ class BaseServiceTest < Minitest::Test
|
|
179
179
|
end
|
180
180
|
|
181
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: {})
|
182
210
|
authenticator = IBMCloudSdkCore::CloudPakForDataAuthenticator.new(
|
183
211
|
username: "hello",
|
184
212
|
password: "world",
|
185
|
-
url: "hello.world"
|
213
|
+
url: "https://hello.world"
|
186
214
|
)
|
187
215
|
refute_nil(authenticator)
|
188
216
|
end
|
@@ -8,21 +8,26 @@ 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
|
+
token_layout = {
|
12
|
+
"username": "dummy",
|
13
|
+
"role": "Admin",
|
14
|
+
"permissions": %w[administrator manage_catalog],
|
15
|
+
"sub": "admin",
|
16
|
+
"iss": "sss",
|
17
|
+
"aud": "sss",
|
18
|
+
"uid": "sss",
|
19
|
+
"iat": Time.now.to_i + 3600,
|
20
|
+
"exp": Time.now.to_i
|
21
|
+
}
|
22
|
+
token = JWT.encode token_layout, "secret", "HS256"
|
11
23
|
response = {
|
12
|
-
"access_token" =>
|
24
|
+
"access_token" => token,
|
13
25
|
"token_type" => "Bearer",
|
14
26
|
"expires_in" => 3600,
|
15
27
|
"expiration" => 1_524_167_011,
|
16
28
|
"refresh_token" => "jy4gl91BQ"
|
17
29
|
}
|
18
30
|
|
19
|
-
# Use default iam_url, client id/secret
|
20
|
-
token_manager = IBMCloudSdkCore::IAMTokenManager.new(
|
21
|
-
apikey: "apikey",
|
22
|
-
url: "https://iam.cloud.ibm.com/identity/token",
|
23
|
-
client_id: "bx",
|
24
|
-
client_secret: "bx"
|
25
|
-
)
|
26
31
|
stub_request(:post, "https://iam.cloud.ibm.com/identity/token")
|
27
32
|
.with(
|
28
33
|
body: { "apikey" => "apikey", "grant_type" => "urn:ibm:params:oauth:grant-type:apikey", "response_type" => "cloud_iam" },
|
@@ -33,16 +38,19 @@ class IAMTokenManagerTest < Minitest::Test
|
|
33
38
|
"Host" => "iam.cloud.ibm.com"
|
34
39
|
}
|
35
40
|
).to_return(status: 200, body: response.to_json, headers: {})
|
41
|
+
# Use default iam_url, client id/secret
|
42
|
+
token_manager = IBMCloudSdkCore::IAMTokenManager.new(
|
43
|
+
apikey: "apikey",
|
44
|
+
url: "https://iam.cloud.ibm.com/identity/token",
|
45
|
+
client_id: "bx",
|
46
|
+
client_secret: "bx"
|
47
|
+
)
|
36
48
|
token_response = token_manager.send(:request_token)
|
37
49
|
assert_equal(response, token_response)
|
38
50
|
end
|
39
51
|
|
40
52
|
def test_request_token_fails
|
41
53
|
iam_url = "https://iam.cloud.ibm.com/identity/token"
|
42
|
-
token_manager = IBMCloudSdkCore::IAMTokenManager.new(
|
43
|
-
apikey: "apikey",
|
44
|
-
url: iam_url
|
45
|
-
)
|
46
54
|
response = {
|
47
55
|
"code" => "500",
|
48
56
|
"error" => "Oh no"
|
@@ -58,17 +66,14 @@ class IAMTokenManagerTest < Minitest::Test
|
|
58
66
|
}
|
59
67
|
).to_return(status: 500, body: response.to_json, headers: {})
|
60
68
|
assert_raises do
|
61
|
-
|
69
|
+
IBMCloudSdkCore::IAMTokenManager.new(
|
70
|
+
apikey: "apikey",
|
71
|
+
url: iam_url
|
72
|
+
)
|
62
73
|
end
|
63
74
|
end
|
64
75
|
|
65
76
|
def test_request_token_fails_catch_exception
|
66
|
-
token_manager = IBMCloudSdkCore::IAMTokenManager.new(
|
67
|
-
apikey: "apikey",
|
68
|
-
url: "https://iam.cloud.ibm.com/identity/token",
|
69
|
-
client_id: "bx",
|
70
|
-
client_secret: "bx"
|
71
|
-
)
|
72
77
|
response = {
|
73
78
|
"code" => "500",
|
74
79
|
"error" => "Oh no"
|
@@ -84,38 +89,18 @@ class IAMTokenManagerTest < Minitest::Test
|
|
84
89
|
}
|
85
90
|
).to_return(status: 401, body: response.to_json, headers: {})
|
86
91
|
begin
|
87
|
-
|
92
|
+
IBMCloudSdkCore::IAMTokenManager.new(
|
93
|
+
apikey: "apikey",
|
94
|
+
url: "https://iam.cloud.ibm.com/identity/token",
|
95
|
+
client_id: "bx",
|
96
|
+
client_secret: "bx"
|
97
|
+
)
|
88
98
|
rescue IBMCloudSdkCore::ApiException => e
|
89
99
|
assert(e.to_s.instance_of?(String))
|
90
100
|
end
|
91
101
|
end
|
92
102
|
|
93
|
-
def test_is_token_expired
|
94
|
-
token_manager = IBMCloudSdkCore::IAMTokenManager.new(
|
95
|
-
apikey: "apikey",
|
96
|
-
url: "https://url.com",
|
97
|
-
client_id: "bx",
|
98
|
-
client_secret: "bx"
|
99
|
-
)
|
100
|
-
|
101
|
-
assert(token_manager.send(:token_expired?))
|
102
|
-
token_manager.instance_variable_set(:@time_to_live, 3600)
|
103
|
-
token_manager.instance_variable_set(:@expire_time, Time.now.to_i + 6000)
|
104
|
-
refute(token_manager.send(:token_expired?))
|
105
|
-
token_manager.instance_variable_set(:@time_to_live, 3600)
|
106
|
-
token_manager.instance_variable_set(:@expire_time, Time.now.to_i - 3600)
|
107
|
-
assert(token_manager.send(:token_expired?))
|
108
|
-
end
|
109
|
-
|
110
103
|
def test_get_token
|
111
|
-
iam_url = "https://iam.cloud.ibm.com/identity/token"
|
112
|
-
token_manager = IBMCloudSdkCore::IAMTokenManager.new(
|
113
|
-
apikey: "apikey",
|
114
|
-
url: iam_url,
|
115
|
-
client_id: "bx",
|
116
|
-
client_secret: "bx"
|
117
|
-
)
|
118
|
-
|
119
104
|
access_token_layout = {
|
120
105
|
"username" => "dummy",
|
121
106
|
"role" => "Admin",
|
@@ -148,8 +133,14 @@ class IAMTokenManagerTest < Minitest::Test
|
|
148
133
|
"Host" => "iam.cloud.ibm.com"
|
149
134
|
}
|
150
135
|
).to_return(status: 200, body: response.to_json, headers: {})
|
151
|
-
|
152
|
-
|
136
|
+
iam_url = "https://iam.cloud.ibm.com/identity/token"
|
137
|
+
token_manager = IBMCloudSdkCore::IAMTokenManager.new(
|
138
|
+
apikey: "apikey",
|
139
|
+
url: iam_url,
|
140
|
+
client_id: "bx",
|
141
|
+
client_secret: "bx"
|
142
|
+
)
|
143
|
+
assert_equal(token_manager.access_token, access_token)
|
153
144
|
end
|
154
145
|
|
155
146
|
def test_client_id_only
|
@@ -8,19 +8,25 @@ WebMock.disable_net_connect!(allow_localhost: true)
|
|
8
8
|
# Unit tests for the CP4D Token Manager
|
9
9
|
class CP4DTokenManagerTest < Minitest::Test
|
10
10
|
def test_request_token
|
11
|
+
token_layout = {
|
12
|
+
"username": "dummy",
|
13
|
+
"role": "Admin",
|
14
|
+
"permissions": %w[administrator manage_catalog],
|
15
|
+
"sub": "admin",
|
16
|
+
"iss": "sss",
|
17
|
+
"aud": "sss",
|
18
|
+
"uid": "sss",
|
19
|
+
"iat": Time.now.to_i + 3600,
|
20
|
+
"exp": Time.now.to_i
|
21
|
+
}
|
22
|
+
token = JWT.encode token_layout, "secret", "HS256"
|
11
23
|
response = {
|
12
|
-
"
|
24
|
+
"accessToken" => token,
|
13
25
|
"token_type" => "Bearer",
|
14
26
|
"expires_in" => 3600,
|
15
27
|
"expiration" => 1_524_167_011,
|
16
28
|
"refresh_token" => "jy4gl91BQ"
|
17
29
|
}
|
18
|
-
|
19
|
-
token_manager = IBMCloudSdkCore::CP4DTokenManager.new(
|
20
|
-
url: "https://the.sixth.one",
|
21
|
-
username: "you",
|
22
|
-
password: "me"
|
23
|
-
)
|
24
30
|
stub_request(:get, "https://the.sixth.one/v1/preauth/validateAuth")
|
25
31
|
.with(
|
26
32
|
headers: {
|
@@ -28,16 +34,17 @@ class CP4DTokenManagerTest < Minitest::Test
|
|
28
34
|
"Host" => "the.sixth.one"
|
29
35
|
}
|
30
36
|
).to_return(status: 200, body: response.to_json, headers: {})
|
31
|
-
token_response = token_manager.send(:request_token)
|
32
|
-
assert_equal(response, token_response)
|
33
|
-
end
|
34
37
|
|
35
|
-
def test_request_token_fails
|
36
38
|
token_manager = IBMCloudSdkCore::CP4DTokenManager.new(
|
37
39
|
url: "https://the.sixth.one",
|
38
40
|
username: "you",
|
39
41
|
password: "me"
|
40
42
|
)
|
43
|
+
token_response = token_manager.send(:request_token)
|
44
|
+
assert_equal(response, token_response)
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_request_token_fails
|
41
48
|
response = {
|
42
49
|
"code" => "500",
|
43
50
|
"error" => "Oh no"
|
@@ -50,7 +57,11 @@ class CP4DTokenManagerTest < Minitest::Test
|
|
50
57
|
}
|
51
58
|
).to_return(status: 500, body: response.to_json, headers: {})
|
52
59
|
begin
|
53
|
-
|
60
|
+
IBMCloudSdkCore::CP4DTokenManager.new(
|
61
|
+
url: "https://the.sixth.one",
|
62
|
+
username: "you",
|
63
|
+
password: "me"
|
64
|
+
)
|
54
65
|
rescue IBMCloudSdkCore::ApiException => e
|
55
66
|
assert(e.to_s.instance_of?(String))
|
56
67
|
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.rc2
|
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-08-
|
11
|
+
date: 2019-08-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: concurrent-ruby
|