diff options
author | tylercloke <tylercloke@gmail.com> | 2015-06-04 12:23:55 -0700 |
---|---|---|
committer | tylercloke <tylercloke@gmail.com> | 2015-06-05 13:47:44 -0700 |
commit | 58048d42caeadc290876a02aaec7eec6f305920a (patch) | |
tree | ea9743fc860b69f2c4c20bb26b7f58a5d2f6f44f /lib/chef | |
parent | 4f2400c8cce57e68f14cb9d4b756ab82b23dd69d (diff) | |
download | chef-58048d42caeadc290876a02aaec7eec6f305920a.tar.gz |
Better API version error handling helper code.
Renamed Chef::Mixin::ApiVersionRequestHandling.handle_version_http_exception -> server_client_api_version_intersection and made it do much more useful / sane things. See comments for details.
Diffstat (limited to 'lib/chef')
-rw-r--r-- | lib/chef/api_client.rb | 8 | ||||
-rw-r--r-- | lib/chef/knife/user_create.rb | 9 | ||||
-rw-r--r-- | lib/chef/knife/user_delete.rb | 8 | ||||
-rw-r--r-- | lib/chef/knife/user_edit.rb | 8 | ||||
-rw-r--r-- | lib/chef/knife/user_reregister.rb | 7 | ||||
-rw-r--r-- | lib/chef/knife/user_show.rb | 7 | ||||
-rw-r--r-- | lib/chef/mixin/api_version_request_handling.rb | 57 | ||||
-rw-r--r-- | lib/chef/user.rb | 8 |
8 files changed, 57 insertions, 55 deletions
diff --git a/lib/chef/api_client.rb b/lib/chef/api_client.rb index 3b777b3574..ad31fb7d7b 100644 --- a/lib/chef/api_client.rb +++ b/lib/chef/api_client.rb @@ -32,7 +32,7 @@ class Chef include Chef::Mixin::FromFile include Chef::Mixin::ParamsValidate - include Chef::ApiVersionRequestHandling + include Chef::Mixin::ApiVersionRequestHandling SUPPORTED_API_VERSIONS = [0,1] @@ -272,7 +272,8 @@ class Chef new_client = chef_rest_v1.put("clients/#{name}", payload) rescue Net::HTTPServerException => e # rescue API V0 if 406 and the server supports V0 - raise e unless handle_version_http_exception(e, SUPPORTED_API_VERSIONS[0], SUPPORTED_API_VERSIONS[-1]) + supported_versions = server_client_api_version_intersection(e, SUPPORTED_API_VERSIONS) + raise e unless supported_versions && supported_versions.include?(0) new_client = chef_rest_v0.put("clients/#{name}", payload) end @@ -308,7 +309,8 @@ class Chef rescue Net::HTTPServerException => e # rescue API V0 if 406 and the server supports V0 - raise e unless handle_version_http_exception(e, SUPPORTED_API_VERSIONS[0], SUPPORTED_API_VERSIONS[-1]) + supported_versions = server_client_api_version_intersection(e, SUPPORTED_API_VERSIONS) + raise e unless supported_versions && supported_versions.include?(0) # under API V0, a key pair will always be created unless public_key is # passed on initial POST diff --git a/lib/chef/knife/user_create.rb b/lib/chef/knife/user_create.rb index 5332e863ae..e73f6be8b6 100644 --- a/lib/chef/knife/user_create.rb +++ b/lib/chef/knife/user_create.rb @@ -78,11 +78,13 @@ knife user create for Open Source 11 Server is being deprecated. Open Source 11 Server user commands now live under the knife osc_user namespace. For backwards compatibility, we will forward this request to knife osc_user create. If you are using an Open Source 11 Server, please use that command to avoid this warning. -If you are not using an Open Source Chef 11 Server install, please read knife user create --help for proper usage. EOF end def run_osc_11_user_create + # run osc_user_create with our input + ARGV.delete("user") + ARGV.unshift("osc_user") Chef::Knife.run(ARGV, Chef::Application::Knife.options) end @@ -93,12 +95,7 @@ EOF # If only 1 arg is passed, assume OSC 11 case. if @name_args.length == 1 ui.warn(osc_11_warning) - - # run osc_user_create with our input - ARGV.delete("user") - ARGV.unshift("osc_user") run_osc_11_user_create - else # EC / CS 12 user create test_mandatory_field(@name_args[0], "username") diff --git a/lib/chef/knife/user_delete.rb b/lib/chef/knife/user_delete.rb index 3a46e33f84..803be6b90c 100644 --- a/lib/chef/knife/user_delete.rb +++ b/lib/chef/knife/user_delete.rb @@ -41,6 +41,9 @@ EOF end def run_osc_11_user_delete + # run osc_user_delete with our input + ARGV.delete("user") + ARGV.unshift("osc_user") Chef::Knife.run(ARGV, Chef::Application::Knife.options) end @@ -82,12 +85,7 @@ EOF # OSC 11 case if object.username.nil? ui.warn(osc_11_warning) - - # run osc_user_delete with our input - ARGV.delete("user") - ARGV.unshift("osc_user") run_osc_11_user_delete - else # proceed with EC / CS delete delete_object(@user_name) end diff --git a/lib/chef/knife/user_edit.rb b/lib/chef/knife/user_edit.rb index 5bed51a1cc..dd2fc02743 100644 --- a/lib/chef/knife/user_edit.rb +++ b/lib/chef/knife/user_edit.rb @@ -41,6 +41,9 @@ EOF end def run_osc_11_user_edit + # run osc_user_create with our input + ARGV.delete("user") + ARGV.unshift("osc_user") Chef::Knife.run(ARGV, Chef::Application::Knife.options) end @@ -62,12 +65,7 @@ EOF # forward to deprecated command if original_user["username"].nil? ui.warn(osc_11_warning) - - # run osc_user_create with our input - ARGV.delete("user") - ARGV.unshift("osc_user") run_osc_11_user_edit - else # EC / CS 12 user create edited_user = edit_data(original_user) if original_user != edited_user diff --git a/lib/chef/knife/user_reregister.rb b/lib/chef/knife/user_reregister.rb index c2b39761cc..eab2245025 100644 --- a/lib/chef/knife/user_reregister.rb +++ b/lib/chef/knife/user_reregister.rb @@ -41,6 +41,9 @@ EOF end def run_osc_11_user_reregister + # run osc_user_edit with our input + ARGV.delete("user") + ARGV.unshift("osc_user") Chef::Knife.run(ARGV, Chef::Application::Knife.options) end @@ -67,10 +70,6 @@ EOF # forward to deprecated command if user.username.nil? ui.warn(osc_11_warning) - - # run osc_user_edit with our input - ARGV.delete("user") - ARGV.unshift("osc_user") run_osc_11_user_reregister else # EC / CS 12 case user.reregister diff --git a/lib/chef/knife/user_show.rb b/lib/chef/knife/user_show.rb index d0e9f64f53..f5e81e9972 100644 --- a/lib/chef/knife/user_show.rb +++ b/lib/chef/knife/user_show.rb @@ -43,6 +43,9 @@ EOF end def run_osc_11_user_show + # run osc_user_edit with our input + ARGV.delete("user") + ARGV.unshift("osc_user") Chef::Knife.run(ARGV, Chef::Application::Knife.options) end @@ -64,10 +67,6 @@ EOF # forward to deprecated command if user.username.nil? ui.warn(osc_11_warning) - - # run osc_user_edit with our input - ARGV.delete("user") - ARGV.unshift("osc_user") run_osc_11_user_show else output(format_for_display(user)) diff --git a/lib/chef/mixin/api_version_request_handling.rb b/lib/chef/mixin/api_version_request_handling.rb index 851bcb4968..20ab3bf452 100644 --- a/lib/chef/mixin/api_version_request_handling.rb +++ b/lib/chef/mixin/api_version_request_handling.rb @@ -17,43 +17,50 @@ # class Chef - module ApiVersionRequestHandling - # takes in an http exception, and a min and max supported API version and - # handles all the versioning cases - # - # it will return false if there was a non-versioning related error - # or the server and the client are not compatible - # - # if the server does not support versioning, then it will return true, and you - # can assume API v0 is safe to send - def handle_version_http_exception(exception, min_client_supported_version, max_client_supported_version) - # only rescue 406 Unacceptable with proper header - return false if exception.response.code != "406" || exception.response["x-ops-server-api-version"].nil? + module Mixin + module ApiVersionRequestHandling + # Input: + # exeception: + # Net::HTTPServerException that may or may not contain the x-ops-server-api-version header + # supported_client_versions: + # An array of Integers that represent the API versions the client supports. + # + # Output: + # nil: + # If the execption was not a 406 or the server does not support versioning + # Array of length zero: + # If there was no intersection between supported client versions and supported server versions + # Arrary of Integers: + # If there was an intersection of supported versions, the array returns will contain that intersection + def server_client_api_version_intersection(exception, supported_client_versions) + # return empty array unless 406 Unacceptable with proper header + return nil if exception.response.code != "406" || exception.response["x-ops-server-api-version"].nil? + + # intersection of versions the server and client support, will be of length zero if no intersection + server_supported_client_versions = Array.new - # if the version header doesn't exist, just assume API v0 - if exception.response["x-ops-server-api-version"] header = Chef::JSONCompat.from_json(exception.response["x-ops-server-api-version"]) min_server_version = Integer(header["min_version"]) max_server_version = Integer(header["max_version"]) - # if the min API version the server supports is greater than the min version the client supports - # and the max API version the server supports is less than the max version the client supports - if min_server_version > min_client_supported_version || max_server_version < max_client_supported_version - # if it had x-ops-server-api-version header, return false - return false + supported_client_versions.each do |version| + if version >= min_server_version && version <= max_server_version + server_supported_client_versions.push(version) + end end + server_supported_client_versions end - true - end - def reregister_only_v0_supported_error_msg(max_version, min_version) -<<-EOH + def reregister_only_v0_supported_error_msg(max_version, min_version) + <<-EOH The reregister command only supports server API version 0. The server that received the request supports a min version of #{min_version} and a max version of #{max_version}. User keys are now managed via the key rotation commmands. -Please refer to the documentation on how to manage your keys via the key rotation commands. +Please refer to the documentation on how to manage your keys via the key rotation commands: +https://docs.chef.io/server_security.html#key-rotation EOH - end + end + end end end diff --git a/lib/chef/user.rb b/lib/chef/user.rb index 1b5f454099..717deb63c3 100644 --- a/lib/chef/user.rb +++ b/lib/chef/user.rb @@ -36,7 +36,7 @@ class Chef include Chef::Mixin::FromFile include Chef::Mixin::ParamsValidate - include Chef::ApiVersionRequestHandling + include Chef::Mixin::ApiVersionRequestHandling SUPPORTED_API_VERSIONS = [0,1] @@ -169,7 +169,8 @@ class Chef end rescue Net::HTTPServerException => e # rescue API V0 if 406 and the server supports V0 - raise e unless handle_version_http_exception(e, SUPPORTED_API_VERSIONS[0], SUPPORTED_API_VERSIONS[-1]) + supported_versions = server_client_api_version_intersection(e, SUPPORTED_API_VERSIONS) + raise e unless supported_versions && supported_versions.include?(0) payload = { :username => @username, :display_name => @display_name, @@ -212,7 +213,8 @@ class Chef raise e end else # for other types of errors, test for API versioning errors right away - raise e unless handle_version_http_exception(e, SUPPORTED_API_VERSIONS[0], SUPPORTED_API_VERSIONS[-1]) + supported_versions = server_client_api_version_intersection(e, SUPPORTED_API_VERSIONS) + raise e unless supported_versions && supported_versions.include?(0) end updated_user = chef_root_rest_v0.put("users/#{username}", payload) end |