Files
coredns/plugin/route53/setup.go

171 lines
5.7 KiB
Go
Raw Normal View History

package route53
import (
"context"
"fmt"
"os"
"strconv"
"strings"
"time"
"github.com/coredns/caddy"
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/pkg/fall"
clog "github.com/coredns/coredns/plugin/pkg/log"
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"
)
var log = clog.NewWithPlugin("route53")
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
}
func setup(c *caddy.Controller) error {
for c.Next() {
keyPairs := map[string]struct{}{}
keys := map[string][]string{}
// 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){}
var fall fall.F
refresh := time.Duration(1) * time.Minute // default update frequency to 1 minute
args := c.RemainingArgs()
for i := range args {
parts := strings.SplitN(args[i], ":", 2)
if len(parts) != 2 {
return plugin.Error("route53", c.Errf("invalid zone %q", args[i]))
}
dns, hostedZoneID := parts[0], parts[1]
if dns == "" || hostedZoneID == "" {
return plugin.Error("route53", c.Errf("invalid zone %q", args[i]))
}
if _, ok := keyPairs[args[i]]; ok {
return plugin.Error("route53", c.Errf("conflict zone %q", args[i]))
}
keyPairs[args[i]] = struct{}{}
keys[dns] = append(keys[dns], hostedZoneID)
}
for c.NextBlock() {
switch c.Val() {
case "aws_access_key":
v := c.RemainingArgs()
if len(v) < 2 {
return plugin.Error("route53", c.Errf("invalid access key: '%v'", v))
}
2025-07-03 10:19:21 +01:00
cfgOpts = append(cfgOpts, config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(v[0], v[1], "")))
log.Warningf("Save aws_access_key in Corefile has been deprecated, please use other authentication methods instead")
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())
})
} else {
return plugin.Error("route53", c.ArgErr())
}
case "upstream":
Default to upstream to self (#2436) * Default to upstream to self This is a backwards incompatible change. This is a massive (cleanup) PR where we default to resolving external names by the coredns process itself, instead of directly forwarding them to some upstream. This ignores any arguments `upstream` may have had and makes it depend on proxy/forward configuration in the Corefile. This allows resolved upstream names to be cached and we have better healthchecking of the upstreams. It also means there is only one way to resolve names, by either using the proxy or forward plugin. The proxy/forward lookup.go functions have been removed. This also lessen the dependency on proxy, meaning deprecating proxy will become easier. Some tests have been removed as well, or moved to the top-level test directory as they now require a full coredns process instead of just the plugin. For the etcd plugin, the entire StubZone resolving is *dropped*! This was a hacky (but working) solution to say the least. If someone cares deeply it can be brought back (maybe)? The pkg/upstream is now very small and almost does nothing. Also the New() function was changed to return a pointer to upstream.Upstream. It also returns only one parameter, so any stragglers using it will encounter a compile error. All documentation has been adapted. This affected the following plugins: * etcd * file * auto * secondary * federation * template * route53 A followup PR will make any upstream directives with arguments an error, right now they are ignored. Signed-off-by: Miek Gieben <miek@miek.nl> * Fix etcd build - probably still fails unit test Signed-off-by: Miek Gieben <miek@miek.nl> * Slightly smarter lookup check in upstream Signed-off-by: Miek Gieben <miek@miek.nl> * Compilez Signed-off-by: Miek Gieben <miek@miek.nl>
2019-01-13 16:54:49 +00:00
c.RemainingArgs() // eats args
case "credentials":
if c.NextArg() {
2025-07-03 10:19:21 +01:00
cfgOpts = append(cfgOpts, config.WithSharedConfigProfile(c.Val()))
} else {
return c.ArgErr()
}
if c.NextArg() {
2025-07-03 10:19:21 +01:00
sharedConfigFiles := []string{c.Val()}
// 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())
}
2025-07-03 10:19:21 +01:00
cfgOpts = append(cfgOpts, config.WithSharedConfigFiles(sharedConfigFiles))
}
case "fallthrough":
fall.SetZonesFromArgs(c.RemainingArgs())
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 {
return plugin.Error("route53", c.Errf("Unable to parse duration: %v", err))
}
if refresh <= 0 {
return plugin.Error("route53", c.Errf("refresh interval must be greater than 0: %q", refreshStr))
}
} else {
return plugin.Error("route53", c.ArgErr())
}
default:
return plugin.Error("route53", c.Errf("unknown property %q", c.Val()))
}
}
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))
}
h, err := New(ctx, client, keys, refresh)
if err != nil {
2022-07-10 20:06:33 +02:00
cancel()
return plugin.Error("route53", c.Errf("failed to create route53 plugin: %v", err))
}
h.Fall = fall
if err := h.Run(ctx); err != nil {
2022-07-10 20:06:33 +02:00
cancel()
return plugin.Error("route53", c.Errf("failed to initialize route53 plugin: %v", err))
}
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
h.Next = next
return h
})
c.OnShutdown(func() error { cancel(); return nil })
}
return nil
}