diff --git a/internal/cmd/commands/dev/dev.go b/internal/cmd/commands/dev/dev.go index 4dbccaf9d7..a7ec5b9a7f 100644 --- a/internal/cmd/commands/dev/dev.go +++ b/internal/cmd/commands/dev/dev.go @@ -25,13 +25,18 @@ import ( "github.com/hashicorp/boundary/internal/iam" "github.com/hashicorp/boundary/internal/intglobals" "github.com/hashicorp/boundary/internal/observability/event" + "github.com/hashicorp/boundary/internal/servers" + "github.com/hashicorp/boundary/internal/servers/store" "github.com/hashicorp/boundary/internal/target/tcp" "github.com/hashicorp/boundary/internal/types/scope" "github.com/hashicorp/go-secure-stdlib/parseutil" "github.com/hashicorp/go-secure-stdlib/strutil" + "github.com/hashicorp/nodeenrollment/types" "github.com/mitchellh/cli" + "github.com/mr-tron/base58" "github.com/posener/complete" "go.uber.org/atomic" + "google.golang.org/protobuf/proto" ) var ( @@ -719,7 +724,7 @@ func (c *Command) Run(args []string) int { case <-c.Context.Done(): return case <-time.After(time.Second): - if err := c.controller.AuthorizeNodeeWorker(req); err != nil { + if err := authorizeWorker(c.Context, c.controller, req); err != nil { c.UI.Error(fmt.Errorf("Error authorizing node: %w", err).Error()) errorEncountered.Store(true) return @@ -805,3 +810,31 @@ func (c *Command) Run(args []string) int { return base.CommandSuccess } + +func authorizeWorker(ctx context.Context, c *controller.Controller, request string) error { + reqBytes, err := base58.FastBase58Decoding(request) + if err != nil { + return fmt.Errorf("error base58-decoding fetch node creds next proto value: %w", err) + } + // Decode the proto into the request + req := new(types.FetchNodeCredentialsRequest) + if err := proto.Unmarshal(reqBytes, req); err != nil { + return fmt.Errorf("error unmarshaling common name value: %w", err) + } + + serversRepo, err := c.ServersRepoFn() + if err != nil { + return fmt.Errorf("error fetching servers repo: %w", err) + } + + _, err = serversRepo.CreateWorker(ctx, &servers.Worker{ + Worker: &store.Worker{ + ScopeId: scope.Global.String(), + }, + }, servers.WithFetchNodeCredentialsRequest(req)) + if err != nil { + return fmt.Errorf("error creating worker: %w", err) + } + + return err +} diff --git a/internal/daemon/controller/controller.go b/internal/daemon/controller/controller.go index f2cb3363ce..1d590ee04b 100644 --- a/internal/daemon/controller/controller.go +++ b/internal/daemon/controller/controller.go @@ -28,7 +28,6 @@ import ( "github.com/hashicorp/boundary/internal/scheduler/job" "github.com/hashicorp/boundary/internal/servers" serversjob "github.com/hashicorp/boundary/internal/servers/job" - "github.com/hashicorp/boundary/internal/servers/store" "github.com/hashicorp/boundary/internal/session" "github.com/hashicorp/boundary/internal/target" "github.com/hashicorp/boundary/internal/types/scope" @@ -38,11 +37,8 @@ import ( "github.com/hashicorp/go-hclog" "github.com/hashicorp/go-secure-stdlib/mlock" "github.com/hashicorp/go-secure-stdlib/pluginutil/v2" - "github.com/hashicorp/nodeenrollment/types" - "github.com/mr-tron/base58" ua "go.uber.org/atomic" "google.golang.org/grpc" - "google.golang.org/protobuf/proto" ) type Controller struct { @@ -402,33 +398,3 @@ func (c *Controller) Shutdown() error { func (c *Controller) WorkerStatusUpdateTimes() *sync.Map { return c.workerStatusUpdateTimes } - -// This is a temporary function until the API is up -func (c *Controller) AuthorizeNodeeWorker(request string) error { - const op = "controller.(Controller).AuthorizeNodeeWorker" - reqBytes, err := base58.FastBase58Decoding(request) - if err != nil { - return fmt.Errorf("(%s) error base58-decoding fetch node creds next proto value: %w", op, err) - } - // Decode the proto into the request - req := new(types.FetchNodeCredentialsRequest) - if err := proto.Unmarshal(reqBytes, req); err != nil { - return fmt.Errorf("(%s) error unmarshaling common name value: %w", op, err) - } - - serversRepo, err := c.ServersRepoFn() - if err != nil { - return fmt.Errorf("(%s) error fetching servers repo: %w", op, err) - } - - _, err = serversRepo.CreateWorker(c.baseContext, &servers.Worker{ - Worker: &store.Worker{ - ScopeId: scope.Global.String(), - }, - }, servers.WithFetchNodeCredentialsRequest(req)) - if err != nil { - return fmt.Errorf("(%s) error creating worker: %w", op, err) - } - - return err -} diff --git a/internal/daemon/controller/handler.go b/internal/daemon/controller/handler.go index d5757a81ab..c06218f44d 100644 --- a/internal/daemon/controller/handler.go +++ b/internal/daemon/controller/handler.go @@ -6,7 +6,6 @@ import ( "encoding/json" "errors" "fmt" - "io" "io/ioutil" "net/http" "net/textproto" @@ -67,7 +66,6 @@ func (c *Controller) apiHandler(props HandlerProperties) (http.Handler, error) { if err != nil { return nil, err } - mux.Handle("/v1/nodes", handleNodes(c)) mux.Handle("/v1/", grpcGwMux) mux.Handle("/", handleUi(c)) @@ -574,42 +572,6 @@ func wrapHandlerWithCallbackInterceptor(h http.Handler, c *Controller) http.Hand }) } -func handleNodes(c *Controller) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { - switch req.Method { - - case http.MethodPost: - body, err := io.ReadAll(req.Body) - req.Body.Close() - if err != nil { - _, _ = w.Write([]byte(err.Error())) - w.WriteHeader(500) - return - } - type val struct { - Request string `json:"request"` - } - var currVal val - if err := json.Unmarshal(body, &currVal); err != nil { - _, _ = w.Write([]byte(err.Error())) - w.WriteHeader(500) - return - } - if err := c.AuthorizeNodeeWorker(currVal.Request); err != nil { - _, _ = w.Write([]byte(err.Error())) - w.WriteHeader(500) - return - } - w.WriteHeader(204) - return - - default: - w.WriteHeader(http.StatusBadRequest) - return - } - }) -} - /* func WrapForwardedForHandler(h http.Handler, authorizedAddrs []*sockaddr.SockAddrMarshaler, rejectNotPresent, rejectNonAuthz bool, hopSkips int) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {