Class: Dnsruby::KeyCache
- Inherits:
-
Object
- Object
- Dnsruby::KeyCache
- Defined in:
- lib/dnsruby/key_cache.rb
Overview
:nodoc: all
Class Method Summary collapse
-
.candidate_identity(name, klass, algorithm, tag) ⇒ Object
A tag identifies a bucket, not a unique key.
-
.key_identity(key) ⇒ Object
TTL and cached OpenSSL objects are not part of a DNSKEY's identity.
Instance Method Summary collapse
- #add(k, expiration = nil) ⇒ Object
- #add_key_with_expiration(k, expiration) ⇒ Object
- #add_rrset(k, expiration = nil) ⇒ Object
- #delete(key) ⇒ Object
- #delete_identity(identity) ⇒ Object
- #each ⇒ Object
- #find_key_for(name) ⇒ Object
- #include_identity?(identity) ⇒ Boolean
-
#initialize(keys = nil, max_keys: nil) ⇒ KeyCache
constructor
Cache includes expiration time for keys Cache removes expired records.
- #keys ⇒ Object
-
#keys_and_expirations ⇒ Object
return @keys.keys.
- #keys_for_name(name) ⇒ Object
- #matching_keys(signatures) ⇒ Object
- #priv_add_key(k, exp) ⇒ Object
- #remove_expired_keys ⇒ Object
Constructor Details
#initialize(keys = nil, max_keys: nil) ⇒ KeyCache
Cache includes expiration time for keys Cache removes expired records
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/dnsruby/key_cache.rb', line 23 def initialize(keys = nil, max_keys: nil) # Store DNSKEY identity against [expiry, key]. A 16-bit key tag can collide. @keys = {} @candidate_index = {} @key_candidates = {} @name_index = {} raise ArgumentError, 'max_keys must be a positive integer' if max_keys && (!max_keys.is_a?(Integer) || max_keys <= 0) @max_keys = max_keys add(keys) end |
Class Method Details
.candidate_identity(name, klass, algorithm, tag) ⇒ Object
A tag identifies a bucket, not a unique key. Keep every colliding key.
74 75 76 |
# File 'lib/dnsruby/key_cache.rb', line 74 def self.candidate_identity(name, klass, algorithm, tag) [name.canonical.freeze, klass.code, algorithm.code, tag].freeze end |
.key_identity(key) ⇒ Object
TTL and cached OpenSSL objects are not part of a DNSKEY's identity. Copy mutable fields so later changes cannot corrupt the hash index.
36 37 38 39 |
# File 'lib/dnsruby/key_cache.rb', line 36 def self.key_identity(key) [key.name.canonical.freeze, key.klass.code, key.type.code, key.flags, key.protocol, key.algorithm.code, key.key.dup.freeze].freeze end |
Instance Method Details
#add(k, expiration = nil) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/dnsruby/key_cache.rb', line 43 def add(k, expiration = nil) if (k == nil) return false elsif (k.instance_of?RRSet) add_rrset(k, expiration) elsif (k.kind_of?KeyCache) k.keys_and_expirations.each { |expiration, key| priv_add_key(key, expiration) } else raise ArgumentError.new("Expected an RRSet or KeyCache! Got #{k.class}") end return true end |
#add_key_with_expiration(k, expiration) ⇒ Object
40 41 42 |
# File 'lib/dnsruby/key_cache.rb', line 40 def add_key_with_expiration(k, expiration) priv_add_key(k, expiration) end |
#add_rrset(k, expiration = nil) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/dnsruby/key_cache.rb', line 58 def add_rrset(k, expiration = nil) return unless k.type == Types.DNSKEY now = Time.now.to_i # Verification supplies an absolute deadline derived from the signature # that actually verified. Never extend it using other supplied RRSIGs. # Explicit callers without that deadline get a conservative TTL bound. unless expiration signatures = k.sigs.select { |sig| SerialTime.valid?(sig.inception, sig.expiration, now) } return if signatures.empty? expiration = ([now + k.ttl] + signatures.map { |sig| SerialTime.absolute(sig.expiration, now) }).min end return if expiration <= now k.rrs.each { |rr| priv_add_key(rr, expiration) } end |
#delete(key) ⇒ Object
132 133 134 |
# File 'lib/dnsruby/key_cache.rb', line 132 def delete(key) delete_identity(self.class.key_identity(key)) end |
#delete_identity(identity) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/dnsruby/key_cache.rb', line 135 def delete_identity(identity) cached = @keys.delete(identity) return unless cached key = cached[1] names = @name_index.fetch(identity.first) names.delete(identity) @name_index.delete(identity.first) if names.empty? candidate = @key_candidates.delete(identity) bucket = @candidate_index[candidate] if bucket bucket.delete(identity) @candidate_index.delete(candidate) if bucket.empty? end key end |
#each ⇒ Object
127 128 129 130 131 |
# File 'lib/dnsruby/key_cache.rb', line 127 def each # Only offer currently-valid keys here remove_expired_keys @keys.values.each {|v| yield v[1]} end |
#find_key_for(name) ⇒ Object
166 167 168 169 |
# File 'lib/dnsruby/key_cache.rb', line 166 def find_key_for(name) each {|key| return key if key.name == name} return false end |
#include_identity?(identity) ⇒ Boolean
123 124 125 |
# File 'lib/dnsruby/key_cache.rb', line 123 def include_identity?(identity) @keys.key?(identity) end |
#keys ⇒ Object
150 151 152 153 154 155 156 157 |
# File 'lib/dnsruby/key_cache.rb', line 150 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_expirations ⇒ Object
return @keys.keys
158 159 160 161 |
# File 'lib/dnsruby/key_cache.rb', line 158 def keys_and_expirations remove_expired_keys @keys.values.map(&:dup) end |
#keys_for_name(name) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/dnsruby/key_cache.rb', line 110 def keys_for_name(name) now = Time.now.to_i (@name_index[name.canonical] || {}).keys.filter_map do |identity| expiration, key = @keys.fetch(identity) if expiration <= now delete_identity(identity) nil else key end end end |
#matching_keys(signatures) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/dnsruby/key_cache.rb', line 93 def matching_keys(signatures) identities = signatures.flat_map do |sig| candidate = self.class.candidate_identity(sig.signers_name, sig.klass, sig.algorithm, sig.key_tag) (@candidate_index[candidate] || {}).keys end.uniq now = Time.now.to_i identities.filter_map do |identity| expiration, key = @keys.fetch(identity) if expiration <= now delete_identity(identity) nil else key end end end |
#priv_add_key(k, exp) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/dnsruby/key_cache.rb', line 78 def priv_add_key(k, exp) identity = self.class.key_identity(k) previous = @keys[identity] return if previous && previous[0] >= exp if @max_keys && !previous && @keys.length >= @max_keys remove_expired_keys delete_identity(@keys.each_key.first) while @keys.length >= @max_keys end @keys[identity] = [exp, k] candidate = self.class.candidate_identity(k.name, k.klass, k.algorithm, k.key_tag) (@candidate_index[candidate] ||= {})[identity] = true @key_candidates[identity] = candidate (@name_index[identity.first] ||= {})[identity] = true end |
#remove_expired_keys ⇒ Object
162 163 164 165 |
# File 'lib/dnsruby/key_cache.rb', line 162 def remove_expired_keys now = Time.now.to_i @keys.each { |identity, (expiration, _key)| delete_identity(identity) if expiration <= now } end |