| 
									
										
										
										
											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"
 | 
					
						
							|  |  |  | 	"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
										 |  |  | 
 | 
					
						
							|  |  |  | 	"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
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/aws/aws-sdk-go/aws"
 | 
					
						
							|  |  |  | 	"github.com/aws/aws-sdk-go/aws/credentials"
 | 
					
						
							| 
									
										
										
										
											2019-07-15 08:56:28 +03:00
										 |  |  | 	"github.com/aws/aws-sdk-go/aws/credentials/ec2rolecreds"
 | 
					
						
							|  |  |  | 	"github.com/aws/aws-sdk-go/aws/ec2metadata"
 | 
					
						
							| 
									
										
										
										
											2018-01-15 09:59:29 -08:00
										 |  |  | 	"github.com/aws/aws-sdk-go/aws/session"
 | 
					
						
							|  |  |  | 	"github.com/aws/aws-sdk-go/service/route53"
 | 
					
						
							|  |  |  | 	"github.com/aws/aws-sdk-go/service/route53/route53iface"
 | 
					
						
							| 
									
										
										
										
											2019-07-03 09:04:47 +08:00
										 |  |  | 	"github.com/caddyserver/caddy"
 | 
					
						
							| 
									
										
										
										
											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
 | 
					
						
							|  |  |  | var f = func(credential *credentials.Credentials) route53iface.Route53API {
 | 
					
						
							|  |  |  | 	return route53.New(session.Must(session.NewSession(&aws.Config{Credentials: credential})))
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-04 00:44:31 +05:30
										 |  |  | 		// Route53 plugin attempts to find AWS credentials by using ChainCredentials.
 | 
					
						
							|  |  |  | 		// And the order of that provider chain is as follows:
 | 
					
						
							|  |  |  | 		// Static AWS keys -> Environment Variables -> Credentials file -> IAM role
 | 
					
						
							|  |  |  | 		// With that said, even though a user doesn't define any credentials in
 | 
					
						
							|  |  |  | 		// Corefile, we should still attempt to read the default credentials file,
 | 
					
						
							|  |  |  | 		// ~/.aws/credentials with the default profile.
 | 
					
						
							|  |  |  | 		sharedProvider := &credentials.SharedCredentialsProvider{}
 | 
					
						
							|  |  |  | 		var providers []credentials.Provider
 | 
					
						
							|  |  |  | 		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()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		for i := 0; i < len(args); i++ {
 | 
					
						
							|  |  |  | 			parts := strings.SplitN(args[i], ":", 2)
 | 
					
						
							|  |  |  | 			if len(parts) != 2 {
 | 
					
						
							| 
									
										
										
										
											2019-08-13 15:02:29 +00:00
										 |  |  | 				return plugin.Error("route53", c.Errf("invalid zone '%s'", 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 == "" {
 | 
					
						
							| 
									
										
										
										
											2019-08-13 15:02:29 +00:00
										 |  |  | 				return plugin.Error("route53", c.Errf("invalid zone '%s'", args[i]))
 | 
					
						
							| 
									
										
										
										
											2018-01-15 09:59:29 -08:00
										 |  |  | 			}
 | 
					
						
							| 
									
										
										
										
											2018-10-10 10:55:54 -07:00
										 |  |  | 			if _, ok := keyPairs[args[i]]; ok {
 | 
					
						
							| 
									
										
										
										
											2019-08-13 15:02:29 +00:00
										 |  |  | 				return plugin.Error("route53", c.Errf("conflict zone '%s'", 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 {
 | 
					
						
							| 
									
										
										
										
											2019-08-13 15:02:29 +00:00
										 |  |  | 					return plugin.Error("route53", c.Errf("invalid access key '%v'", v))
 | 
					
						
							| 
									
										
										
										
											2018-01-15 09:59:29 -08:00
										 |  |  | 				}
 | 
					
						
							| 
									
										
										
										
											2018-09-25 15:57:16 -07:00
										 |  |  | 				providers = append(providers, &credentials.StaticProvider{
 | 
					
						
							|  |  |  | 					Value: credentials.Value{
 | 
					
						
							|  |  |  | 						AccessKeyID:     v[0],
 | 
					
						
							|  |  |  | 						SecretAccessKey: v[1],
 | 
					
						
							|  |  |  | 					},
 | 
					
						
							|  |  |  | 				})
 | 
					
						
							| 
									
										
										
										
											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() {
 | 
					
						
							|  |  |  | 					sharedProvider.Profile = c.Val()
 | 
					
						
							|  |  |  | 				} else {
 | 
					
						
							|  |  |  | 					return c.ArgErr()
 | 
					
						
							|  |  |  | 				}
 | 
					
						
							|  |  |  | 				if c.NextArg() {
 | 
					
						
							|  |  |  | 					sharedProvider.Filename = c.Val()
 | 
					
						
							|  |  |  | 				}
 | 
					
						
							| 
									
										
										
										
											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 {
 | 
					
						
							| 
									
										
										
										
											2019-08-13 15:02:29 +00:00
										 |  |  | 						return plugin.Error("route53", c.Errf("Unable to parse duration: '%v'", err))
 | 
					
						
							| 
									
										
										
										
											2019-08-03 18:07:28 -07:00
										 |  |  | 					}
 | 
					
						
							|  |  |  | 					if refresh <= 0 {
 | 
					
						
							| 
									
										
										
										
											2019-08-13 15:02:29 +00:00
										 |  |  | 						return plugin.Error("route53", c.Errf("refresh interval must be greater than 0: %s", 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:
 | 
					
						
							| 
									
										
										
										
											2019-08-13 15:02:29 +00:00
										 |  |  | 				return plugin.Error("route53", c.Errf("unknown property '%s'", c.Val()))
 | 
					
						
							| 
									
										
										
										
											2018-01-15 09:59:29 -08:00
										 |  |  | 			}
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2019-10-01 10:12:10 +03:30
										 |  |  | 
 | 
					
						
							|  |  |  | 		session, err := session.NewSession(&aws.Config{})
 | 
					
						
							|  |  |  | 		if err != nil {
 | 
					
						
							|  |  |  | 			return plugin.Error("route53", err)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-15 08:56:28 +03:00
										 |  |  | 		providers = append(providers, &credentials.EnvProvider{}, sharedProvider, &ec2rolecreds.EC2RoleProvider{
 | 
					
						
							| 
									
										
										
										
											2019-10-01 10:12:10 +03:30
										 |  |  | 			Client: ec2metadata.New(session),
 | 
					
						
							| 
									
										
										
										
											2019-07-15 08:56:28 +03:00
										 |  |  | 		})
 | 
					
						
							| 
									
										
										
										
											2019-07-04 00:44:31 +05:30
										 |  |  | 		client := f(credentials.NewChainCredentials(providers))
 | 
					
						
							|  |  |  | 		ctx := context.Background()
 | 
					
						
							| 
									
										
										
										
											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 {
 | 
					
						
							| 
									
										
										
										
											2019-08-13 15:02:29 +00: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 {
 | 
					
						
							| 
									
										
										
										
											2019-08-13 15:02:29 +00: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
 | 
					
						
							|  |  |  | 		})
 | 
					
						
							| 
									
										
										
										
											2018-01-15 09:59:29 -08:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 	return nil
 | 
					
						
							|  |  |  | }
 |