Handler project (#44)
* Renamed fakeRepo to mockRepo * Changing around TODOs to capture current plans with the code. * Remove the repo interface and use a real DB for tests. * Fixed a few tests. * Adding checks for properly formatted identifiers. * Updating code to account for recent merges. Relaxing some restrictions on what an id can look like. * Added small comment explaining why RegisterGrpcGatewayer interface exists. * Move project service up a call level. Long term I think it would make sense to define these services even higher up the call stack, potentially at the initialization of the controller server, near where the DB is initialized. * Making these fields align with those in the resources under hosts/... * Hard code the service initialization in the handler registration code. * Regenerate project related code proto code. * Removing type field for project. * Adds basic project handling in CLI/SDK (#47) * Adds SDK for creating and reading projects * Reorganizes a bit * Adds CLI for creating and reading projects * Fixes some some generation bugs * Adds redirect support for default org ID * Fix protobuf import references being moved. * Update client test to look for "orgs" and "projects" instead of "org" and "project" * Update expected segments test * Fix setAddr handling when default organization is in use * Set client in target after create or read Co-authored-by: Jim Lambert <jimlambrt@Jims-MBP-3.home> Co-authored-by: Jim <jlambert@hashicorp.com> Co-authored-by: Michael Gaffney <mgaffney@users.noreply.github.com> Co-authored-by: Jeff Mitchell <jeffrey.mitchell@gmail.com>
This commit is contained in:
36 files changed
+4104
-112
No files matched your search
@@ -3,6 +3,8 @@ package controller
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"path"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/grpc-ecosystem/grpc-gateway/runtime"
|
||||
@@ -12,6 +14,7 @@ import (
|
||||
"github.com/hashicorp/watchtower/internal/servers/controller/handlers/host_catalogs"
|
||||
"github.com/hashicorp/watchtower/internal/servers/controller/handlers/host_sets"
|
||||
"github.com/hashicorp/watchtower/internal/servers/controller/handlers/hosts"
|
||||
"github.com/hashicorp/watchtower/internal/servers/controller/handlers/projects"
|
||||
)
|
||||
|
||||
type HandlerProperties struct {
|
||||
@@ -20,28 +23,31 @@ type HandlerProperties struct {
|
||||
|
||||
// Handler returns an http.Handler for the services. This can be used on
|
||||
// its own to mount the Vault API within another web server.
|
||||
func Handler(props HandlerProperties) http.Handler {
|
||||
func (c *Controller) handler(props HandlerProperties) http.Handler {
|
||||
// Create the muxer to handle the actual endpoints
|
||||
mux := http.NewServeMux()
|
||||
|
||||
mux.Handle("/v1/", handleGrpcGateway())
|
||||
mux.Handle("/v1/", handleGrpcGateway(c))
|
||||
|
||||
genericWrappedHandler := wrapGenericHandler(mux, props)
|
||||
genericWrappedHandler := wrapGenericHandler(mux, c, props)
|
||||
|
||||
return genericWrappedHandler
|
||||
}
|
||||
|
||||
func handleGrpcGateway() http.Handler {
|
||||
ignored := context.Background()
|
||||
func handleGrpcGateway(c *Controller) http.Handler {
|
||||
// Register*ServiceHandlerServer methods ignore the passed in ctx. Using the baseContext now just in case this changes
|
||||
// in the future, at which point we'll want to be using the baseContext.
|
||||
ctx := c.baseContext
|
||||
mux := runtime.NewServeMux()
|
||||
services.RegisterHostCatalogServiceHandlerServer(ignored, mux, &host_catalogs.Service{})
|
||||
services.RegisterHostSetServiceHandlerServer(ignored, mux, &host_sets.Service{})
|
||||
services.RegisterHostServiceHandlerServer(ignored, mux, &hosts.Service{})
|
||||
services.RegisterHostCatalogServiceHandlerServer(ctx, mux, &host_catalogs.Service{})
|
||||
services.RegisterHostSetServiceHandlerServer(ctx, mux, &host_sets.Service{})
|
||||
services.RegisterHostServiceHandlerServer(ctx, mux, &hosts.Service{})
|
||||
services.RegisterProjectServiceHandlerServer(ctx, mux, projects.NewService(c.IamRepo))
|
||||
|
||||
return mux
|
||||
}
|
||||
|
||||
func wrapGenericHandler(h http.Handler, props HandlerProperties) http.Handler {
|
||||
func wrapGenericHandler(h http.Handler, c *Controller, props HandlerProperties) http.Handler {
|
||||
var maxRequestDuration time.Duration
|
||||
var maxRequestSize int64
|
||||
if props.ListenerConfig != nil {
|
||||
@@ -54,7 +60,19 @@ func wrapGenericHandler(h http.Handler, props HandlerProperties) http.Handler {
|
||||
if maxRequestSize == 0 {
|
||||
maxRequestSize = globals.DefaultMaxRequestSize
|
||||
}
|
||||
var defaultOrgId string
|
||||
if c != nil && c.conf != nil {
|
||||
defaultOrgId = c.conf.DefaultOrgId
|
||||
}
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if defaultOrgId != "" {
|
||||
splitPath := strings.Split(r.URL.Path, "/")
|
||||
if len(splitPath) >= 3 && splitPath[2] == "projects" {
|
||||
http.Redirect(w, r, path.Join("/v1/orgs", defaultOrgId, strings.Join(splitPath[2:], "/")), 307)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Set the Cache-Control header for all responses returned
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
|
||||
|
||||
Reference in new issue
Block a user