wingrunr21 / gitolite

A Ruby interface for the gitolite git backend system
https://github.com/wingrunr21/gitolite
MIT License
82 stars 37 forks source link

Add ssh_key from string #10

Closed Nerian closed 12 years ago

Nerian commented 12 years ago

Hi Stafford,

Awesome Gem! Thanks for doing this.

I having some trouble adding a ssh key.

public_key

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC4XIH/zioV68R2qJMrTmmnALWFsGzca/sJY8OuQCLeCZSKUtsrjfKAdL2UMxghSo6tiHYOoi7eai+EKpTwEow95tVpbRia8oeWOamzsLRP2ZhtQnuLIaknANBQl4ZszybQzLm6WV0/fsXaewPk5afSkSB5aZl8jpKQgDxILKc6sGLKjBNkOkr+iOC+sjoeDvWzTLZt6LOy1Sq4xO8qyDriqMD2GAhoGQl6kc0WtR7XfuifsWXB5iXOk+ZnTSSv9k7tEpXOf93tutv+9VNibItuNHVbprr5AWha58OcjF8r7baIT9NMXlnHHt6Pj+eRW2JGDX87pTvq3g0iCthe8U0R blabla
admin_repo = Gitolite::GitoliteAdmin.new("gitolite-admin")

key = Gitolite::SSHKey.new("", public_key, "", "mike")
admin_repo.add_key(key)

admin_repo.save_and_apply

That end up actually adding this key:

 ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC4XIH/zioV68R2qJMrTmmnALWFsGzca/sJY8OuQCLeCZSKUtsrjfKAdL2UMxghSo6tiHYOoi7eai+EKpTwEow95tVpbRia8oeWOamzsLRP2ZhtQnuLIaknANBQl4ZszybQzLm6WV0/fsXaewPk5afSkSB5aZl8jpKQgDxILKc6sGLKjBNkOkr+iOC+sjoeDvWzTLZt6LOy1Sq4xO8qyDriqMD2GAhoGQl6kc0WtR7XfuifsWXB5iXOk+ZnTSSv9k7tEpXOf93tutv+9VNibItuNHVbprr5AWha58OcjF8r7baIT9NMXlnHHt6Pj+eRW2JGDX87pTvq3g0iCthe8U0R blabla

Notice the whitespace at the start :)

So the key is not valid. I don't know if I am doing something wrong or there is a bug here. Please let me know what you think.

Also, what I would really like to be able to do is something like this:

admin_repo = Gitolite::GitoliteAdmin.new("gitolite-admin")

admin_repo.add_key_from_string(key, 'mike')

admin_repo.save_and_apply

Thanks!

Nerian commented 12 years ago

The problem is here: https://github.com/wingrunr21/gitolite/blob/master/lib/gitolite/ssh_key.rb#L44

That's where the whitespace comes from.

Nerian commented 12 years ago

In the mean time, this works:

    admin_repo = Gitolite::GitoliteAdmin.new("gitolite-admin")

    key = Gitolite::SSHKey.new("", public_key, "", "mike")

    def key.to_s
      @blob
    end

    admin_repo.add_key(key)

    admin_repo.save_and_apply
wingrunr21 commented 12 years ago

The problem is because you are constructing the key incorrectly. If you look at https://github.com/wingrunr21/gitolite/blob/master/lib/gitolite/ssh_key.rb#L14, you can see that the constructor would require the ssh key string to be split first. So you would need to do this:

parts = public_key.split
key = Gitolite::SSHKey.new(*parts, 'mike')

The add_key_from_string method looks like a good idea. I will also add a from_string method to SSHKey so you can just pass a string to get an SSHKey object.

Nerian commented 12 years ago

Thanks!

wingrunr21 commented 12 years ago

Implemented in 1887007dfc

key = Gitolite::SSHKey.from_string(some_string, 'owner')

There is not an add_key_from_string method. A GitoliteAdmin object is not responsible for that, it just wants a key. To add a key from a string do the above and add the key in the normal manner.