Class: Travis::Deploy::SecureKey

Inherits:
Object
  • Object
show all
Defined in:
lib/travis/deploy/secure_key.rb

Defined Under Namespace

Classes: FetchKeyError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(slug, host = nil) ⇒ SecureKey

Returns a new instance of SecureKey.



14
15
16
17
# File 'lib/travis/deploy/secure_key.rb', line 14

def initialize(slug, host = nil)
  @slug = slug
  @host = host || "api.travis-ci.org"
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



12
13
14
# File 'lib/travis/deploy/secure_key.rb', line 12

def host
  @host
end

#slugObject (readonly)

Returns the value of attribute slug.



12
13
14
# File 'lib/travis/deploy/secure_key.rb', line 12

def slug
  @slug
end

Instance Method Details

#encrypt(secret) ⇒ Object



19
20
21
# File 'lib/travis/deploy/secure_key.rb', line 19

def encrypt(secret)
  encrypted = key.public_encrypt(secret)
end

#fetch_keyObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/travis/deploy/secure_key.rb', line 27

def fetch_key
  uri = URI.parse("https://#{host}/repos/#{slug}/key")

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true

  request = Net::HTTP::Get.new(uri.request_uri, 'Accept' => 'application/vnd.travis-ci.2+json')

  response = http.request(request)

  if response.code.to_i == 200
    body = MultiJson.decode(response.body)
    public_key = body['key']
    begin
      OpenSSL::PKey::RSA.new(public_key)
    rescue OpenSSL::PKey::RSAError
      # unsure why, but it seems that some keys are generated in a
      # wrong way
      public_key.gsub!('RSA PUBLIC KEY', 'PUBLIC KEY')
      OpenSSL::PKey::RSA.new(public_key)
    end
  else
    raise FetchKeyError
  end
end

#keyObject



23
24
25
# File 'lib/travis/deploy/secure_key.rb', line 23

def key
  @key ||= fetch_key
end