-
Notifications
You must be signed in to change notification settings - Fork 161
Expand file tree
/
Copy pathkey_pair.rb
More file actions
51 lines (42 loc) · 1.26 KB
/
key_pair.rb
File metadata and controls
51 lines (42 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
module Travis::API::V3
class Models::KeyPair < Travis::Settings::Model
include Models::JsonSync, Models::Fingerprint
attribute :description, String
attribute :value, Travis::Settings::EncryptedValue
attribute :repository_id, Integer
validates_each :value do |record, attr, value|
record.errors.add(attr, :missing_attr) if value.blank?
record.errors.add(attr, :invalid_pem) unless record.valid_pem?
end
def fingerprint_source
value.decrypt
end
def public_key
return unless value.decrypt
OpenSSL::PKey::RSA.new(value.decrypt).public_key.to_s
rescue OpenSSL::PKey::RSAError
nil
end
def to_h
{ 'ssh_key' => attributes.slice(:description, :value, :repository_id).stringify_keys }
end
def valid_pem?
value.decrypt && OpenSSL::PKey::RSA.new(value.decrypt)
true
rescue OpenSSL::PKey::RSAError
false
end
def update(attributes = {})
super
return false unless valid?
self.tap { |kp| kp.sync! }
end
def delete(repository)
repository.settings = repository.settings.tap { |setting| setting.delete("ssh_key")}.to_json
repository.save!
end
def repository
V3::Models::Repository.find(repository_id)
end
end
end