Class: Dnsruby::ValidatorThread

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

Overview

Runs local DNSSEC validation outside SelectThread, using a bounded pool. Nested trust discovery shares the original validation budget.

Constant Summary collapse

WORKER_COUNT =

:nodoc: all

4
QUEUE_CAPACITY =
64

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ ValidatorThread

Returns a new instance of ValidatorThread.



75
76
77
78
79
# File 'lib/dnsruby/validator_thread.rb', line 75

def initialize(*args)
  @client_id, @client_queue, @response, @error, @query, @st, @res, @transmission_budget = args
  @registered = false
  prepare_budget if should_validate
end

Class Method Details

.enqueue(validator) ⇒ Object

Chain discovery performs synchronous queries, so validation must run away from the selector. Bound both threads and queued responses under load.



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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/dnsruby/validator_thread.rb', line 27

def self.enqueue(validator)
  rejected = []
  accepted = @worker_mutex.synchronize do
    # Threads do not survive fork. Parent work belongs to the parent, while
    # the child's first admission must start its own pool.
    if @worker_pid != Process.pid
      @worker_pid = Process.pid
      @work_queue = SizedQueue.new(QUEUE_CAPACITY)
      @workers = []
    end
    @workers.select!(&:alive?)
    begin
      (WORKER_COUNT - @workers.length).times do
        queue = @work_queue
        @workers << Thread.new do
          loop do
            work = queue.pop
            begin
              work.do_validate
            rescue StandardError => error
              Dnsruby.log.error { "DNSSEC validation worker: #{error}" }
            end
          end
        end
      end
    rescue ThreadError
      # A partial pool remains usable and is replenished on later admission.
      # If no worker could start, fail queued work rather than strand it.
    end
    if @workers.empty?
      begin
        loop { rejected << @work_queue.pop(true) }
      rescue ThreadError
      end
      false
    else
      begin
        @work_queue.push(validator, true)
        true
      rescue ThreadError
        false
      end
    end
  end
  rejected.each(&:reject_capacity)
  accepted
end

Instance Method Details

#cache_if_valid(query, response) ⇒ Object



201
202
203
204
# File 'lib/dnsruby/validator_thread.rb', line 201

def cache_if_valid(query, response)
  return if @error
  PacketSender.cache(query, response)
end

#do_validate ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/dnsruby/validator_thread.rb', line 112

def do_validate
  needs_validation = should_validate
  return if needs_validation && !register
  validated_ok = if needs_validation
    SelectThread::TransmissionBudget.with(@transmission_budget) do
      ValidationBudget.with(@budget) { validate(@query, @response, @res) }
    end
  else
    validate(@query, @response, @res)
  end
  validated_ok = false if @error && !@error.is_a?(NXDomain)
  if @response.security_level == Message::SecurityLevel.BOGUS
    detail = @response.security_error.to_s
    raise VerifyError, detail.empty? ? 'DNSSEC validation failed' : detail
  end
  @budget.check! if needs_validation
  cache_if_valid(@query, @response) if validated_ok
  @st.push_validation_response_to_select(@client_id, @client_queue, @response,
    validated_ok ? nil : @response.security_error, @query, @res)
rescue StandardError => error
  # Resolver observers ignore late completions; direct PacketSender callers
  # still need a terminal error after cancellation or their sender closing.
  unless error.is_a?(VerifyError) || error.is_a?(RecursionLimitError)
    error = VerifyError.new("DNSSEC validation failed: #{error.message}")
  end
  finish_error(error)
ensure
  unregister
end

#reject_capacity ⇒ Object



156
157
158
159
160
# File 'lib/dnsruby/validator_thread.rb', line 156

def reject_capacity
  finish_error(ValidationLimitError.new('DNSSEC validation capacity exceeded'))
ensure
  unregister
end

#run ⇒ Object



101
102
103
104
105
106
107
108
109
110
# File 'lib/dnsruby/validator_thread.rb', line 101

def run
  if should_validate
    return unless register
    unless self.class.enqueue(self)
      reject_capacity
    end
  else
    do_validate
  end
end

#should_validate ⇒ Object



162
163
164
# File 'lib/dnsruby/validator_thread.rb', line 162

def should_validate
  return ValidatorThread.requires_validation?(@query, @response, @error, @res)
end

#validate(query, response, res) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/dnsruby/validator_thread.rb', line 179

def validate(query, response, res)
  return true unless should_validate
  ValidationBudget.with do
    begin
      validated = Dnssec.validate_with_query(query, response)
      if !validated || response.security_level == Message::SecurityLevel.BOGUS
        error = response.security_error
        error = VerifyError.new(error.to_s.empty? ? 'DNSSEC validation failed' : error.to_s) unless error.is_a?(VerifyError)
        response.security_error = error
        response.security_level = Message::SecurityLevel.BOGUS
        return false
      end
      true
    rescue VerifyError => e
      response.security_error = e
      response.security_level = Message::SecurityLevel.BOGUS
      #  Response security_level should already be set
      false
    end
  end
end