| 
									
										
										
										
											2018-01-15 09:59:29 -08:00
										 |  |  | package route53
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							| 
									
										
										
										
											2018-09-17 11:19:07 -07:00
										 |  |  | 	"context"
 | 
					
						
							| 
									
										
										
										
											2019-08-03 18:07:28 -07:00
										 |  |  | 	"fmt"
 | 
					
						
							| 
									
										
										
										
											2022-05-09 10:35:42 -07:00
										 |  |  | 	"os"
 | 
					
						
							| 
									
										
										
										
											2019-08-03 18:07:28 -07:00
										 |  |  | 	"strconv"
 | 
					
						
							| 
									
										
										
										
											2018-01-15 09:59:29 -08:00
										 |  |  | 	"strings"
 | 
					
						
							| 
									
										
										
										
											2019-08-03 18:07:28 -07:00
										 |  |  | 	"time"
 | 
					
						
							| 
									
										
										
										
											2018-01-15 09:59:29 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-24 18:14:41 +02:00
										 |  |  | 	"github.com/coredns/caddy"
 | 
					
						
							| 
									
										
										
										
											2018-01-15 09:59:29 -08:00
										 |  |  | 	"github.com/coredns/coredns/core/dnsserver"
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/plugin"
 | 
					
						
							| 
									
										
										
										
											2018-09-25 11:41:05 -07:00
										 |  |  | 	"github.com/coredns/coredns/plugin/pkg/fall"
 | 
					
						
							| 
									
										
										
										
											2018-09-17 11:19:07 -07:00
										 |  |  | 	clog "github.com/coredns/coredns/plugin/pkg/log"
 | 
					
						
							| 
									
										
										
										
											2018-01-15 09:59:29 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-03 10:19:21 +01: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/feature/ec2/imds"
 | 
					
						
							|  |  |  | 	"github.com/aws/aws-sdk-go-v2/service/route53"
 | 
					
						
							| 
									
										
										
										
											2018-01-15 09:59:29 -08:00
										 |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-17 11:19:07 -07:00
										 |  |  | var log = clog.NewWithPlugin("route53")
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-03 07:21:11 +01:00
										 |  |  | func init() { plugin.Register("route53", setup) }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // exposed for testing
 | 
					
						
							| 
									
										
										
										
											2025-07-03 10:19:21 +01:00
										 |  |  | type route53Client interface {
 | 
					
						
							|  |  |  | 	ActivateKeySigningKey(ctx context.Context, params *route53.ActivateKeySigningKeyInput, optFns ...func(*route53.Options)) (*route53.ActivateKeySigningKeyOutput, error)
 | 
					
						
							|  |  |  | 	ListHostedZonesByName(ctx context.Context, params *route53.ListHostedZonesByNameInput, optFns ...func(*route53.Options)) (*route53.ListHostedZonesByNameOutput, error)
 | 
					
						
							|  |  |  | 	ListResourceRecordSets(ctx context.Context, params *route53.ListResourceRecordSetsInput, optFns ...func(*route53.Options)) (*route53.ListResourceRecordSetsOutput, error)
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var f = func(ctx context.Context, cfgOpts []func(*config.LoadOptions) error, clientOpts []func(*route53.Options)) (route53Client, error) {
 | 
					
						
							|  |  |  | 	cfg, err := config.LoadDefaultConfig(ctx, cfgOpts...)
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		return nil, err
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	// If no region is specified, retrieve one from IMDS (SDK v1 used the AWS global partition as a fallback, v2 doesn't)
 | 
					
						
							|  |  |  | 	if cfg.Region == "" {
 | 
					
						
							|  |  |  | 		imdsClient := imds.NewFromConfig(cfg)
 | 
					
						
							|  |  |  | 		region, err := imdsClient.GetRegion(ctx, &imds.GetRegionInput{})
 | 
					
						
							|  |  |  | 		if err != nil {
 | 
					
						
							|  |  |  | 			return nil, fmt.Errorf("failed to get region from IMDS: %w", err)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		cfg.Region = region.Region
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return route53.NewFromConfig(cfg, clientOpts...), nil
 | 
					
						
							| 
									
										
										
										
											2018-01-15 09:59:29 -08:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-03 07:21:11 +01:00
										 |  |  | func setup(c *caddy.Controller) error {
 | 
					
						
							| 
									
										
										
										
											2019-07-04 00:44:31 +05:30
										 |  |  | 	for c.Next() {
 | 
					
						
							|  |  |  | 		keyPairs := map[string]struct{}{}
 | 
					
						
							|  |  |  | 		keys := map[string][]string{}
 | 
					
						
							| 
									
										
										
										
											2018-09-25 15:57:16 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-09 10:35:42 -07:00
										 |  |  | 		// Route53 plugin attempts to load AWS credentials following default SDK chaining.
 | 
					
						
							|  |  |  | 		// The order configuration is loaded in is:
 | 
					
						
							|  |  |  | 		// * Static AWS keys set in Corefile (deprecated)
 | 
					
						
							|  |  |  | 		// * Environment Variables
 | 
					
						
							|  |  |  | 		// * Shared Credentials file
 | 
					
						
							|  |  |  | 		// * Shared Configuration file (if AWS_SDK_LOAD_CONFIG is set to truthy value)
 | 
					
						
							|  |  |  | 		// * EC2 Instance Metadata (credentials only)
 | 
					
						
							| 
									
										
										
										
											2025-07-03 10:19:21 +01:00
										 |  |  | 		cfgOpts := []func(*config.LoadOptions) error{}
 | 
					
						
							|  |  |  | 		clientOpts := []func(*route53.Options){}
 | 
					
						
							| 
									
										
										
										
											2019-07-04 00:44:31 +05:30
										 |  |  | 		var fall fall.F
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-03 18:07:28 -07:00
										 |  |  | 		refresh := time.Duration(1) * time.Minute // default update frequency to 1 minute
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-15 09:59:29 -08:00
										 |  |  | 		args := c.RemainingArgs()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-29 03:50:55 +03:00
										 |  |  | 		for i := range args {
 | 
					
						
							| 
									
										
										
										
											2018-01-15 09:59:29 -08:00
										 |  |  | 			parts := strings.SplitN(args[i], ":", 2)
 | 
					
						
							|  |  |  | 			if len(parts) != 2 {
 | 
					
						
							| 
									
										
										
										
											2020-08-31 15:39:01 +02:00
										 |  |  | 				return plugin.Error("route53", c.Errf("invalid zone %q", args[i]))
 | 
					
						
							| 
									
										
										
										
											2018-01-15 09:59:29 -08:00
										 |  |  | 			}
 | 
					
						
							| 
									
										
										
										
											2018-10-10 10:55:54 -07:00
										 |  |  | 			dns, hostedZoneID := parts[0], parts[1]
 | 
					
						
							|  |  |  | 			if dns == "" || hostedZoneID == "" {
 | 
					
						
							| 
									
										
										
										
											2020-08-31 15:39:01 +02:00
										 |  |  | 				return plugin.Error("route53", c.Errf("invalid zone %q", args[i]))
 | 
					
						
							| 
									
										
										
										
											2018-01-15 09:59:29 -08:00
										 |  |  | 			}
 | 
					
						
							| 
									
										
										
										
											2018-10-10 10:55:54 -07:00
										 |  |  | 			if _, ok := keyPairs[args[i]]; ok {
 | 
					
						
							| 
									
										
										
										
											2020-08-31 15:39:01 +02:00
										 |  |  | 				return plugin.Error("route53", c.Errf("conflict zone %q", args[i]))
 | 
					
						
							| 
									
										
										
										
											2018-01-15 09:59:29 -08:00
										 |  |  | 			}
 | 
					
						
							| 
									
										
										
										
											2018-10-10 10:55:54 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			keyPairs[args[i]] = struct{}{}
 | 
					
						
							|  |  |  | 			keys[dns] = append(keys[dns], hostedZoneID)
 | 
					
						
							| 
									
										
										
										
											2018-01-15 09:59:29 -08:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		for c.NextBlock() {
 | 
					
						
							|  |  |  | 			switch c.Val() {
 | 
					
						
							|  |  |  | 			case "aws_access_key":
 | 
					
						
							|  |  |  | 				v := c.RemainingArgs()
 | 
					
						
							|  |  |  | 				if len(v) < 2 {
 | 
					
						
							| 
									
										
										
										
											2020-08-31 15:39:01 +02:00
										 |  |  | 					return plugin.Error("route53", c.Errf("invalid access key: '%v'", v))
 | 
					
						
							| 
									
										
										
										
											2018-01-15 09:59:29 -08:00
										 |  |  | 				}
 | 
					
						
							| 
									
										
										
										
											2025-07-03 10:19:21 +01:00
										 |  |  | 				cfgOpts = append(cfgOpts, config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(v[0], v[1], "")))
 | 
					
						
							| 
									
										
										
										
											2022-03-11 11:32:44 -08:00
										 |  |  | 				log.Warningf("Save aws_access_key in Corefile has been deprecated, please use other authentication methods instead")
 | 
					
						
							| 
									
										
										
										
											2021-11-08 14:45:45 +00:00
										 |  |  | 			case "aws_endpoint":
 | 
					
						
							|  |  |  | 				if c.NextArg() {
 | 
					
						
							| 
									
										
										
										
											2025-07-03 10:19:21 +01:00
										 |  |  | 					clientOpts = append(clientOpts, func(o *route53.Options) {
 | 
					
						
							|  |  |  | 						o.BaseEndpoint = aws.String(c.Val())
 | 
					
						
							|  |  |  | 					})
 | 
					
						
							| 
									
										
										
										
											2021-11-08 14:45:45 +00:00
										 |  |  | 				} else {
 | 
					
						
							|  |  |  | 					return plugin.Error("route53", c.ArgErr())
 | 
					
						
							|  |  |  | 				}
 | 
					
						
							| 
									
										
										
										
											2018-09-17 11:19:07 -07:00
										 |  |  | 			case "upstream":
 | 
					
						
							| 
									
										
										
										
											2019-01-13 16:54:49 +00:00
										 |  |  | 				c.RemainingArgs() // eats args
 | 
					
						
							| 
									
										
										
										
											2018-09-25 15:57:16 -07:00
										 |  |  | 			case "credentials":
 | 
					
						
							|  |  |  | 				if c.NextArg() {
 | 
					
						
							| 
									
										
										
										
											2025-07-03 10:19:21 +01:00
										 |  |  | 					cfgOpts = append(cfgOpts, config.WithSharedConfigProfile(c.Val()))
 | 
					
						
							| 
									
										
										
										
											2018-09-25 15:57:16 -07:00
										 |  |  | 				} else {
 | 
					
						
							|  |  |  | 					return c.ArgErr()
 | 
					
						
							|  |  |  | 				}
 | 
					
						
							|  |  |  | 				if c.NextArg() {
 | 
					
						
							| 
									
										
										
										
											2025-07-03 10:19:21 +01:00
										 |  |  | 					sharedConfigFiles := []string{c.Val()}
 | 
					
						
							| 
									
										
										
										
											2022-05-09 10:35:42 -07:00
										 |  |  | 					// If AWS_SDK_LOAD_CONFIG is set also load ~/.aws/config to stay consistent
 | 
					
						
							|  |  |  | 					// with default SDK behavior.
 | 
					
						
							|  |  |  | 					if ok, _ := strconv.ParseBool(os.Getenv("AWS_SDK_LOAD_CONFIG")); ok {
 | 
					
						
							| 
									
										
										
										
											2025-07-03 10:19:21 +01:00
										 |  |  | 						sharedConfigFiles = append(sharedConfigFiles, config.DefaultSharedConfigFilename())
 | 
					
						
							| 
									
										
										
										
											2022-05-09 10:35:42 -07:00
										 |  |  | 					}
 | 
					
						
							| 
									
										
										
										
											2025-07-03 10:19:21 +01:00
										 |  |  | 					cfgOpts = append(cfgOpts, config.WithSharedConfigFiles(sharedConfigFiles))
 | 
					
						
							| 
									
										
										
										
											2018-09-25 15:57:16 -07:00
										 |  |  | 				}
 | 
					
						
							| 
									
										
										
										
											2018-09-25 11:41:05 -07:00
										 |  |  | 			case "fallthrough":
 | 
					
						
							|  |  |  | 				fall.SetZonesFromArgs(c.RemainingArgs())
 | 
					
						
							| 
									
										
										
										
											2019-08-03 18:07:28 -07:00
										 |  |  | 			case "refresh":
 | 
					
						
							|  |  |  | 				if c.NextArg() {
 | 
					
						
							|  |  |  | 					refreshStr := c.Val()
 | 
					
						
							|  |  |  | 					_, err := strconv.Atoi(refreshStr)
 | 
					
						
							|  |  |  | 					if err == nil {
 | 
					
						
							|  |  |  | 						refreshStr = fmt.Sprintf("%ss", c.Val())
 | 
					
						
							|  |  |  | 					}
 | 
					
						
							|  |  |  | 					refresh, err = time.ParseDuration(refreshStr)
 | 
					
						
							|  |  |  | 					if err != nil {
 | 
					
						
							| 
									
										
										
										
											2020-08-31 15:39:01 +02:00
										 |  |  | 						return plugin.Error("route53", c.Errf("Unable to parse duration: %v", err))
 | 
					
						
							| 
									
										
										
										
											2019-08-03 18:07:28 -07:00
										 |  |  | 					}
 | 
					
						
							|  |  |  | 					if refresh <= 0 {
 | 
					
						
							| 
									
										
										
										
											2020-08-31 15:39:01 +02:00
										 |  |  | 						return plugin.Error("route53", c.Errf("refresh interval must be greater than 0: %q", refreshStr))
 | 
					
						
							| 
									
										
										
										
											2019-08-03 18:07:28 -07:00
										 |  |  | 					}
 | 
					
						
							|  |  |  | 				} else {
 | 
					
						
							| 
									
										
										
										
											2019-08-13 15:02:29 +00:00
										 |  |  | 					return plugin.Error("route53", c.ArgErr())
 | 
					
						
							| 
									
										
										
										
											2019-08-03 18:07:28 -07:00
										 |  |  | 				}
 | 
					
						
							| 
									
										
										
										
											2018-01-15 09:59:29 -08:00
										 |  |  | 			default:
 | 
					
						
							| 
									
										
										
										
											2020-08-31 15:39:01 +02:00
										 |  |  | 				return plugin.Error("route53", c.Errf("unknown property %q", c.Val()))
 | 
					
						
							| 
									
										
										
										
											2018-01-15 09:59:29 -08:00
										 |  |  | 			}
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2019-10-01 10:12:10 +03:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-24 05:37:01 -07:00
										 |  |  | 		ctx, cancel := context.WithCancel(context.Background())
 | 
					
						
							| 
									
										
										
										
											2025-07-03 10:19:21 +01:00
										 |  |  | 		client, err := f(ctx, cfgOpts, clientOpts)
 | 
					
						
							|  |  |  | 		if err != nil {
 | 
					
						
							|  |  |  | 			cancel()
 | 
					
						
							|  |  |  | 			return plugin.Error("route53", c.Errf("failed to create route53 client: %v", err))
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2019-08-13 15:02:29 +00:00
										 |  |  | 		h, err := New(ctx, client, keys, refresh)
 | 
					
						
							| 
									
										
										
										
											2019-07-04 00:44:31 +05:30
										 |  |  | 		if err != nil {
 | 
					
						
							| 
									
										
										
										
											2022-07-10 20:06:33 +02:00
										 |  |  | 			cancel()
 | 
					
						
							| 
									
										
										
										
											2020-08-31 15:39:01 +02:00
										 |  |  | 			return plugin.Error("route53", c.Errf("failed to create route53 plugin: %v", err))
 | 
					
						
							| 
									
										
										
										
											2019-07-04 00:44:31 +05:30
										 |  |  | 		}
 | 
					
						
							|  |  |  | 		h.Fall = fall
 | 
					
						
							|  |  |  | 		if err := h.Run(ctx); err != nil {
 | 
					
						
							| 
									
										
										
										
											2022-07-10 20:06:33 +02:00
										 |  |  | 			cancel()
 | 
					
						
							| 
									
										
										
										
											2020-08-31 15:39:01 +02:00
										 |  |  | 			return plugin.Error("route53", c.Errf("failed to initialize route53 plugin: %v", err))
 | 
					
						
							| 
									
										
										
										
											2019-07-04 00:44:31 +05:30
										 |  |  | 		}
 | 
					
						
							|  |  |  | 		dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
 | 
					
						
							|  |  |  | 			h.Next = next
 | 
					
						
							|  |  |  | 			return h
 | 
					
						
							|  |  |  | 		})
 | 
					
						
							| 
									
										
										
										
											2020-10-24 05:37:01 -07:00
										 |  |  | 		c.OnShutdown(func() error { cancel(); return nil })
 | 
					
						
							| 
									
										
										
										
											2018-01-15 09:59:29 -08:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 	return nil
 | 
					
						
							|  |  |  | }
 |