Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove knife user support for open source Chef Server < 12 #7841

Merged
merged 2 commits into from Nov 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 0 additions & 51 deletions lib/chef/knife/osc_user_delete.rb

This file was deleted.

53 changes: 0 additions & 53 deletions lib/chef/knife/osc_user_show.rb

This file was deleted.

108 changes: 32 additions & 76 deletions lib/chef/knife/user_create.rb
Expand Up @@ -45,18 +45,6 @@ class UserCreate < Knife
description: "API V1 (Chef Server 12.1+) 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
Expand All @@ -67,82 +55,50 @@ def create_user_from_hash(hash)
Chef::UserV1.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.
NOTE: Backwards compatibility for Open Source 11 Server in these commands will be removed
in Chef 15 which will be released April 2019.
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

def run
# 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)
run_osc_11_user_create
else # EC / CS 12 user create
test_mandatory_field(@name_args[0], "username")
user.username @name_args[0]

test_mandatory_field(@name_args[0], "username")
user.username @name_args[0]
test_mandatory_field(@name_args[1], "display name")
user.display_name @name_args[1]

test_mandatory_field(@name_args[1], "display name")
user.display_name @name_args[1]
test_mandatory_field(@name_args[2], "first name")
user.first_name @name_args[2]

test_mandatory_field(@name_args[2], "first name")
user.first_name @name_args[2]
test_mandatory_field(@name_args[3], "last name")
user.last_name @name_args[3]

test_mandatory_field(@name_args[3], "last name")
user.last_name @name_args[3]
test_mandatory_field(@name_args[4], "email")
user.email @name_args[4]

test_mandatory_field(@name_args[4], "email")
user.email @name_args[4]
test_mandatory_field(@name_args[5], "password")
user.password @name_args[5]

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[: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

if !config[:prevent_keygen] && !config[:user_key]
user.create_key(true)
end
if config[:user_key]
user.public_key File.read(File.expand_path(config[:user_key]))
end

if config[:user_key]
user.public_key File.read(File.expand_path(config[:user_key]))
end
output = edit_hash(user)
final_user = create_user_from_hash(output)

output = edit_hash(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
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
end
end
Expand Down
54 changes: 1 addition & 53 deletions lib/chef/knife/user_delete.rb
Expand Up @@ -28,42 +28,6 @@ class UserDelete < Knife

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.
NOTE: Backwards compatibility for Open Source 11 Server in these commands will be removed
in Chef 15 which will be released April 2019.
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

# 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::UserV1.load(user_name)
object.destroy
end

output(format_for_display(object)) if config[:print_after]
msg("Deleted #{user_name}")
end

def run
@user_name = @name_args[0]

Expand All @@ -73,23 +37,7 @@ def run
exit 1
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::UserV1, @user_name)
#
# Also delete our override of delete_object above
object = Chef::UserV1.load(@user_name)

# OSC 11 case
if object.username.nil?
ui.warn(osc_11_warning)
run_osc_11_user_delete
else # proceed with EC / CS delete
delete_object(@user_name)
end
delete_object(Chef::UserV1, @user_name)
end
end
end
Expand Down
46 changes: 8 additions & 38 deletions lib/chef/knife/user_edit.rb
Expand Up @@ -28,26 +28,6 @@ class UserEdit < Knife

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.
NOTE: Backwards compatibility for Open Source 11 Server in these commands will be removed
in Chef 15 which will be released April 2019.
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

def run
@user_name = @name_args[0]

Expand All @@ -57,24 +37,14 @@ def run
exit 1
end

original_user = Chef::UserV1.load(@user_name).to_h
# 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_11_user_edit
else # EC / CS 12 user create
edited_user = edit_hash(original_user)
if original_user != edited_user
user = Chef::UserV1.from_hash(edited_user)
user.update
ui.msg("Saved #{user}.")
else
ui.msg("User unchanged, not saving.")
end
original_user = Chef::UserV1.load(@user_name).to_hash
edited_user = edit_hash(original_user)
if original_user != edited_user
user = Chef::UserV1.from_hash(edited_user)
user.update
ui.msg("Saved #{user}.")
else
ui.msg("User unchanged, not saving.")
end
end
end
Expand Down
2 changes: 0 additions & 2 deletions lib/chef/knife/user_list.rb
Expand Up @@ -18,8 +18,6 @@

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
Expand Down