From 71a8a412a2c0e0b4aef75d4a67db5b1c1a7887ad Mon Sep 17 00:00:00 2001 From: Floris van der Grinten Date: Tue, 5 Mar 2019 14:47:25 +0100 Subject: [PATCH 1/4] Renamed to REST proxy and project split into cmd and pkg --- README.md | 2 +- cmd/secrethub-proxy/main.go | 53 ++++++++++++++++++++++ go.mod | 9 ++++ go.sum | 31 +++++++++++++ main.go => pkg/restproxy/rest_proxy.go | 62 ++++++-------------------- 5 files changed, 108 insertions(+), 49 deletions(-) create mode 100644 cmd/secrethub-proxy/main.go rename main.go => pkg/restproxy/rest_proxy.go (53%) diff --git a/README.md b/README.md index 95552da..73fb5e8 100644 --- a/README.md +++ b/README.md @@ -1 +1 @@ -# secrethub-clientd +# secrethub-proxy diff --git a/cmd/secrethub-proxy/main.go b/cmd/secrethub-proxy/main.go new file mode 100644 index 0000000..c44b6ae --- /dev/null +++ b/cmd/secrethub-proxy/main.go @@ -0,0 +1,53 @@ +package main + +import ( + "flag" + "fmt" + "os" + + "github.com/keylockerbv/secrethub-clientd/pkg/restproxy" + "github.com/keylockerbv/secrethub-go/pkg/secrethub" +) + +var ( + credential string + credentialPassphrase string + port int + client secrethub.Client +) + +func init() { + flag.StringVar(&credential, "C", "", "(Required) SecretHub credential") + flag.StringVar(&credentialPassphrase, "P", "", "Passphrase to unlock SecretHub credential") + flag.IntVar(&port, "p", 8080, "HTTP port to listen on") + flag.Parse() + + if credential == "" { + flag.Usage() + exit(fmt.Errorf("credential is required")) + } + + cred, err := secrethub.NewCredential(credential, credentialPassphrase) + if err != nil { + exit(err) + } + + client = secrethub.NewClient(cred, nil) +} + +func main() { + clientd := restproxy.SecretHubRESTProxy{ + Client: &client, + Port: port, + } + fmt.Println("SecretHub REST proxy started, press ^C to stop it") + err := clientd.Start() + if err != nil { + exit(err) + } +} + +func exit(err error) { + fmt.Printf("secrethub-clientd: error: %v\n", err) + os.Exit(1) +} diff --git a/go.mod b/go.mod index 3c6b999..de5a5a6 100644 --- a/go.mod +++ b/go.mod @@ -1,7 +1,16 @@ module github.com/keylockerbv/secrethub-clientd require ( + github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc // indirect + github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf // indirect + github.com/atotto/clipboard v0.1.1 // indirect github.com/gorilla/mux v1.7.0 + github.com/jmoiron/sqlx v1.2.0 // indirect + github.com/keylockerbv/secrethub v0.17.0 github.com/keylockerbv/secrethub-go v0.0.0-20190225132925-244d98858e9d + github.com/mattn/go-sqlite3 v1.10.0 // indirect + github.com/stretchr/testify v1.3.0 // indirect golang.org/x/crypto v0.0.0-20190225124518-7f87c0fbb88b // indirect + google.golang.org/appengine v1.4.0 // indirect + gopkg.in/alecthomas/kingpin.v2 v2.2.6 // indirect ) diff --git a/go.sum b/go.sum index 6dfd139..83d3586 100644 --- a/go.sum +++ b/go.sum @@ -1,19 +1,34 @@ bitbucket.org/zombiezen/cardcpx v0.0.0-20150417151802-902f68ff43ef h1:Y5Zf3CYdrdGE7GOuK/MNN98GS1V8mOfeiJlISrKUcEo= bitbucket.org/zombiezen/cardcpx v0.0.0-20150417151802-902f68ff43ef/go.mod h1:ZJR5FpaQx7Bt2bzIV3gBaCInI1+kG949WhNYYlRr8eA= +github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc h1:cAKDfWh5VpdgMhJosfJnn5/FoN2SRZ4p7fJNX58YPaU= +github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= +github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf h1:qet1QNfXsQxTZqLG4oE62mJzwPIB8+Tee4RNCL9ulrY= +github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= github.com/asaskevich/govalidator v0.0.0-20180720115003-f9ffefc3facf h1:eg0MeVzsP1G42dRafH3vf+al2vQIJU0YHX+1Tw87oco= github.com/asaskevich/govalidator v0.0.0-20180720115003-f9ffefc3facf/go.mod h1:lB+ZfQJz7igIIfQNfa7Ml4HSf2uFQQRzpGGRXenZAgY= +github.com/atotto/clipboard v0.1.1 h1:WSoEbAS70E5gw8FbiqFlp69MGsB6dUb4l+0AGGLiVGw= +github.com/atotto/clipboard v0.1.1/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI= github.com/certifi/gocertifi v0.0.0-20190105021004-abcd57078448 h1:8tNk6SPXzLDnATTrWoI5Bgw9s/x4uf0kmBpk21NZgI4= github.com/certifi/gocertifi v0.0.0-20190105021004-abcd57078448/go.mod h1:GJKEexRPVJrBSOjoqN5VNOIKJ5Q3RViH6eu3puDRwx4= +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/docker/go-units v0.3.3 h1:Xk8S3Xj5sLGlG5g67hJmYMmUgXv5N4PhkjJHHqrwnTk= github.com/docker/go-units v0.3.3/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk= github.com/getsentry/raven-go v0.2.0 h1:no+xWJRb5ZI7eE8TWgIq1jLulQiIoLG0IfYxv5JYMGs= github.com/getsentry/raven-go v0.2.0/go.mod h1:KungGk8q33+aIAZUIVWZDr2OfAEBsO49PX4NzFV5kcQ= github.com/go-chi/chi v4.0.1+incompatible h1:RSRC5qmFPtO90t7pTL0DBMNpZFsb/sHF3RXVlDgFisA= github.com/go-chi/chi v4.0.1+incompatible/go.mod h1:eB3wogJHnLi3x/kFX2A+IbTBlXxmMeXJVKy9tTv1XzQ= +github.com/go-sql-driver/mysql v1.4.0 h1:7LxgVwFb2hIQtMm87NdgAVfXjnt4OePseqT1tKx+opk= +github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk= github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck= github.com/gorilla/mux v1.7.0 h1:tOSd0UKHQd6urX6ApfOn4XdBMY6Sh1MfxV3kmaazO+U= github.com/gorilla/mux v1.7.0/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs= +github.com/jmoiron/sqlx v1.2.0 h1:41Ip0zITnmWNR/vHV+S4m+VoUivnWY5E4OJfLZjCJMA= +github.com/jmoiron/sqlx v1.2.0/go.mod h1:1FEQNm3xlJgrMD+FBdI9+xvCksHtbpVBBw5dYhBSsks= +github.com/keylockerbv/secrethub v0.17.0 h1:ynvH2JWotytvtLpGE8Bu4hPIV1m+OS/tTlmTGlv8Hmk= +github.com/keylockerbv/secrethub v0.17.0/go.mod h1:EibHbXBS5k+Cw+XEo4rveG+ONA2Czl1VFpVl7+jDYOc= github.com/keylockerbv/secrethub-go v0.0.0-20190225132925-244d98858e9d h1:NxCFGfkmBF9RaloxF5c/J5knS3FE/g+uWOZ9ENucc1M= github.com/keylockerbv/secrethub-go v0.0.0-20190225132925-244d98858e9d/go.mod h1:U086plZMagUfy92G4DgCsRAns20Q5j+Rf8bYDXiDxMw= github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= @@ -24,15 +39,31 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348 h1:MtvEpTB6LX3vkb4ax0b5D2DHbNAUsen0Gx5wZoq3lV4= github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348/go.mod h1:B69LEHPfb2qLo0BaaOLcbitczOKLWTsrBG9LczfCD4k= +github.com/lib/pq v1.0.0 h1:X5PMW56eZitiTeO7tKzZxFCSpbFZJtkMMooicw2us9A= +github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= +github.com/mattn/go-sqlite3 v1.9.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= +github.com/mattn/go-sqlite3 v1.10.0 h1:jbhqpg7tQe4SupckyijYiy0mJJ/pRyHvXf7JdWK860o= +github.com/mattn/go-sqlite3 v1.10.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= github.com/op/go-logging v0.0.0-20160315200505-970db520ece7 h1:lDH9UUVJtmYCjyT0CI4q8xvlXPxeZ0gYCVvWbmPlp88= github.com/op/go-logging v0.0.0-20160315200505-970db520ece7/go.mod h1:HzydrMdWErDVzsI23lYNej1Htcns9BCg93Dk0bBINWk= github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww= github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= golang.org/x/crypto v0.0.0-20190211182817-74369b46fc67 h1:ng3VDlRp5/DHpSWl02R4rM9I+8M2rhmsuLwAMmkLQWE= golang.org/x/crypto v0.0.0-20190211182817-74369b46fc67/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190225124518-7f87c0fbb88b h1:+/WWzjwW6gidDJnMKWLKLX1gxn7irUTF1fLpQovfQ5M= golang.org/x/crypto v0.0.0-20190225124518-7f87c0fbb88b/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= +golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +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/alecthomas/kingpin.v2 v2.2.6 h1:jMFz6MfLP0/4fUyZle81rXUoxOBFi19VUFKVDOQfozc= +gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/main.go b/pkg/restproxy/rest_proxy.go similarity index 53% rename from main.go rename to pkg/restproxy/rest_proxy.go index 74b532b..3052c2c 100644 --- a/main.go +++ b/pkg/restproxy/rest_proxy.go @@ -1,65 +1,36 @@ -package main +package restproxy import ( - "flag" "fmt" "io" "io/ioutil" "net/http" - "os" "github.com/gorilla/mux" - "github.com/keylockerbv/secrethub-go/pkg/api" - "github.com/keylockerbv/secrethub-go/pkg/errio" "github.com/keylockerbv/secrethub-go/pkg/secrethub" + "github.com/keylockerbv/secrethub/api" + "github.com/keylockerbv/secrethub/core/errio" ) -var ( - credential string - credentialPassphrase string - port int - client secrethub.Client -) - -func init() { - flag.StringVar(&credential, "C", "", "(Required) SecretHub credential") - flag.StringVar(&credentialPassphrase, "P", "", "Passphrase to unlock SecretHub credential") - flag.IntVar(&port, "p", 8080, "HTTP port to listen on") - flag.Parse() - - if credential == "" { - flag.Usage() - exit(fmt.Errorf("credential is required")) - } - - cred, err := secrethub.NewCredential(credential, credentialPassphrase) - if err != nil { - exit(err) - } - - client = secrethub.NewClient(cred, nil) +// SecretHubRESTProxy exposes SecretHub Client functionality with a RESTful interface +type SecretHubRESTProxy struct { + Port int + Client *secrethub.Client } -func main() { - err := startHTTPServer() - if err != nil { - exit(err) - } -} - -func startHTTPServer() error { +// Start starts the SecretHub REST proxy +func (c *SecretHubRESTProxy) Start() error { mux := mux.NewRouter() v1 := mux.PathPrefix("/v1/").Subrouter() v1.PathPrefix("/secrets/").Handler( - http.StripPrefix("/v1/secrets/", http.HandlerFunc(handleSecret)), + http.StripPrefix("/v1/secrets/", http.HandlerFunc(c.handleSecret)), ) - fmt.Println("SecretHub Clientd started, press ^C to exit") - return http.ListenAndServe(fmt.Sprintf(":%v", port), mux) + return http.ListenAndServe(fmt.Sprintf(":%v", c.Port), mux) } -func handleSecret(w http.ResponseWriter, r *http.Request) { +func (c *SecretHubRESTProxy) handleSecret(w http.ResponseWriter, r *http.Request) { path := r.URL.Path err := api.ValidateSecretPath(path) if err != nil { @@ -70,7 +41,7 @@ func handleSecret(w http.ResponseWriter, r *http.Request) { switch r.Method { case "GET": - secret, err := client.Secrets().Versions().GetWithData(path) + secret, err := (*c.Client).Secrets().Versions().GetWithData(path) if err != nil { var errCode int @@ -97,7 +68,7 @@ func handleSecret(w http.ResponseWriter, r *http.Request) { return } - _, err = client.Secrets().Write(path, secret) + _, err = (*c.Client).Secrets().Write(path, secret) if err != nil { var errCode int @@ -127,8 +98,3 @@ func handleSecret(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusMethodNotAllowed) } } - -func exit(err error) { - fmt.Printf("secrethub-clientd: error: %v\n", err) - os.Exit(1) -} From 41bc1504ad25e75eba1ff550b4ee2bfb3244d86b Mon Sep 17 00:00:00 2001 From: Floris van der Grinten Date: Tue, 5 Mar 2019 17:05:36 +0100 Subject: [PATCH 2/4] REST proxy type private with public constructor --- cmd/secrethub-proxy/main.go | 33 +++++++++++++++++---- pkg/restproxy/rest_proxy.go | 58 ++++++++++++++++++++++++++++--------- 2 files changed, 72 insertions(+), 19 deletions(-) diff --git a/cmd/secrethub-proxy/main.go b/cmd/secrethub-proxy/main.go index c44b6ae..894b66a 100644 --- a/cmd/secrethub-proxy/main.go +++ b/cmd/secrethub-proxy/main.go @@ -3,7 +3,10 @@ package main import ( "flag" "fmt" + "net/http" "os" + "os/signal" + "syscall" "github.com/keylockerbv/secrethub-clientd/pkg/restproxy" "github.com/keylockerbv/secrethub-go/pkg/secrethub" @@ -36,18 +39,36 @@ func init() { } func main() { - clientd := restproxy.SecretHubRESTProxy{ - Client: &client, - Port: port, + proxy := restproxy.NewSecretHubRESTProxy(client, port) + + go gracefulShutdown(proxy) + + log("SecretHub REST proxy started, press ^C to stop it") + err := proxy.Start() + if err != nil && err != http.ErrServerClosed { + exit(err) } - fmt.Println("SecretHub REST proxy started, press ^C to stop it") - err := clientd.Start() +} + +func gracefulShutdown(proxy restproxy.SecretHubProxy) { + sigint := make(chan os.Signal, 1) + + signal.Notify(sigint, os.Interrupt) + signal.Notify(sigint, syscall.SIGTERM) + <-sigint + + log("Shutting down gracefully...") + err := proxy.Stop() if err != nil { exit(err) } } func exit(err error) { - fmt.Printf("secrethub-clientd: error: %v\n", err) + fmt.Printf("secrethub-proxy: error: %v\n", err) os.Exit(1) } + +func log(message string) { + fmt.Printf("secrethub-proxy: %v\n", message) +} diff --git a/pkg/restproxy/rest_proxy.go b/pkg/restproxy/rest_proxy.go index 3052c2c..d561552 100644 --- a/pkg/restproxy/rest_proxy.go +++ b/pkg/restproxy/rest_proxy.go @@ -1,6 +1,7 @@ package restproxy import ( + "context" "fmt" "io" "io/ioutil" @@ -12,25 +13,56 @@ import ( "github.com/keylockerbv/secrethub/core/errio" ) -// SecretHubRESTProxy exposes SecretHub Client functionality with a RESTful interface -type SecretHubRESTProxy struct { - Port int - Client *secrethub.Client +// SecretHubProxy gives the SecretHub Client a certain communication layer +type SecretHubProxy interface { + Start() error + Stop() error } -// Start starts the SecretHub REST proxy -func (c *SecretHubRESTProxy) Start() error { - mux := mux.NewRouter() - v1 := mux.PathPrefix("/v1/").Subrouter() +type secretHubRESTProxy struct { + SecretHubProxy + client secrethub.Client + server *http.Server +} + +// NewSecretHubRESTProxy creates a proxy of the SecretHub Client, giving it a RESTful interface +func NewSecretHubRESTProxy(client secrethub.Client, port int) SecretHubProxy { + if port == 0 { + port = 8080 + } + + router := mux.NewRouter() + proxy := &secretHubRESTProxy{ + client: client, + server: &http.Server{ + Addr: fmt.Sprintf(":%v", port), + Handler: router, + }, + } + proxy.addRoutes(router) + + return proxy +} + +func (proxy *secretHubRESTProxy) addRoutes(r *mux.Router) { + v1 := r.PathPrefix("/v1/").Subrouter() v1.PathPrefix("/secrets/").Handler( - http.StripPrefix("/v1/secrets/", http.HandlerFunc(c.handleSecret)), + http.StripPrefix("/v1/secrets/", http.HandlerFunc(proxy.handleSecret)), ) +} + +// Start starts the SecretHub REST proxy, starting an HTTP server +func (proxy *secretHubRESTProxy) Start() error { + return proxy.server.ListenAndServe() +} - return http.ListenAndServe(fmt.Sprintf(":%v", c.Port), mux) +// Stop stops the SecretHub REST proxy, stopping the HTTP server +func (proxy *secretHubRESTProxy) Stop() error { + return proxy.server.Shutdown(context.Background()) } -func (c *SecretHubRESTProxy) handleSecret(w http.ResponseWriter, r *http.Request) { +func (proxy *secretHubRESTProxy) handleSecret(w http.ResponseWriter, r *http.Request) { path := r.URL.Path err := api.ValidateSecretPath(path) if err != nil { @@ -41,7 +73,7 @@ func (c *SecretHubRESTProxy) handleSecret(w http.ResponseWriter, r *http.Request switch r.Method { case "GET": - secret, err := (*c.Client).Secrets().Versions().GetWithData(path) + secret, err := proxy.client.Secrets().Versions().GetWithData(path) if err != nil { var errCode int @@ -68,7 +100,7 @@ func (c *SecretHubRESTProxy) handleSecret(w http.ResponseWriter, r *http.Request return } - _, err = (*c.Client).Secrets().Write(path, secret) + _, err = proxy.client.Secrets().Write(path, secret) if err != nil { var errCode int From 1837a965624dd8cabcd4310c83d267b8c0791c73 Mon Sep 17 00:00:00 2001 From: Floris van der Grinten Date: Tue, 5 Mar 2019 17:06:59 +0100 Subject: [PATCH 3/4] Renamed mod name --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index de5a5a6..b4d9450 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/keylockerbv/secrethub-clientd +module github.com/keylockerbv/secrethub-proxy require ( github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc // indirect From 7d3724f1fc0171d5c057f2faf430f45d9b81e087 Mon Sep 17 00:00:00 2001 From: Floris van der Grinten Date: Wed, 6 Mar 2019 11:21:27 +0100 Subject: [PATCH 4/4] Naming adjustments --- cmd/secrethub-proxy/main.go | 6 +++--- go.sum | 1 + pkg/restproxy/rest_proxy.go | 32 ++++++++++++++++---------------- 3 files changed, 20 insertions(+), 19 deletions(-) diff --git a/cmd/secrethub-proxy/main.go b/cmd/secrethub-proxy/main.go index 894b66a..6f8a890 100644 --- a/cmd/secrethub-proxy/main.go +++ b/cmd/secrethub-proxy/main.go @@ -8,8 +8,8 @@ import ( "os/signal" "syscall" - "github.com/keylockerbv/secrethub-clientd/pkg/restproxy" "github.com/keylockerbv/secrethub-go/pkg/secrethub" + "github.com/keylockerbv/secrethub-proxy/pkg/restproxy" ) var ( @@ -39,7 +39,7 @@ func init() { } func main() { - proxy := restproxy.NewSecretHubRESTProxy(client, port) + proxy := restproxy.NewRESTProxy(client, port) go gracefulShutdown(proxy) @@ -50,7 +50,7 @@ func main() { } } -func gracefulShutdown(proxy restproxy.SecretHubProxy) { +func gracefulShutdown(proxy restproxy.ClientProxy) { sigint := make(chan os.Signal, 1) signal.Notify(sigint, os.Interrupt) diff --git a/go.sum b/go.sum index 83d3586..c5dcb3a 100644 --- a/go.sum +++ b/go.sum @@ -29,6 +29,7 @@ github.com/jmoiron/sqlx v1.2.0 h1:41Ip0zITnmWNR/vHV+S4m+VoUivnWY5E4OJfLZjCJMA= github.com/jmoiron/sqlx v1.2.0/go.mod h1:1FEQNm3xlJgrMD+FBdI9+xvCksHtbpVBBw5dYhBSsks= github.com/keylockerbv/secrethub v0.17.0 h1:ynvH2JWotytvtLpGE8Bu4hPIV1m+OS/tTlmTGlv8Hmk= github.com/keylockerbv/secrethub v0.17.0/go.mod h1:EibHbXBS5k+Cw+XEo4rveG+ONA2Czl1VFpVl7+jDYOc= +github.com/keylockerbv/secrethub-clientd v0.0.0-20190225134359-9c3d34a96705 h1:5fRK0U5z2CtqZsRha/5+cdyazD/4zjzcfODaoE2ZCzI= github.com/keylockerbv/secrethub-go v0.0.0-20190225132925-244d98858e9d h1:NxCFGfkmBF9RaloxF5c/J5knS3FE/g+uWOZ9ENucc1M= github.com/keylockerbv/secrethub-go v0.0.0-20190225132925-244d98858e9d/go.mod h1:U086plZMagUfy92G4DgCsRAns20Q5j+Rf8bYDXiDxMw= github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= diff --git a/pkg/restproxy/rest_proxy.go b/pkg/restproxy/rest_proxy.go index d561552..040c714 100644 --- a/pkg/restproxy/rest_proxy.go +++ b/pkg/restproxy/rest_proxy.go @@ -13,26 +13,26 @@ import ( "github.com/keylockerbv/secrethub/core/errio" ) -// SecretHubProxy gives the SecretHub Client a certain communication layer -type SecretHubProxy interface { +// ClientProxy gives the SecretHub Client a certain communication layer +type ClientProxy interface { Start() error Stop() error } -type secretHubRESTProxy struct { - SecretHubProxy +type restProxy struct { + ClientProxy client secrethub.Client server *http.Server } -// NewSecretHubRESTProxy creates a proxy of the SecretHub Client, giving it a RESTful interface -func NewSecretHubRESTProxy(client secrethub.Client, port int) SecretHubProxy { +// NewRESTProxy creates a proxy for the SecretHub Client, giving it a RESTful interface +func NewRESTProxy(client secrethub.Client, port int) ClientProxy { if port == 0 { port = 8080 } router := mux.NewRouter() - proxy := &secretHubRESTProxy{ + proxy := &restProxy{ client: client, server: &http.Server{ Addr: fmt.Sprintf(":%v", port), @@ -44,25 +44,25 @@ func NewSecretHubRESTProxy(client secrethub.Client, port int) SecretHubProxy { return proxy } -func (proxy *secretHubRESTProxy) addRoutes(r *mux.Router) { +func (p *restProxy) addRoutes(r *mux.Router) { v1 := r.PathPrefix("/v1/").Subrouter() v1.PathPrefix("/secrets/").Handler( - http.StripPrefix("/v1/secrets/", http.HandlerFunc(proxy.handleSecret)), + http.StripPrefix("/v1/secrets/", http.HandlerFunc(p.handleSecret)), ) } // Start starts the SecretHub REST proxy, starting an HTTP server -func (proxy *secretHubRESTProxy) Start() error { - return proxy.server.ListenAndServe() +func (p *restProxy) Start() error { + return p.server.ListenAndServe() } // Stop stops the SecretHub REST proxy, stopping the HTTP server -func (proxy *secretHubRESTProxy) Stop() error { - return proxy.server.Shutdown(context.Background()) +func (p *restProxy) Stop() error { + return p.server.Shutdown(context.Background()) } -func (proxy *secretHubRESTProxy) handleSecret(w http.ResponseWriter, r *http.Request) { +func (p *restProxy) handleSecret(w http.ResponseWriter, r *http.Request) { path := r.URL.Path err := api.ValidateSecretPath(path) if err != nil { @@ -73,7 +73,7 @@ func (proxy *secretHubRESTProxy) handleSecret(w http.ResponseWriter, r *http.Req switch r.Method { case "GET": - secret, err := proxy.client.Secrets().Versions().GetWithData(path) + secret, err := p.client.Secrets().Versions().GetWithData(path) if err != nil { var errCode int @@ -100,7 +100,7 @@ func (proxy *secretHubRESTProxy) handleSecret(w http.ResponseWriter, r *http.Req return } - _, err = proxy.client.Secrets().Write(path, secret) + _, err = p.client.Secrets().Write(path, secret) if err != nil { var errCode int