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

Dynamically adding ssh keys. #341

Closed
ghost opened this issue Jan 10, 2021 · 7 comments
Closed

Dynamically adding ssh keys. #341

ghost opened this issue Jan 10, 2021 · 7 comments

Comments

@ghost
Copy link

ghost commented Jan 10, 2021

How do I dynamically add SSH keys to my asyncSSH server?

await asyncssh.create_server(
    MainSSHServer, '', settings['settings']['port'],
    server_host_keys=['ext/keys/main/id_rsa'],
    authorized_client_keys=get_ssh_keys(),
    process_factory=handle_client
)

I know I can hard code them in here, but how do I add them after the server is up?

@ronf
Copy link
Owner

ronf commented Jan 10, 2021

Right now, there's no way to modify the set of options associated with an SSH listener once it has been created, and among other things those options include the set of server host keys to use. However, as a workaround, you should be able to close the existing listener and start a new one very quickly, and it wouldn't affect existing connections. There might be a small window where you're not accepting connections, but I wouldn't expect it to be very noticeable, particularly if you used the SSHServerConnectionOptions object to get everything ready to go before doing the switchover. You could even pass the previous "options" object to the new one, just updating the parts you want to change, so you don't have to repeat all those other arguments multiple times.

Allowing the listener's options to be updated dynamically should be possible, but it would require some rearranging of the way the options object works. Right now, that object is designed to basically be immutable once created. You can derive new sets of options from an existing set, but that makes a copy and doesn't change the original object.

@ghost
Copy link
Author

ghost commented Jan 10, 2021

Ohh okay thank you!

@ronf
Copy link
Owner

ronf commented Jan 10, 2021

I took a look at this today, and it turned out to be fairly straightforward to do -- see commit 5990144 in the "develop" branch. It adds an update() method on the object returned by listen/listen_reverse which takes the same keyword arguments as listen/listen_reverse does, allowing you to change any of these setting and have that change apply to future accepted connections. The only thing you can't change is settings related to setting up the listening sockets themselves. For that, you need to do a new listen call.

In your example, you'd do something like:

    listener = await asyncssh.create_server(
        MainSSHServer, '', settings['settings']['port'],
        server_host_keys=['ext/keys/main/id_rsa'],
        authorized_client_keys=get_ssh_keys(),
        process_factory=handle_client
    )

and then sometime later:

    listener.update(server_host_keys=['some_new_list_of_keys'])

You could add any other parameters present in SSHServerConnectionOptions as well with the same approach, setting whichever options you want to change.

@ghost
Copy link
Author

ghost commented Jan 15, 2021

oh thank you so much! that makes my work a lot easier thanks :)

@ghost
Copy link
Author

ghost commented Jan 15, 2021

Sorry for the late response :/

@ronf
Copy link
Owner

ronf commented Jan 16, 2021

No problem - let me know if you have any problems with it.

@ronf
Copy link
Owner

ronf commented May 1, 2021

This is now available in AsyncSSH 2.6.0.

@ronf ronf closed this as completed May 1, 2021
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

1 participant