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

Add a common method for asking for a new password #251

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 1 addition & 26 deletions lib/manageiq/appliance_console/database_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,32 +121,7 @@ def ask_for_database_credentials(password_twice = true)
self.port = ask_for_integer("port number", nil, port) unless local?
self.database = just_ask("name of the database on #{host}", database) unless local?
self.username = just_ask("username", username) unless local?
count = 0
loop do
password1 = ask_for_password("database password on #{host}", password)
# if they took the default, just bail
break if (password1 == password)

if password1.strip.length == 0
say("\nPassword can not be empty, please try again")
next
end
if password_twice
password2 = ask_for_password("database password again")
if password1 == password2
self.password = password1
break
elsif count > 0 # only reprompt password once
raise "passwords did not match"
else
count += 1
say("\nThe passwords did not match, please try again")
end
else
self.password = password1
break
end
end
self.password = ask_for_new_password("database password on #{host}", :default => password, :confirm_password => password_twice)
end

def friendly_inspect
Expand Down
23 changes: 4 additions & 19 deletions lib/manageiq/appliance_console/database_replication.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module ManageIQ
module ApplianceConsole
class DatabaseReplication
include ManageIQ::ApplianceConsole::Logging
include ManageIQ::ApplianceConsole::Prompts

PGPASS_FILE = '/var/lib/pgsql/.pgpass'.freeze
NETWORK_INTERFACE = 'eth0'.freeze
Expand Down Expand Up @@ -115,25 +116,9 @@ def write_pgpass_file
private

def ask_for_cluster_database_credentials
self.database_name = just_ask("cluster database name", database_name)
self.database_user = just_ask("cluster database username", database_user)

count = 0
loop do
count += 1
password1 = ask_for_password("cluster database password", database_password)
# if they took the default, just bail
break if password1 == database_password
password2 = ask_for_password("cluster database password")
if password1 == password2
self.database_password = password1
break
elsif count > 1 # only reprompt password once
raise RuntimeError, "passwords did not match"
else
say("\nThe passwords did not match, please try again")
end
end
self.database_name = just_ask("cluster database name", database_name)
self.database_user = just_ask("cluster database username", database_user)
self.database_password = ask_for_new_password("cluster database password", :default => database_password)
end

def run_repmgr_command(cmd, params = {})
Expand Down
1 change: 1 addition & 0 deletions lib/manageiq/appliance_console/message_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module ManageIQ
module ApplianceConsole
class MessageConfiguration
include ManageIQ::ApplianceConsole::ManageiqUserMixin
include ManageIQ::ApplianceConsole::Prompts

attr_reader :message_keystore_username, :message_keystore_password,
:message_server_host, :message_server_port,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def ask_for_parameters
@message_server_host = "127.0.0.1" if @message_server_host.include?("localhost")

@message_keystore_username = ask_for_string("Message Keystore Username", message_keystore_username)
@message_keystore_password = ask_for_password("Message Keystore Password")
@message_keystore_password = ask_for_new_password("Message Keystore Password")
@message_persistent_disk = ask_for_persistent_disk
end

Expand Down
21 changes: 21 additions & 0 deletions lib/manageiq/appliance_console/prompts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,27 @@ def ask_for_password(prompt, default = nil)
pass == "********" ? (default || "") : pass
end

def ask_for_new_password(prompt, default: nil, allow_empty: false, retry_limit: 1, confirm_password: true)
count = 0
loop do
password1 = ask_for_password(prompt, default)
if password1.strip.empty? && !allow_empty
say("\nPassword can not be empty, please try again")
next
end

return password1 if password1 == default || !confirm_password

password2 = ask_for_password(prompt)
return password1 if password1 == password2

raise "passwords did not match" if count >= retry_limit

count += 1
say("\nThe passwords did not match, please try again")
end
end

