Skip to content

Commit

Permalink
Merge pull request #20771 from NickLaMuro/embedded_ansible_scm_cred_e…
Browse files Browse the repository at this point in the history
…nsure_newline

[EmbeddedAnsible] Ensure newline for :ssh_key_data

(cherry picked from commit 1895a65)
  • Loading branch information
kbrock authored and simaishi committed Nov 9, 2020
1 parent 0813e69 commit 4a50d87
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ class ManageIQ::Providers::EmbeddedAnsible::AutomationManager::ScmCredential < M
alias ssh_key_data auth_key
alias ssh_key_unlock auth_key_password

before_validation :ensure_newline_for_ssh_key

def self.display_name(number = 1)
n_('Credential (SCM)', 'Credentials (SCM)', number)
end
Expand All @@ -54,4 +56,10 @@ def self.params_to_attributes(params)

attrs
end

private

def ensure_newline_for_ssh_key
self.auth_key = "#{auth_key}\n" if auth_key.present? && auth_key[-1] != "\n"
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -299,61 +299,86 @@
end

context "ScmCredential" do
it_behaves_like 'an embedded_ansible credential' do
let(:credential_class) { embedded_ansible::ScmCredential }
let(:credential_class) { embedded_ansible::ScmCredential }
let(:expected_ssh_key) { "secret2\n" }

let(:params) do
{
:name => "Scm Credential",
:userid => "userid",
:password => "secret1",
:ssh_key_data => passed_in_ssh_key,
:ssh_key_unlock => "secret3"
}
end
let(:queue_create_params) do
{
:name => "Scm Credential",
:userid => "userid",
:password => ManageIQ::Password.encrypt("secret1"),
:ssh_key_data => ManageIQ::Password.encrypt(passed_in_ssh_key),
:ssh_key_unlock => ManageIQ::Password.encrypt("secret3")
}
end
let(:params_to_attributes) do
{
:name => "Scm Credential",
:userid => "userid",
:password => "secret1",
:auth_key => passed_in_ssh_key,
:auth_key_password => "secret3",
}
end
let(:expected_values) do
{
:name => "Scm Credential",
:userid => "userid",
:password => "secret1",
:ssh_key_data => expected_ssh_key,
:ssh_key_unlock => "secret3",
:password_encrypted => ManageIQ::Password.try_encrypt("secret1"),
:auth_key_encrypted => expected_ssh_key.present? ? ManageIQ::Password.try_encrypt(expected_ssh_key) : expected_ssh_key,
:auth_key_password_encrypted => ManageIQ::Password.try_encrypt("secret3")
}
end
let(:params_to_attrs) { [:auth_key, :auth_key_password] }
let(:update_params) do
{
:name => "Updated Credential",
:password => "supersecret"
}
end
let(:update_queue_params) do
{
:name => "Updated Credential",
:password => ManageIQ::Password.encrypt("supersecret")
}
end

let(:params) do
{
:name => "Scm Credential",
:userid => "userid",
:password => "secret1",
:ssh_key_data => "secret2",
:ssh_key_unlock => "secret3"
}
end
let(:queue_create_params) do
{
:name => "Scm Credential",
:userid => "userid",
:password => ManageIQ::Password.encrypt("secret1"),
:ssh_key_data => ManageIQ::Password.encrypt("secret2"),
:ssh_key_unlock => ManageIQ::Password.encrypt("secret3")
}
end
let(:params_to_attributes) do
{
:name => "Scm Credential",
:userid => "userid",
:password => "secret1",
:auth_key => "secret2",
:auth_key_password => "secret3",
}
end
let(:expected_values) do
{
:name => "Scm Credential",
:userid => "userid",
:password => "secret1",
:ssh_key_data => "secret2",
:ssh_key_unlock => "secret3",
:password_encrypted => ManageIQ::Password.try_encrypt("secret1"),
:auth_key_encrypted => ManageIQ::Password.try_encrypt("secret2"),
:auth_key_password_encrypted => ManageIQ::Password.try_encrypt("secret3")
}
end
let(:params_to_attrs) { [:auth_key, :auth_key_password] }
let(:update_params) do
{
:name => "Updated Credential",
:password => "supersecret"
}
end
let(:update_queue_params) do
{
:name => "Updated Credential",
:password => ManageIQ::Password.encrypt("supersecret")
}
end
context "with an SSH key that ends with a newline" do
let(:passed_in_ssh_key) { "secret2\n" }

it_behaves_like 'an embedded_ansible credential'
end

context "with an SSH key that does not end with a newline" do
let(:passed_in_ssh_key) { "secret2" }

it_behaves_like 'an embedded_ansible credential'
end

context "with an nil SSH key" do
let(:passed_in_ssh_key) { nil }
let(:expected_ssh_key) { nil }

it_behaves_like 'an embedded_ansible credential'
end

context "with a empty string SSH key" do
let(:passed_in_ssh_key) { "" }
let(:expected_ssh_key) { "" }

it_behaves_like 'an embedded_ansible credential'
end
end

Expand Down

0 comments on commit 4a50d87

Please sign in to comment.