├── CHANGELOG.md ├── .gitignore ├── internal └── cli │ ├── commands │ ├── commands.go │ ├── version.go │ ├── dropdb.go │ ├── createdb.go │ ├── del.go │ ├── get.go │ └── put.go │ └── cli.go ├── cmd └── ten34 │ └── main.go ├── go.mod ├── pkg └── client │ ├── backend │ ├── backend.go │ └── route53.go │ └── client.go ├── Makefile ├── LICENSE.txt ├── README.md ├── CODE_OF_CONDUCT.md └── go.sum /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .env 2 | /bin/ 3 | /dist/ 4 | /tmp/ 5 | /vendor/ -------------------------------------------------------------------------------- /internal/cli/commands/commands.go: -------------------------------------------------------------------------------- 1 | package commands 2 | -------------------------------------------------------------------------------- /cmd/ten34/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "github.com/craftyphotons/ten34/internal/cli" 5 | ) 6 | 7 | func main() { 8 | cli.Start() 9 | } 10 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/craftyphotons/ten34 2 | 3 | go 1.13 4 | 5 | require ( 6 | github.com/aws/aws-sdk-go v1.25.6 7 | github.com/spf13/cobra v0.0.5 8 | golang.org/x/net v0.0.0-20191003171128-d98b1b443823 // indirect 9 | ) 10 | -------------------------------------------------------------------------------- /internal/cli/commands/version.go: -------------------------------------------------------------------------------- 1 | package commands 2 | 3 | import "github.com/spf13/cobra" 4 | 5 | // NewVersionCommand returns the "version" command 6 | func NewVersionCommand() *cobra.Command { 7 | cmd := &cobra.Command{ 8 | Use: "version", 9 | Short: "Prints version information", 10 | Run: versionCommandFunc, 11 | } 12 | 13 | return cmd 14 | } 15 | 16 | func versionCommandFunc(cmd *cobra.Command, args []string) { 17 | } 18 | -------------------------------------------------------------------------------- /pkg/client/backend/backend.go: -------------------------------------------------------------------------------- 1 | package backend 2 | 3 | import ( 4 | "net/url" 5 | ) 6 | 7 | // Backend provides a database backend implementation 8 | type Backend interface { 9 | // Setup sets up a backend session 10 | Setup(db url.URL) error 11 | 12 | // CreateDB creates a database 13 | CreateDB(db url.URL) error 14 | 15 | // DropDB deletes a database 16 | DropDB(db url.URL) error 17 | 18 | // Delete deletes a key from the database 19 | Delete(db url.URL, key string) error 20 | 21 | // Get retrieves keys from the database 22 | Get(db url.URL, key string) (string, error) 23 | 24 | // Put writes a key-value pair to the database 25 | Put(db url.URL, key, val string) error 26 | } 27 | -------------------------------------------------------------------------------- /internal/cli/cli.go: -------------------------------------------------------------------------------- 1 | package cli 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | 7 | "github.com/craftyphotons/ten34/internal/cli/commands" 8 | 9 | "github.com/spf13/cobra" 10 | ) 11 | 12 | var rootCmd = &cobra.Command{ 13 | Use: "ten34", 14 | Short: "DNS as a key-value database", 15 | } 16 | 17 | func init() { 18 | rootCmd.AddCommand( 19 | commands.NewCreatedbCommand(), 20 | commands.NewDropdbCommand(), 21 | commands.NewDelCommand(), 22 | commands.NewGetCommand(), 23 | commands.NewPutCommand(), 24 | commands.NewVersionCommand(), 25 | ) 26 | } 27 | 28 | // Start starts the ten34 CLI 29 | func Start() { 30 | if err := rootCmd.Execute(); err != nil { 31 | fmt.Println(err) 32 | os.Exit(1) 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /internal/cli/commands/dropdb.go: -------------------------------------------------------------------------------- 1 | package commands 2 | 3 | import ( 4 | "fmt" 5 | "net/url" 6 | "os" 7 | 8 | "github.com/craftyphotons/ten34/pkg/client" 9 | "github.com/spf13/cobra" 10 | ) 11 | 12 | // NewDropdbCommand returns the "dropdb" command 13 | func NewDropdbCommand() *cobra.Command { 14 | cmd := &cobra.Command{ 15 | Use: "dropdb [options] ", 16 | Short: "Deletes a database", 17 | Run: dropdbCommandFunc, 18 | } 19 | 20 | return cmd 21 | } 22 | 23 | func dropdbCommandFunc(cmd *cobra.Command, args []string) { 24 | db, err := url.Parse(args[0]) 25 | if err != nil { 26 | fmt.Println(err) 27 | os.Exit(1) 28 | } 29 | 30 | client, err := client.New(*db) 31 | if err != nil { 32 | fmt.Println(err) 33 | os.Exit(1) 34 | } 35 | 36 | err = client.DropDB() 37 | if err != nil { 38 | fmt.Println(err) 39 | os.Exit(1) 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /internal/cli/commands/createdb.go: -------------------------------------------------------------------------------- 1 | package commands 2 | 3 | import ( 4 | "fmt" 5 | "net/url" 6 | "os" 7 | 8 | "github.com/craftyphotons/ten34/pkg/client" 9 | "github.com/spf13/cobra" 10 | ) 11 | 12 | // NewCreatedbCommand returns the "createdb" command 13 | func NewCreatedbCommand() *cobra.Command { 14 | cmd := &cobra.Command{ 15 | Use: "createdb [options] ", 16 | Short: "Creates a database", 17 | Run: createdbCommandFunc, 18 | } 19 | 20 | return cmd 21 | } 22 | 23 | func createdbCommandFunc(cmd *cobra.Command, args []string) { 24 | db, err := url.Parse(args[0]) 25 | if err != nil { 26 | fmt.Println(err) 27 | os.Exit(1) 28 | } 29 | 30 | client, err := client.New(*db) 31 | if err != nil { 32 | fmt.Println(err) 33 | os.Exit(1) 34 | } 35 | 36 | err = client.CreateDB() 37 | if err != nil { 38 | fmt.Println(err) 39 | os.Exit(1) 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | export GOOS=$(shell go env GOOS) 2 | export GO_BUILD=env GO11MODULE=on go build -ldflags="-s -w" 3 | export GO_INSTALL=env GO11MODULE=on go install 4 | export GO_TEST=env GOTRACEBACK=all GO11MODULE=on go test 5 | export GO_VET=env GO11MODULE=on go vet 6 | export GO_RUN=env GO11MODULE=on go run 7 | export PATH := $(PWD)/bin/$(GOOS):$(PATH) 8 | 9 | SOURCES := $(shell find . -name '*.go' -not -name '*_test.go') go.mod go.sum 10 | SOURCES_NO_VENDOR := $(shell find . -path ./vendor -prune -o -name "*.go" -not -name '*_test.go' -print) 11 | 12 | all: clean vet test build 13 | 14 | bench: 15 | $(GO_TEST) -bench=. -run=^$$ ./... 16 | 17 | build: $(SOURCES) 18 | $(GO_BUILD) -o bin/ten34 cmd/ten34/main.go 19 | 20 | clean: 21 | $(RM) -r bin 22 | $(RM) -r dist 23 | 24 | fmt: $(SOURCES_NO_VENDOR) 25 | gofmt -w -s $^ 26 | 27 | test: 28 | $(GO_TEST) ./... 29 | 30 | tidy: 31 | GO11MODULE=on go mod tidy 32 | 33 | vet: 34 | $(GO_VET) -v ./... 35 | 36 | .PHONY: all bench clean fmt test tidy -------------------------------------------------------------------------------- /internal/cli/commands/del.go: -------------------------------------------------------------------------------- 1 | package commands 2 | 3 | import ( 4 | "fmt" 5 | "net/url" 6 | "os" 7 | 8 | "github.com/craftyphotons/ten34/pkg/client" 9 | "github.com/spf13/cobra" 10 | ) 11 | 12 | var delDatabase string 13 | 14 | // NewDelCommand returns the "del" command 15 | func NewDelCommand() *cobra.Command { 16 | cmd := &cobra.Command{ 17 | Use: "del [options] ", 18 | Short: "Deletes the specified key", 19 | Run: delCommandFunc, 20 | } 21 | 22 | cmd.Flags().StringVarP(&delDatabase, "database", "d", "", "URI of the database") 23 | 24 | return cmd 25 | } 26 | 27 | func delCommandFunc(cmd *cobra.Command, args []string) { 28 | key := args[0] 29 | 30 | db, err := url.Parse(delDatabase) 31 | if err != nil { 32 | fmt.Println(err) 33 | os.Exit(1) 34 | } 35 | 36 | client, err := client.New(*db) 37 | if err != nil { 38 | fmt.Println(err) 39 | os.Exit(1) 40 | } 41 | 42 | err = client.Delete(key) 43 | if err != nil { 44 | fmt.Println(err) 45 | os.Exit(1) 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /internal/cli/commands/get.go: -------------------------------------------------------------------------------- 1 | package commands 2 | 3 | import ( 4 | "fmt" 5 | "net/url" 6 | "os" 7 | 8 | "github.com/craftyphotons/ten34/pkg/client" 9 | "github.com/spf13/cobra" 10 | ) 11 | 12 | var getDatabase string 13 | 14 | // NewGetCommand returns the "get" command 15 | func NewGetCommand() *cobra.Command { 16 | cmd := &cobra.Command{ 17 | Use: "get [options] ", 18 | Short: "Gets the specified key", 19 | Run: getCommandFunc, 20 | } 21 | 22 | cmd.Flags().StringVarP(&getDatabase, "database", "d", "", "URI of the database") 23 | 24 | return cmd 25 | } 26 | 27 | func getCommandFunc(cmd *cobra.Command, args []string) { 28 | key := args[0] 29 | 30 | db, err := url.Parse(getDatabase) 31 | if err != nil { 32 | fmt.Println(err) 33 | os.Exit(1) 34 | } 35 | 36 | client, err := client.New(*db) 37 | if err != nil { 38 | fmt.Println(err) 39 | os.Exit(1) 40 | } 41 | 42 | values, err := client.Get(key) 43 | if err != nil { 44 | fmt.Println(err) 45 | os.Exit(1) 46 | } 47 | fmt.Println(values) 48 | } 49 | -------------------------------------------------------------------------------- /internal/cli/commands/put.go: -------------------------------------------------------------------------------- 1 | package commands 2 | 3 | import ( 4 | "fmt" 5 | "net/url" 6 | "os" 7 | 8 | "github.com/craftyphotons/ten34/pkg/client" 9 | "github.com/spf13/cobra" 10 | ) 11 | 12 | var putDatabase string 13 | 14 | // NewPutCommand returns the "put" command 15 | func NewPutCommand() *cobra.Command { 16 | cmd := &cobra.Command{ 17 | Use: "put [options] ", 18 | Short: "Writes the specified key", 19 | Run: putCommandFunc, 20 | } 21 | 22 | cmd.Flags().StringVarP(&putDatabase, "database", "d", "", "URI of the database") 23 | 24 | return cmd 25 | } 26 | 27 | func putCommandFunc(cmd *cobra.Command, args []string) { 28 | key := args[0] 29 | val := args[1] 30 | 31 | db, err := url.Parse(putDatabase) 32 | if err != nil { 33 | fmt.Println(err) 34 | os.Exit(1) 35 | } 36 | 37 | client, err := client.New(*db) 38 | if err != nil { 39 | fmt.Println(err) 40 | os.Exit(1) 41 | } 42 | 43 | err = client.Put(key, val) 44 | if err != nil { 45 | fmt.Println(err) 46 | os.Exit(1) 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2019 Tony Burns 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /pkg/client/client.go: -------------------------------------------------------------------------------- 1 | package client 2 | 3 | import ( 4 | "net/url" 5 | 6 | "github.com/craftyphotons/ten34/pkg/client/backend" 7 | ) 8 | 9 | const ( 10 | // BackendSchemeRoute53 is the scheme to use for the Route53 backend 11 | BackendSchemeRoute53 = "route53" 12 | ) 13 | 14 | // Client provides a database client session 15 | type Client struct { 16 | // Backend is the database backend 17 | Backend backend.Backend 18 | 19 | // URI is the uniform resource identifier of the database 20 | URI url.URL 21 | } 22 | 23 | // New creates a client for a new database session 24 | func New(uri url.URL) (*Client, error) { 25 | scheme := uri.Scheme 26 | client := &Client{URI: uri} 27 | 28 | switch scheme { 29 | case BackendSchemeRoute53: 30 | client.Backend = backend.NewRoute53(uri) 31 | } 32 | 33 | err := client.Backend.Setup(uri) 34 | 35 | return client, err 36 | } 37 | 38 | // CreateDB creates a database 39 | func (c *Client) CreateDB() error { 40 | return c.Backend.CreateDB(c.URI) 41 | } 42 | 43 | // DropDB deletes a database 44 | func (c *Client) DropDB() error { 45 | return c.Backend.DropDB(c.URI) 46 | } 47 | 48 | // Delete deletes a key from the database 49 | func (c *Client) Delete(key string) error { 50 | return c.Backend.Delete(c.URI, key) 51 | } 52 | 53 | // Get retrieves keys from the database 54 | func (c *Client) Get(key string) (string, error) { 55 | return c.Backend.Get(c.URI, key) 56 | } 57 | 58 | // Put writes a key-value pair to the database 59 | func (c *Client) Put(key, val string) error { 60 | return c.Backend.Put(c.URI, key, val) 61 | } 62 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ten34 2 | 3 | A globally-distributed, eventually-consistent, 100% available key-value store. 4 | 5 | > Route 53 isn't really a database, but then again, neither is Redis. 6 | 7 | _[Corey Quinn](https://twitter.com/QuinnyPig/status/1173371936342044672)_ 8 | 9 | ## Usage 10 | 11 | ### Creating a database 12 | 13 | ```shell 14 | ten34 createdb route53://my.db 15 | ``` 16 | 17 | ### Deleting a database 18 | 19 | ```shell 20 | ten34 dropdb route53://my.db 21 | ``` 22 | 23 | ### Setting a key 24 | 25 | ```shell 26 | ten34 -d route53://my.db put foo bar 27 | ``` 28 | 29 | ### Getting a key 30 | 31 | ```shell 32 | ten34 -d route53://my.db get foo 33 | ``` 34 | 35 | ### Deleting a key 36 | 37 | ```shell 38 | ten34 -d route53://my.db del foo -d 39 | ``` 40 | 41 | ## Development 42 | 43 | $ make build 44 | 45 | ## Contributing 46 | 47 | Bug reports and pull requests are welcome on GitHub at https://github.com/craftyphotons/ten34. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct. 48 | 49 | ## License 50 | 51 | The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT). 52 | 53 | ## Code of Conduct 54 | 55 | Everyone interacting in the ten34 project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/craftyphotons/ten34/blob/master/CODE_OF_CONDUCT.md). 56 | 57 | ## Additional Disclaimer 58 | 59 | In addition to the terms of the MIT license, this project and its maintainers shall not be held responsible for costs and repercussions resulting from its use. This includes but is not limited to account closure by your cloud service provider for violating their terms of service and the disappointment of your peers for usage of this project for your actual database. -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to making participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, gender identity and expression, level of experience, 9 | nationality, personal appearance, race, religion, or sexual identity and 10 | orientation. 11 | 12 | ## Our Standards 13 | 14 | Examples of behavior that contributes to creating a positive environment 15 | include: 16 | 17 | * Using welcoming and inclusive language 18 | * Being respectful of differing viewpoints and experiences 19 | * Gracefully accepting constructive criticism 20 | * Focusing on what is best for the community 21 | * Showing empathy towards other community members 22 | 23 | Examples of unacceptable behavior by participants include: 24 | 25 | * The use of sexualized language or imagery and unwelcome sexual attention or 26 | advances 27 | * Trolling, insulting/derogatory comments, and personal or political attacks 28 | * Public or private harassment 29 | * Publishing others' private information, such as a physical or electronic 30 | address, without explicit permission 31 | * Other conduct which could reasonably be considered inappropriate in a 32 | professional setting 33 | 34 | ## Our Responsibilities 35 | 36 | Project maintainers are responsible for clarifying the standards of acceptable 37 | behavior and are expected to take appropriate and fair corrective action in 38 | response to any instances of unacceptable behavior. 39 | 40 | Project maintainers have the right and responsibility to remove, edit, or 41 | reject comments, commits, code, wiki edits, issues, and other contributions 42 | that are not aligned to this Code of Conduct, or to ban temporarily or 43 | permanently any contributor for other behaviors that they deem inappropriate, 44 | threatening, offensive, or harmful. 45 | 46 | ## Scope 47 | 48 | This Code of Conduct applies both within project spaces and in public spaces 49 | when an individual is representing the project or its community. Examples of 50 | representing a project or community include using an official project e-mail 51 | address, posting via an official social media account, or acting as an appointed 52 | representative at an online or offline event. Representation of a project may be 53 | further defined and clarified by project maintainers. 54 | 55 | ## Enforcement 56 | 57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 58 | reported by contacting the project team at tony@tonyburns.net. All 59 | complaints will be reviewed and investigated and will result in a response that 60 | is deemed necessary and appropriate to the circumstances. The project team is 61 | obligated to maintain confidentiality with regard to the reporter of an incident. 62 | Further details of specific enforcement policies may be posted separately. 63 | 64 | Project maintainers who do not follow or enforce the Code of Conduct in good 65 | faith may face temporary or permanent repercussions as determined by other 66 | members of the project's leadership. 67 | 68 | ## Attribution 69 | 70 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, 71 | available at [http://contributor-covenant.org/version/1/4][version] 72 | 73 | [homepage]: http://contributor-covenant.org 74 | [version]: http://contributor-covenant.org/version/1/4/ 75 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= 2 | github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8= 3 | github.com/aws/aws-sdk-go v1.25.6 h1:Rmg2pgKXoCfNe0KQb4LNSNmHqMdcgBjpMeXK9IjHWq8= 4 | github.com/aws/aws-sdk-go v1.25.6/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo= 5 | github.com/coreos/etcd v3.3.10+incompatible h1:jFneRYjIvLMLhDLCzuTuU4rSJUjRplcJQ7pD7MnhC04= 6 | github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE= 7 | github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk= 8 | github.com/coreos/go-semver v0.2.0 h1:3Jm3tLmsgAYcjC+4Up7hJrFBPr+n7rAqYeSw/SZazuY= 9 | github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= 10 | github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE= 11 | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 12 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 13 | github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= 14 | github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= 15 | github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= 16 | github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= 17 | github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af h1:pmfjZENx5imkbgOkpRUYLnmbU7UEFbjtDA2hxJ1ichM= 18 | github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= 19 | github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= 20 | github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= 21 | github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= 22 | github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= 23 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 24 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 25 | github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g= 26 | github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ= 27 | github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= 28 | github.com/spf13/cobra v0.0.5 h1:f0B+LkLX6DtmRH1isoNA9VTtNUK9K8xYd28JNNfOv/s= 29 | github.com/spf13/cobra v0.0.5/go.mod h1:3K3wKZymM7VvHMDS9+Akkh4K60UwM26emMESw8tLCHU= 30 | github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo= 31 | github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg= 32 | github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= 33 | github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s= 34 | github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= 35 | github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= 36 | github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8 h1:3SVOIvH7Ae1KRYyQWRjXWJEA9sS/c/pjvH++55Gr648= 37 | github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0= 38 | github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= 39 | golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= 40 | golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= 41 | golang.org/x/net v0.0.0-20191003171128-d98b1b443823 h1:Ypyv6BNJh07T1pUSrehkLemqPKXhus2MkfktJ91kRh4= 42 | golang.org/x/net v0.0.0-20191003171128-d98b1b443823/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= 43 | golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 44 | golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 45 | golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= 46 | golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= 47 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 48 | gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 49 | -------------------------------------------------------------------------------- /pkg/client/backend/route53.go: -------------------------------------------------------------------------------- 1 | package backend 2 | 3 | import ( 4 | "fmt" 5 | "net" 6 | "net/url" 7 | "strings" 8 | "time" 9 | 10 | "github.com/aws/aws-sdk-go/aws" 11 | "github.com/aws/aws-sdk-go/aws/awserr" 12 | "github.com/aws/aws-sdk-go/aws/session" 13 | "github.com/aws/aws-sdk-go/service/route53" 14 | ) 15 | 16 | // Route53 implements a key-value store on top of AWS Route53 hosted zones and DNS 17 | type Route53 struct { 18 | // HostedZoneID is the ID of the Route53 hosted zone 19 | HostedZoneID string 20 | 21 | // HosteddZoneName is the name of the Route53 hosted zone 22 | HostedZoneName string 23 | 24 | // URI is the uniform resource identifier of the database 25 | URI url.URL 26 | 27 | client *route53.Route53 28 | } 29 | 30 | // NewRoute53 creates a new Route53 database backend session 31 | func NewRoute53(uri url.URL) *Route53 { 32 | backend := &Route53{ 33 | HostedZoneID: "", 34 | HostedZoneName: "", 35 | URI: uri, 36 | } 37 | return backend 38 | } 39 | 40 | // Setup sets up a backend session 41 | func (be *Route53) Setup(uri url.URL) error { 42 | be.HostedZoneName = fmt.Sprintf("%s.", be.URI.Host) 43 | 44 | sess, err := session.NewSession() 45 | if err != nil { 46 | fmt.Println("failed to create session") 47 | return err 48 | } 49 | 50 | be.client = route53.New(sess) 51 | 52 | return nil 53 | } 54 | 55 | // CreateDB creates a database 56 | func (be *Route53) CreateDB(db url.URL) error { 57 | listInput := &route53.ListHostedZonesByNameInput{} 58 | resp, err := be.client.ListHostedZonesByName(listInput) 59 | if err != nil { 60 | fmt.Println(err) 61 | return err 62 | } 63 | 64 | for _, v := range resp.HostedZones { 65 | hzn := *v.Name 66 | if hzn == be.HostedZoneName { 67 | id := *v.Id 68 | id = strings.TrimPrefix(id, "/hostedzone/") 69 | return fmt.Errorf("database already exists: %s", id) 70 | } 71 | } 72 | 73 | t := time.Now().Unix() 74 | callerRef := fmt.Sprintf("%v", t) 75 | createInput := &route53.CreateHostedZoneInput{ 76 | CallerReference: aws.String(callerRef), 77 | Name: aws.String(be.HostedZoneName), 78 | } 79 | _, err = be.client.CreateHostedZone(createInput) 80 | if err != nil { 81 | return fmt.Errorf("failed to create database: %s", be.URI.String()) 82 | } 83 | 84 | return nil 85 | } 86 | 87 | // DropDB deletes a database 88 | func (be *Route53) DropDB(db url.URL) error { 89 | be.setHostedZoneID() 90 | if be.HostedZoneID == "" { 91 | return fmt.Errorf("database does not exist: %s", be.URI.Host) 92 | } 93 | 94 | input := &route53.DeleteHostedZoneInput{ 95 | Id: aws.String(be.HostedZoneID), 96 | } 97 | _, err := be.client.DeleteHostedZone(input) 98 | if err != nil { 99 | return fmt.Errorf("failed to delete database: %s", be.URI.String()) 100 | } 101 | 102 | return nil 103 | } 104 | 105 | // Delete deletes a key from the database 106 | func (be *Route53) Delete(db url.URL, key string) error { 107 | be.setHostedZoneID() 108 | if be.HostedZoneID == "" { 109 | return fmt.Errorf("database does not exist: %s", be.URI.Host) 110 | } 111 | 112 | dnsKey := fmt.Sprintf("%s.%s", key, be.HostedZoneName) 113 | 114 | listInput := &route53.ListResourceRecordSetsInput{ 115 | HostedZoneId: aws.String(be.HostedZoneID), 116 | StartRecordName: aws.String(dnsKey), 117 | StartRecordType: aws.String("TXT"), 118 | MaxItems: aws.String("1"), 119 | } 120 | resp, err := be.client.ListResourceRecordSets(listInput) 121 | 122 | if err != nil { 123 | if aerr, ok := err.(awserr.Error); ok { 124 | switch aerr.Code() { 125 | case route53.ErrCodeNoSuchHostedZone: 126 | fmt.Println(route53.ErrCodeNoSuchHostedZone, aerr.Error()) 127 | case route53.ErrCodeInvalidInput: 128 | fmt.Println(route53.ErrCodeInvalidInput, aerr.Error()) 129 | default: 130 | fmt.Println(aerr.Error()) 131 | } 132 | } else { 133 | // Print the error, cast err to awserr.Error to get the Code and 134 | // Message from an error. 135 | fmt.Println(err.Error()) 136 | } 137 | return err 138 | } 139 | 140 | if len(resp.ResourceRecordSets) == 0 { 141 | return fmt.Errorf("key does not exist: %s", key) 142 | } 143 | 144 | deleteInput := &route53.ChangeResourceRecordSetsInput{ 145 | ChangeBatch: &route53.ChangeBatch{ 146 | Changes: []*route53.Change{ 147 | { 148 | Action: aws.String("DELETE"), 149 | ResourceRecordSet: &route53.ResourceRecordSet{ 150 | Name: aws.String(dnsKey), 151 | ResourceRecords: resp.ResourceRecordSets[0].ResourceRecords, 152 | TTL: aws.Int64(60), 153 | Type: aws.String("TXT"), 154 | }, 155 | }, 156 | }, 157 | Comment: aws.String(fmt.Sprintf("Deleting key %s", key)), 158 | }, 159 | HostedZoneId: aws.String(be.HostedZoneID), 160 | } 161 | 162 | _, err = be.client.ChangeResourceRecordSets(deleteInput) 163 | if err != nil { 164 | if aerr, ok := err.(awserr.Error); ok { 165 | switch aerr.Code() { 166 | case route53.ErrCodeNoSuchHostedZone: 167 | fmt.Println(route53.ErrCodeNoSuchHostedZone, aerr.Error()) 168 | case route53.ErrCodeNoSuchHealthCheck: 169 | fmt.Println(route53.ErrCodeNoSuchHealthCheck, aerr.Error()) 170 | case route53.ErrCodeInvalidChangeBatch: 171 | fmt.Println(route53.ErrCodeInvalidChangeBatch, aerr.Error()) 172 | case route53.ErrCodeInvalidInput: 173 | fmt.Println(route53.ErrCodeInvalidInput, aerr.Error()) 174 | case route53.ErrCodePriorRequestNotComplete: 175 | fmt.Println(route53.ErrCodePriorRequestNotComplete, aerr.Error()) 176 | default: 177 | fmt.Println(aerr.Error()) 178 | } 179 | } else { 180 | // Print the error, cast err to awserr.Error to get the Code and 181 | // Message from an error. 182 | fmt.Println(err.Error()) 183 | } 184 | return err 185 | } 186 | return nil 187 | } 188 | 189 | // Get retrieves keys from the database 190 | func (be *Route53) Get(db url.URL, key string) (string, error) { 191 | be.setHostedZoneID() 192 | 193 | resolvable := true 194 | lookupHost := strings.TrimSuffix(be.HostedZoneName, ".") 195 | _, err := net.LookupNS(lookupHost) 196 | if err != nil { 197 | resolvable = false 198 | } 199 | 200 | dnsKey := fmt.Sprintf("%s.%s", key, be.HostedZoneName) 201 | 202 | if resolvable { 203 | fmt.Println("DNS is resolvable") 204 | vals, err := net.LookupTXT(dnsKey) 205 | if err != nil || len(vals) == 0 { 206 | return "", fmt.Errorf("key not found: %s", key) 207 | } 208 | return vals[0], nil 209 | } 210 | 211 | fmt.Println("DNS is not resolvable, falling back to ListResourceRecordSets") 212 | 213 | listInput := &route53.ListResourceRecordSetsInput{ 214 | HostedZoneId: aws.String(be.HostedZoneID), 215 | StartRecordName: aws.String(dnsKey), 216 | StartRecordType: aws.String("TXT"), 217 | MaxItems: aws.String("1"), 218 | } 219 | resp, err := be.client.ListResourceRecordSets(listInput) 220 | 221 | if err != nil { 222 | if aerr, ok := err.(awserr.Error); ok { 223 | switch aerr.Code() { 224 | case route53.ErrCodeNoSuchHostedZone: 225 | fmt.Println(route53.ErrCodeNoSuchHostedZone, aerr.Error()) 226 | case route53.ErrCodeInvalidInput: 227 | fmt.Println(route53.ErrCodeInvalidInput, aerr.Error()) 228 | default: 229 | fmt.Println(aerr.Error()) 230 | } 231 | } else { 232 | // Print the error, cast err to awserr.Error to get the Code and 233 | // Message from an error. 234 | fmt.Println(err.Error()) 235 | } 236 | return "", err 237 | } 238 | 239 | if len(resp.ResourceRecordSets) == 0 { 240 | return "", fmt.Errorf("key does not exist: %s", key) 241 | } 242 | 243 | val := *resp.ResourceRecordSets[0].ResourceRecords[0].Value 244 | val = strings.TrimPrefix(val, "\"") 245 | val = strings.TrimSuffix(val, "\"") 246 | return val, nil 247 | } 248 | 249 | // Put writes a key-value pair to the database 250 | func (be *Route53) Put(db url.URL, key, val string) error { 251 | be.setHostedZoneID() 252 | if be.HostedZoneID == "" { 253 | return fmt.Errorf("database does not exist: %s", be.URI.Host) 254 | } 255 | 256 | dnsKey := fmt.Sprintf("%s.%s", key, be.HostedZoneName) 257 | input := &route53.ChangeResourceRecordSetsInput{ 258 | ChangeBatch: &route53.ChangeBatch{ 259 | Changes: []*route53.Change{ 260 | { 261 | Action: aws.String("UPSERT"), 262 | ResourceRecordSet: &route53.ResourceRecordSet{ 263 | Name: aws.String(dnsKey), 264 | ResourceRecords: []*route53.ResourceRecord{ 265 | { 266 | Value: aws.String(fmt.Sprintf("\"%s\"", val)), 267 | }, 268 | }, 269 | TTL: aws.Int64(60), 270 | Type: aws.String("TXT"), 271 | }, 272 | }, 273 | }, 274 | Comment: aws.String(fmt.Sprintf("Updating key %s", key)), 275 | }, 276 | HostedZoneId: aws.String(be.HostedZoneID), 277 | } 278 | 279 | _, err := be.client.ChangeResourceRecordSets(input) 280 | if err != nil { 281 | if aerr, ok := err.(awserr.Error); ok { 282 | switch aerr.Code() { 283 | case route53.ErrCodeNoSuchHostedZone: 284 | fmt.Println(route53.ErrCodeNoSuchHostedZone, aerr.Error()) 285 | case route53.ErrCodeNoSuchHealthCheck: 286 | fmt.Println(route53.ErrCodeNoSuchHealthCheck, aerr.Error()) 287 | case route53.ErrCodeInvalidChangeBatch: 288 | fmt.Println(route53.ErrCodeInvalidChangeBatch, aerr.Error()) 289 | case route53.ErrCodeInvalidInput: 290 | fmt.Println(route53.ErrCodeInvalidInput, aerr.Error()) 291 | case route53.ErrCodePriorRequestNotComplete: 292 | fmt.Println(route53.ErrCodePriorRequestNotComplete, aerr.Error()) 293 | default: 294 | fmt.Println(aerr.Error()) 295 | } 296 | } else { 297 | // Print the error, cast err to awserr.Error to get the Code and 298 | // Message from an error. 299 | fmt.Println(err.Error()) 300 | } 301 | return err 302 | } 303 | return nil 304 | } 305 | 306 | func (be *Route53) setHostedZoneID() error { 307 | input := &route53.ListHostedZonesByNameInput{} 308 | resp, err := be.client.ListHostedZonesByName(input) 309 | 310 | if err != nil { 311 | fmt.Println(err) 312 | return err 313 | } 314 | 315 | for _, v := range resp.HostedZones { 316 | hzn := *v.Name 317 | if hzn == be.HostedZoneName { 318 | id := *v.Id 319 | be.HostedZoneID = strings.TrimPrefix(id, "/hostedzone/") 320 | return nil 321 | } 322 | } 323 | return nil 324 | } 325 | --------------------------------------------------------------------------------