2019-12-13 00:25:12 +03:00
|
|
|
// Copyright 2019 The go-ethereum Authors
|
|
|
|
// This file is part of go-ethereum.
|
|
|
|
//
|
|
|
|
// go-ethereum is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// go-ethereum is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-03-19 15:15:57 +03:00
|
|
|
"context"
|
2019-12-13 00:25:12 +03:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-01-17 13:32:29 +03:00
|
|
|
"sort"
|
2019-12-13 00:25:12 +03:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2021-03-19 15:15:57 +03:00
|
|
|
"time"
|
2019-12-13 00:25:12 +03:00
|
|
|
|
2021-03-19 15:15:57 +03:00
|
|
|
"github.com/aws/aws-sdk-go-v2/aws"
|
|
|
|
"github.com/aws/aws-sdk-go-v2/config"
|
|
|
|
"github.com/aws/aws-sdk-go-v2/credentials"
|
|
|
|
"github.com/aws/aws-sdk-go-v2/service/route53"
|
|
|
|
"github.com/aws/aws-sdk-go-v2/service/route53/types"
|
2019-12-13 00:25:12 +03:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p/dnsdisc"
|
|
|
|
"gopkg.in/urfave/cli.v1"
|
|
|
|
)
|
|
|
|
|
2020-03-27 01:55:33 +03:00
|
|
|
const (
|
|
|
|
// Route53 limits change sets to 32k of 'RDATA size'. Change sets are also limited to
|
|
|
|
// 1000 items. UPSERTs count double.
|
|
|
|
// https://docs.aws.amazon.com/Route53/latest/DeveloperGuide/DNSLimitations.html#limits-api-requests-changeresourcerecordsets
|
|
|
|
route53ChangeSizeLimit = 32000
|
|
|
|
route53ChangeCountLimit = 1000
|
2021-03-19 15:15:57 +03:00
|
|
|
maxRetryLimit = 60
|
2020-03-27 01:55:33 +03:00
|
|
|
)
|
2020-01-17 13:32:29 +03:00
|
|
|
|
2019-12-13 00:25:12 +03:00
|
|
|
var (
|
|
|
|
route53AccessKeyFlag = cli.StringFlag{
|
|
|
|
Name: "access-key-id",
|
|
|
|
Usage: "AWS Access Key ID",
|
|
|
|
EnvVar: "AWS_ACCESS_KEY_ID",
|
|
|
|
}
|
|
|
|
route53AccessSecretFlag = cli.StringFlag{
|
|
|
|
Name: "access-key-secret",
|
|
|
|
Usage: "AWS Access Key Secret",
|
|
|
|
EnvVar: "AWS_SECRET_ACCESS_KEY",
|
|
|
|
}
|
|
|
|
route53ZoneIDFlag = cli.StringFlag{
|
|
|
|
Name: "zone-id",
|
|
|
|
Usage: "Route53 Zone ID",
|
|
|
|
}
|
2021-03-20 02:22:24 +03:00
|
|
|
route53RegionFlag = cli.StringFlag{
|
|
|
|
Name: "aws-region",
|
|
|
|
Usage: "AWS Region",
|
|
|
|
Value: "eu-central-1",
|
|
|
|
}
|
2019-12-13 00:25:12 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type route53Client struct {
|
2021-03-19 15:15:57 +03:00
|
|
|
api *route53.Client
|
2019-12-13 00:25:12 +03:00
|
|
|
zoneID string
|
|
|
|
}
|
|
|
|
|
2020-01-17 13:32:29 +03:00
|
|
|
type recordSet struct {
|
|
|
|
values []string
|
|
|
|
ttl int64
|
|
|
|
}
|
|
|
|
|
2019-12-13 00:25:12 +03:00
|
|
|
// newRoute53Client sets up a Route53 API client from command line flags.
|
|
|
|
func newRoute53Client(ctx *cli.Context) *route53Client {
|
|
|
|
akey := ctx.String(route53AccessKeyFlag.Name)
|
|
|
|
asec := ctx.String(route53AccessSecretFlag.Name)
|
|
|
|
if akey == "" || asec == "" {
|
2021-03-20 02:22:24 +03:00
|
|
|
exit(fmt.Errorf("need Route53 Access Key ID and secret to proceed"))
|
2019-12-13 00:25:12 +03:00
|
|
|
}
|
2021-03-19 15:15:57 +03:00
|
|
|
creds := aws.NewCredentialsCache(credentials.NewStaticCredentialsProvider(akey, asec, ""))
|
|
|
|
cfg, err := config.LoadDefaultConfig(context.Background(), config.WithCredentialsProvider(creds))
|
2019-12-13 00:25:12 +03:00
|
|
|
if err != nil {
|
2021-03-19 15:15:57 +03:00
|
|
|
exit(fmt.Errorf("can't initialize AWS configuration: %v", err))
|
2019-12-13 00:25:12 +03:00
|
|
|
}
|
2021-03-20 02:22:24 +03:00
|
|
|
cfg.Region = ctx.String(route53RegionFlag.Name)
|
2019-12-13 00:25:12 +03:00
|
|
|
return &route53Client{
|
2021-03-19 15:15:57 +03:00
|
|
|
api: route53.NewFromConfig(cfg),
|
2019-12-13 00:25:12 +03:00
|
|
|
zoneID: ctx.String(route53ZoneIDFlag.Name),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// deploy uploads the given tree to Route53.
|
|
|
|
func (c *route53Client) deploy(name string, t *dnsdisc.Tree) error {
|
|
|
|
if err := c.checkZone(name); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute DNS changes.
|
2020-01-17 13:32:29 +03:00
|
|
|
existing, err := c.collectRecords(name)
|
2019-12-13 00:25:12 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-01-17 13:32:29 +03:00
|
|
|
log.Info(fmt.Sprintf("Found %d TXT records", len(existing)))
|
|
|
|
|
|
|
|
records := t.ToTXT(name)
|
|
|
|
changes := c.computeChanges(name, records, existing)
|
2019-12-13 00:25:12 +03:00
|
|
|
if len(changes) == 0 {
|
|
|
|
log.Info("No DNS changes needed")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-19 15:15:57 +03:00
|
|
|
// Submit all change batches.
|
2020-03-27 01:55:33 +03:00
|
|
|
batches := splitChanges(changes, route53ChangeSizeLimit, route53ChangeCountLimit)
|
2021-03-19 15:15:57 +03:00
|
|
|
changesToCheck := make([]*route53.ChangeResourceRecordSetsOutput, len(batches))
|
2020-01-17 13:32:29 +03:00
|
|
|
for i, changes := range batches {
|
|
|
|
log.Info(fmt.Sprintf("Submitting %d changes to Route53", len(changes)))
|
2021-03-19 15:15:57 +03:00
|
|
|
batch := &types.ChangeBatch{
|
|
|
|
Changes: changes,
|
|
|
|
Comment: aws.String(fmt.Sprintf("enrtree update %d/%d of %s at seq %d", i+1, len(batches), name, t.Seq())),
|
|
|
|
}
|
2020-01-17 13:32:29 +03:00
|
|
|
req := &route53.ChangeResourceRecordSetsInput{HostedZoneId: &c.zoneID, ChangeBatch: batch}
|
2021-03-19 15:15:57 +03:00
|
|
|
changesToCheck[i], err = c.api.ChangeResourceRecordSets(context.TODO(), req)
|
2020-01-17 13:32:29 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-19 15:15:57 +03:00
|
|
|
}
|
2019-12-13 00:25:12 +03:00
|
|
|
|
2021-03-25 14:32:32 +03:00
|
|
|
// Wait for all change batches to propagate.
|
2021-03-19 15:15:57 +03:00
|
|
|
for _, change := range changesToCheck {
|
|
|
|
log.Info(fmt.Sprintf("Waiting for change request %s", *change.ChangeInfo.Id))
|
|
|
|
wreq := &route53.GetChangeInput{Id: change.ChangeInfo.Id}
|
|
|
|
var count int
|
|
|
|
for {
|
|
|
|
wresp, err := c.api.GetChange(context.TODO(), wreq)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
count++
|
|
|
|
|
|
|
|
if wresp.ChangeInfo.Status == types.ChangeStatusInsync || count >= maxRetryLimit {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(30 * time.Second)
|
2020-01-17 13:32:29 +03:00
|
|
|
}
|
|
|
|
}
|
2021-03-19 15:15:57 +03:00
|
|
|
|
2020-01-17 13:32:29 +03:00
|
|
|
return nil
|
2019-12-13 00:25:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// checkZone verifies zone information for the given domain.
|
|
|
|
func (c *route53Client) checkZone(name string) (err error) {
|
|
|
|
if c.zoneID == "" {
|
|
|
|
c.zoneID, err = c.findZoneID(name)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// findZoneID searches for the Zone ID containing the given domain.
|
|
|
|
func (c *route53Client) findZoneID(name string) (string, error) {
|
|
|
|
log.Info(fmt.Sprintf("Finding Route53 Zone ID for %s", name))
|
|
|
|
var req route53.ListHostedZonesByNameInput
|
|
|
|
for {
|
2021-03-19 15:15:57 +03:00
|
|
|
resp, err := c.api.ListHostedZonesByName(context.TODO(), &req)
|
2019-12-13 00:25:12 +03:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
for _, zone := range resp.HostedZones {
|
|
|
|
if isSubdomain(name, *zone.Name) {
|
|
|
|
return *zone.Id, nil
|
|
|
|
}
|
|
|
|
}
|
2021-03-19 15:15:57 +03:00
|
|
|
if !resp.IsTruncated {
|
2019-12-13 00:25:12 +03:00
|
|
|
break
|
|
|
|
}
|
|
|
|
req.DNSName = resp.NextDNSName
|
|
|
|
req.HostedZoneId = resp.NextHostedZoneId
|
|
|
|
}
|
|
|
|
return "", errors.New("can't find zone ID for " + name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// computeChanges creates DNS changes for the given record.
|
2021-03-19 15:15:57 +03:00
|
|
|
func (c *route53Client) computeChanges(name string, records map[string]string, existing map[string]recordSet) []types.Change {
|
2019-12-13 00:25:12 +03:00
|
|
|
// Convert all names to lowercase.
|
|
|
|
lrecords := make(map[string]string, len(records))
|
|
|
|
for name, r := range records {
|
|
|
|
lrecords[strings.ToLower(name)] = r
|
|
|
|
}
|
|
|
|
records = lrecords
|
|
|
|
|
2021-03-19 15:15:57 +03:00
|
|
|
var changes []types.Change
|
2021-03-25 14:32:32 +03:00
|
|
|
for path, newValue := range records {
|
|
|
|
prevRecords, exists := existing[path]
|
|
|
|
prevValue := strings.Join(prevRecords.values, "")
|
|
|
|
|
|
|
|
// prevValue contains quoted strings, encode newValue to compare.
|
|
|
|
newValue = splitTXT(newValue)
|
|
|
|
|
|
|
|
// Assign TTL.
|
2020-01-17 13:32:29 +03:00
|
|
|
ttl := int64(rootTTL)
|
2019-12-13 00:25:12 +03:00
|
|
|
if path != name {
|
2020-01-17 13:32:29 +03:00
|
|
|
ttl = int64(treeNodeTTL)
|
2019-12-13 00:25:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if !exists {
|
|
|
|
// Entry is unknown, push a new one
|
2021-03-25 14:32:32 +03:00
|
|
|
log.Info(fmt.Sprintf("Creating %s = %s", path, newValue))
|
|
|
|
changes = append(changes, newTXTChange("CREATE", path, ttl, newValue))
|
|
|
|
} else if prevValue != newValue || prevRecords.ttl != ttl {
|
2019-12-13 00:25:12 +03:00
|
|
|
// Entry already exists, only change its content.
|
2021-03-25 14:32:32 +03:00
|
|
|
log.Info(fmt.Sprintf("Updating %s from %s to %s", path, prevValue, newValue))
|
|
|
|
changes = append(changes, newTXTChange("UPSERT", path, ttl, newValue))
|
2019-12-13 00:25:12 +03:00
|
|
|
} else {
|
2021-03-25 14:32:32 +03:00
|
|
|
log.Debug(fmt.Sprintf("Skipping %s = %s", path, newValue))
|
2019-12-13 00:25:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate over the old records and delete anything stale.
|
2020-01-17 13:32:29 +03:00
|
|
|
for path, set := range existing {
|
2019-12-13 00:25:12 +03:00
|
|
|
if _, ok := records[path]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Stale entry, nuke it.
|
2020-02-05 17:29:59 +03:00
|
|
|
log.Info(fmt.Sprintf("Deleting %s = %q", path, strings.Join(set.values, "")))
|
|
|
|
changes = append(changes, newTXTChange("DELETE", path, set.ttl, set.values...))
|
2019-12-13 00:25:12 +03:00
|
|
|
}
|
2020-01-17 13:32:29 +03:00
|
|
|
|
|
|
|
sortChanges(changes)
|
|
|
|
return changes
|
|
|
|
}
|
|
|
|
|
|
|
|
// sortChanges ensures DNS changes are in leaf-added -> root-changed -> leaf-deleted order.
|
2021-03-19 15:15:57 +03:00
|
|
|
func sortChanges(changes []types.Change) {
|
2020-01-17 13:32:29 +03:00
|
|
|
score := map[string]int{"CREATE": 1, "UPSERT": 2, "DELETE": 3}
|
|
|
|
sort.Slice(changes, func(i, j int) bool {
|
2021-03-19 15:15:57 +03:00
|
|
|
if changes[i].Action == changes[j].Action {
|
2020-01-17 13:32:29 +03:00
|
|
|
return *changes[i].ResourceRecordSet.Name < *changes[j].ResourceRecordSet.Name
|
|
|
|
}
|
2021-03-19 15:15:57 +03:00
|
|
|
return score[string(changes[i].Action)] < score[string(changes[j].Action)]
|
2020-01-17 13:32:29 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// splitChanges splits up DNS changes such that each change batch
|
|
|
|
// is smaller than the given RDATA limit.
|
2021-03-19 15:15:57 +03:00
|
|
|
func splitChanges(changes []types.Change, sizeLimit, countLimit int) [][]types.Change {
|
2020-03-27 01:55:33 +03:00
|
|
|
var (
|
2021-03-19 15:15:57 +03:00
|
|
|
batches [][]types.Change
|
2020-03-27 01:55:33 +03:00
|
|
|
batchSize int
|
|
|
|
batchCount int
|
|
|
|
)
|
2020-01-17 13:32:29 +03:00
|
|
|
for _, ch := range changes {
|
|
|
|
// Start new batch if this change pushes the current one over the limit.
|
2020-03-27 01:55:33 +03:00
|
|
|
count := changeCount(ch)
|
|
|
|
size := changeSize(ch) * count
|
|
|
|
overSize := batchSize+size > sizeLimit
|
|
|
|
overCount := batchCount+count > countLimit
|
|
|
|
if len(batches) == 0 || overSize || overCount {
|
2020-01-17 13:32:29 +03:00
|
|
|
batches = append(batches, nil)
|
|
|
|
batchSize = 0
|
2020-03-27 01:55:33 +03:00
|
|
|
batchCount = 0
|
2020-01-17 13:32:29 +03:00
|
|
|
}
|
|
|
|
batches[len(batches)-1] = append(batches[len(batches)-1], ch)
|
|
|
|
batchSize += size
|
2020-03-27 01:55:33 +03:00
|
|
|
batchCount += count
|
2020-01-17 13:32:29 +03:00
|
|
|
}
|
|
|
|
return batches
|
|
|
|
}
|
|
|
|
|
|
|
|
// changeSize returns the RDATA size of a DNS change.
|
2021-03-19 15:15:57 +03:00
|
|
|
func changeSize(ch types.Change) int {
|
2020-01-17 13:32:29 +03:00
|
|
|
size := 0
|
|
|
|
for _, rr := range ch.ResourceRecordSet.ResourceRecords {
|
|
|
|
if rr.Value != nil {
|
|
|
|
size += len(*rr.Value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return size
|
2019-12-13 00:25:12 +03:00
|
|
|
}
|
|
|
|
|
2021-03-19 15:15:57 +03:00
|
|
|
func changeCount(ch types.Change) int {
|
|
|
|
if ch.Action == types.ChangeActionUpsert {
|
2020-03-27 01:55:33 +03:00
|
|
|
return 2
|
|
|
|
}
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2019-12-13 00:25:12 +03:00
|
|
|
// collectRecords collects all TXT records below the given name.
|
2020-01-17 13:32:29 +03:00
|
|
|
func (c *route53Client) collectRecords(name string) (map[string]recordSet, error) {
|
2019-12-13 00:25:12 +03:00
|
|
|
var req route53.ListResourceRecordSetsInput
|
2021-03-19 15:15:57 +03:00
|
|
|
req.HostedZoneId = &c.zoneID
|
2020-01-17 13:32:29 +03:00
|
|
|
existing := make(map[string]recordSet)
|
2021-03-25 14:32:32 +03:00
|
|
|
for page := 0; ; page++ {
|
|
|
|
log.Info("Loading existing TXT records", "name", name, "zone", c.zoneID, "page", page)
|
2021-03-19 15:15:57 +03:00
|
|
|
resp, err := c.api.ListResourceRecordSets(context.TODO(), &req)
|
|
|
|
if err != nil {
|
|
|
|
return existing, err
|
|
|
|
}
|
2019-12-13 00:25:12 +03:00
|
|
|
for _, set := range resp.ResourceRecordSets {
|
2021-03-19 15:15:57 +03:00
|
|
|
if !isSubdomain(*set.Name, name) || set.Type != types.RRTypeTxt {
|
2019-12-13 00:25:12 +03:00
|
|
|
continue
|
|
|
|
}
|
2020-01-17 13:32:29 +03:00
|
|
|
s := recordSet{ttl: *set.TTL}
|
2019-12-13 00:25:12 +03:00
|
|
|
for _, rec := range set.ResourceRecords {
|
2020-01-17 13:32:29 +03:00
|
|
|
s.values = append(s.values, *rec.Value)
|
2019-12-13 00:25:12 +03:00
|
|
|
}
|
2020-01-17 13:32:29 +03:00
|
|
|
name := strings.TrimSuffix(*set.Name, ".")
|
|
|
|
existing[name] = s
|
2019-12-13 00:25:12 +03:00
|
|
|
}
|
2021-03-19 15:15:57 +03:00
|
|
|
|
|
|
|
if !resp.IsTruncated {
|
|
|
|
break
|
|
|
|
}
|
2021-03-25 14:32:32 +03:00
|
|
|
// Set the cursor to the next batch. From the AWS docs:
|
2021-03-20 12:35:22 +03:00
|
|
|
//
|
|
|
|
// To display the next page of results, get the values of NextRecordName,
|
|
|
|
// NextRecordType, and NextRecordIdentifier (if any) from the response. Then submit
|
|
|
|
// another ListResourceRecordSets request, and specify those values for
|
|
|
|
// StartRecordName, StartRecordType, and StartRecordIdentifier.
|
2021-03-19 15:15:57 +03:00
|
|
|
req.StartRecordIdentifier = resp.NextRecordIdentifier
|
2021-03-20 12:35:22 +03:00
|
|
|
req.StartRecordName = resp.NextRecordName
|
|
|
|
req.StartRecordType = resp.NextRecordType
|
2021-03-19 15:15:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return existing, nil
|
2019-12-13 00:25:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// newTXTChange creates a change to a TXT record.
|
2021-03-19 15:15:57 +03:00
|
|
|
func newTXTChange(action, name string, ttl int64, values ...string) types.Change {
|
|
|
|
r := types.ResourceRecordSet{
|
|
|
|
Type: types.RRTypeTxt,
|
|
|
|
Name: &name,
|
|
|
|
TTL: &ttl,
|
|
|
|
}
|
|
|
|
var rrs []types.ResourceRecord
|
2019-12-13 00:25:12 +03:00
|
|
|
for _, val := range values {
|
2021-03-19 15:15:57 +03:00
|
|
|
var rr types.ResourceRecord
|
|
|
|
rr.Value = aws.String(val)
|
2019-12-13 00:25:12 +03:00
|
|
|
rrs = append(rrs, rr)
|
|
|
|
}
|
2021-03-19 15:15:57 +03:00
|
|
|
|
|
|
|
r.ResourceRecords = rrs
|
|
|
|
|
|
|
|
return types.Change{
|
|
|
|
Action: types.ChangeAction(action),
|
|
|
|
ResourceRecordSet: &r,
|
|
|
|
}
|
2019-12-13 00:25:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// isSubdomain returns true if name is a subdomain of domain.
|
|
|
|
func isSubdomain(name, domain string) bool {
|
|
|
|
domain = strings.TrimSuffix(domain, ".")
|
|
|
|
name = strings.TrimSuffix(name, ".")
|
|
|
|
return strings.HasSuffix("."+name, "."+domain)
|
|
|
|
}
|
|
|
|
|
|
|
|
// splitTXT splits value into a list of quoted 255-character strings.
|
2020-02-05 17:29:59 +03:00
|
|
|
func splitTXT(value string) string {
|
|
|
|
var result strings.Builder
|
2019-12-13 00:25:12 +03:00
|
|
|
for len(value) > 0 {
|
|
|
|
rlen := len(value)
|
|
|
|
if rlen > 253 {
|
|
|
|
rlen = 253
|
|
|
|
}
|
2020-02-05 17:29:59 +03:00
|
|
|
result.WriteString(strconv.Quote(value[:rlen]))
|
2019-12-13 00:25:12 +03:00
|
|
|
value = value[rlen:]
|
|
|
|
}
|
2020-02-05 17:29:59 +03:00
|
|
|
return result.String()
|
2019-12-13 00:25:12 +03:00
|
|
|
}
|