summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortylercloke <tylercloke@gmail.com>2015-06-02 16:54:21 -0700
committertylercloke <tylercloke@gmail.com>2015-06-05 10:38:49 -0700
commit4f2400c8cce57e68f14cb9d4b756ab82b23dd69d (patch)
tree836ac3d3f71ec2ab234f975d2ed2f4fad93e94a5
parenteb49eedb6333bc1c6b82d0c7befffb9188c5b523 (diff)
downloadchef-4f2400c8cce57e68f14cb9d4b756ab82b23dd69d.tar.gz
Backwards compatible support for OSC 11 in knife user commands.
All knife user commands can detect when they have failed for OSC 11 reasons and forward the request to backwards compatible knife osc_user commands.
-rw-r--r--lib/chef/api_client.rb23
-rw-r--r--lib/chef/formatters/error_inspectors/api_error_formatting.rb8
-rw-r--r--lib/chef/knife/client_create.rb7
-rw-r--r--lib/chef/knife/osc_user_create.rb5
-rw-r--r--lib/chef/knife/osc_user_delete.rb9
-rw-r--r--lib/chef/knife/osc_user_edit.rb9
-rw-r--r--lib/chef/knife/osc_user_list.rb9
-rw-r--r--lib/chef/knife/osc_user_reregister.rb9
-rw-r--r--lib/chef/knife/osc_user_show.rb9
-rw-r--r--lib/chef/knife/user_create.rb116
-rw-r--r--lib/chef/knife/user_delete.rb56
-rw-r--r--lib/chef/knife/user_edit.rb46
-rw-r--r--lib/chef/knife/user_list.rb3
-rw-r--r--lib/chef/knife/user_reregister.rb48
-rw-r--r--lib/chef/knife/user_show.rb32
-rw-r--r--lib/chef/osc_user.rb2
-rw-r--r--lib/chef/user.rb43
-rw-r--r--spec/unit/knife/osc_user_create_spec.rb9
-rw-r--r--spec/unit/knife/osc_user_delete_spec.rb9
-rw-r--r--spec/unit/knife/osc_user_edit_spec.rb7
-rw-r--r--spec/unit/knife/osc_user_list_spec.rb7
-rw-r--r--spec/unit/knife/osc_user_reregister_spec.rb5
-rw-r--r--spec/unit/knife/osc_user_show_spec.rb5
-rw-r--r--spec/unit/knife/user_create_spec.rb39
-rw-r--r--spec/unit/knife/user_delete_spec.rb42
-rw-r--r--spec/unit/knife/user_edit_spec.rb43
-rw-r--r--spec/unit/knife/user_list_spec.rb10
-rw-r--r--spec/unit/knife/user_reregister_spec.rb55
-rw-r--r--spec/unit/knife/user_show_spec.rb46
-rw-r--r--spec/unit/osc_user_spec.rb7
30 files changed, 521 insertions, 197 deletions
diff --git a/lib/chef/api_client.rb b/lib/chef/api_client.rb
index d486cc9808..3b777b3574 100644
--- a/lib/chef/api_client.rb
+++ b/lib/chef/api_client.rb
@@ -1,7 +1,7 @@
#
# Author:: Adam Jacob (<adam@chef.io>)
# Author:: Nuo Yan (<nuo@chef.io>)
-# Copyright:: Copyright (c) 2015 Chef Software, Inc.
+# Copyright:: Copyright (c) 2008, 2015 Chef Software, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -54,10 +54,12 @@ class Chef
@chef_rest_v1 ||= Chef::ServerAPI.new(Chef::Config[:chef_server_url], {:api_version => "1"})
end
+ # will default to the current version (Chef::Authenticator::DEFAULT_SERVER_API_VERSION)
def http_api
@http_api ||= Chef::REST.new(Chef::Config[:chef_server_url])
end
+ # will default to the current version (Chef::Authenticator::DEFAULT_SERVER_API_VERSION)
def self.http_api
Chef::REST.new(Chef::Config[:chef_server_url])
end
@@ -304,15 +306,15 @@ class Chef
new_client.delete('chef_key')
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])
+ 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])
- # under API V0, a key pair will always be created unless public_key is
- # passed on initial POST
- payload[:public_key] = public_key unless public_key.nil?
+ # under API V0, a key pair will always be created unless public_key is
+ # passed on initial POST
+ payload[:public_key] = public_key unless public_key.nil?
- new_client = chef_rest_v0.post("clients", payload)
+ new_client = chef_rest_v0.post("clients", payload)
end
Chef::ApiClient.from_hash(self.to_hash.merge(new_client))
end
@@ -322,10 +324,5 @@ class Chef
"client[#{@name}]"
end
- # def inspect
- # "Chef::ApiClient name:'#{name}' admin:'#{admin.inspect}' validator:'#{validator}' " +
- # "public_key:'#{public_key}' private_key:'#{private_key}'"
- # end
-
end
end
diff --git a/lib/chef/formatters/error_inspectors/api_error_formatting.rb b/lib/chef/formatters/error_inspectors/api_error_formatting.rb
index d68b38e41f..05ee3132a7 100644
--- a/lib/chef/formatters/error_inspectors/api_error_formatting.rb
+++ b/lib/chef/formatters/error_inspectors/api_error_formatting.rb
@@ -75,10 +75,10 @@ E
max_server_version = version_header["max_version"]
error_description.section("Incompatible server API version:",<<-E)
-This version of the API that this Chef request specified is not supported by the Chef server you sent this request to
-The Chef server you sent the request to supports a min API version of #{min_server_version} and a max API version of #{max_server_version}
-Chef just made a request with an API version of #{client_api_version}
-Please either update your Chef client or server to be a compatible set
+This version of the API that this Chef request specified is not supported by the Chef server you sent this request to.
+The server supports a min API version of #{min_server_version} and a max API version of #{max_server_version}.
+Chef just made a request with an API version of #{client_api_version}.
+Please either update your Chef client or server to be a compatible set.
E
else
describe_http_error(error_description)
diff --git a/lib/chef/knife/client_create.rb b/lib/chef/knife/client_create.rb
index 17c1204c2d..570c1ee950 100644
--- a/lib/chef/knife/client_create.rb
+++ b/lib/chef/knife/client_create.rb
@@ -46,7 +46,7 @@ class Chef
option :public_key,
:short => "-p FILE",
:long => "--public-key",
- :description => "Set the initial default key for the client from a file on disk (cannot pass with --create-key)."
+ :description => "Set the initial default key for the client from a file on disk (cannot pass with --prevent-keygen)."
option :prevent_keygen,
:short => "-k",
@@ -61,7 +61,8 @@ class Chef
end
def create_client(client)
- client.create
+ # should not be using save :( bad behavior
+ client.save
end
def run
@@ -74,7 +75,7 @@ class Chef
exit 1
end
- unless config[:prevent_keygen]
+ if !config[:prevent_keygen] && !config[:public_key]
client.create_key(true)
end
diff --git a/lib/chef/knife/osc_user_create.rb b/lib/chef/knife/osc_user_create.rb
index d13a3a3615..c368296040 100644
--- a/lib/chef/knife/osc_user_create.rb
+++ b/lib/chef/knife/osc_user_create.rb
@@ -21,8 +21,7 @@ require 'chef/knife'
# DEPRECATION NOTE
# This code only remains to support users still operating with
# Open Source Chef Server 11 and should be removed once support
-# for OSC 11 ends. New development should occur in the Chef Server 12
-# version of this file.
+# for OSC 11 ends. New development should occur in user_create.rb.
class Chef
class Knife
class OscUserCreate < Knife
@@ -95,4 +94,4 @@ class Chef
end
end
end
-end \ No newline at end of file
+end
diff --git a/lib/chef/knife/osc_user_delete.rb b/lib/chef/knife/osc_user_delete.rb
index baadeec9ef..d6fbd4a6a9 100644
--- a/lib/chef/knife/osc_user_delete.rb
+++ b/lib/chef/knife/osc_user_delete.rb
@@ -19,10 +19,9 @@
require 'chef/knife'
# DEPRECATION NOTE
-# This code only remains to support users still operating with
-# Open Source Chef Server 11 and should be removed once support
-# for OSC 11 ends. New development should occur in the Chef Server 12
-# version of this file.
+# This code only remains to support users still operating with
+# Open Source Chef Server 11 and should be removed once support
+# for OSC 11 ends. New development should occur in the user_delete.rb.
class Chef
class Knife
@@ -49,4 +48,4 @@ class Chef
end
end
-end \ No newline at end of file
+end
diff --git a/lib/chef/knife/osc_user_edit.rb b/lib/chef/knife/osc_user_edit.rb
index d8afb13859..4c38674d08 100644
--- a/lib/chef/knife/osc_user_edit.rb
+++ b/lib/chef/knife/osc_user_edit.rb
@@ -19,10 +19,9 @@
require 'chef/knife'
# DEPRECATION NOTE
-# This code only remains to support users still operating with
-# Open Source Chef Server 11 and should be removed once support
-# for OSC 11 ends. New development should occur in the Chef Server 12
-# version of this file.
+# This code only remains to support users still operating with
+# Open Source Chef Server 11 and should be removed once support
+# for OSC 11 ends. New development should occur in user_edit.rb.
class Chef
class Knife
@@ -56,4 +55,4 @@ class Chef
end
end
end
-end \ No newline at end of file
+end
diff --git a/lib/chef/knife/osc_user_list.rb b/lib/chef/knife/osc_user_list.rb
index 10666b2c19..92f049cd19 100644
--- a/lib/chef/knife/osc_user_list.rb
+++ b/lib/chef/knife/osc_user_list.rb
@@ -19,10 +19,9 @@
require 'chef/knife'
# DEPRECATION NOTE
-# This code only remains to support users still operating with
-# Open Source Chef Server 11 and should be removed once support
-# for OSC 11 ends. New development should occur in the Chef Server 12
-# version of this file.
+# This code only remains to support users still operating with
+# Open Source Chef Server 11 and should be removed once support
+# for OSC 11 ends. New development should occur in user_list.rb.
class Chef
class Knife
@@ -45,4 +44,4 @@ class Chef
end
end
end
-end \ No newline at end of file
+end
diff --git a/lib/chef/knife/osc_user_reregister.rb b/lib/chef/knife/osc_user_reregister.rb
index d940820546..a71e0aa677 100644
--- a/lib/chef/knife/osc_user_reregister.rb
+++ b/lib/chef/knife/osc_user_reregister.rb
@@ -19,10 +19,9 @@
require 'chef/knife'
# DEPRECATION NOTE
-# This code only remains to support users still operating with
-# Open Source Chef Server 11 and should be removed once support
-# for OSC 11 ends. New development should occur in the Chef Server 12
-# version of this file.
+# This code only remains to support users still operating with
+# Open Source Chef Server 11 and should be removed once support
+# for OSC 11 ends. New development should occur in user_reregister.rb.
class Chef
class Knife
@@ -62,4 +61,4 @@ class Chef
end
end
end
-end \ No newline at end of file
+end
diff --git a/lib/chef/knife/osc_user_show.rb b/lib/chef/knife/osc_user_show.rb
index a383a764b2..6a41ddae88 100644
--- a/lib/chef/knife/osc_user_show.rb
+++ b/lib/chef/knife/osc_user_show.rb
@@ -19,10 +19,9 @@
require 'chef/knife'
# DEPRECATION NOTE
-# This code only remains to support users still operating with
-# Open Source Chef Server 11 and should be removed once support
-# for OSC 11 ends. New development should occur in the Chef Server 12
-# version of this file.
+# This code only remains to support users still operating with
+# Open Source Chef Server 11 and should be removed once support
+# for OSC 11 ends. New development should occur in user_show.rb.
class Chef
class Knife
@@ -52,4 +51,4 @@ class Chef
end
end
-end \ No newline at end of file
+end
diff --git a/lib/chef/knife/user_create.rb b/lib/chef/knife/user_create.rb
index 18addda741..5332e863ae 100644
--- a/lib/chef/knife/user_create.rb
+++ b/lib/chef/knife/user_create.rb
@@ -1,7 +1,7 @@
#
# Author:: Steven Danna (<steve@chef.io>)
# Author:: Tyler Cloke (<tyler@chef.io>)
-# Copyright:: Copyright (c) 2015 Chef Software, Inc.
+# Copyright:: Copyright (c) 2012, 2015 Chef Software, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -18,6 +18,7 @@
#
require 'chef/knife'
+require 'chef/knife/osc_user_create'
class Chef
class Knife
@@ -37,7 +38,7 @@ class Chef
option :user_key,
:long => "--user-key FILENAME",
- :description => "Set the initial default key for the user from a file on disk (cannot pass with --create-key)."
+ :description => "Set the initial default key for the user from a file on disk (cannot pass with --prevent-keygen)."
option :prevent_keygen,
:short => "-k",
@@ -45,6 +46,18 @@ class Chef
:description => "API V1 only. Prevent server from generating a default key pair for you. Cannot be passed with --user-key.",
:boolean => true
+ option :admin,
+ :short => "-a",
+ :long => "--admin",
+ :description => "DEPRECATED: Open Source Chef 11 only. Create the user as an admin.",
+ :boolean => true
+
+ option :user_password,
+ :short => "-p PASSWORD",
+ :long => "--password PASSWORD",
+ :description => "DEPRECATED: Open Source Chef 11 only. Password for newly created user.",
+ :default => ""
+
banner "knife user create USERNAME DISPLAY_NAME FIRST_NAME LAST_NAME EMAIL PASSWORD (options)"
def user
@@ -55,52 +68,87 @@ class Chef
Chef::User.from_hash(hash).create
end
+ def osc_11_warning
+<<-EOF
+IF YOU ARE USING CHEF SERVER 12+, PLEASE FOLLOW THE INSTRUCTIONS UNDER knife user create --help.
+You only passed a single argument to knife user create.
+For backwards compatibility, when only a single argument is passed,
+knife user create assumes you want Open Source 11 Server user creation.
+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
+ Chef::Knife.run(ARGV, Chef::Application::Knife.options)
+ end
+
def run
- test_mandatory_field(@name_args[0], "username")
- user.username @name_args[0]
+ # DEPRECATION NOTE
+ # Remove this if statement and corrosponding code post OSC 11 support.
+ #
+ # If only 1 arg is passed, assume OSC 11 case.
+ if @name_args.length == 1
+ ui.warn(osc_11_warning)
- test_mandatory_field(@name_args[1], "display name")
- user.display_name @name_args[1]
+ # run osc_user_create with our input
+ ARGV.delete("user")
+ ARGV.unshift("osc_user")
+ run_osc_11_user_create
- test_mandatory_field(@name_args[2], "first name")
- user.first_name @name_args[2]
+ else # EC / CS 12 user create
- test_mandatory_field(@name_args[3], "last name")
- user.last_name @name_args[3]
+ test_mandatory_field(@name_args[0], "username")
+ user.username @name_args[0]
- test_mandatory_field(@name_args[4], "email")
- user.email @name_args[4]
+ test_mandatory_field(@name_args[1], "display name")
+ user.display_name @name_args[1]
- test_mandatory_field(@name_args[5], "password")
- user.password @name_args[5]
+ test_mandatory_field(@name_args[2], "first name")
+ user.first_name @name_args[2]
- if config[:user_key] && config[:prevent_keygen]
- show_usage
- ui.fatal("You cannot pass --user-key and --prevent-keygen")
- exit 1
- end
+ test_mandatory_field(@name_args[3], "last name")
+ user.last_name @name_args[3]
- unless config[:prevent_keygen]
- user.create_key(true)
- end
+ test_mandatory_field(@name_args[4], "email")
+ user.email @name_args[4]
- if config[:user_key]
- user.public_key File.read(File.expand_path(config[:user_key]))
- end
+ test_mandatory_field(@name_args[5], "password")
+ user.password @name_args[5]
+
+ if config[:user_key] && config[:prevent_keygen]
+ show_usage
+ ui.fatal("You cannot pass --user-key and --prevent-keygen")
+ exit 1
+ end
+
+ if !config[:prevent_keygen] && !config[:user_key]
+ user.create_key(true)
+ end
- output = edit_data(user)
- final_user = create_user_from_hash(output)
+ if config[:user_key]
+ user.public_key File.read(File.expand_path(config[:user_key]))
+ end
- ui.info("Created #{user}")
- if final_user.private_key
- if config[:file]
- File.open(config[:file], "w") do |f|
- f.print(final_user.private_key)
+ output = edit_data(user)
+ final_user = create_user_from_hash(output)
+
+ ui.info("Created #{user}")
+ if final_user.private_key
+ if config[:file]
+ File.open(config[:file], "w") do |f|
+ f.print(final_user.private_key)
+ end
+ else
+ ui.msg final_user.private_key
end
- else
- ui.msg final_user.private_key
end
end
+
+
end
end
end
diff --git a/lib/chef/knife/user_delete.rb b/lib/chef/knife/user_delete.rb
index b7af11bec8..3a46e33f84 100644
--- a/lib/chef/knife/user_delete.rb
+++ b/lib/chef/knife/user_delete.rb
@@ -29,6 +29,37 @@ class Chef
banner "knife user delete USER (options)"
+ def osc_11_warning
+<<-EOF
+The Chef Server you are using does not support the username field.
+This means it is an Open Source 11 Server.
+knife user delete 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 delete.
+If you are using an Open Source 11 Server, please use that command to avoid this warning.
+EOF
+ end
+
+ def run_osc_11_user_delete
+ Chef::Knife.run(ARGV, Chef::Application::Knife.options)
+ end
+
+ # DEPRECATION NOTE
+ # Delete this override method after OSC 11 support is dropped
+ def delete_object(user_name)
+ confirm("Do you really want to delete #{user_name}")
+
+ if Kernel.block_given?
+ object = block.call
+ else
+ object = Chef::User.load(user_name)
+ object.destroy
+ end
+
+ output(format_for_display(object)) if config[:print_after]
+ self.msg("Deleted #{user_name}")
+ end
+
def run
@user_name = @name_args[0]
@@ -38,9 +69,30 @@ class Chef
exit 1
end
- delete_object(Chef::User, @user_name)
- end
+ # DEPRECATION NOTE
+ #
+ # Below is modification of Chef::Knife.delete_object to detect OSC 11 server.
+ # When OSC 11 is deprecated, simply delete all this and go back to:
+ #
+ # delete_object(Chef::User, @user_name)
+ #
+ # Also delete our override of delete_object above
+ object = Chef::User.load(@user_name)
+
+ # 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
+
+ end
end
end
end
diff --git a/lib/chef/knife/user_edit.rb b/lib/chef/knife/user_edit.rb
index ae319c8872..5bed51a1cc 100644
--- a/lib/chef/knife/user_edit.rb
+++ b/lib/chef/knife/user_edit.rb
@@ -29,6 +29,21 @@ class Chef
banner "knife user edit USER (options)"
+ def osc_11_warning
+<<-EOF
+The Chef Server you are using does not support the username field.
+This means it is an Open Source 11 Server.
+knife user edit for Open Source 11 Server is being deprecated.
+Open Source 11 Server user commands now live under the knife oc_user namespace.
+For backwards compatibility, we will forward this request to knife osc_user edit.
+If you are using an Open Source 11 Server, please use that command to avoid this warning.
+EOF
+ end
+
+ def run_osc_11_user_edit
+ Chef::Knife.run(ARGV, Chef::Application::Knife.options)
+ end
+
def run
@user_name = @name_args[0]
@@ -39,14 +54,31 @@ class Chef
end
original_user = Chef::User.load(@user_name).to_hash
- edited_user = edit_data(original_user)
- if original_user != edited_user
- user = Chef::User.from_hash(edited_user)
- user.update
- ui.msg("Saved #{user}.")
- else
- ui.msg("User unchaged, not saving.")
+
+ # DEPRECATION NOTE
+ # Remove this if statement and corrosponding code post OSC 11 support.
+ #
+ # if username is nil, we are in the OSC 11 case,
+ # 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
+ user = Chef::User.from_hash(edited_user)
+ user.update
+ ui.msg("Saved #{user}.")
+ else
+ ui.msg("User unchaged, not saving.")
+ end
end
+
end
end
end
diff --git a/lib/chef/knife/user_list.rb b/lib/chef/knife/user_list.rb
index 5d2e735a73..7ae43dadc9 100644
--- a/lib/chef/knife/user_list.rb
+++ b/lib/chef/knife/user_list.rb
@@ -18,6 +18,8 @@
require 'chef/knife'
+# NOTE: only knife user command that is backwards compatible with OSC 11,
+# so no deprecation warnings are necessary.
class Chef
class Knife
class UserList < Knife
@@ -37,6 +39,7 @@ class Chef
def run
output(format_list_for_display(Chef::User.list))
end
+
end
end
end
diff --git a/lib/chef/knife/user_reregister.rb b/lib/chef/knife/user_reregister.rb
index 946150e6e4..c2b39761cc 100644
--- a/lib/chef/knife/user_reregister.rb
+++ b/lib/chef/knife/user_reregister.rb
@@ -29,6 +29,21 @@ class Chef
banner "knife user reregister USER (options)"
+ def osc_11_warning
+<<-EOF
+The Chef Server you are using does not support the username field.
+This means it is an Open Source 11 Server.
+knife user reregister 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 reregister.
+If you are using an Open Source 11 Server, please use that command to avoid this warning.
+EOF
+ end
+
+ def run_osc_11_user_reregister
+ Chef::Knife.run(ARGV, Chef::Application::Knife.options)
+ end
+
option :file,
:short => "-f FILE",
:long => "--file FILE",
@@ -43,16 +58,33 @@ class Chef
exit 1
end
- user = Chef::User.load(@user_name).reregister
- Chef::Log.debug("Updated user data: #{user.inspect}")
- key = user.private_key
- if config[:file]
- File.open(config[:file], "w") do |f|
- f.print(key)
+ user = Chef::User.load(@user_name)
+
+ # DEPRECATION NOTE
+ # Remove this if statement and corrosponding code post OSC 11 support.
+ #
+ # if username is nil, we are in the OSC 11 case,
+ # 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
+ Chef::Log.debug("Updated user data: #{user.inspect}")
+ key = user.private_key
+ if config[:file]
+ File.open(config[:file], "w") do |f|
+ f.print(key)
+ end
+ else
+ ui.msg key
end
- else
- ui.msg key
end
+
end
end
end
diff --git a/lib/chef/knife/user_show.rb b/lib/chef/knife/user_show.rb
index 61ea101e4c..d0e9f64f53 100644
--- a/lib/chef/knife/user_show.rb
+++ b/lib/chef/knife/user_show.rb
@@ -31,6 +31,21 @@ class Chef
banner "knife user show USER (options)"
+ def osc_11_warning
+<<-EOF
+The Chef Server you are using does not support the username field.
+This means it is an Open Source 11 Server.
+knife user show 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 show.
+If you are using an Open Source 11 Server, please use that command to avoid this warning.
+EOF
+ end
+
+ def run_osc_11_user_show
+ Chef::Knife.run(ARGV, Chef::Application::Knife.options)
+ end
+
def run
@user_name = @name_args[0]
@@ -41,7 +56,22 @@ class Chef
end
user = Chef::User.load(@user_name)
- output(format_for_display(user))
+
+ # DEPRECATION NOTE
+ # Remove this if statement and corrosponding code post OSC 11 support.
+ #
+ # if username is nil, we are in the OSC 11 case,
+ # 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))
+ end
end
end
diff --git a/lib/chef/osc_user.rb b/lib/chef/osc_user.rb
index d2e329c944..52bfd11108 100644
--- a/lib/chef/osc_user.rb
+++ b/lib/chef/osc_user.rb
@@ -26,7 +26,7 @@ require 'chef/search/query'
# DEPRECATION NOTE
# This class was previously Chef::User. It is the code to support the User object
# corrosponding to the Open Source Chef Server 11 and only still exists to support
-# users still on OSC 11.
+# users still on OSC 11.
#
# Chef::User now supports Chef Server 12.
#
diff --git a/lib/chef/user.rb b/lib/chef/user.rb
index 2ffba18005..1b5f454099 100644
--- a/lib/chef/user.rb
+++ b/lib/chef/user.rb
@@ -25,6 +25,12 @@ require 'chef/mixin/api_version_request_handling'
require 'chef/exceptions'
require 'chef/server_api'
+# OSC 11 BACKWARDS COMPATIBILITY NOTE (remove after OSC 11 support ends)
+#
+# In general, Chef::User is no longer expected to support Open Source Chef 11 Server requests.
+# The object that handles those requests has been moved to the Chef::OscUser namespace.
+#
+# Exception: self.list is backwards compatible with OSC 11
class Chef
class User
@@ -132,6 +138,7 @@ class Chef
end
def destroy
+ # will default to the current API version (Chef::Authenticator::DEFAULT_SERVER_API_VERSION)
Chef::REST.new(Chef::Config[:chef_server_url]).delete("users/#{@username}")
end
@@ -278,15 +285,17 @@ class Chef
def self.list(inflate=false)
response = Chef::REST.new(Chef::Config[:chef_server_url]).get('users')
- # Gross. Transforms an API response in the form of:
- # [ { "user" => { "username" => USERNAME }}, ...]
- # into the form
- # { "USERNAME" => "URI" }
- users = Hash.new
- response.each do |u|
- name = u['user']['username']
- users[name] = Chef::Config[:chef_server_url] + "/users/#{name}"
- end
+ users = if response.is_a?(Array)
+ # EC 11 / CS 12 V0, V1
+ # GET /organizations/<org>/users
+ transform_list_response(response)
+ else
+ # OSC 11
+ # GET /users
+ # EC 11 / CS 12 V0, V1
+ # GET /users
+ response # OSC
+ end
if inflate
users.inject({}) do |user_map, (name, _url)|
@@ -299,9 +308,25 @@ class Chef
end
def self.load(username)
+ # will default to the current API version (Chef::Authenticator::DEFAULT_SERVER_API_VERSION)
response = Chef::REST.new(Chef::Config[:chef_server_url]).get("users/#{username}")
Chef::User.from_hash(response)
end
+ # Gross. Transforms an API response in the form of:
+ # [ { "user" => { "username" => USERNAME }}, ...]
+ # into the form
+ # { "USERNAME" => "URI" }
+ def self.transform_list_response(response)
+ new_response = Hash.new
+ response.each do |u|
+ name = u['user']['username']
+ new_response[name] = Chef::Config[:chef_server_url] + "/users/#{name}"
+ end
+ new_response
+ end
+
+ private_class_method :transform_list_response
+
end
end
diff --git a/spec/unit/knife/osc_user_create_spec.rb b/spec/unit/knife/osc_user_create_spec.rb
index 4de4d1fa00..1b17d0d22f 100644
--- a/spec/unit/knife/osc_user_create_spec.rb
+++ b/spec/unit/knife/osc_user_create_spec.rb
@@ -21,10 +21,9 @@ require 'spec_helper'
Chef::Knife::OscUserCreate.load_deps
# DEPRECATION NOTE
-# This code only remains to support users still operating with
-# Open Source Chef Server 11 and should be removed once support
-# for OSC 11 ends. New development should occur in the Chef Server 12
-# version of this file.
+# This code only remains to support users still operating with
+# Open Source Chef Server 11 and should be removed once support
+# for OSC 11 ends. New development should occur in user_create_spec.rb.
describe Chef::Knife::OscUserCreate do
before(:each) do
@@ -91,4 +90,4 @@ describe Chef::Knife::OscUserCreate do
expect(File).to receive(:open).with("/tmp/a_file", "w").and_yield(filehandle)
@knife.run
end
-end \ No newline at end of file
+end
diff --git a/spec/unit/knife/osc_user_delete_spec.rb b/spec/unit/knife/osc_user_delete_spec.rb
index 7b351579a4..0e16393ffe 100644
--- a/spec/unit/knife/osc_user_delete_spec.rb
+++ b/spec/unit/knife/osc_user_delete_spec.rb
@@ -19,10 +19,9 @@
require 'spec_helper'
# DEPRECATION NOTE
-# This code only remains to support users still operating with
-# Open Source Chef Server 11 and should be removed once support
-# for OSC 11 ends. New development should occur in the Chef Server 12
-# version of this file.
+# This code only remains to support users still operating with
+# Open Source Chef Server 11 and should be removed once support
+# for OSC 11 ends. New development should occur in user_delete_spec.rb.
describe Chef::Knife::OscUserDelete do
before(:each) do
@@ -42,4 +41,4 @@ describe Chef::Knife::OscUserDelete do
expect(@knife.ui).to receive(:fatal)
expect { @knife.run }.to raise_error(SystemExit)
end
-end \ No newline at end of file
+end
diff --git a/spec/unit/knife/osc_user_edit_spec.rb b/spec/unit/knife/osc_user_edit_spec.rb
index 457f310603..71a9192389 100644
--- a/spec/unit/knife/osc_user_edit_spec.rb
+++ b/spec/unit/knife/osc_user_edit_spec.rb
@@ -19,10 +19,9 @@
require 'spec_helper'
# DEPRECATION NOTE
-# This code only remains to support users still operating with
-# Open Source Chef Server 11 and should be removed once support
-# for OSC 11 ends. New development should occur in the Chef Server 12
-# version of this file.
+# This code only remains to support users still operating with
+# Open Source Chef Server 11 and should be removed once support
+# for OSC 11 ends. New development should occur in user_edit_spec.rb.
describe Chef::Knife::OscUserEdit do
before(:each) do
diff --git a/spec/unit/knife/osc_user_list_spec.rb b/spec/unit/knife/osc_user_list_spec.rb
index 001b6099cf..59a15be058 100644
--- a/spec/unit/knife/osc_user_list_spec.rb
+++ b/spec/unit/knife/osc_user_list_spec.rb
@@ -19,10 +19,9 @@
require 'spec_helper'
# DEPRECATION NOTE
-# This code only remains to support users still operating with
-# Open Source Chef Server 11 and should be removed once support
-# for OSC 11 ends. New development should occur in the Chef Server 12
-# version of this file.
+# This code only remains to support users still operating with
+# Open Source Chef Server 11 and should be removed once support
+# for OSC 11 ends. New development should occur in user_list_spec.rb.
describe Chef::Knife::OscUserList do
before(:each) do
diff --git a/spec/unit/knife/osc_user_reregister_spec.rb b/spec/unit/knife/osc_user_reregister_spec.rb
index 13cb3000dc..406bbf1f3e 100644
--- a/spec/unit/knife/osc_user_reregister_spec.rb
+++ b/spec/unit/knife/osc_user_reregister_spec.rb
@@ -20,9 +20,8 @@ require 'spec_helper'
# DEPRECATION NOTE
# This code only remains to support users still operating with
-# Open Source Chef Server 11 and should be removed once support
-# for OSC 11 ends. New development should occur in the Chef Server 12
-# version of this file.
+# Open Source Chef Server 11 and should be removed once support
+# for OSC 11 ends. New development should occur in user_reregister_spec.rb.
describe Chef::Knife::OscUserReregister do
before(:each) do
diff --git a/spec/unit/knife/osc_user_show_spec.rb b/spec/unit/knife/osc_user_show_spec.rb
index 4a5d23d99a..67b9b45809 100644
--- a/spec/unit/knife/osc_user_show_spec.rb
+++ b/spec/unit/knife/osc_user_show_spec.rb
@@ -20,9 +20,8 @@ require 'spec_helper'
# DEPRECATION NOTE
# This code only remains to support users still operating with
-# Open Source Chef Server 11 and should be removed once support
-# for OSC 11 ends. New development should occur in the Chef Server 12
-# version of this file.
+# Open Source Chef Server 11 and should be removed once support
+# for OSC 11 ends. New development should occur user_show_spec.rb.
describe Chef::Knife::OscUserShow do
before(:each) do
diff --git a/spec/unit/knife/user_create_spec.rb b/spec/unit/knife/user_create_spec.rb
index 879d4d4889..49d62cc2d7 100644
--- a/spec/unit/knife/user_create_spec.rb
+++ b/spec/unit/knife/user_create_spec.rb
@@ -1,7 +1,7 @@
#
# Author:: Steven Danna (<steve@chef.io>)
# Author:: Tyler Cloke (<tyler@chef.io>)
-# Copyright:: Copyright (c) 2015 Chef Software, Inc.
+# Copyright:: Copyright (c) 2012, 2015 Chef Software, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -35,6 +35,26 @@ describe Chef::Knife::UserCreate do
before(:each) do
allow(knife.ui).to receive(:stdout).and_return(stdout)
allow(knife.ui).to receive(:stderr).and_return(stderr)
+ allow(knife.ui).to receive(:warn)
+ end
+
+ # delete this once OSC11 support is gone
+ context "when only one name_arg is passed" do
+ before do
+ knife.name_args = ['some_user']
+ allow(knife).to receive(:run_osc_11_user_create).and_raise(SystemExit)
+ end
+
+ it "displays the osc warning" do
+ expect(knife.ui).to receive(:warn).with(knife.osc_11_warning)
+ expect{ knife.run }.to raise_error(SystemExit)
+ end
+
+ it "calls knife osc_user create" do
+ expect(knife).to receive(:run_osc_11_user_create)
+ expect{ knife.run }.to raise_error(SystemExit)
+ end
+
end
context "when USERNAME isn't specified" do
@@ -45,13 +65,16 @@ describe Chef::Knife::UserCreate do
end
end
- context "when DISPLAY_NAME isn't specified" do
- # from spec/support/shared/unit/knife_shared.rb
- it_should_behave_like "mandatory field missing" do
- let(:name_args) { ['some_user'] }
- let(:fieldname) { 'display name' }
- end
- end
+ # uncomment once OSC11 support is gone,
+ # pending doesn't work for shared_examples_for by default
+ #
+ # context "when DISPLAY_NAME isn't specified" do
+ # # from spec/support/shared/unit/knife_shared.rb
+ # it_should_behave_like "mandatory field missing" do
+ # let(:name_args) { ['some_user'] }
+ # let(:fieldname) { 'display name' }
+ # end
+ # end
context "when FIRST_NAME isn't specified" do
# from spec/support/shared/unit/knife_shared.rb
diff --git a/spec/unit/knife/user_delete_spec.rb b/spec/unit/knife/user_delete_spec.rb
index 94cfbf3db1..e49c781358 100644
--- a/spec/unit/knife/user_delete_spec.rb
+++ b/spec/unit/knife/user_delete_spec.rb
@@ -19,21 +19,47 @@
require 'spec_helper'
describe Chef::Knife::UserDelete do
+ let(:knife) { Chef::Knife::UserDelete.new }
+ let(:user) { double('user_object') }
+ let(:stdout) { StringIO.new }
+
before(:each) do
Chef::Knife::UserDelete.load_deps
- @knife = Chef::Knife::UserDelete.new
- @knife.name_args = [ 'my_user' ]
+ knife.name_args = [ 'my_user' ]
+ allow(Chef::User).to receive(:load).and_return(user)
+ allow(user).to receive(:username).and_return('my_user')
+ allow(knife.ui).to receive(:stderr).and_return(stdout)
+ allow(knife.ui).to receive(:stdout).and_return(stdout)
+ end
+
+ # delete this once OSC11 support is gone
+ context "when the username field is not supported by the server" do
+ before do
+ allow(knife).to receive(:run_osc_11_user_delete).and_raise(SystemExit)
+ allow(user).to receive(:username).and_return(nil)
+ end
+
+ it "displays the osc warning" do
+ expect(knife.ui).to receive(:warn).with(knife.osc_11_warning)
+ expect{ knife.run }.to raise_error(SystemExit)
+ end
+
+ it "forwards the command to knife osc_user edit" do
+ expect(knife).to receive(:run_osc_11_user_delete)
+ expect{ knife.run }.to raise_error(SystemExit)
+ end
end
it 'deletes the user' do
- expect(@knife).to receive(:delete_object).with(Chef::User, 'my_user')
- @knife.run
+ #expect(knife).to receive(:delete_object).with(Chef::User, 'my_user')
+ expect(knife).to receive(:delete_object).with('my_user')
+ knife.run
end
it 'prints usage and exits when a user name is not provided' do
- @knife.name_args = []
- expect(@knife).to receive(:show_usage)
- expect(@knife.ui).to receive(:fatal)
- expect { @knife.run }.to raise_error(SystemExit)
+ knife.name_args = []
+ expect(knife).to receive(:show_usage)
+ expect(knife.ui).to receive(:fatal)
+ expect { knife.run }.to raise_error(SystemExit)
end
end
diff --git a/spec/unit/knife/user_edit_spec.rb b/spec/unit/knife/user_edit_spec.rb
index 0eb75cfa9b..15a7726b20 100644
--- a/spec/unit/knife/user_edit_spec.rb
+++ b/spec/unit/knife/user_edit_spec.rb
@@ -19,29 +19,48 @@
require 'spec_helper'
describe Chef::Knife::UserEdit do
+ let(:knife) { Chef::Knife::UserEdit.new }
+
before(:each) do
@stderr = StringIO.new
@stdout = StringIO.new
Chef::Knife::UserEdit.load_deps
- @knife = Chef::Knife::UserEdit.new
- allow(@knife.ui).to receive(:stderr).and_return(@stderr)
- allow(@knife.ui).to receive(:stdout).and_return(@stdout)
- @knife.name_args = [ 'my_user' ]
- @knife.config[:disable_editing] = true
+ allow(knife.ui).to receive(:stderr).and_return(@stderr)
+ allow(knife.ui).to receive(:stdout).and_return(@stdout)
+ knife.name_args = [ 'my_user' ]
+ knife.config[:disable_editing] = true
+ end
+
+ # delete this once OSC11 support is gone
+ context "when the username field is not supported by the server" do
+ before do
+ allow(knife).to receive(:run_osc_11_user_edit).and_raise(SystemExit)
+ allow(Chef::User).to receive(:load).and_return({"username" => nil})
+ end
+
+ it "displays the osc warning" do
+ expect(knife.ui).to receive(:warn).with(knife.osc_11_warning)
+ expect{ knife.run }.to raise_error(SystemExit)
+ end
+
+ it "forwards the command to knife osc_user edit" do
+ expect(knife).to receive(:run_osc_11_user_edit)
+ expect{ knife.run }.to raise_error(SystemExit)
+ end
end
it 'loads and edits the user' do
- data = { :name => "my_user" }
+ data = { "username" => "my_user" }
allow(Chef::User).to receive(:load).with("my_user").and_return(data)
- expect(@knife).to receive(:edit_data).with(data).and_return(data)
- @knife.run
+ expect(knife).to receive(:edit_data).with(data).and_return(data)
+ knife.run
end
it 'prints usage and exits when a user name is not provided' do
- @knife.name_args = []
- expect(@knife).to receive(:show_usage)
- expect(@knife.ui).to receive(:fatal)
- expect { @knife.run }.to raise_error(SystemExit)
+ knife.name_args = []
+ expect(knife).to receive(:show_usage)
+ expect(knife.ui).to receive(:fatal)
+ expect { knife.run }.to raise_error(SystemExit)
end
end
diff --git a/spec/unit/knife/user_list_spec.rb b/spec/unit/knife/user_list_spec.rb
index db097a5c16..9990cc802d 100644
--- a/spec/unit/knife/user_list_spec.rb
+++ b/spec/unit/knife/user_list_spec.rb
@@ -19,14 +19,18 @@
require 'spec_helper'
describe Chef::Knife::UserList do
+ let(:knife) { Chef::Knife::UserList.new }
+ let(:stdout) { StringIO.new }
+
before(:each) do
Chef::Knife::UserList.load_deps
- @knife = Chef::Knife::UserList.new
+ allow(knife.ui).to receive(:stderr).and_return(stdout)
+ allow(knife.ui).to receive(:stdout).and_return(stdout)
end
it 'lists the users' do
expect(Chef::User).to receive(:list)
- expect(@knife).to receive(:format_list_for_display)
- @knife.run
+ expect(knife).to receive(:format_list_for_display)
+ knife.run
end
end
diff --git a/spec/unit/knife/user_reregister_spec.rb b/spec/unit/knife/user_reregister_spec.rb
index 1268716f40..412a6ec374 100644
--- a/spec/unit/knife/user_reregister_spec.rb
+++ b/spec/unit/knife/user_reregister_spec.rb
@@ -19,35 +19,56 @@
require 'spec_helper'
describe Chef::Knife::UserReregister do
- before(:each) do
+ let(:knife) { Chef::Knife::UserReregister.new }
+ let(:user_mock) { double('user_mock', :private_key => "private_key") }
+ let(:stdout) { StringIO.new }
+
+ before do
Chef::Knife::UserReregister.load_deps
- @knife = Chef::Knife::UserReregister.new
- @knife.name_args = [ 'a_user' ]
- @user_mock = double('user_mock', :private_key => "private_key")
- allow(Chef::User).to receive(:load).and_return(@user_mock)
- @stdout = StringIO.new
- allow(@knife.ui).to receive(:stdout).and_return(@stdout)
+ knife.name_args = [ 'a_user' ]
+ allow(Chef::User).to receive(:load).and_return(user_mock)
+ allow(knife.ui).to receive(:stdout).and_return(stdout)
+ allow(knife.ui).to receive(:stderr).and_return(stdout)
+ allow(user_mock).to receive(:username).and_return('a_user')
+ end
+
+ # delete this once OSC11 support is gone
+ context "when the username field is not supported by the server" do
+ before do
+ allow(knife).to receive(:run_osc_11_user_reregister).and_raise(SystemExit)
+ allow(user_mock).to receive(:username).and_return(nil)
+ end
+
+ it "displays the osc warning" do
+ expect(knife.ui).to receive(:warn).with(knife.osc_11_warning)
+ expect{ knife.run }.to raise_error(SystemExit)
+ end
+
+ it "forwards the command to knife osc_user edit" do
+ expect(knife).to receive(:run_osc_11_user_reregister)
+ expect{ knife.run }.to raise_error(SystemExit)
+ end
end
it 'prints usage and exits when a user name is not provided' do
- @knife.name_args = []
- expect(@knife).to receive(:show_usage)
- expect(@knife.ui).to receive(:fatal)
- expect { @knife.run }.to raise_error(SystemExit)
+ knife.name_args = []
+ expect(knife).to receive(:show_usage)
+ expect(knife.ui).to receive(:fatal)
+ expect { knife.run }.to raise_error(SystemExit)
end
it 'reregisters the user and prints the key' do
- expect(@user_mock).to receive(:reregister).and_return(@user_mock)
- @knife.run
- expect(@stdout.string).to match( /private_key/ )
+ expect(user_mock).to receive(:reregister).and_return(user_mock)
+ knife.run
+ expect(stdout.string).to match( /private_key/ )
end
it 'writes the private key to a file when --file is specified' do
- expect(@user_mock).to receive(:reregister).and_return(@user_mock)
- @knife.config[:file] = '/tmp/a_file'
+ expect(user_mock).to receive(:reregister).and_return(user_mock)
+ knife.config[:file] = '/tmp/a_file'
filehandle = StringIO.new
expect(File).to receive(:open).with('/tmp/a_file', 'w').and_yield(filehandle)
- @knife.run
+ knife.run
expect(filehandle.string).to eq("private_key")
end
end
diff --git a/spec/unit/knife/user_show_spec.rb b/spec/unit/knife/user_show_spec.rb
index f97cbc3f13..43392a3a5c 100644
--- a/spec/unit/knife/user_show_spec.rb
+++ b/spec/unit/knife/user_show_spec.rb
@@ -19,23 +19,47 @@
require 'spec_helper'
describe Chef::Knife::UserShow do
- before(:each) do
+ let(:knife) { Chef::Knife::UserShow.new }
+ let(:user_mock) { double('user_mock') }
+ let(:stdout) { StringIO.new }
+
+ before do
Chef::Knife::UserShow.load_deps
- @knife = Chef::Knife::UserShow.new
- @knife.name_args = [ 'my_user' ]
- @user_mock = double('user_mock')
+ knife.name_args = [ 'my_user' ]
+ allow(user_mock).to receive(:username).and_return('my_user')
+ allow(knife.ui).to receive(:stderr).and_return(stdout)
+ allow(knife.ui).to receive(:stdout).and_return(stdout)
+ end
+
+ # delete this once OSC11 support is gone
+ context "when the username field is not supported by the server" do
+ before do
+ allow(knife).to receive(:run_osc_11_user_show).and_raise(SystemExit)
+ allow(Chef::User).to receive(:load).with('my_user').and_return(user_mock)
+ allow(user_mock).to receive(:username).and_return(nil)
+ end
+
+ it "displays the osc warning" do
+ expect(knife.ui).to receive(:warn).with(knife.osc_11_warning)
+ expect{ knife.run }.to raise_error(SystemExit)
+ end
+
+ it "forwards the command to knife osc_user edit" do
+ expect(knife).to receive(:run_osc_11_user_show)
+ expect{ knife.run }.to raise_error(SystemExit)
+ end
end
it 'loads and displays the user' do
- expect(Chef::User).to receive(:load).with('my_user').and_return(@user_mock)
- expect(@knife).to receive(:format_for_display).with(@user_mock)
- @knife.run
+ expect(Chef::User).to receive(:load).with('my_user').and_return(user_mock)
+ expect(knife).to receive(:format_for_display).with(user_mock)
+ knife.run
end
it 'prints usage and exits when a user name is not provided' do
- @knife.name_args = []
- expect(@knife).to receive(:show_usage)
- expect(@knife.ui).to receive(:fatal)
- expect { @knife.run }.to raise_error(SystemExit)
+ knife.name_args = []
+ expect(knife).to receive(:show_usage)
+ expect(knife.ui).to receive(:fatal)
+ expect { knife.run }.to raise_error(SystemExit)
end
end
diff --git a/spec/unit/osc_user_spec.rb b/spec/unit/osc_user_spec.rb
index 26cbccdf56..678486a16d 100644
--- a/spec/unit/osc_user_spec.rb
+++ b/spec/unit/osc_user_spec.rb
@@ -18,9 +18,8 @@
# DEPRECATION NOTE
# This code only remains to support users still operating with
-# Open Source Chef Server 11 and should be removed once support
-# for OSC 11 ends. New development should occur in the Chef Server 12
-# version of this file.
+# Open Source Chef Server 11 and should be removed once support
+# for OSC 11 ends. New development should occur in user_spec.rb.
require 'spec_helper'
@@ -274,4 +273,4 @@ describe Chef::OscUser do
end
end
end
-end \ No newline at end of file
+end