Skip to content
This repository has been archived by the owner on Mar 8, 2023. It is now read-only.

Renamed to REST proxy and project split into cmd and pkg #2

Merged
merged 4 commits into from
Mar 6, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 additions & 6 deletions cmd/secrethub-proxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -36,18 +39,36 @@ func init() {
}

func main() {
clientd := restproxy.SecretHubRESTProxy{
Client: &client,
Port: port,
proxy := restproxy.NewSecretHubRESTProxy(client, port)

go gracefulShutdown(proxy)
SimonBarendse marked this conversation as resolved.
Show resolved Hide resolved

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)
}
58 changes: 45 additions & 13 deletions pkg/restproxy/rest_proxy.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package restproxy

import (
"context"
"fmt"
"io"
"io/ioutil"
Expand All @@ -12,25 +13,56 @@ import (
"github.com/keylockerbv/secrethub/core/errio"
florisvdg marked this conversation as resolved.
Show resolved Hide resolved
)

// 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 {
florisvdg marked this conversation as resolved.
Show resolved Hide resolved
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 {
florisvdg marked this conversation as resolved.
Show resolved Hide resolved
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 {
florisvdg marked this conversation as resolved.
Show resolved Hide resolved
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) {
florisvdg marked this conversation as resolved.
Show resolved Hide resolved
path := r.URL.Path
err := api.ValidateSecretPath(path)
if err != nil {
Expand All @@ -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

Expand All @@ -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

Expand Down