From 1840f6cbb873168eadcad3c367b637783a8e10da Mon Sep 17 00:00:00 2001 From: Daniel Berger Date: Fri, 3 May 2019 14:42:33 -0400 Subject: [PATCH 01/11] Add default credentials if authentication not directly associated with conversion host in ansible_playbook method. --- app/models/conversion_host.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/conversion_host.rb b/app/models/conversion_host.rb index 774e085424a..cfcf47e7a1c 100644 --- a/app/models/conversion_host.rb +++ b/app/models/conversion_host.rb @@ -311,7 +311,7 @@ def ansible_playbook(playbook, extra_vars = {}, miq_task_id = nil, auth_type = n command = "ansible-playbook #{playbook} --inventory #{host}, --become --extra-vars=\"ansible_ssh_common_args='-o StrictHostKeyChecking=no'\"" - auth = authentication_type(auth_type) || authentications.first + auth = authentication_type(auth_type) || authentications.first || find_credentials command << " --user #{auth.userid}" case auth From cee75fb8ade460af618c1bd06c3dc0d5047c5131 Mon Sep 17 00:00:00 2001 From: Daniel Berger Date: Fri, 3 May 2019 15:10:48 -0400 Subject: [PATCH 02/11] Fixed error message handler in find_credentials. --- app/models/conversion_host.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/models/conversion_host.rb b/app/models/conversion_host.rb index cfcf47e7a1c..aef05f88e66 100644 --- a/app/models/conversion_host.rb +++ b/app/models/conversion_host.rb @@ -251,10 +251,10 @@ def find_credentials(msg = nil) resource.authentication_type('default') unless authentication - msg = "Credentials not found for conversion host #{name} or resource #{resource.name}" - msg << " #{msg}" if msg - _log.error(msg) - raise MiqException::Error, msg + error_msg = "Credentials not found for conversion host #{name} or resource #{resource.name}" + error_msg << " #{msg}" if msg + _log.error(error_msg) + raise MiqException::Error, error_msg end authentication From 6b22c6069626d1ab577316140b6280fbd3102634 Mon Sep 17 00:00:00 2001 From: Daniel Berger Date: Mon, 6 May 2019 08:23:48 -0400 Subject: [PATCH 03/11] Modify find_credentials so that it accepts an auth_type, and update the ansible_playbook method to use it. --- app/models/conversion_host.rb | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/app/models/conversion_host.rb b/app/models/conversion_host.rb index aef05f88e66..c70942b7291 100644 --- a/app/models/conversion_host.rb +++ b/app/models/conversion_host.rb @@ -242,17 +242,18 @@ def resource_supports_conversion_host # Find the credentials for the associated resource. By default it will # look for a v2v auth type. If that is not found, it will look for the - # authentication associated with the resource using ssh_keypair or default, - # in that order, as the authtype. + # first associated authentication. If one still isn't found, then it will + # look for the authentication associated with the resource using the + # 'ssh_keypair' auth type, and finally 'default'. # - def find_credentials(msg = nil) - authentication = authentication_type('v2v') || + def find_credentials(auth_type = 'v2v') + authentication = authentication_type(auth_type) || + authentications.first || resource.authentication_type('ssh_keypair') || resource.authentication_type('default') unless authentication error_msg = "Credentials not found for conversion host #{name} or resource #{resource.name}" - error_msg << " #{msg}" if msg _log.error(error_msg) raise MiqException::Error, error_msg end @@ -311,7 +312,7 @@ def ansible_playbook(playbook, extra_vars = {}, miq_task_id = nil, auth_type = n command = "ansible-playbook #{playbook} --inventory #{host}, --become --extra-vars=\"ansible_ssh_common_args='-o StrictHostKeyChecking=no'\"" - auth = authentication_type(auth_type) || authentications.first || find_credentials + auth = find_credentials command << " --user #{auth.userid}" case auth From f4607f233b12f85b2ffd5b1ac6114d324727a8bb Mon Sep 17 00:00:00 2001 From: Daniel Berger Date: Mon, 6 May 2019 09:16:47 -0400 Subject: [PATCH 04/11] Explicitly pass auth_type argument to find_credentials. Fix find credentials for resources where the authentication is associated with the EMS. --- app/models/conversion_host.rb | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/app/models/conversion_host.rb b/app/models/conversion_host.rb index c70942b7291..c245f70dfd8 100644 --- a/app/models/conversion_host.rb +++ b/app/models/conversion_host.rb @@ -241,16 +241,22 @@ def resource_supports_conversion_host end # Find the credentials for the associated resource. By default it will - # look for a v2v auth type. If that is not found, it will look for the - # first associated authentication. If one still isn't found, then it will - # look for the authentication associated with the resource using the - # 'ssh_keypair' auth type, and finally 'default'. + # look for a v2v auth type if no argument is passed in. If that is not found, + # it will look for the first associated authentication. If one still isn't + # found, then it will look for the authentication associated with the resource + # using the 'ssh_keypair' auth type, and finally 'default'. # - def find_credentials(auth_type = 'v2v') - authentication = authentication_type(auth_type) || - authentications.first || - resource.authentication_type('ssh_keypair') || - resource.authentication_type('default') + def find_credentials(auth_type = nil) + authentication = authentication_type(auth_type) || authentication_type('v2v') || authentications.first || + + if authentication.blank? + if resource.respond_to?(:authentication_type) + authentication ||= resource.authentication_type('ssh_keypair') || resource.authentication_type('default') + else + ems = resource.ext_management_system + authentication ||= ems.authentication_type('ssh_keypair') || ems.authentication_type('default') + end + end unless authentication error_msg = "Credentials not found for conversion host #{name} or resource #{resource.name}" @@ -312,7 +318,7 @@ def ansible_playbook(playbook, extra_vars = {}, miq_task_id = nil, auth_type = n command = "ansible-playbook #{playbook} --inventory #{host}, --become --extra-vars=\"ansible_ssh_common_args='-o StrictHostKeyChecking=no'\"" - auth = find_credentials + auth = find_credentials(auth_type) command << " --user #{auth.userid}" case auth From aa3cde88e76026f98e896efd0f1b4d6160699979 Mon Sep 17 00:00:00 2001 From: Daniel Berger Date: Mon, 6 May 2019 13:09:55 -0400 Subject: [PATCH 05/11] Fix stray pipe char. --- app/models/conversion_host.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/conversion_host.rb b/app/models/conversion_host.rb index c245f70dfd8..bc862ccce0d 100644 --- a/app/models/conversion_host.rb +++ b/app/models/conversion_host.rb @@ -247,7 +247,7 @@ def resource_supports_conversion_host # using the 'ssh_keypair' auth type, and finally 'default'. # def find_credentials(auth_type = nil) - authentication = authentication_type(auth_type) || authentication_type('v2v') || authentications.first || + authentication = authentication_type(auth_type) || authentication_type('v2v') || authentications.first if authentication.blank? if resource.respond_to?(:authentication_type) From 24b2b4563ba6a5c36661c567ef598735d2b10352 Mon Sep 17 00:00:00 2001 From: Daniel Berger Date: Tue, 7 May 2019 10:54:39 -0400 Subject: [PATCH 06/11] Remove useless || --- app/models/conversion_host.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/conversion_host.rb b/app/models/conversion_host.rb index bc862ccce0d..cbfd253aa35 100644 --- a/app/models/conversion_host.rb +++ b/app/models/conversion_host.rb @@ -251,10 +251,10 @@ def find_credentials(auth_type = nil) if authentication.blank? if resource.respond_to?(:authentication_type) - authentication ||= resource.authentication_type('ssh_keypair') || resource.authentication_type('default') + authentication = resource.authentication_type('ssh_keypair') || resource.authentication_type('default') else ems = resource.ext_management_system - authentication ||= ems.authentication_type('ssh_keypair') || ems.authentication_type('default') + authentication = ems.authentication_type('ssh_keypair') || ems.authentication_type('default') end end From b7aa6518535b42dbd81dc916027f966d32c8012c Mon Sep 17 00:00:00 2001 From: Daniel Berger Date: Tue, 7 May 2019 12:52:02 -0400 Subject: [PATCH 07/11] Simplify the find_credentials method a bit. --- app/models/conversion_host.rb | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/app/models/conversion_host.rb b/app/models/conversion_host.rb index cbfd253aa35..02840a62bbb 100644 --- a/app/models/conversion_host.rb +++ b/app/models/conversion_host.rb @@ -250,12 +250,8 @@ def find_credentials(auth_type = nil) authentication = authentication_type(auth_type) || authentication_type('v2v') || authentications.first if authentication.blank? - if resource.respond_to?(:authentication_type) - authentication = resource.authentication_type('ssh_keypair') || resource.authentication_type('default') - else - ems = resource.ext_management_system - authentication = ems.authentication_type('ssh_keypair') || ems.authentication_type('default') - end + res = resource.respond_to?(:authentication_type) ? resource : resource.ext_management_system + authentication = res.authentication_type('ssh_keypair') || res.authentication_type('default') end unless authentication From 4e89c389310a59c996c68d0e95526c0d318ef3d3 Mon Sep 17 00:00:00 2001 From: Daniel Berger Date: Tue, 7 May 2019 12:55:16 -0400 Subject: [PATCH 08/11] Default to v2v auth_type for find_credentials. --- app/models/conversion_host.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/conversion_host.rb b/app/models/conversion_host.rb index 02840a62bbb..0b01a85ab62 100644 --- a/app/models/conversion_host.rb +++ b/app/models/conversion_host.rb @@ -246,8 +246,8 @@ def resource_supports_conversion_host # found, then it will look for the authentication associated with the resource # using the 'ssh_keypair' auth type, and finally 'default'. # - def find_credentials(auth_type = nil) - authentication = authentication_type(auth_type) || authentication_type('v2v') || authentications.first + def find_credentials(auth_type = 'v2v') + authentication = authentication_type(auth_type) || authentications.first if authentication.blank? res = resource.respond_to?(:authentication_type) ? resource : resource.ext_management_system From 3ba0ca868ed7ad05760f39d16ce227caa7dfd54a Mon Sep 17 00:00:00 2001 From: Daniel Berger Date: Tue, 7 May 2019 13:50:44 -0400 Subject: [PATCH 09/11] Check for explicit authentication types instead of just defaulting to first one. --- app/models/conversion_host.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/models/conversion_host.rb b/app/models/conversion_host.rb index 0b01a85ab62..1988b4af046 100644 --- a/app/models/conversion_host.rb +++ b/app/models/conversion_host.rb @@ -247,7 +247,9 @@ def resource_supports_conversion_host # using the 'ssh_keypair' auth type, and finally 'default'. # def find_credentials(auth_type = 'v2v') - authentication = authentication_type(auth_type) || authentications.first + authentication = authentication_type(auth_type) || + authentication_type('ssh_keypair') || + authentication_type('default') if authentication.blank? res = resource.respond_to?(:authentication_type) ? resource : resource.ext_management_system From d2cc525ea2a345acc77de5a7c5def4f296e9f2a1 Mon Sep 17 00:00:00 2001 From: Daniel Berger Date: Tue, 7 May 2019 13:55:05 -0400 Subject: [PATCH 10/11] Don't default on first auth type check. --- app/models/conversion_host.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/app/models/conversion_host.rb b/app/models/conversion_host.rb index 1988b4af046..2c8b6250978 100644 --- a/app/models/conversion_host.rb +++ b/app/models/conversion_host.rb @@ -247,9 +247,7 @@ def resource_supports_conversion_host # using the 'ssh_keypair' auth type, and finally 'default'. # def find_credentials(auth_type = 'v2v') - authentication = authentication_type(auth_type) || - authentication_type('ssh_keypair') || - authentication_type('default') + authentication = authentication_type(auth_type) if authentication.blank? res = resource.respond_to?(:authentication_type) ? resource : resource.ext_management_system From 802a13b818a9e78ffcea9f696c91d1f316db6f84 Mon Sep 17 00:00:00 2001 From: Daniel Berger Date: Tue, 7 May 2019 13:56:46 -0400 Subject: [PATCH 11/11] Update comments for find_credentials. --- app/models/conversion_host.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/models/conversion_host.rb b/app/models/conversion_host.rb index 2c8b6250978..4712310939d 100644 --- a/app/models/conversion_host.rb +++ b/app/models/conversion_host.rb @@ -241,10 +241,10 @@ def resource_supports_conversion_host end # Find the credentials for the associated resource. By default it will - # look for a v2v auth type if no argument is passed in. If that is not found, - # it will look for the first associated authentication. If one still isn't - # found, then it will look for the authentication associated with the resource - # using the 'ssh_keypair' auth type, and finally 'default'. + # look for a v2v auth type if no argument is passed in. + # + # If one isn't found, then it will look for the authentication associated + # with the resource using the 'ssh_keypair' auth type, and finally 'default'. # def find_credentials(auth_type = 'v2v') authentication = authentication_type(auth_type)