From 40e9f05410e801222ce34d813debb6b75ed9d446 Mon Sep 17 00:00:00 2001 From: Zachary Seguin Date: Mon, 20 Mar 2023 22:42:21 -0400 Subject: [PATCH] Initial release --- .gitignore | 2 ++ cmd/bindify.go | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++ cmd/root.go | 43 ++++++++++++++++++++++ go.mod | 21 +++++++++++ go.sum | 33 +++++++++++++++++ main.go | 7 ++++ 6 files changed, 202 insertions(+) create mode 100644 .gitignore create mode 100644 cmd/bindify.go create mode 100644 cmd/root.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ac72ed6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.vscode/ +dnssync diff --git a/cmd/bindify.go b/cmd/bindify.go new file mode 100644 index 0000000..deeb130 --- /dev/null +++ b/cmd/bindify.go @@ -0,0 +1,96 @@ +package cmd + +import ( + "context" + "fmt" + "net" + "os" + "strings" + + "github.com/efficientip-labs/solidserver-go-client/sdsclient" + "github.com/spf13/cobra" + "golang.org/x/exp/slog" +) + +var log = slog.New(slog.NewTextHandler(os.Stderr)) + +var nameservers []string +var contact string +var apiUrl string + +// bindifyCmd represents the bindify command +var bindifyCmd = &cobra.Command{ + Use: "bindify", + Short: "Convert an IPAM zone to a Bind zone", + Args: cobra.ExactArgs(1), + Run: func(cmd *cobra.Command, args []string) { + zone := args[0] + + if len(nameservers) == 0 { + log.Error("There must be at least one nameserver defined.") + os.Exit(1) + } + + config := sdsclient.NewConfiguration() + config.Servers[0].URL = apiUrl + + ddi := sdsclient.NewAPIClient(config) + auth := context.WithValue(context.Background(), sdsclient.ContextBasicAuth, sdsclient.BasicAuth{ + UserName: os.Getenv("IPAM_USERNAME"), + Password: os.Getenv("IPAM_PASSWORD"), + }) + + // List zone records + rrsets, _, err := ddi.DnsApi.DnsRrList(auth).Where(fmt.Sprintf("zone_name='%s'", zone)).Orderby("rr_full_name").Limit(10000).Execute() + if err.Error() != "" { + log.Error("failed to list records", err, "zone", zone) + os.Exit(1) + } + + wroteNS := false + + for _, rrset := range *rrsets.Data { + switch *rrset.RrType { + case "NS": + if *rrset.RrFullName == zone { + // We replace the NS record with our own. + if !wroteNS { + for _, nameserver := range nameservers { + fmt.Printf("%s. %s IN %s %s.\n", *rrset.RrFullName, *rrset.RrTtl, *rrset.RrType, nameserver) + } + + wroteNS = true + } + } else { + fmt.Printf("%s. %s IN %s %s.\n", *rrset.RrFullName, *rrset.RrTtl, *rrset.RrType, *rrset.RrValue1) + } + case "CNAME": + fmt.Printf("%s. %s IN %s %s.\n", *rrset.RrFullName, *rrset.RrTtl, *rrset.RrType, *rrset.RrValue1) + case "SOA": + // We replace the nameserver and contact name in the SOA. + fmt.Printf("%s. %s IN %s %s. %s. %s %s %s %s %s\n", *rrset.RrFullName, *rrset.RrTtl, *rrset.RrType, nameservers[0], strings.ReplaceAll(contact, "@", "."), *rrset.RrValue3, *rrset.RrValue4, *rrset.RrValue5, *rrset.RrValue6, *rrset.RrValue7) + case "TXT": + fmt.Printf("%s. %s IN %s %q\n", *rrset.RrFullName, *rrset.RrTtl, *rrset.RrType, *rrset.RrValue1) + case "MX": + fmt.Printf("%s. %s IN %s %s %s.\n", *rrset.RrFullName, *rrset.RrTtl, *rrset.RrType, *rrset.RrValue1, *rrset.RrValue2) + case "SRV": + fmt.Printf("%s. %s IN %s %s %s %s %s.\n", *rrset.RrFullName, *rrset.RrTtl, *rrset.RrType, *rrset.RrValue1, *rrset.RrValue2, *rrset.RrValue3, *rrset.RrValue4) + case "AAAA": + ip := net.ParseIP(*rrset.RrValue1) + fmt.Printf("%s. %s IN %s %s\n", *rrset.RrFullName, *rrset.RrTtl, *rrset.RrType, ip.String()) + default: + fmt.Printf("%s. %s IN %s %s\n", *rrset.RrFullName, *rrset.RrTtl, *rrset.RrType, *rrset.RrValue1) + } + + } + }, +} + +func init() { + rootCmd.AddCommand(bindifyCmd) + + bindifyCmd.Flags().StringVar(&apiUrl, "api-url", "https://ipam.private.uwaterloo.ca/api/v2.0", "Base URL for the IPAM system.") + + bindifyCmd.Flags().StringSliceVar(&nameservers, "nameservers", []string{"ext-dns1.csclub.uwaterloo.ca", "ext-dns2.csclub.uwaterloo.ca"}, "Nameservers for this zone.") + bindifyCmd.Flags().StringVar(&contact, "contact", "systems-committee@csclub.uwaterloo.ca", "Contact for the SOA record.") +} diff --git a/cmd/root.go b/cmd/root.go new file mode 100644 index 0000000..0c874f5 --- /dev/null +++ b/cmd/root.go @@ -0,0 +1,43 @@ +package cmd + +import ( + "os" + + "github.com/spf13/cobra" +) + +// rootCmd represents the base command when called without any subcommands +var rootCmd = &cobra.Command{ + Use: "dnssync", + Short: "A brief description of your application", + Long: `A longer description that spans multiple lines and likely contains +examples and usage of using your application. For example: + +Cobra is a CLI library for Go that empowers applications. +This application is a tool to generate the needed files +to quickly create a Cobra application.`, + // Uncomment the following line if your bare application + // has an action associated with it: + // Run: func(cmd *cobra.Command, args []string) { }, +} + +// Execute adds all child commands to the root command and sets flags appropriately. +// This is called by main.main(). It only needs to happen once to the rootCmd. +func Execute() { + err := rootCmd.Execute() + if err != nil { + os.Exit(1) + } +} + +func init() { + // Here you will define your flags and configuration settings. + // Cobra supports persistent flags, which, if defined here, + // will be global for your application. + + // rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.dnssync.yaml)") + + // Cobra also supports local flags, which will only run + // when this action is called directly. + rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..683230b --- /dev/null +++ b/go.mod @@ -0,0 +1,21 @@ +module git.csclub.uwaterloo.ca/public/dnssync + +go 1.20 + +require ( + github.com/efficientip-labs/solidserver-go-client/sdsclient v0.0.0-20211104144013-8c21394c8e32 + github.com/spf13/cobra v1.6.1 + golang.org/x/exp v0.0.0-20230315142452-642cacee5cc0 +) + +require ( + github.com/go-logr/logr v1.2.0 // indirect + github.com/golang/protobuf v1.2.0 // indirect + github.com/inconshreveable/mousetrap v1.0.1 // indirect + github.com/spf13/pflag v1.0.5 // indirect + golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e // indirect + golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45 // indirect + google.golang.org/appengine v1.4.0 // indirect + k8s.io/klog v1.0.0 // indirect + k8s.io/klog/v2 v2.90.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..5de272b --- /dev/null +++ b/go.sum @@ -0,0 +1,33 @@ +cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= +github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/efficientip-labs/solidserver-go-client/sdsclient v0.0.0-20211104144013-8c21394c8e32 h1:+AB2gXIeQG5uNy41G9LYTZDI4qjRut+1KsqW41+3DAU= +github.com/efficientip-labs/solidserver-go-client/sdsclient v0.0.0-20211104144013-8c21394c8e32/go.mod h1:g3w+I5FpUdOvvaIQESCbJvaWU/o1IXX1YhfSzfCnqhg= +github.com/go-logr/logr v0.1.0/go.mod h1:ixOQHD9gLJUVQQ2ZOR7zLEifBX6tGkNJF4QyIY7sIas= +github.com/go-logr/logr v1.2.0 h1:QK40JKJyMdUDz+h+xvCsru/bJhvG0UxvePV0ufL/AcE= +github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= +github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc= +github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/spf13/cobra v1.6.1 h1:o94oiPyS4KD1mPy2fmcYYHHfCxLqYjJOhGsCHFZtEzA= +github.com/spf13/cobra v1.6.1/go.mod h1:IOw/AERYS7UzyrGinqmz6HLUo219MORXGxhbaJUqzrY= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +golang.org/x/exp v0.0.0-20230315142452-642cacee5cc0 h1:pVgRXcIictcr+lBQIFeiwuwtDIs4eL21OuM9nyAADmo= +golang.org/x/exp v0.0.0-20230315142452-642cacee5cc0/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc= +golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e h1:bRhVy7zSSasaqNksaRZiA5EEI+Ei4I1nO5Jh72wfHlg= +golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45 h1:SVwTIAaPC2U/AvvLNZ2a7OVsmBpC8L5BlwK1whH3hm0= +golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= +golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +google.golang.org/appengine v1.4.0 h1:/wp5JvzpHIxhs/dumFmF7BXTf3Z+dd4uXta4kVyO508= +google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +k8s.io/klog v1.0.0 h1:Pt+yjF5aB1xDSVbau4VsWe+dQNzA0qv1LlXdC2dF6Q8= +k8s.io/klog v1.0.0/go.mod h1:4Bi6QPql/J/LkTDqv7R/cd3hPo4k2DG6Ptcz060Ez5I= +k8s.io/klog/v2 v2.90.0 h1:VkTxIV/FjRXn1fgNNcKGM8cfmL1Z33ZjXRTVxKCoF5M= +k8s.io/klog/v2 v2.90.0/go.mod h1:y1WjHnz7Dj687irZUWR/WLkLc5N1YHtjLdmgWjndZn0= diff --git a/main.go b/main.go new file mode 100644 index 0000000..3fb8937 --- /dev/null +++ b/main.go @@ -0,0 +1,7 @@ +package main + +import "git.csclub.uwaterloo.ca/public/dnssync/cmd" + +func main() { + cmd.Execute() +}