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

[EmbeddedAnsible] Ensure newline for :ssh_key_data #20771

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
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,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 @@ -66,4 +68,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