-
-
Save d6rkaiz/72a10592e7059f2151c2dd96fb904b3c to your computer and use it in GitHub Desktop.
Hook for letsencrypt.sh to do DNS challenges
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'aws-sdk' | |
require 'pry' | |
require 'awesome_print' | |
require 'domain_name' | |
# ------------------------------------------------------------------------------ | |
# Credentials | |
# ------------------------------------------------------------------------------ | |
# pick up AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY by default from | |
# environment | |
Aws.config.update({ | |
region: 'ap-northeast-1', | |
}) | |
# ------------------------------------------------------------------------------ | |
def setup_dns(fqdn, domain, txt_challenge) | |
route53 = Aws::Route53::Client.new() | |
hosted_zone = route53.list_hosted_zones_by_name( | |
{dns_name: "#{domain}."}).hosted_zones[0] | |
changes = [] | |
changes << { | |
action: "UPSERT", | |
resource_record_set: { | |
name: "_acme-challenge.#{fqdn}.", | |
type: "TXT", | |
ttl: 60, | |
resource_records: [ | |
value: "\"#{txt_challenge}\"", | |
], | |
}, | |
} | |
resp = route53.change_resource_record_sets({ | |
hosted_zone_id: hosted_zone.id, | |
change_batch: { | |
changes: changes, | |
}, | |
}) | |
ap resp | |
sleep 20 | |
end | |
def delete_dns(fqdn, domain, txt_challenge) | |
route53 = Aws::Route53::Client.new() | |
hosted_zone = route53.list_hosted_zones_by_name( | |
{dns_name: "#{domain}."}).hosted_zones[0] | |
changes = [] | |
changes << { | |
action: "DELETE", | |
resource_record_set: { | |
name: "_acme-challenge.#{fqdn}.", | |
type: "TXT", | |
ttl: 60, | |
resource_records: [ | |
value: "\"#{txt_challenge}\"", | |
], | |
}, | |
} | |
resp = route53.change_resource_record_sets({ | |
hosted_zone_id: hosted_zone.id, | |
change_batch: { | |
changes: changes, | |
}, | |
}) | |
ap resp | |
sleep 10 | |
end | |
if __FILE__ == $0 | |
hook_stage = ARGV[0] | |
fqdn = ARGV[1] | |
txt_challenge = ARGV[3] | |
domain = DomainName(fqdn).domain | |
puts " hook_stage: #{hook_stage}" | |
puts " fqdn: #{fqdn}" | |
puts " domain: #{domain}" | |
puts "txt_challenge: #{txt_challenge}" | |
if hook_stage == "deploy_challenge" | |
setup_dns(fqdn, domain, txt_challenge) | |
elsif hook_stage == "clean_challenge" | |
delete_dns(fqdn, domain, txt_challenge) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment