Bug 1642008

Summary: [cloud-init][RHEL7.5]Multiple files for AuthorizedKeysFile are not correctly handled by cloud-init
Product: Red Hat Enterprise Linux 7 Reporter: Oliver Falk <ofalk>
Component: cloud-initAssignee: Eduardo Otubo <eterrell>
Status: CLOSED CURRENTRELEASE QA Contact: Huijuan Zhao <huzhao>
Severity: high Docs Contact:
Priority: urgent    
Version: 7.5CC: eterrell, huzhao, jgreguske, ldu, leiwang, linl, lrotenbe, mkalinin, peter.vreman, pkomarov, ribarry, sacpatil, wshi, xiliang, yacao, yujiang, yuxisun
Target Milestone: rc   
Target Release: ---   
Hardware: All   
OS: Linux   
Whiteboard:
Fixed In Version: Doc Type: If docs needed, set a value
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2020-03-18 12:43:08 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:
Bug Depends On:    
Bug Blocks: 1122832, 1797441    

Description Oliver Falk 2018-10-23 11:30:39 UTC
Description of problem:
cloud-init (precisely 'ssh_util.py') doesn't correctly handle the case when multiple files are given for 'AuthorizedKeysFile' option in sshd_config.


How reproducible: Always


Steps to Reproduce:
1. Create a custom AMI with AuthorizedKeysFile in /etc/ssh/sshd_config set to something like this:

    AuthorizedKeysFile .ssh/authorized_keys /etc/ssh/userkeys/%u

The important point here is, that there are at least 2 files listed, separated by space, as documented in the man page (see also Additional info).

2. Start it and let cloud-init populate the authorized_keys.
3. Check ~ec2-user/.ssh/ - it looks like this:
ec2-user@ip-xx-xx-xx-xx:~$ sudo find /home/ec2-user/.ssh -ls
25165888    0 drwx------   3 ec2-user ec2-user       53 Oct 22 08:33 /home/ec2-user/.ssh
25165889    4 -rw-------   1 ec2-user ec2-user      393 Jun 28 13:09 /home/ec2-user/.ssh/authorized_keys
    2340    0 drwxr-xr-x   3 root     root           17 Oct 22 08:33 /home/ec2-user/.ssh/authorized_keys\
8390840     0 drwxr-xr-x   3 root     root           17 Oct 22 08:33 /home/ec2-user/.ssh/authorized_keys\ /etc
16799997    0 drwxr-xr-x   3 root     root           22 Oct 22 08:33 /home/ec2-user/.ssh/authorized_keys\ /etc/ssh
25168288    0 drwx------   2 root     root           22 Oct 22 08:33 /home/ec2-user/.ssh/authorized_keys\ /etc/ssh/userkeys
25168289    4 -rw-------   1 ec2-user ec2-user      393 Oct 22 08:33 /home/ec2-user/.ssh/authorized_keys\ /etc/ssh/userkeys/ec2-user


Actual results:
Login with new additional key pair not possible, since not correctly set up.


Expected results:
~ec2-user/.ssh/authorized_keys is modified and no additional directory in ~ec2-user/.ssh is created


Additional info:
Having listed multiple files in AuthorizedKeysFile is allowed according to the sshd_config man page (man 5 sshd_config):

     AuthorizedKeysFile
             Specifies the file that contains the public keys used for user
             authentication.  The format is described in the AUTHORIZED_KEYS
             FILE FORMAT section of sshd(8).  Arguments to AuthorizedKeysFile
             accept the tokens described in the TOKENS section.  After expan‐
             sion, AuthorizedKeysFile is taken to be an absolute path or one
             relative to the user's home directory.  Multiple files may be
             listed, separated by whitespace.  Alternately this option may be
             set to none to skip checking for user keys in files.  The default
             is ".ssh/authorized_keys .ssh/authorized_keys2".

Comment 6 Yuhui Jiang 2019-04-02 10:06:58 UTC
Reproduced this issue on Azure as well.

Comment 19 Eduardo Otubo 2019-11-21 18:51:10 UTC
Pull request send to upstream:
https://github.com/canonical/cloud-init/pull/58

Waiting for it to be reviewed and merged.

Comment 20 Eduardo Otubo 2019-11-29 13:08:21 UTC
(In reply to Eduardo Otubo from comment #19)
> Pull request send to upstream:
> https://github.com/canonical/cloud-init/pull/58
> 
> Waiting for it to be reviewed and merged.

Couple of more fixes done, still need review and merge:
https://github.com/canonical/cloud-init/pull/60