Class: Acmesmith::ChallengeResponders::Cloudflare

Inherits:
Base
  • Object
show all
Defined in:
lib/acmesmith/challenge_responders/cloudflare.rb

Defined Under Namespace

Classes: AmbiguousZones, ZoneNotFound

Constant Summary collapse

DEFAULT_COMMENT =
'Managed by acmesmith for ACME dns-01'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(api_token:, zone_id_map: {}, substitution_map: {}, ttl: 60, wait_time_seconds: 30, api_base: nil, client: nil) ⇒ Cloudflare

Returns a new instance of Cloudflare.



20
21
22
23
24
25
26
27
28
29
# File 'lib/acmesmith/challenge_responders/cloudflare.rb', line 20

def initialize(api_token:, zone_id_map: {}, substitution_map: {}, ttl: 60, wait_time_seconds: 30, api_base: nil, client: nil)
  @client = client || ::Acmesmith::Cloudflare::Client.new(api_token: api_token, api_base: api_base)
  @zone_id_map = normalize_keys(zone_id_map)
  @substitution_map = normalize_keys(substitution_map)
  @ttl = ttl
  @wait_time_seconds = wait_time_seconds

  @zones_by_name = {}
  @created_record_ids = Hash.new { |hash, key| hash[key] = [] }
end

Instance Method Details

#cap_respond_all?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/acmesmith/challenge_responders/cloudflare.rb', line 16

def cap_respond_all?
  true
end

#cleanup_all(*domain_and_challenges) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/acmesmith/challenge_responders/cloudflare.rb', line 59

def cleanup_all(*domain_and_challenges)
  domain_and_challenges = apply_substitution_for_domain_and_challenges(domain_and_challenges)

  puts "=> Removing DNS record changes from Cloudflare"
  puts

  domain_and_challenges.each do |domain, challenge|
    zone_id = find_zone_id(domain)
    record_name = record_name_for(domain, challenge)
    record_ids = take_record_ids(zone_id, record_name, challenge.record_type, challenge.record_content)
    record_ids = find_record_ids(zone_id, record_name, challenge.record_type, challenge.record_content) if record_ids.empty?

    if record_ids.empty?
      puts " * #{record_name} #{challenge.record_type} #{challenge.record_content} [ not found ]"
      next
    end

    record_ids.each do |record_id|
      print " * deleting #{record_name} #{challenge.record_type} #{challenge.record_content} ..."
      @client.delete_dns_record(zone_id: zone_id, record_id: record_id)
      puts " [ ok ] #{record_id}"
    end
  end

  puts
end

#respond_all(*domain_and_challenges) ⇒ Object



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
# File 'lib/acmesmith/challenge_responders/cloudflare.rb', line 31

def respond_all(*domain_and_challenges)
  domain_and_challenges = apply_substitution_for_domain_and_challenges(domain_and_challenges)

  puts "=> Requesting DNS record changes on Cloudflare"
  puts

  domain_and_challenges.each do |domain, challenge|
    zone_id = find_zone_id(domain)
    record_name = record_name_for(domain, challenge)

    print " * #{record_name} #{challenge.record_type} #{challenge.record_content} ..."
    response = @client.create_dns_record(
      zone_id: zone_id,
      name: record_name,
      type: challenge.record_type,
      content: challenge.record_content,
      ttl: @ttl,
      comment: DEFAULT_COMMENT,
    )
    record_id = response.fetch('result').fetch('id')
    @created_record_ids[record_identity(zone_id, record_name, challenge.record_type, challenge.record_content)] << record_id
    puts " [ ok ] #{record_id}"
  end

  puts
  wait_for_sync
end

#support?(type) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/acmesmith/challenge_responders/cloudflare.rb', line 12

def support?(type)
  type == 'dns-01'
end