def ask_for_string(prompt, default = nil)
just_ask(prompt, default)
end
Expand Down
22 changes: 17 additions & 5 deletions spec/message_configuration_server_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,19 @@
it "should prompt for message_keystore_username and message_keystore_password" do
expect(subject).to receive(:ask_for_string).with("Message Server Hostname or IP address", "my-host-name.example.com").and_return("my-host-name.example.com")
expect(subject).to receive(:ask_for_string).with("Message Keystore Username", message_keystore_username).and_return("admin")
expect(subject).to receive(:ask_for_password).with("Message Keystore Password").and_return("top_secret")
expect(subject).to receive(:just_ask).with(/Message Keystore Password/i, anything).twice.and_return("top_secret")

allow(subject).to receive(:say).at_least(5).times

expect(subject.send(:ask_questions)).to be_truthy
end

it "should re-prompt when an empty message_keystore_password is given" do
expect(subject).to receive(:ask_for_string).with("Message Server Hostname or IP address", "my-host-name.example.com").and_return("my-host-name.example.com")
expect(subject).to receive(:ask_for_string).with("Message Keystore Username", message_keystore_username).and_return("admin")
expect(subject).to receive(:just_ask).with(/Message Keystore Password/i, anything).and_return("")
expect(subject).to receive(:just_ask).with(/Message Keystore Password/i, anything).twice.and_return("top_secret")
expect(subject).to receive(:say).with("\nPassword can not be empty, please try again")

allow(subject).to receive(:say).at_least(5).times

Expand All @@ -57,7 +69,7 @@
it "should display Server Hostname and Keystore Username" do
allow(subject).to receive(:ask_for_string).with("Message Server Hostname or IP address", "my-host-name.example.com").and_return("my-host-name.example.com")
allow(subject).to receive(:ask_for_string).with("Message Keystore Username", message_keystore_username).and_return("admin")
allow(subject).to receive(:ask_for_password).with("Message Keystore Password").and_return("top_secret")
expect(subject).to receive(:just_ask).with(/Message Keystore Password/i, anything).twice.and_return("top_secret")

expect(subject).to receive(:say).with("\nMessage Server Parameters:\n\n")
expect(subject).to receive(:say).with("\nMessage Server Configuration:\n")
Expand All @@ -78,7 +90,7 @@
message_persistent_disk = LinuxAdmin::Disk.new(:path => "/tmp/disk")
expect(subject).to receive(:ask_for_string).with("Message Server Hostname or IP address", "my-host-name.example.com").and_return("my-host-name.example.com")
expect(subject).to receive(:ask_for_string).with("Message Keystore Username", message_keystore_username).and_return("admin")
expect(subject).to receive(:ask_for_password).with("Message Keystore Password").and_return("top_secret")
expect(subject).to receive(:just_ask).with(/Message Keystore Password/i, anything).twice.and_return("top_secret")
expect(subject).to receive(:ask_for_disk).with("Persistent disk").and_return(message_persistent_disk)

allow(subject).to receive(:say).at_least(5).times
Expand Down Expand Up @@ -366,7 +378,7 @@
expect(subject).to receive(:say).with(/Message Server Parameters/)
expect(subject).to receive(:ask_for_string).with("Message Server Hostname or IP address", anything).and_return("localhost")
expect(subject).to receive(:ask_for_string).with("Message Keystore Username", anything).and_return("admin")
expect(subject).to receive(:ask_for_password).with("Message Keystore Password").and_return("top_secret")
expect(subject).to receive(:just_ask).with(/Message Keystore Password/i, anything).twice.and_return("top_secret")
expect(subject).to receive(:ask_for_disk).with("Persistent disk").and_return("/tmp/disk")

subject.ask_for_parameters
Expand All @@ -377,7 +389,7 @@
expect(subject).to receive(:say).with(/Message Server Parameters/)
expect(subject).to receive(:ask_for_string).with("Message Server Hostname or IP address", anything).and_return("localhost.localadmin")
expect(subject).to receive(:ask_for_string).with("Message Keystore Username", anything).and_return("admin")
expect(subject).to receive(:ask_for_password).with("Message Keystore Password").and_return("top_secret")
expect(subject).to receive(:just_ask).with(/Message Keystore Password/i, anything).twice.and_return("top_secret")
expect(subject).to receive(:ask_for_disk).with("Persistent disk").and_return("/tmp/disk")

subject.ask_for_parameters
Expand Down