48 lines
1.4 KiB
Ruby
Raw Normal View History

2016-09-24 13:52:43 +02:00
module Hbc
class DSL
class Gpg
KEY_PARAMETERS = Set.new [
2016-10-14 20:33:16 +02:00
:key_id,
:key_url,
]
2016-08-18 22:11:42 +03:00
2016-09-24 13:52:43 +02:00
VALID_PARAMETERS = Set.new []
VALID_PARAMETERS.merge KEY_PARAMETERS
2016-08-18 22:11:42 +03:00
2016-09-24 13:52:43 +02:00
attr_accessor(*VALID_PARAMETERS)
attr_accessor :signature
2016-08-18 22:11:42 +03:00
2016-09-24 13:52:43 +02:00
def initialize(signature, parameters = {})
@parameters = parameters
@signature = URI(signature) unless signature == :embedded
2016-09-24 13:52:43 +02:00
parameters.each do |hkey, hvalue|
raise "invalid 'gpg' parameter: '#{hkey.inspect}'" unless VALID_PARAMETERS.include?(hkey)
writer_method = "#{hkey}=".to_sym
2017-10-03 08:29:20 +02:00
hvalue = URI(hvalue) if hkey == :key_url
2016-09-24 13:52:43 +02:00
valid_id?(hvalue) if hkey == :key_id
send(writer_method, hvalue)
end
return if KEY_PARAMETERS.intersection(parameters.keys).length == 1
raise "'gpg' stanza must include exactly one of: '#{KEY_PARAMETERS.to_a}'"
end
2016-08-18 22:11:42 +03:00
2016-09-24 13:52:43 +02:00
def valid_id?(id)
legal_lengths = Set.new [8, 16, 40]
is_valid = id.is_a?(String) && legal_lengths.include?(id.length) && id[/^[0-9a-f]+$/i]
2016-09-24 13:52:43 +02:00
raise "invalid ':key_id' value: '#{id.inspect}'" unless is_valid
2016-08-18 22:11:42 +03:00
2016-09-24 13:52:43 +02:00
is_valid
end
2016-08-18 22:11:42 +03:00
2016-09-24 13:52:43 +02:00
def to_yaml
2017-10-03 08:29:20 +02:00
# bug, :key_url value is not represented as an instance of URI
2016-09-24 13:52:43 +02:00
[@signature, @parameters].to_yaml
end
2016-08-18 22:11:42 +03:00
2016-09-24 13:52:43 +02:00
def to_s
@signature.to_s
end
end
2016-08-18 22:11:42 +03:00
end
end