Class: Bullion::ChallengeClient

Inherits:
Object
  • Object
show all
Defined in:
lib/bullion/challenge_client.rb

Overview

Superclass for executing ACMEv2 Challenges

Constant Summary collapse

ChallengeClientMetric =
Prometheus::Client::Histogram.new(
  :challenge_execution_seconds,
  docstring: "Challenge execution histogram in seconds",
  labels: %i[acme_type status]
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(challenge) ⇒ ChallengeClient

Returns a new instance of ChallengeClient.



15
16
17
# File 'lib/bullion/challenge_client.rb', line 15

def initialize(challenge)
  @challenge = challenge
end

Instance Attribute Details

#challengeObject

Returns the value of attribute challenge.



13
14
15
# File 'lib/bullion/challenge_client.rb', line 13

def challenge
  @challenge
end

Instance Method Details

#attempt(retries: 4) ⇒ Object

rubocop:disable Metrics/AbcSize rubocop:disable Metrics/MethodLength



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
52
53
54
55
# File 'lib/bullion/challenge_client.rb', line 21

def attempt(retries: 4)
  tries = 0
  success = false

  challenge.update!(status: "processing")

  benchtime = Benchmark.realtime do
    until success || tries >= retries
      tries += 1
      success = perform
      if success
        LOGGER.info "Validated #{type} #{identifier}"
        challenge.status = "valid"
        challenge.validated = Time.now
      else
        sleep rand(2..4)
      end
    end
  end

  unless success
    LOGGER.info "Failed to validate #{type} #{identifier}"
    challenge.status = "invalid"
    challenge.authorization.update!(status: "invalid")
    challenge.authorization.order.update!(status: "invalid")
  end

  challenge.save

  ChallengeClientMetric.observe(
    benchtime, labels: { acme_type: type, status: challenge.status }
  )

  success
end

#identifierObject

rubocop:enable Metrics/AbcSize rubocop:enable Metrics/MethodLength



59
60
61
# File 'lib/bullion/challenge_client.rb', line 59

def identifier
  challenge.identifier
end