Class: Dnsruby::KeyCache

Inherits:
Object
  • Object
show all
Defined in:
lib/dnsruby/key_cache.rb

Overview

:nodoc: all

Instance Method Summary collapse

Constructor Details

#initialize(keys = nil) ⇒ KeyCache

Cache includes expiration time for keys

Cache removes expired records


21
22
23
24
25
# File 'lib/dnsruby/key_cache.rb', line 21

def initialize(keys = nil)
  #  Store key tag against [expiry, key]
  @keys = {}
  add(keys)
end

Instance Method Details

#add(k) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/dnsruby/key_cache.rb', line 29

def add(k)
  if (k == nil)
    return false
  elsif (k.instance_of?RRSet)
    add_rrset(k)
  elsif (k.kind_of?KeyCache)
    kaes = k.keys_and_expirations
    kaes.keys.each { |keykey|
      #             priv_add_key(keykey, kaes[keykey])
      priv_add_key(keykey[1], keykey[0])
    }
  else
    raise ArgumentError.new("Expected an RRSet or KeyCache! Got #{k.class}")
  end
  return true
end

#add_key_with_expiration(k, expiration) ⇒ Object



26
27
28
# File 'lib/dnsruby/key_cache.rb', line 26

def add_key_with_expiration(k, expiration)
  priv_add_key(k, expiration)
end

#add_rrset(k) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/dnsruby/key_cache.rb', line 46

def add_rrset(k)
  #  Get expiration from the RRSIG
  #  There can be several RRSIGs here, one for each key which has signed the RRSet
  #  We want to choose the one with the most secure signing algorithm, key length,
  #  and the longest expiration time - not easy!
  #  for now, we simply accept all signed keys
  k.sigs.each { |sig|
    if (sig.type_covered = Types.DNSKEY)
      if (sig.inception <= Time.now.to_i)
        #  Check sig.expiration, sig.algorithm
        if (sig.expiration > Time.now.to_i)
          #  add the keys to the store
          k.rrs.each {|rr| priv_add_key(rr, sig.expiration)}
        end
      end
    end
  }
end

#eachObject



74
75
76
77
78
# File 'lib/dnsruby/key_cache.rb', line 74

def each
  #  Only offer currently-valid keys here
  remove_expired_keys
  @keys.values.each {|v| yield v[1]}
end

#find_key_for(name) ⇒ Object



96
97
98
99
# File 'lib/dnsruby/key_cache.rb', line 96

def find_key_for(name)
  each {|key| return key if key.name == name}
  return false
end

#keysObject



79
80
81
82
83
84
85
86
# File 'lib/dnsruby/key_cache.rb', line 79

def keys
  #  Only offer currently-valid keys here
  remove_expired_keys
  ks = []
  @keys.values.each {|a| ks.push(a[1])}
  return ks
  #         return @keys.keys
end

#keys_and_expirationsObject

return @keys.keys



87
88
89
90
# File 'lib/dnsruby/key_cache.rb', line 87

def keys_and_expirations
  remove_expired_keys
  return keys.values
end

#priv_add_key(k, exp) ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/dnsruby/key_cache.rb', line 65

def priv_add_key(k, exp)
  #  Check that the key does not already exist with a longer expiration!
  if (@keys[k] == nil)
    @keys[k.key_tag] = [exp,k]
  elsif ((@keys[k])[0] < exp)
    @keys[k.key_tag] = [exp,k]
  end
end

#remove_expired_keysObject



91
92
93
94
95
# File 'lib/dnsruby/key_cache.rb', line 91

def remove_expired_keys
  @keys.delete_if {|k,v|
    v[0] < Time.now.to_i
  }
end