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

Read/write AuthorizedKeysFile #92

Open
mpounsett opened this issue Mar 10, 2022 · 4 comments
Open

Read/write AuthorizedKeysFile #92

mpounsett opened this issue Mar 10, 2022 · 4 comments

Comments

@mpounsett
Copy link

It would be helpful to also be able to write the contents of an AuthorizedKeysFile instance out to disk, in addition to parsing authorized_keys. This probably wouldn't involve too much refactoring of the class.

@ojarva
Copy link
Owner

ojarva commented Mar 12, 2022

What would be the usecase for this?

To me, it sounds like a better idea to write whatever data was used as a source when loading authorized_keys to this library. If this library would allow modifying key options, then writing would definitely make sense.

@mpounsett
Copy link
Author

The use case would be for writing code that manages the contents of authorized keys files. So, load an authorized_keys file into an instance of the class, replace one or more keys, then write the file out again.

The class wouldn't necessarily need to do file management itself, but it could have a to_str() method or something that could be redirected to a file.

@mpounsett
Copy link
Author

mpounsett commented Mar 12, 2022

I threw together an example of what I'm talking about at mpounsett/python-sshpubkeys@34b8d7c

Usage would look something like this (using StringIO in place of a real file):

import io
from sshpubkeys import AuthorizedKeysFile
from tests.valid_keys import keys

# slice 3 keys off the top of the test keys
authorized_keys = "\n".join([x[0] for x in keys][:3])
f = io.StringIO(authorized_keys)
key_file = AuthorizedKeysFile(f, strict=False)
# modify the key list here
f.seek(0)
f.write(str(key_file))
f.seek(0)
print(f.read())

If you really wanted to get fancy, AuthorizedKeysFile could be reimplemented as a context manager, allowing automatic writes to take place on close.

with open(keyfile, mode='w') as f:
   with AuthorizedKeysFile(f, strict=False) as key_list:
      key_list.append(SSHKey(...))

@tuxiqae
Copy link

tuxiqae commented Oct 27, 2022

Any news on that? It could be quite useful for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants