chef-infra-api 0.9.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +201 -0
  3. data/lib/chef-api.rb +96 -0
  4. data/lib/chef-api/aclable.rb +35 -0
  5. data/lib/chef-api/authentication.rb +300 -0
  6. data/lib/chef-api/boolean.rb +6 -0
  7. data/lib/chef-api/configurable.rb +80 -0
  8. data/lib/chef-api/connection.rb +507 -0
  9. data/lib/chef-api/defaults.rb +197 -0
  10. data/lib/chef-api/error_collection.rb +44 -0
  11. data/lib/chef-api/errors.rb +64 -0
  12. data/lib/chef-api/multipart.rb +164 -0
  13. data/lib/chef-api/resource.rb +21 -0
  14. data/lib/chef-api/resources/base.rb +960 -0
  15. data/lib/chef-api/resources/client.rb +84 -0
  16. data/lib/chef-api/resources/collection_proxy.rb +234 -0
  17. data/lib/chef-api/resources/cookbook.rb +24 -0
  18. data/lib/chef-api/resources/cookbook_version.rb +23 -0
  19. data/lib/chef-api/resources/data_bag.rb +136 -0
  20. data/lib/chef-api/resources/data_bag_item.rb +53 -0
  21. data/lib/chef-api/resources/environment.rb +16 -0
  22. data/lib/chef-api/resources/group.rb +16 -0
  23. data/lib/chef-api/resources/node.rb +20 -0
  24. data/lib/chef-api/resources/organization.rb +22 -0
  25. data/lib/chef-api/resources/partial_search.rb +44 -0
  26. data/lib/chef-api/resources/principal.rb +11 -0
  27. data/lib/chef-api/resources/role.rb +18 -0
  28. data/lib/chef-api/resources/search.rb +47 -0
  29. data/lib/chef-api/resources/user.rb +82 -0
  30. data/lib/chef-api/schema.rb +150 -0
  31. data/lib/chef-api/util.rb +119 -0
  32. data/lib/chef-api/validator.rb +16 -0
  33. data/lib/chef-api/validators/base.rb +82 -0
  34. data/lib/chef-api/validators/required.rb +11 -0
  35. data/lib/chef-api/validators/type.rb +23 -0
  36. data/lib/chef-api/version.rb +3 -0
  37. data/templates/errors/abstract_method.erb +5 -0
  38. data/templates/errors/cannot_regenerate_key.erb +1 -0
  39. data/templates/errors/chef_api_error.erb +1 -0
  40. data/templates/errors/file_not_found.erb +1 -0
  41. data/templates/errors/http_bad_request.erb +3 -0
  42. data/templates/errors/http_forbidden_request.erb +3 -0
  43. data/templates/errors/http_gateway_timeout.erb +3 -0
  44. data/templates/errors/http_method_not_allowed.erb +3 -0
  45. data/templates/errors/http_not_acceptable.erb +3 -0
  46. data/templates/errors/http_not_found.erb +3 -0
  47. data/templates/errors/http_server_unavailable.erb +1 -0
  48. data/templates/errors/http_unauthorized_request.erb +3 -0
  49. data/templates/errors/insufficient_file_permissions.erb +1 -0
  50. data/templates/errors/invalid_resource.erb +1 -0
  51. data/templates/errors/invalid_validator.erb +1 -0
  52. data/templates/errors/missing_url_parameter.erb +1 -0
  53. data/templates/errors/not_a_directory.erb +1 -0
  54. data/templates/errors/resource_already_exists.erb +1 -0
  55. data/templates/errors/resource_not_found.erb +1 -0
  56. data/templates/errors/resource_not_mutable.erb +1 -0
  57. data/templates/errors/unknown_attribute.erb +1 -0
  58. metadata +130 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 2f1a019204906d755e2df904227b405895b18997286a0e01391c38e860c8f8eb
4
+ data.tar.gz: 8da7a860ead9c3a62ae286829b08e0edc9f039daa2664dd67864142efa929101
5
+ SHA512:
6
+ metadata.gz: 0ed650663f51d8abc528c664aa62b947612e068924fa6a734f571a4a9ab48052a27b993d855ae627d6e1ace68a2debcd87b2eef8f41d84a23b2551212982da48
7
+ data.tar.gz: 7f955aecd324dd67821d5c82d8753cfb6eb010300b8dfd6565ee631def443a627aef218998a1e730c1a744fa9832889d8c4d323da26b4c115de102ebfd58f6a1
data/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2013 Seth Vargo <sethvargo@gmail.com>
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,96 @@
1
+ require 'json'
2
+ require 'logify'
3
+ require 'pathname'
4
+ require 'chef-api/version'
5
+
6
+ module ChefAPI
7
+ autoload :Authentication, 'chef-api/authentication'
8
+ autoload :Boolean, 'chef-api/boolean'
9
+ autoload :Configurable, 'chef-api/configurable'
10
+ autoload :Connection, 'chef-api/connection'
11
+ autoload :Defaults, 'chef-api/defaults'
12
+ autoload :Error, 'chef-api/errors'
13
+ autoload :ErrorCollection, 'chef-api/error_collection'
14
+ autoload :Multipart, 'chef-api/multipart'
15
+ autoload :Resource, 'chef-api/resource'
16
+ autoload :Schema, 'chef-api/schema'
17
+ autoload :Util, 'chef-api/util'
18
+ autoload :Validator, 'chef-api/validator'
19
+
20
+ #
21
+ # @todo Document this and why it's important
22
+ #
23
+ UNSET = Object.new
24
+
25
+ class << self
26
+ include ChefAPI::Configurable
27
+
28
+ #
29
+ # Set the log level.
30
+ #
31
+ # @example Set the log level to :info
32
+ # ChefAPI.log_level = :info
33
+ #
34
+ # @param [Symbol] level
35
+ # the log level to set
36
+ #
37
+ def log_level=(level)
38
+ Logify.level = level
39
+ end
40
+
41
+ #
42
+ # Get the current log level.
43
+ #
44
+ # @return [Symbol]
45
+ #
46
+ def log_level
47
+ Logify.level
48
+ end
49
+
50
+ #
51
+ # The source root of the ChefAPI gem. This is useful when requiring files
52
+ # that are relative to the root of the project.
53
+ #
54
+ # @return [Pathname]
55
+ #
56
+ def root
57
+ @root ||= Pathname.new(File.expand_path('../../', __FILE__))
58
+ end
59
+
60
+ #
61
+ # API connection object based off the configured options in {Configurable}.
62
+ #
63
+ # @return [ChefAPI::Connection]
64
+ #
65
+ def connection
66
+ unless @connection && @connection.same_options?(options)
67
+ @connection = ChefAPI::Connection.new(options)
68
+ end
69
+
70
+ @connection
71
+ end
72
+
73
+ #
74
+ # Delegate all methods to the connection object, essentially making the
75
+ # module object behave like a {Connection}.
76
+ #
77
+ def method_missing(m, *args, &block)
78
+ if connection.respond_to?(m)
79
+ connection.send(m, *args, &block)
80
+ else
81
+ super
82
+ end
83
+ end
84
+
85
+ #
86
+ # Delegating +respond_to+ to the {Connection}.
87
+ #
88
+ def respond_to_missing?(m, include_private = false)
89
+ connection.respond_to?(m) || super
90
+ end
91
+ end
92
+ end
93
+
94
+ # Load the initial default values
95
+ ChefAPI.setup
96
+
@@ -0,0 +1,35 @@
1
+ module ChefAPI
2
+ module AclAble
3
+ def acl_path
4
+ self.resource_path + '/_acl'
5
+ end
6
+
7
+ def load_acl
8
+ data = self.class.connection.get(acl_path)
9
+ # make deep copy
10
+ @orig_acl_data = Marshal.load(Marshal.dump(data))
11
+ data.freeze
12
+ @acl = data
13
+ end
14
+
15
+ def acl
16
+ unless @acl
17
+ self.load_acl
18
+ end
19
+ @acl
20
+ end
21
+
22
+ def save!
23
+ super
24
+ if @acl != @orig_acl_data
25
+ %w(create update grant read delete).each{|action|
26
+ if @acl[action] != @orig_acl_data[action]
27
+ url = "#{self.acl_path}/#{action}"
28
+ self.class.connection.put(url, {action => @acl[action]}.to_json)
29
+ end
30
+ }
31
+ end
32
+ end
33
+ end
34
+
35
+ end
@@ -0,0 +1,300 @@
1
+ require 'base64'
2
+ require 'digest'
3
+ require 'openssl'
4
+ require 'time'
5
+
6
+ #
7
+ # DEBUG steps:
8
+ #
9
+ # check .chomp
10
+ #
11
+
12
+ module ChefAPI
13
+ class Authentication
14
+ include Logify
15
+
16
+ # @todo: Enable this in the future when Mixlib::Authentication supports
17
+ # signing the full request body instead of just the uploaded file parameter.
18
+ SIGN_FULL_BODY = false
19
+
20
+ SIGNATURE = 'algorithm=sha1;version=1.0;'.freeze
21
+
22
+ # Headers
23
+ X_OPS_SIGN = 'X-Ops-Sign'.freeze
24
+ X_OPS_USERID = 'X-Ops-Userid'.freeze
25
+ X_OPS_TIMESTAMP = 'X-Ops-Timestamp'.freeze
26
+ X_OPS_CONTENT_HASH = 'X-Ops-Content-Hash'.freeze
27
+ X_OPS_AUTHORIZATION = 'X-Ops-Authorization'.freeze
28
+
29
+ class << self
30
+ #
31
+ # Create a new signing object from the given options. All options are
32
+ # required.
33
+ #
34
+ # @see (#initialize)
35
+ #
36
+ # @option options [String] :user
37
+ # @option options [String, OpenSSL::PKey::RSA] :key
38
+ # @option options [String, Symbol] verb
39
+ # @option options [String] :path
40
+ # @option options [String, IO] :body
41
+ #
42
+ def from_options(options = {})
43
+ user = options.fetch(:user)
44
+ key = options.fetch(:key)
45
+ verb = options.fetch(:verb)
46
+ path = options.fetch(:path)
47
+ body = options.fetch(:body)
48
+
49
+ new(user, key, verb, path, body)
50
+ end
51
+ end
52
+
53
+ #
54
+ # Create a new Authentication object for signing. Creating an instance will
55
+ # not run any validations or perform any operations (this is on purpose).
56
+ #
57
+ # @param [String] user
58
+ # the username/client/user of the user to sign the request. In Hosted
59
+ # Chef land, this is your "client". In Supermarket land, this is your
60
+ # "username".
61
+ # @param [String, OpenSSL::PKey::RSA] key
62
+ # the path to a private key on disk, the raw private key (as a String),
63
+ # or the raw private key (as an OpenSSL::PKey::RSA instance)
64
+ # @param [Symbol, String] verb
65
+ # the verb for the request (e.g. +:get+)
66
+ # @param [String] path
67
+ # the "path" part of the URI (e.g. +/path/to/resource+)
68
+ # @param [String, IO] body
69
+ # the body to sign for the request, as a raw string or an IO object to be
70
+ # read in chunks
71
+ #
72
+ def initialize(user, key, verb, path, body)
73
+ @user = user
74
+ @key = key
75
+ @verb = verb
76
+ @path = path
77
+ @body = body
78
+ end
79
+
80
+ #
81
+ # The fully-qualified headers for this authentication object of the form:
82
+ #
83
+ # {
84
+ # 'X-Ops-Sign' => 'algorithm=sha1;version=1.1',
85
+ # 'X-Ops-Userid' => 'sethvargo',
86
+ # 'X-Ops-Timestamp' => '2014-07-07T02:17:15Z',
87
+ # 'X-Ops-Content-Hash' => '...',
88
+ # 'x-Ops-Authorization-1' => '...'
89
+ # 'x-Ops-Authorization-2' => '...'
90
+ # 'x-Ops-Authorization-3' => '...'
91
+ # # ...
92
+ # }
93
+ #
94
+ # @return [Hash]
95
+ # the signing headers
96
+ #
97
+ def headers
98
+ {
99
+ X_OPS_SIGN => SIGNATURE,
100
+ X_OPS_USERID => @user,
101
+ X_OPS_TIMESTAMP => canonical_timestamp,
102
+ X_OPS_CONTENT_HASH => content_hash,
103
+ }.merge(signature_lines)
104
+ end
105
+
106
+ #
107
+ # The canonical body. This could be an IO object (such as +#body_stream+),
108
+ # an actual string (such as +#body+), or just the empty string if the
109
+ # request's body and stream was nil.
110
+ #
111
+ # @return [String, IO]
112
+ #
113
+ def content_hash
114
+ return @content_hash if @content_hash
115
+
116
+ if SIGN_FULL_BODY
117
+ @content_hash = hash(@body || '').chomp
118
+ else
119
+ if @body.is_a?(Multipart::MultiIO)
120
+ filepart = @body.ios.find { |io| io.is_a?(Multipart::MultiIO) }
121
+ file = filepart.ios.find { |io| !io.is_a?(StringIO) }
122
+
123
+ @content_hash = hash(file).chomp
124
+ else
125
+ @content_hash = hash(@body || '').chomp
126
+ end
127
+ end
128
+
129
+ @content_hash
130
+ end
131
+
132
+ #
133
+ # Parse the given private key. Users can specify the private key as:
134
+ #
135
+ # - the path to the key on disk
136
+ # - the raw string key
137
+ # - an +OpenSSL::PKey::RSA object+
138
+ #
139
+ # Any other implementations are not supported and will likely explode.
140
+ #
141
+ # @todo
142
+ # Handle errors when the file cannot be read due to insufficient
143
+ # permissions
144
+ #
145
+ # @return [OpenSSL::PKey::RSA]
146
+ # the RSA private key as an OpenSSL object
147
+ #
148
+ def canonical_key
149
+ return @canonical_key if @canonical_key
150
+
151
+ log.info "Parsing private key..."
152
+
153
+ if @key.nil?
154
+ log.warn "No private key given!"
155
+ raise 'No private key given!'
156
+ end
157
+
158
+ if @key.is_a?(OpenSSL::PKey::RSA)
159
+ log.debug "Detected private key is an OpenSSL Ruby object"
160
+ @canonical_key = @key
161
+ elsif @key =~ /(.+)\.pem$/ || File.exists?(File.expand_path(@key))
162
+ log.debug "Detected private key is the path to a file"
163
+ contents = File.read(File.expand_path(@key))
164
+ @canonical_key = OpenSSL::PKey::RSA.new(contents)
165
+ else
166
+ log.debug "Detected private key was the literal string key"
167
+ @canonical_key = OpenSSL::PKey::RSA.new(@key)
168
+ end
169
+
170
+ @canonical_key
171
+ end
172
+
173
+
174
+ #
175
+ # The canonical path, with duplicate and trailing slashes removed. This
176
+ # value is then hashed.
177
+ #
178
+ # @example
179
+ # "/zip//zap/foo" #=> "/zip/zap/foo"
180
+ #
181
+ # @return [String]
182
+ #
183
+ def canonical_path
184
+ @canonical_path ||= hash(@path.squeeze('/').gsub(/(\/)+$/,'')).chomp
185
+ end
186
+
187
+ #
188
+ # The iso8601 timestamp for this request. This value must be cached so it
189
+ # is persisted throughout this entire request.
190
+ #
191
+ # @return [String]
192
+ #
193
+ def canonical_timestamp
194
+ @canonical_timestamp ||= Time.now.utc.iso8601
195
+ end
196
+
197
+ #
198
+ # The uppercase verb.
199
+ #
200
+ # @example
201
+ # :get #=> "GET"
202
+ #
203
+ # @return [String]
204
+ #
205
+ def canonical_method
206
+ @canonical_method ||= @verb.to_s.upcase
207
+ end
208
+
209
+ #
210
+ # The canonical request, from the path, body, user, and current timestamp.
211
+ #
212
+ # @return [String]
213
+ #
214
+ def canonical_request
215
+ [
216
+ "Method:#{canonical_method}",
217
+ "Hashed Path:#{canonical_path}",
218
+ "X-Ops-Content-Hash:#{content_hash}",
219
+ "X-Ops-Timestamp:#{canonical_timestamp}",
220
+ "X-Ops-UserId:#{@user}",
221
+ ].join("\n")
222
+ end
223
+
224
+ #
225
+ # The canonical request, encrypted by the given private key.
226
+ #
227
+ # @return [String]
228
+ #
229
+ def encrypted_request
230
+ canonical_key.private_encrypt(canonical_request)
231
+ end
232
+
233
+ #
234
+ # The +X-Ops-Authorization-N+ headers. This method takes the encrypted
235
+ # request, splits on a newline, and creates a signed header authentication
236
+ # request. N begins at 1, not 0 because the original author of
237
+ # Mixlib::Authentication did not believe in computer science.
238
+ #
239
+ # @return [Hash]
240
+ #
241
+ def signature_lines
242
+ signature = Base64.encode64(encrypted_request)
243
+ signature.split(/\n/).each_with_index.inject({}) do |hash, (line, index)|
244
+ hash["#{X_OPS_AUTHORIZATION}-#{index + 1}"] = line
245
+ hash
246
+ end
247
+ end
248
+
249
+ private
250
+
251
+ #
252
+ # Hash the given object.
253
+ #
254
+ # @param [String, IO] object
255
+ # a string or IO object to hash
256
+ #
257
+ # @return [String]
258
+ # the hashed value
259
+ #
260
+ def hash(object)
261
+ if object.respond_to?(:read)
262
+ digest_io(object)
263
+ else
264
+ digest_string(object)
265
+ end
266
+ end
267
+
268
+ #
269
+ # Digest the given IO, reading in 1024 bytes at one time.
270
+ #
271
+ # @param [IO] io
272
+ # the IO (or File object)
273
+ #
274
+ # @return [String]
275
+ #
276
+ def digest_io(io)
277
+ digester = Digest::SHA1.new
278
+
279
+ while buffer = io.read(1024)
280
+ digester.update(buffer)
281
+ end
282
+
283
+ io.rewind
284
+
285
+ Base64.encode64(digester.digest)
286
+ end
287
+
288
+ #
289
+ # Digest a string.
290
+ #
291
+ # @param [String] string
292
+ # the string to digest
293
+ #
294
+ # @return [String]
295
+ #
296
+ def digest_string(string)
297
+ Base64.encode64(Digest::SHA1.digest(string))
298
+ end
299
+ end
300
+ end