Skip to content

Conversation

@weizhouapache
Copy link
Member

Description

This PR provides the option to add kvm hosts with empty or wrong password.
To support this, the cloudstack ssh public key needs to be added in the ~/.ssh/authorized_keys on host.

Thanks @rhtyd for the idea !

Types of changes

  • Breaking change (fix or feature that would cause existing functionality to change)
  • New feature (non-breaking change which adds functionality)
  • Bug fix (non-breaking change which fixes an issue)
  • Enhancement (improves an existing feature and functionality)
  • Cleanup (Code refactoring and cleanup, that may add test cases)

Feature/Enhancement Scale or Bug Severity

Feature/Enhancement Scale

  • Major
  • Minor

Screenshots (if appropriate):

  1. get cloudstack public key from management server (/var/lib/cloudstack/management/.ssh/id_rsa.pub)

image

  1. add the key to ~/.ssh/authorized_keys on kvm hosts

image

  1. add kvm host with empty password

How Has This Been Tested?

  1. add host, with correct password, works as expected.

  2. put host to maitenance, stop cloudstack-agent, cancel maintenance, remove the host. all work.

  3. add host, with empty password (ssh public key is added to ~/.ssh/authorized_keys), works as expected.

  4. put host to maitenance, stop cloudstack-agent, cancel maintenance. all work as expected.

@weizhouapache weizhouapache changed the title kvm: add hosts using cloustack ssh private key (ccc2021 hachathon ) kvm: add hosts using cloustack ssh private key Nov 12, 2021
@weizhouapache
Copy link
Member Author

@blueorangutan package

@blueorangutan
Copy link

@weizhouapache a Jenkins job has been kicked to build packages. I'll keep you posted as I make progress.

