Skip to content
This repository has been archived by the owner on Oct 30, 2018. It is now read-only.

"authorized_key" re-orders the keys and comments in the pre-existing authorized_keys file #4780

Closed
gerhard-tinned opened this issue Sep 10, 2016 · 12 comments · Fixed by #5339

Comments

@gerhard-tinned
Copy link

ISSUE TYPE
  • Bug Report
COMPONENT NAME

system/authorized_key

ANSIBLE VERSION
ansible 2.1.1.0
CONFIGURATION
OS / ENVIRONMENT

Managing from: LinuxMint
Managing: CentOS 6

SUMMARY

authorized_key is re-ordering the lines in a pre-existing authorized_keys file. This causes comments and the related lines to be re-ordered.

STEPS TO REPRODUCE

The managed host (B) should have a authorized_keys file containing multiple keys and comments. Example could look like this:

# The following is allice's key
ssh-rsa <key content-removed> ssh-key-comment-allice
# The following is bob's key
ssh-rsa <key content-removed> ssh-key-comment-bob
# The following is eve's key
ssh-rsa <key content-removed> ssh-key-comment-eve

After adding a key from another host (A) to the host (B) using the "authorized_key" task, below the pre-existing authorized_keys file as shown above is completely reordered.


---
- name: Generate ssh key on host (A)
  user: name=root generate_ssh_key=yes ssh_key_bits=4096 ssh_key_file=.ssh/id_rsa ssh_key_type=rsa

- name: Download generated ssh key from host (A)
  fetch: src=/root/.ssh/id_rsa.pub dest=/tmp/ansible/{{ ansible_hostname }}_id_rsa.pub 
         flat=yes validate_checksum=yes

- name: Upload/install the ssh-key from host (A) to host (B)
  authorized_key: user=root state=present 
      key="{{ lookup('file', '/tmp/ansible/{{ ansible_hostname }}_id_rsa.pub') }}"
  delegate_to: "{{ host_B }}"
EXPECTED RESULTS
# The following is allice's key
ssh-rsa <key content-removed> ssh-key-comment-allice
# The following is bob's key
ssh-rsa <key content-removed> ssh-key-comment-bob
# The following is eve's key
ssh-rsa <key content-removed> ssh-key-comment-eve
ssh-rsa <key content-removed> ssh-key-comment-joe

As shown in the example, it would be expected to see the added key (in the example "joe") at the end of the file keeping the pre-existing lines unchanged.

ACTUAL RESULTS

The result of the reordering might look like this containing the added key for "joe". The exact reason and ordering is not known at this point as of such the following example is just an example.

# The following is allice's key
ssh-rsa <key content-removed> ssh-key-comment-eve
# The following is eve's key
ssh-rsa <key content-removed> ssh-key-comment-joe
# The following is bob's key
ssh-rsa <key content-removed> ssh-key-comment-allice
ssh-rsa <key content-removed> ssh-key-comment-bob

@gerhard-tinned gerhard-tinned changed the title "authorized_key" rearranges the keys and comments in the pre-existing authorized_keys file "authorized_key" re-orders the keys and comments in the pre-existing authorized_keys file Sep 10, 2016
@ansibot
Copy link

ansibot commented Sep 12, 2016

@ansible ping, this issue is waiting for your response.
click here for bot help

@gerhard-tinned
Copy link
Author

bot_broke

What Information is missing? I provided all I know and also all the fields required according to the bot help.

@jctanner
Copy link
Contributor

The bot would mark the issue needs info if it were waiting on you. In this case the ball is in ansible's court and the bot is pinging the whole org to respond

Sent from my iPhone

On Sep 13, 2016, at 7:15 AM, Gerhard notifications@github.com wrote:

bot_broke

What Information is missing? I provided all I know and also all the fields required according to the bot help.


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or mute the thread.

@gerhard-tinned
Copy link
Author

Is this the wrong place for this ticket? Is this a dead project? What is the expected time until someone is at least responding? Other tickets wait since more the a month....

@ansibot
Copy link

ansibot commented Sep 28, 2016

@ansible, ping. This issue is still waiting on your response.
click here for bot help

1 similar comment
@ansibot
Copy link

ansibot commented Oct 14, 2016

@ansible, ping. This issue is still waiting on your response.
click here for bot help

@gerhard-tinned
Copy link
Author

I am waiting too !!!

@abadger
Copy link
Contributor

abadger commented Oct 20, 2016

As a workaround you should be able to use lineinfile instead of authorized_keys... The present authorized_keys code treats the file something like a database of records and so it doesn't preserve order. I'll take a look to see if there's an easy way to enhance it or not.

alikins added a commit to alikins/ansible-modules-core that referenced this issue Oct 20, 2016
Track the ordering of keys in the original file (rank)
and try to preserve it when writing out updates.

Fixes ansible#4780
@alikins
Copy link
Contributor

alikins commented Oct 20, 2016

As shown in the example, it would be expected to see the added key (in the example "joe") at the end of the file keeping the pre-existing lines unchanged.

In the current pr (#5339), it does that afaict.

However, it ignores comments in the 'key' argument passed to the module. Since the existing module can get a newline separated string with multiple key lines in it, the module just ignores comments lines in the new key info. That seems reasonable, but I am not sure what expectations are.

ie,
existing

# I like candy
ssh-rsa somekeydata somekeyalias
# It is a very pleasant temperature outside today.
ssh-rsa otherkeydata otherkeyalias

and new key

# I don't like candy
ssh-rsa otherkeydata otherkeyalias
# It is cold.
ssh-rsa somekeydata somekeyalias

# The end of file is nigh.

I have no idea what the resulting file should look like, aside from having the two keys in the new ordering in it.

So the module just ignores the incoming comments. I am okay with that.
(Need to add a test to verify).

alikins added a commit that referenced this issue Oct 21, 2016
* Make authorized_key preserve key order

Track the ordering of keys in the original file (rank)
and try to preserve it when writing out updates.

Fixes #4780
@gerhard-tinned
Copy link
Author

OK, I dont really get it. Is it fixed with this commit?

Can you tell me in which version or maybe even when it will be available?

@alikins
Copy link
Contributor

alikins commented Oct 24, 2016

It's merged to devel/ branch (the future 2.3) currently. It is not currently backported to stable-2.2/ (the future 2.2 release).

Is the buggy reordering in 2.1 breaking things? If it is generating invalid authorized_keys files?

karmab pushed a commit to karmab/ansible-modules-core that referenced this issue Oct 24, 2016
* Make authorized_key preserve key order

Track the ordering of keys in the original file (rank)
and try to preserve it when writing out updates.

Fixes ansible#4780
@gerhard-tinned
Copy link
Author

It is not technically generating invalid files. Logically it is creating invalid files as the comments do not appear where they should. As the comments are not technically used for anything from ssh daemon, they are, strictly technically speaking, working.

The fact that comments stay in exactly the line they where before but the keys are reordered, causes keys and comments not to match up anymore. This causes chaos which can only manually be solved - depending on the amount of keys - with a lot of manual work.

bdowling pushed a commit to bdowling/ansible-modules-core that referenced this issue Oct 28, 2016
* Make authorized_key preserve key order

Track the ordering of keys in the original file (rank)
and try to preserve it when writing out updates.

Fixes ansible#4780
alikins added a commit to ansible/ansible that referenced this issue Nov 29, 2016
alikins added a commit to alikins/ansible-modules-core that referenced this issue Dec 13, 2016
Track the ordering of keys in the original file (rank)
and try to preserve it when writing out updates.

Fixes ansible#4780
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants