Browse Source

Quick implementation of clean- and deploy_challenge with ruby-pdns

master
Silke 5 years ago
parent
commit
feb60e475d
  1. 1
      .gitignore
  2. 6
      config.dist.yml
  3. 76
      letsencrypt-pdns

1
.gitignore

@ -0,0 +1 @@
config.yml

6
config.dist.yml

@ -0,0 +1,6 @@
powerdns:
host: ns0.example.com
port: 8081
api_key: key
version: 0
wait_time: 60

76
letsencrypt-pdns

@ -0,0 +1,76 @@
#!/usr/bin/env ruby
require 'yaml'
require_relative 'ruby-pdns/lib/pdns'
# Class for Letsencrypt.sh with the PDNS api
class LetsEncryptPDNS
# Load config and create PDNS object
def initialize
config = load_config
@wait = config['wait_time'] || 0
@pdns = PDNS.new(
config['powerdns']['host'],
config['powerdns']['port'],
config['powerdns']['api_key'],
config['powerdns']['version']
)
end
# Show help
def help
abort('Usage: letsencrypt-pdns [help|deploy_challenge|clean_challenge] ' \
'[domain] [unused token] [dns token]')
end
# Get zone from hostname
def zone(hostname)
zone = hostname.split('.')[-2..-1].join('.')
@pdns.new_zone('localhost', zone)
end
# Load the config file
def load_config
YAML.load_file(File.join(__dir__, 'config.yml'))
end
# Remove a challenge token
def clean(hostname)
pdns = zone(hostname)
pdns.remove(
name: "_acme-challenge.#{hostname}",
type: 'TXT',
ttl: 10
)
end
# Deploy a challenge token and wait for the configured time
def deploy(hostname, token)
pdns = zone(hostname)
result = pdns.update(
name: "_acme-challenge.#{hostname}",
type: 'TXT',
ttl: 10,
records: ['"' + token + '"']
)
sleep(@wait)
result
end
end
pdns = LetsEncryptPDNS.new
result = case ARGV[0]
when 'help'
pdns.help
when 'clean_challenge'
pdns.help unless ARGV.size == 4
pdns.clean(ARGV[1])
when 'deploy_challenge'
pdns.help unless ARGV.size == 4
pdns.deploy(ARGV[1], ARGV[3])
else
# Unknown instruction, exit
exit 0
end
abort('Error: ' + result['error']) if result.key? 'error'
Loading…
Cancel
Save