Skip to content

Commit

Permalink
make pubkey print out key in ssh encoding, add --pem flag for old for…
Browse files Browse the repository at this point in the history
…mat, fixes #50
  • Loading branch information
rkh committed Jul 19, 2013
1 parent 306d53f commit ec4b76d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -424,11 +424,17 @@ Outputs the public key for a repository.
$ travis pubkey
Public key for travis-ci/travis:

ssh-rsa ...
$ travis pubkey -r rails/rails > rails.key

The `--pem` flag will print out the key PEM encoded:

$ travis pubkey --pem
Public key for travis-ci/travis:

-----BEGIN PUBLIC KEY-----
...
-----END PUBLIC KEY-----

$ travis pubkey -r rails/rails > rails.key

#### `restart`

Expand Down
5 changes: 4 additions & 1 deletion lib/travis/cli/pubkey.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
module Travis
module CLI
class Pubkey < RepoCommand
on('-p', '--[no-]pem', 'encode in format used by pem')

def run
say key, "Public key for #{color(repository.slug, :info)}:\n\n%s", :bold
end

private

def key
repository.public_key.to_s
key = repository.public_key
pem? ? key.to_s : key.to_ssh
end
end
end
Expand Down
16 changes: 16 additions & 0 deletions lib/travis/client/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ def encrypt(value)
Base64.encode64(encrypted).gsub(/\s+/, "")
end

def to_ssh
['ssh-rsa ', "\0\0\0\assh-rsa#{sized_bytes(to_rsa.e)}#{sized_bytes(to_rsa.n)}"].pack('a*m').gsub("\n", '')
end

def to_rsa
@to_rsa ||= OpenSSL::PKey::RSA.new(to_s)
rescue OpenSSL::PKey::RSAError
Expand All @@ -27,6 +31,18 @@ def to_rsa
def ==(other)
other.to_s == self
end

private

def sized_bytes(value)
bytes = to_byte_array(value.to_i)
[bytes.size, *bytes].pack('NC*')
end

def to_byte_array(num, *significant)
return significant if num.between?(-1, 0) and significant[0][7] == num[7]
to_byte_array(*num.divmod(256)) + significant
end
end

include States
Expand Down

0 comments on commit ec4b76d

Please sign in to comment.