train-rest 0.4.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE +202 -0
- data/lib/train-rest/auth_handler/awsv4.rb +119 -0
- data/lib/train-rest/auth_handler/hmac-signature.rb +39 -0
- data/lib/train-rest/auth_handler.rb +26 -1
- data/lib/train-rest/connection.rb +28 -12
- data/lib/train-rest/errors.rb +6 -0
- data/lib/train-rest/version.rb +1 -1
- data/lib/train-rest.rb +5 -1
- metadata +25 -10
- data/Gemfile +0 -3
- data/README.md +0 -280
- data/train-rest.gemspec +0 -30
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 73a28c27d6e8385d3d8356c363847ddae1380ff0d0e75d6f919f64cefb315637
|
4
|
+
data.tar.gz: 1b0140ce7079d48ac070b05607acc1b142c987cd598a7f60fdc28ff3025dc3f6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 293e8e03b838b888108eb25ac164080f76aa0291420bcd04feb8503245c3149ff3d03257ad01df3ea3d2fae018094a0ec85c1e4a3b052b1a07c3cc0db69b0244
|
7
|
+
data.tar.gz: a816054a1e6db4bd24134249adaedd933b16b9796c3cfde7afa90f4e66ea70d9cdf744bea124ee2e93b383d6ad4b666e4bdd2a4ae363fa09056bad3eb85ada6f
|
data/LICENSE
ADDED
@@ -0,0 +1,202 @@
|
|
1
|
+
|
2
|
+
Apache License
|
3
|
+
Version 2.0, January 2004
|
4
|
+
http://www.apache.org/licenses/
|
5
|
+
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
7
|
+
|
8
|
+
1. Definitions.
|
9
|
+
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
12
|
+
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
14
|
+
the copyright owner that is granting the License.
|
15
|
+
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
17
|
+
other entities that control, are controlled by, or are under common
|
18
|
+
control with that entity. For the purposes of this definition,
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
20
|
+
direction or management of such entity, whether by contract or
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
23
|
+
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
25
|
+
exercising permissions granted by this License.
|
26
|
+
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
28
|
+
including but not limited to software source code, documentation
|
29
|
+
source, and configuration files.
|
30
|
+
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
32
|
+
transformation or translation of a Source form, including but
|
33
|
+
not limited to compiled object code, generated documentation,
|
34
|
+
and conversions to other media types.
|
35
|
+
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
37
|
+
Object form, made available under the License, as indicated by a
|
38
|
+
copyright notice that is included in or attached to the work
|
39
|
+
(an example is provided in the Appendix below).
|
40
|
+
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
47
|
+
the Work and Derivative Works thereof.
|
48
|
+
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
50
|
+
the original version of the Work and any modifications or additions
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
62
|
+
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
65
|
+
subsequently incorporated within the Work.
|
66
|
+
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
73
|
+
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
79
|
+
where such license applies only to those patent claims licensable
|
80
|
+
by such Contributor that are necessarily infringed by their
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
83
|
+
institute patent litigation against any entity (including a
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
86
|
+
or contributory patent infringement, then any patent licenses
|
87
|
+
granted to You under this License for that Work shall terminate
|
88
|
+
as of the date such litigation is filed.
|
89
|
+
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
92
|
+
modifications, and in Source or Object form, provided that You
|
93
|
+
meet the following conditions:
|
94
|
+
|
95
|
+
(a) You must give any other recipients of the Work or
|
96
|
+
Derivative Works a copy of this License; and
|
97
|
+
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
99
|
+
stating that You changed the files; and
|
100
|
+
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
103
|
+
attribution notices from the Source form of the Work,
|
104
|
+
excluding those notices that do not pertain to any part of
|
105
|
+
the Derivative Works; and
|
106
|
+
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
109
|
+
include a readable copy of the attribution notices contained
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
112
|
+
of the following places: within a NOTICE text file distributed
|
113
|
+
as part of the Derivative Works; within the Source form or
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
115
|
+
within a display generated by the Derivative Works, if and
|
116
|
+
wherever such third-party notices normally appear. The contents
|
117
|
+
of the NOTICE file are for informational purposes only and
|
118
|
+
do not modify the License. You may add Your own attribution
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
121
|
+
that such additional attribution notices cannot be construed
|
122
|
+
as modifying the License.
|
123
|
+
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
125
|
+
may provide additional or different license terms and conditions
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
129
|
+
the conditions stated in this License.
|
130
|
+
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
134
|
+
this License, without any additional terms or conditions.
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
136
|
+
the terms of any separate license agreement you may have executed
|
137
|
+
with Licensor regarding such Contributions.
|
138
|
+
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
141
|
+
except as required for reasonable and customary use in describing the
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
143
|
+
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
153
|
+
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
159
|
+
incidental, or consequential damages of any character arising as a
|
160
|
+
result of this License or out of the use or inability to use the
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
163
|
+
other commercial damages or losses), even if such Contributor
|
164
|
+
has been advised of the possibility of such damages.
|
165
|
+
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
169
|
+
or other liability obligations and/or rights consistent with this
|
170
|
+
License. However, in accepting such obligations, You may act only
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
175
|
+
of your accepting any such warranty or additional liability.
|
176
|
+
|
177
|
+
END OF TERMS AND CONDITIONS
|
178
|
+
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
180
|
+
|
181
|
+
To apply the Apache License to your work, attach the following
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
183
|
+
replaced with your own identifying information. (Don't include
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
185
|
+
comment syntax for the file format. We also recommend that a
|
186
|
+
file or class name and description of purpose be included on the
|
187
|
+
same "printed page" as the copyright notice for easier
|
188
|
+
identification within third-party archives.
|
189
|
+
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
191
|
+
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
193
|
+
you may not use this file except in compliance with the License.
|
194
|
+
You may obtain a copy of the License at
|
195
|
+
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
197
|
+
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
201
|
+
See the License for the specific language governing permissions and
|
202
|
+
limitations under the License.
|
@@ -0,0 +1,119 @@
|
|
1
|
+
require 'aws-sigv4'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
require_relative "../auth_handler"
|
5
|
+
|
6
|
+
module TrainPlugins
|
7
|
+
module Rest
|
8
|
+
class AWSV4 < AuthHandler
|
9
|
+
VALID_CREDENTIALS = %w[
|
10
|
+
access_keys
|
11
|
+
].freeze
|
12
|
+
|
13
|
+
SIGNED_HEADERS = %w[
|
14
|
+
content-type host x-amz-date x-amz-target
|
15
|
+
].freeze
|
16
|
+
|
17
|
+
def check_options
|
18
|
+
options[:credentials] ||= "access_keys"
|
19
|
+
|
20
|
+
unless VALID_CREDENTIALS.include? credentials
|
21
|
+
raise ArgumentError.new("Invalid type of credentials: #{credentials}")
|
22
|
+
end
|
23
|
+
|
24
|
+
if access_keys?
|
25
|
+
raise ArgumentError.new('Missing `access_key` credential') unless access_key
|
26
|
+
raise ArgumentError.new('Missing `secret_access_key` credential') unless secret_access_key
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def signature_based?
|
31
|
+
true
|
32
|
+
end
|
33
|
+
|
34
|
+
def process(payload: "", headers: {}, url: "", method: nil)
|
35
|
+
headers.merge! ({
|
36
|
+
'Accept-Encoding' => 'identity',
|
37
|
+
'User-Agent' => "train-rest/#{TrainPlugins::Rest::VERSION}",
|
38
|
+
'Content-Type' => 'application/x-amz-json-1.0'
|
39
|
+
})
|
40
|
+
|
41
|
+
signed_headers = headers.select do |name, _value|
|
42
|
+
SIGNED_HEADERS.include? name.downcase
|
43
|
+
end
|
44
|
+
|
45
|
+
@url = url
|
46
|
+
|
47
|
+
signature = signer(url).sign_request(
|
48
|
+
http_method: method.to_s.upcase,
|
49
|
+
url: url,
|
50
|
+
headers: signed_headers,
|
51
|
+
body: payload.to_json
|
52
|
+
)
|
53
|
+
|
54
|
+
{
|
55
|
+
headers: headers.merge(signature.headers)
|
56
|
+
}
|
57
|
+
end
|
58
|
+
|
59
|
+
def process_error(error)
|
60
|
+
raise AuthenticationError.new("Authentication failed: #{error.response.to_s.chop}") if error.response.code == 401
|
61
|
+
raise BadRequest.new("Bad request: #{error.response.to_s.chop}") if error.response.code == 400
|
62
|
+
|
63
|
+
message = JSON.parse(error.response.to_s)
|
64
|
+
|
65
|
+
raise AuthenticationError.new(message["message"] || message["__type"])
|
66
|
+
rescue JSON::ParserError => e
|
67
|
+
raise AuthenticationError.new(error.response.to_s)
|
68
|
+
end
|
69
|
+
|
70
|
+
def access_key
|
71
|
+
options[:access_key] || ENV['AWS_ACCESS_KEY_ID']
|
72
|
+
end
|
73
|
+
|
74
|
+
def region(url = default_url)
|
75
|
+
url.delete_prefix('https://').split('.').at(1)
|
76
|
+
end
|
77
|
+
|
78
|
+
private
|
79
|
+
|
80
|
+
def credentials
|
81
|
+
options[:credentials]
|
82
|
+
end
|
83
|
+
|
84
|
+
def default_url
|
85
|
+
options[:endpoint]
|
86
|
+
end
|
87
|
+
|
88
|
+
def access_keys?
|
89
|
+
credentials == 'access_keys'
|
90
|
+
end
|
91
|
+
|
92
|
+
def secret_access_key
|
93
|
+
options[:secret_access_key] || ENV['AWS_SECRET_ACCESS_KEY']
|
94
|
+
end
|
95
|
+
|
96
|
+
def service(url)
|
97
|
+
url.delete_prefix('https://').split('.').at(0)
|
98
|
+
end
|
99
|
+
|
100
|
+
def signer(url)
|
101
|
+
Aws::Sigv4::Signer.new(
|
102
|
+
service: service(url),
|
103
|
+
region: region(url),
|
104
|
+
|
105
|
+
**signer_credentials
|
106
|
+
)
|
107
|
+
end
|
108
|
+
|
109
|
+
def signer_credentials
|
110
|
+
if access_keys?
|
111
|
+
{
|
112
|
+
access_key_id: access_key,
|
113
|
+
secret_access_key: secret_access_key
|
114
|
+
}
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require_relative "../auth_handler"
|
2
|
+
|
3
|
+
module TrainPlugins
|
4
|
+
module Rest
|
5
|
+
# Authentication via HMAC Signature.
|
6
|
+
class HmacSignature < AuthHandler
|
7
|
+
def check_options
|
8
|
+
raise ArgumentError.new("Need :hmac_secret for HMAC signatures") unless options[:hmac_secret]
|
9
|
+
|
10
|
+
options[:header] ||= "X-Signature"
|
11
|
+
options[:digest] ||= "SHA256"
|
12
|
+
end
|
13
|
+
|
14
|
+
def hmac_secret
|
15
|
+
options[:hmac_secret]
|
16
|
+
end
|
17
|
+
|
18
|
+
def digest
|
19
|
+
options[:digest]
|
20
|
+
end
|
21
|
+
|
22
|
+
def header
|
23
|
+
options[:header]
|
24
|
+
end
|
25
|
+
|
26
|
+
def signature_based?
|
27
|
+
true
|
28
|
+
end
|
29
|
+
|
30
|
+
def process(payload: "", headers: {}, url: "", method: nil)
|
31
|
+
{
|
32
|
+
headers: {
|
33
|
+
header => OpenSSL::HMAC.hexdigest(digest, hmac_secret, payload)
|
34
|
+
}
|
35
|
+
}
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -67,6 +67,30 @@ module TrainPlugins
|
|
67
67
|
{ headers: auth_headers }
|
68
68
|
end
|
69
69
|
|
70
|
+
# This Auth Handler will need payload, URI and headers, e.g. for signatures.
|
71
|
+
#
|
72
|
+
# @return [Boolean]
|
73
|
+
def signature_based?
|
74
|
+
false
|
75
|
+
end
|
76
|
+
|
77
|
+
# Return headers based on payload signing.
|
78
|
+
#
|
79
|
+
# @param [Hash] data different types of data for processing
|
80
|
+
# @option data [String] :payload contents of the message body
|
81
|
+
# @option data [Hash] :headers existing headers to the request
|
82
|
+
# @option data [String] :url URL which will be requested
|
83
|
+
# @option data [Symbol] :method Method to execute
|
84
|
+
# @returns [Hash]
|
85
|
+
def process(payload: "", headers: {}, url: "", method: nil)
|
86
|
+
{}
|
87
|
+
end
|
88
|
+
|
89
|
+
# Allow processing errors related to authentication.
|
90
|
+
#
|
91
|
+
# @param [RestClient::Exception] error raw error data
|
92
|
+
def process_error(_error); end
|
93
|
+
|
70
94
|
class << self
|
71
95
|
private
|
72
96
|
|
@@ -77,7 +101,8 @@ module TrainPlugins
|
|
77
101
|
# @see https://github.com/chef/chef/blob/main/lib/chef/mixin/convert_to_class_name.rb
|
78
102
|
def convert_to_snake_case(str)
|
79
103
|
str = str.dup
|
80
|
-
str.gsub!(/[A-Z]
|
104
|
+
str.gsub!(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
|
105
|
+
str.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
|
81
106
|
str.downcase!
|
82
107
|
str.sub!(/^\_/, "")
|
83
108
|
str
|
@@ -114,6 +114,21 @@ module TrainPlugins
|
|
114
114
|
# Merge override headers + request specific headers
|
115
115
|
parameters[:headers].merge!(override_headers || {})
|
116
116
|
parameters[:headers].merge!(headers)
|
117
|
+
|
118
|
+
# Merge payload based headers (e.g. signature-based auth)
|
119
|
+
if auth_handler.signature_based?
|
120
|
+
auth_signature = auth_handler.process(
|
121
|
+
payload: data,
|
122
|
+
headers: parameters[:headers],
|
123
|
+
url: parameters[:url],
|
124
|
+
method: method
|
125
|
+
)
|
126
|
+
|
127
|
+
parameters[:headers].merge! auth_signature[:headers]
|
128
|
+
else
|
129
|
+
parameters[:headers].merge! auth_parameters[:headers]
|
130
|
+
end
|
131
|
+
|
117
132
|
parameters.compact!
|
118
133
|
|
119
134
|
logger.info format("[REST] => %s", parameters.to_s) if options[:debug_rest]
|
@@ -121,6 +136,9 @@ module TrainPlugins
|
|
121
136
|
|
122
137
|
logger.info format("[REST] <= %s", response.to_s) if options[:debug_rest]
|
123
138
|
transform_response(response, json_processing)
|
139
|
+
|
140
|
+
rescue RestClient::Exception => error
|
141
|
+
auth_handler.process_error(error)
|
124
142
|
end
|
125
143
|
|
126
144
|
# Allow switching generic handlers for an API-specific one.
|
@@ -144,12 +162,21 @@ module TrainPlugins
|
|
144
162
|
options[:auth_type]
|
145
163
|
end
|
146
164
|
|
165
|
+
attr_writer :auth_handler
|
166
|
+
|
147
167
|
# Auth Handlers-faced API
|
148
168
|
|
149
169
|
def auth_parameters
|
150
170
|
auth_handler.auth_parameters
|
151
171
|
end
|
152
172
|
|
173
|
+
def auth_handler
|
174
|
+
desired_handler = auth_handler_classes.detect { |handler| handler.name == auth_type.to_s }
|
175
|
+
raise NameError.new(format("Authentication handler %s not found", auth_type.to_s)) unless desired_handler
|
176
|
+
|
177
|
+
@auth_handler ||= desired_handler.new(self)
|
178
|
+
end
|
179
|
+
|
153
180
|
private
|
154
181
|
|
155
182
|
def global_parameters
|
@@ -160,8 +187,6 @@ module TrainPlugins
|
|
160
187
|
headers: options[:headers],
|
161
188
|
}
|
162
189
|
|
163
|
-
params.merge!(auth_parameters)
|
164
|
-
|
165
190
|
params
|
166
191
|
end
|
167
192
|
|
@@ -184,23 +209,14 @@ module TrainPlugins
|
|
184
209
|
:basic if options[:username] && options[:password]
|
185
210
|
end
|
186
211
|
|
187
|
-
attr_writer :auth_handler
|
188
|
-
|
189
212
|
def auth_handler_classes
|
190
|
-
AuthHandler.descendants
|
213
|
+
::TrainPlugins::Rest::AuthHandler.descendants
|
191
214
|
end
|
192
215
|
|
193
216
|
def auth_handlers
|
194
217
|
auth_handler_classes.map { |handler| handler.name.to_sym }
|
195
218
|
end
|
196
219
|
|
197
|
-
def auth_handler
|
198
|
-
desired_handler = auth_handler_classes.detect { |handler| handler.name == auth_type.to_s }
|
199
|
-
raise NameError.new(format("Authentication handler %s not found", auth_type.to_s)) unless desired_handler
|
200
|
-
|
201
|
-
@auth_handler ||= desired_handler.new(self)
|
202
|
-
end
|
203
|
-
|
204
220
|
def login
|
205
221
|
logger.info format("REST Login via %s authentication handler", auth_type.to_s) unless %i{anonymous basic}.include? auth_type
|
206
222
|
|
data/lib/train-rest/version.rb
CHANGED
data/lib/train-rest.rb
CHANGED
@@ -1,14 +1,18 @@
|
|
1
1
|
libdir = File.dirname(__FILE__)
|
2
2
|
$LOAD_PATH.unshift(libdir) unless $LOAD_PATH.include?(libdir)
|
3
3
|
|
4
|
+
require "train-rest/errors"
|
4
5
|
require "train-rest/version"
|
5
6
|
|
6
7
|
require "train-rest/transport"
|
7
8
|
require "train-rest/connection"
|
8
9
|
|
10
|
+
require "train-rest/auth_handler"
|
11
|
+
require "train-rest/auth_handler/awsv4"
|
9
12
|
require "train-rest/auth_handler/anonymous"
|
10
13
|
require "train-rest/auth_handler/authtype-apikey"
|
11
|
-
require "train-rest/auth_handler/header"
|
12
14
|
require "train-rest/auth_handler/basic"
|
13
15
|
require "train-rest/auth_handler/bearer"
|
16
|
+
require "train-rest/auth_handler/header"
|
17
|
+
require "train-rest/auth_handler/hmac-signature"
|
14
18
|
require "train-rest/auth_handler/redfish"
|
metadata
CHANGED
@@ -1,17 +1,31 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: train-rest
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Thomas Heinen
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-09-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: aws-sigv4
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.5'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.5'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: train-core
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
16
30
|
requirements:
|
17
31
|
- - "~>"
|
@@ -58,14 +72,14 @@ dependencies:
|
|
58
72
|
requirements:
|
59
73
|
- - "~>"
|
60
74
|
- !ruby/object:Gem::Version
|
61
|
-
version: '
|
75
|
+
version: '2.2'
|
62
76
|
type: :development
|
63
77
|
prerelease: false
|
64
78
|
version_requirements: !ruby/object:Gem::Requirement
|
65
79
|
requirements:
|
66
80
|
- - "~>"
|
67
81
|
- !ruby/object:Gem::Version
|
68
|
-
version: '
|
82
|
+
version: '2.2'
|
69
83
|
- !ruby/object:Gem::Dependency
|
70
84
|
name: guard
|
71
85
|
requirement: !ruby/object:Gem::Requirement
|
@@ -115,21 +129,22 @@ executables: []
|
|
115
129
|
extensions: []
|
116
130
|
extra_rdoc_files: []
|
117
131
|
files:
|
118
|
-
-
|
119
|
-
- README.md
|
132
|
+
- LICENSE
|
120
133
|
- lib/train-rest.rb
|
121
134
|
- lib/train-rest/auth_handler.rb
|
122
135
|
- lib/train-rest/auth_handler/anonymous.rb
|
123
136
|
- lib/train-rest/auth_handler/authtype-apikey.rb
|
137
|
+
- lib/train-rest/auth_handler/awsv4.rb
|
124
138
|
- lib/train-rest/auth_handler/basic.rb
|
125
139
|
- lib/train-rest/auth_handler/bearer.rb
|
126
140
|
- lib/train-rest/auth_handler/header.rb
|
141
|
+
- lib/train-rest/auth_handler/hmac-signature.rb
|
127
142
|
- lib/train-rest/auth_handler/redfish.rb
|
128
143
|
- lib/train-rest/connection.rb
|
144
|
+
- lib/train-rest/errors.rb
|
129
145
|
- lib/train-rest/transport.rb
|
130
146
|
- lib/train-rest/version.rb
|
131
|
-
-
|
132
|
-
homepage: https://github.com/sgre-chef/train-rest
|
147
|
+
homepage: https://github.com/tecracer-chef/train-rest
|
133
148
|
licenses:
|
134
149
|
- Apache-2.0
|
135
150
|
metadata: {}
|
@@ -148,7 +163,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
148
163
|
- !ruby/object:Gem::Version
|
149
164
|
version: '0'
|
150
165
|
requirements: []
|
151
|
-
rubygems_version: 3.0.3
|
166
|
+
rubygems_version: 3.0.3.1
|
152
167
|
signing_key:
|
153
168
|
specification_version: 4
|
154
169
|
summary: Train transport for REST
|
data/Gemfile
DELETED
data/README.md
DELETED
@@ -1,280 +0,0 @@
|
|
1
|
-
# train-rest - Train transport
|
2
|
-
|
3
|
-
Provides a transport to communicate easily with RESTful APIs.
|
4
|
-
|
5
|
-
## Requirements
|
6
|
-
|
7
|
-
- Gem `rest-client` in Version 2.1
|
8
|
-
|
9
|
-
## Installation
|
10
|
-
|
11
|
-
You will have to build this gem yourself to install it as it is not yet on
|
12
|
-
Rubygems.Org. For this there is a rake task which makes this a one-liner:
|
13
|
-
|
14
|
-
```bash
|
15
|
-
rake install:local
|
16
|
-
```
|
17
|
-
|
18
|
-
## Transport parameters
|
19
|
-
|
20
|
-
| Option | Explanation | Default |
|
21
|
-
| -------------------- | --------------------------------------- | ----------- |
|
22
|
-
| `endpoint` | Endpoint of the REST API | _required_ |
|
23
|
-
| `verify_ssl` | Check certificate and chain | true |
|
24
|
-
| `auth_type` | Authentication type | `anonymous` |
|
25
|
-
| `debug_rest` | Enable debugging of HTTP traffic | false |
|
26
|
-
| `logger` | Alternative logging class | |
|
27
|
-
|
28
|
-
## Authenticators
|
29
|
-
|
30
|
-
### Anonymous
|
31
|
-
|
32
|
-
Identifier: `auth_type: :anonymous`
|
33
|
-
|
34
|
-
No actions for authentication, logging in/out or session handing are made. This
|
35
|
-
assumes a public API.
|
36
|
-
|
37
|
-
### Authtype Apikey
|
38
|
-
|
39
|
-
This will inject a HTTP header `Authorization: Apikey xxxx` with the passed
|
40
|
-
API key into requests.
|
41
|
-
|
42
|
-
Identifier: `auth_type: :authtype_apikey`
|
43
|
-
|
44
|
-
| Option | Explanation | Default |
|
45
|
-
| -------------------- | --------------------------------------- | ----------- |
|
46
|
-
| `apikey` | API Key for authentication | _required_ |
|
47
|
-
|
48
|
-
### Basic (RFC 2617)
|
49
|
-
|
50
|
-
Identifier: `auth_type: :basic`
|
51
|
-
|
52
|
-
| Option | Explanation | Default |
|
53
|
-
| -------------------- | --------------------------------------- | ----------- |
|
54
|
-
| `username` | Username for `basic` authentication | _required_ |
|
55
|
-
| `password` | Password for `basic` authentication | _required_ |
|
56
|
-
|
57
|
-
If you supply a `username` and a `password`, authentication will automatically
|
58
|
-
switch to `basic`.
|
59
|
-
|
60
|
-
### Bearer (RFC 7650)
|
61
|
-
|
62
|
-
This will inject a HTTP header `Authorization: Bearer xxxx` with the passed
|
63
|
-
token into requests.
|
64
|
-
|
65
|
-
Identifier: `auth_type: :bearer`
|
66
|
-
|
67
|
-
| Option | Explanation | Default |
|
68
|
-
| -------------------- | --------------------------------------- | ----------- |
|
69
|
-
| `token` | Tokenb to pass | _required_ |
|
70
|
-
|
71
|
-
### Header-based
|
72
|
-
|
73
|
-
This will inject an additional HTTP header with the passed value. If no name
|
74
|
-
for the header is passed, it will default to `X-API-Key`.
|
75
|
-
|
76
|
-
Identifier: `auth_type: :header`
|
77
|
-
|
78
|
-
| Option | Explanation | Default |
|
79
|
-
| -------------------- | --------------------------------------- | ----------- |
|
80
|
-
| `apikey` | API Key for authentication | _required_ |
|
81
|
-
| `header` | Name of the HTTP header to include | `X-API-Key` |
|
82
|
-
|
83
|
-
### Redfish
|
84
|
-
|
85
|
-
Identifier: `auth_type: :redfish`
|
86
|
-
|
87
|
-
| Option | Explanation | Default |
|
88
|
-
| -------------------- | --------------------------------------- | ----------- |
|
89
|
-
| `username` | Username for `redfish` authentication | _required_ |
|
90
|
-
| `password` | Password for `redfish` authentication | _required_ |
|
91
|
-
|
92
|
-
For access to integrated management controllers on standardized server hardware.
|
93
|
-
The Redfish standard is defined in <http://www.dmtf.org/standards/redfish> and
|
94
|
-
this handler does initial login, reuses the received session and logs out when
|
95
|
-
closing the transport cleanly.
|
96
|
-
|
97
|
-
Known vendors which implement RedFish based management for their systems include
|
98
|
-
HPE, Dell, IBM, SuperMicro, Lenovo, Huawei and others.
|
99
|
-
|
100
|
-
## Debugging and use in Chef
|
101
|
-
|
102
|
-
You can activate debugging by setting the `debug_rest` flag to `true'. Please
|
103
|
-
note, that this will also log any confidential parts of HTTP traffic as well.
|
104
|
-
|
105
|
-
For better integration into Chef custom resources, all REST debug output will
|
106
|
-
be printed on `info` level. This allows debugging Chef resources without
|
107
|
-
crawling through all other debug output:
|
108
|
-
|
109
|
-
```ruby
|
110
|
-
train = Train.create('rest', {
|
111
|
-
endpoint: 'https://api.example.com/v1/',
|
112
|
-
debug_rest: true,
|
113
|
-
logger: Chef::Log
|
114
|
-
})
|
115
|
-
```
|
116
|
-
|
117
|
-
## Request Methods
|
118
|
-
|
119
|
-
This transport does not implement the `run_command` method, as there is no
|
120
|
-
line-based protocol to execute commands against. Instead, it implements its own
|
121
|
-
custom methods which suit REST interfaces. Trying to call this method will
|
122
|
-
throw an Exception.
|
123
|
-
|
124
|
-
### Generic Request
|
125
|
-
|
126
|
-
The `request` methods allows to send free-form requests against any defined or
|
127
|
-
custom methods.
|
128
|
-
|
129
|
-
`request(path, method = :get, request_parameters: {}, data: nil, headers: {},
|
130
|
-
json_processing: true)`
|
131
|
-
|
132
|
-
- `path`: The path to request, which will be appended to the `endpoint`
|
133
|
-
- `method`: The HTTP method in Ruby Symbol syntax
|
134
|
-
- `request_parameters`: A hash of parameters to the `rest-client` request
|
135
|
-
method for additional settings
|
136
|
-
- `data`: Data for actions like `:post` or `:put`. Not all methods accept
|
137
|
-
a data body.
|
138
|
-
- `headers`: Additional headers for the request
|
139
|
-
- `json_processing`: If the response is a JSON and you want to receive a
|
140
|
-
processed Hash/Array instead of text
|
141
|
-
|
142
|
-
For `request_parameters` and `headers`, there is data mixed in to add
|
143
|
-
authenticator responses, JSON processing etc. Please check the
|
144
|
-
implementation in `connection.rb` for details.
|
145
|
-
|
146
|
-
### Convenience Methods
|
147
|
-
|
148
|
-
Simplified wrappers are generated for the most common request types:
|
149
|
-
|
150
|
-
- `delete(path, request_parameters: {}, headers: {}, json_processing: true)`
|
151
|
-
- `head(path, request_parameters: {}, headers: {}, json_processing: true)`
|
152
|
-
- `get(path, request_parameters: {}, headers: {}, json_processing: true)`
|
153
|
-
- `post(path, request_parameters: {}, data: nil, headers: {}, json_processing: true)`
|
154
|
-
- `put(path, request_parameters: {}, data: nil, headers: {}, json_processing: true)`
|
155
|
-
- `patch(path, request_parameters: {}, data: nil, headers: {}, json_processing: true)`
|
156
|
-
|
157
|
-
## Example use
|
158
|
-
|
159
|
-
```ruby
|
160
|
-
require 'train-rest'
|
161
|
-
|
162
|
-
train = Train.create('rest', {
|
163
|
-
endpoint: 'https://api.example.com/v1/',
|
164
|
-
|
165
|
-
logger: Logger.new($stdout, level: :info)
|
166
|
-
})
|
167
|
-
conn = train.connection
|
168
|
-
|
169
|
-
# Get some hypothetical data
|
170
|
-
data = conn.get('device/1/settings')
|
171
|
-
|
172
|
-
# Modify + Patch
|
173
|
-
data['disabled'] = false
|
174
|
-
conn.patch('device/1/settings', data)
|
175
|
-
|
176
|
-
conn.close
|
177
|
-
```
|
178
|
-
|
179
|
-
Example for basic authentication:
|
180
|
-
|
181
|
-
```ruby
|
182
|
-
require 'train-rest'
|
183
|
-
|
184
|
-
# This will immediately do a login and add headers
|
185
|
-
train = Train.create('rest', {
|
186
|
-
endpoint: 'https://api.example.com/v1/',
|
187
|
-
|
188
|
-
auth_type: :basic,
|
189
|
-
username: 'admin',
|
190
|
-
password: '*********'
|
191
|
-
})
|
192
|
-
conn = train.connection
|
193
|
-
|
194
|
-
# ... do work, each request will resend Basic authentication headers ...
|
195
|
-
|
196
|
-
conn.close
|
197
|
-
```
|
198
|
-
|
199
|
-
Example for logging into a RedFish based system. Please note that the RedFish
|
200
|
-
authentication handler will append `redfish/v1` to the endpoint automatically,
|
201
|
-
if it is not present.
|
202
|
-
|
203
|
-
Due to this, you can use RedFish systems either with a base URL like in the
|
204
|
-
example below or with a full one. Your own code needs to match the style
|
205
|
-
you choose.
|
206
|
-
|
207
|
-
```ruby
|
208
|
-
require 'train-rest'
|
209
|
-
|
210
|
-
# This will immediately do a login and add headers
|
211
|
-
train = Train.create('rest', {
|
212
|
-
endpoint: 'https://10.20.30.40',
|
213
|
-
verify_ssl: false,
|
214
|
-
|
215
|
-
auth_type: :redfish,
|
216
|
-
username: 'iloadmin',
|
217
|
-
password: '*********'
|
218
|
-
})
|
219
|
-
conn = train.connection
|
220
|
-
|
221
|
-
# ... do work ...
|
222
|
-
|
223
|
-
# Handles logout as well
|
224
|
-
conn.close
|
225
|
-
```
|
226
|
-
|
227
|
-
## Use with Redfish, Your Custom Resources and Chef Target Mode
|
228
|
-
|
229
|
-
1. Set up a credentials file under `~/.chef/credentials` or `/etc/chef/credentials`:
|
230
|
-
|
231
|
-
```toml
|
232
|
-
['10.0.0.1']
|
233
|
-
endpoint = 'https://10.0.0.1/redfish/v1/'
|
234
|
-
username = 'user'
|
235
|
-
password = 'pass'
|
236
|
-
verify_ssl = false
|
237
|
-
auth_type = 'redfish'
|
238
|
-
```
|
239
|
-
|
240
|
-
1. Configure Chef to use the REST transport in `client.rb`:
|
241
|
-
|
242
|
-
```toml
|
243
|
-
target_mode.protocol = "rest"
|
244
|
-
```
|
245
|
-
|
246
|
-
1. Write your custom resources for REST APIs
|
247
|
-
1. Mark them up using the REST methods for target mode:
|
248
|
-
|
249
|
-
```ruby
|
250
|
-
provides :rest_resource, target_mode: true, platform: 'rest'
|
251
|
-
```
|
252
|
-
|
253
|
-
1. Run against the defiend targets via Chef Target Mode:
|
254
|
-
|
255
|
-
```shell
|
256
|
-
chef-client --local-mode --target 10.0.0.1 --runlist 'recipe[my-cookbook::setup]'
|
257
|
-
```
|
258
|
-
|
259
|
-
## Use with Prerecorded API responses
|
260
|
-
|
261
|
-
For testing during and after development, not all APIs can be used to verify your solution against.
|
262
|
-
The VCR gem offers the possibility to hook into web requests and intercept them to play back canned
|
263
|
-
responses.
|
264
|
-
|
265
|
-
Please read the documentation of the VCR gem on how to record your API and the concepts like
|
266
|
-
"cassettes", "libraries" and matchers.
|
267
|
-
|
268
|
-
The following options are available in train-rest for this:
|
269
|
-
|
270
|
-
| Option | Explanation | Default |
|
271
|
-
| -------------------- | --------------------------------------- | ------------ |
|
272
|
-
| `vcr_cassette` | Name of the response file | nil |
|
273
|
-
| `vcr_library` | Directory to search responses in | `vcr` |
|
274
|
-
| `vcr_match_on` | Elements to match request by | `method uri` |
|
275
|
-
| `vcr_record` | Recording mode | `none` |
|
276
|
-
| `vcr_hook_into` | Base library for intercepting | `webmock` |
|
277
|
-
|
278
|
-
VCR will only be required as a Gem and activated, if you supply a cassette name.
|
279
|
-
|
280
|
-
You can use all these settings in your Chef Target Mode `credentials` file as well.
|
data/train-rest.gemspec
DELETED
@@ -1,30 +0,0 @@
|
|
1
|
-
lib = File.expand_path("lib", __dir__)
|
2
|
-
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
-
require "train-rest/version"
|
4
|
-
|
5
|
-
Gem::Specification.new do |spec|
|
6
|
-
spec.name = "train-rest"
|
7
|
-
spec.version = TrainPlugins::Rest::VERSION
|
8
|
-
spec.authors = ["Thomas Heinen"]
|
9
|
-
spec.email = ["theinen@tecracer.de"]
|
10
|
-
spec.summary = "Train transport for REST"
|
11
|
-
spec.description = "Provides a transport to communicate easily with RESTful APIs."
|
12
|
-
spec.homepage = "https://github.com/sgre-chef/train-rest"
|
13
|
-
spec.license = "Apache-2.0"
|
14
|
-
|
15
|
-
spec.files = %w{
|
16
|
-
README.md train-rest.gemspec Gemfile
|
17
|
-
} + Dir.glob(
|
18
|
-
"lib/**/*", File::FNM_DOTMATCH
|
19
|
-
).reject { |f| File.directory?(f) }
|
20
|
-
spec.require_paths = ["lib"]
|
21
|
-
|
22
|
-
spec.add_dependency "train", "~> 3.0"
|
23
|
-
spec.add_dependency "rest-client", "~> 2.1"
|
24
|
-
|
25
|
-
spec.add_development_dependency "bump", "~> 0.9"
|
26
|
-
spec.add_development_dependency "chefstyle", "~> 0.14"
|
27
|
-
spec.add_development_dependency "guard", "~> 2.16"
|
28
|
-
spec.add_development_dependency "mdl", "~> 0.9"
|
29
|
-
spec.add_development_dependency "rake", "~> 13.0"
|
30
|
-
end
|