final String privateKey = _configDao.getValue("ssh.privatekey");
if (!SSHCmdHelper.acquireAuthorizedConnectionWithPublicKey(sshConnection, username, privateKey)) {
s_logger.error("Failed to authenticate with ssh key");
if (!sshConnection.authenticateWithPassword(username, password)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit - maybe do a check if password was even passed (empty?)

@yadvr
Copy link
Member

yadvr commented Nov 12, 2021

Left some comments but nice hack @weizhouapache !

@yadvr
Copy link
Member

yadvr commented Nov 12, 2021

(of course your changes are closer to what maybe reviewed/tested merged so I'm being very thorough otherwise LGTM!)

Copy link
Member

@yadvr yadvr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

<div class="form__label"><span class="required">* </span>{{ $t('label.password') }}</div>
<span class="required required-label">{{ $t('label.required') }}</span>
<div class="form__item" v-if="selectedClusterHyperVisorType !== 'VMware'">
<div class="form__label">{{ $t('label.password') }}</div>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe be have it not required just for kvm? (for ex. required for xenserver?)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ping @weizhouapache when you've time - why not put explicit checks for example a radio/button or toggle to switch between password or ssh-key auth just for KVM (however for XenServer too this feature can be done).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rhtyd
good idea. will make the UI change.
let's support kvm at first, then xenserver (this is not needed for vmware).

if (StringUtils.isNotBlank(privateKey)) {
File privateKeyFile = null;
try {
privateKeyFile = File.createTempFile("cloudstack-host-", null);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@weizhouapache
This temp. hack may not be required as mgmt server saves the ssh private/pub keys at: /var/lib/cloudstack/management/.ssh/id_rsa on the mgmt server, why not just read/use that?

In fact if this is saved/assumed, then we don't even need to read the private key from db and pass it here. Thoughts?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: needs checking on all distros (this path), on centos7, centos/rocky8, ubuntu and suse. cc @davidjumani if you know/remember distro specific paths.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@weizhouapache This temp. hack may not be required as mgmt server saves the ssh private/pub keys at: /var/lib/cloudstack/management/.ssh/id_rsa on the mgmt server, why not just read/use that?

In fact if this is saved/assumed, then we don't even need to read the private key from db and pass it here. Thoughts?

@rhtyd
I have considered it. but at the end I decided to use temp file so that the process will not rely on any file on management server.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case let's fix the mod bits of the file to be just read-only (0400)

@yadvr yadvr changed the base branch from main to 4.16 November 12, 2021 10:51
@blueorangutan
Copy link

Packaging result: ✔️ el7 ✔️ el8 ✔️ debian ✔️ suse15. SL-JID 1699

Copy link
Contributor

@DaanHoogland DaanHoogland left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good, just one attack vector (of questionable vulnerability admitted). We might need to revisit that part. Also documentation on how to use it/prepare hosts will be needed.
Good Job @weizhouapache

return false;
}
try {
if (!sshConnection.authenticateWithPublicKey(username, privateKeyFile, null)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@weizhouapache can we pass the privatekey content to this method (or similar available methods)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rhtyd yes, I will use char[], testing it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rhtyd @DaanHoogland
updated this PR to use char[] in server authentication. thanks for your review !

@weizhouapache
Copy link
Member Author

@blueorangutan package

@weizhouapache
Copy link
Member Author

@blueorangutan test keepEnv

@blueorangutan
Copy link

@weizhouapache a Trillian-Jenkins test job (centos7 mgmt + kvm-centos7) has been kicked to run smoke tests

@weizhouapache weizhouapache changed the title (ccc2021 hachathon ) kvm: add hosts using cloustack ssh private key (ccc2021 hackathon ) kvm: add hosts using cloustack ssh private key Nov 12, 2021
@weizhouapache weizhouapache changed the title (ccc2021 hackathon ) kvm: add hosts using cloustack ssh private key (ccc2021 hackathon ) kvm: add hosts using cloudstack ssh private key Nov 12, 2021
@yadvr yadvr added this to the 4.16.1.0 milestone Nov 15, 2021
@DaanHoogland
Copy link
Contributor

@weizhouapache this is not in draft, is it ready for merge? (just making sure)

@weizhouapache
Copy link
Member Author

@weizhouapache this is not in draft, is it ready for merge? (just making sure)

@DaanHoogland
it is ready for review (not merge).

@weizhouapache
Copy link
Member Author

weizhouapache commented Nov 24, 2021

@DaanHoogland @rhtyd
made a UI change as per Rohit's comment.

@weizhouapache weizhouapache force-pushed the 4.16-add-kvm-host-ssh-privatekey branch from 382d6a8 to 045fa5a Compare November 24, 2021 10:24
s_logger.debug("Failed to authenticate");
throw new DiscoveredWithErrorException("Authentication error");

final String privateKey = _configDao.getValue("ssh.privatekey");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@weizhouapache I think, it is better to try with ssh key or password (based on the authentication mode), not with other when one of it fails. You can determine authentication mode, either from the user input or may be from password/privatekey config.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sureshanaparti
isn't it better to support both ? hosts are still manageable if one of the authentication fails.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if one authentication fails, try other may not be good.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if one authentication fails, try other may not be good.

@sureshanaparti ok. let's ask other guys.

@rhtyd @DaanHoogland what's your opinion ?

@sureshanaparti
Copy link
Contributor

@weizhouapache Does multiple kvm hosts in a cluster supports different authentication mechanism (few with passwords, and others with ssh key)? If so, is this info maintained in management server?

@yadvr
Copy link
Member

yadvr commented Dec 3, 2021

LGTM, @weizhouapache is this ready for testing ? cc @borisstoyanov @vladimirpetrov
@blueorangutan package

@blueorangutan
Copy link

@rhtyd a Jenkins job has been kicked to build packages. I'll keep you posted as I make progress.

@weizhouapache
Copy link
Member Author

LGTM, @weizhouapache is this ready for testing ? cc @borisstoyanov @vladimirpetrov @blueorangutan package

@rhtyd yes, it is ready for review and testing.

@blueorangutan
Copy link

@weizhouapache a Jenkins job has been kicked to build packages. I'll keep you posted as I make progress.

@weizhouapache
Copy link
Member Author

@weizhouapache Does multiple kvm hosts in a cluster supports different authentication mechanism (few with passwords, and others with ssh key)? If so, is this info maintained in management server?

@sureshanaparti yes, if host_details table has record with name=password for the host, the host supports password authentication (as well as ssh key authentication).
if not, the host supports ssh key authentication only.

I think the main issue for now is, password authentication does not work sometimes, for example by ssh protocol, incorrect ssh settings, wrong password, password is changed. it should not be a problem/risk to ssh from mgmt server to hosts by both options, as they all use internal IPs.

@blueorangutan
Copy link

Packaging result: ✔️ el7 ✔️ el8 ✔️ debian ✔️ suse15. SL-JID 1828

@weizhouapache
Copy link
Member Author

@blueorangutan test

@blueorangutan
Copy link

@weizhouapache a Trillian-Jenkins test job (centos7 mgmt + kvm-centos7) has been kicked to run smoke tests

@blueorangutan
Copy link

Trillian Build Failed (tid-2615)

@blueorangutan
Copy link

Packaging result: ✔️ el7 ✔️ el8 ✔️ debian ✔️ suse15. SL-JID 1829

@weizhouapache
Copy link
Member Author

@blueorangutan test

1 similar comment
@sureshanaparti
Copy link
Contributor

@blueorangutan test

@blueorangutan
Copy link

@sureshanaparti a Trillian-Jenkins test job (centos7 mgmt + kvm-centos7) has been kicked to run smoke tests

@blueorangutan
Copy link

Trillian test result (tid-2616)
Environment: kvm-centos7 (x2), Advanced Networking with Mgmt server 7
Total time taken: 30475 seconds
Marvin logs: https://github.com/blueorangutan/acs-prs/releases/download/trillian/pr5684-t2616-kvm-centos7.zip
Smoke tests completed. 91 look OK, 0 have errors
Only failed tests results shown below:

Test Result Time (s) Test File

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

No open projects
Status: Done

Development

Successfully merging this pull request may close these issues.

5 participants