* Adding explicit MPL license for sub-package. This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Updating the license from MPL to Business Source License Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at https://hashi.co/bsl-blog, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl. * Update copyright file headers to BUS-1.1 * Rerun make gen This will pick up the last of the license changes * Revert "Rerun make gen" This reverts commit dbe0968bffd92745d8663c6940debae098395a6b. * Remove copywrite from generation script The current version of copywrite is not sophisticated enough to only apply the copywrite changes to certain subdirectories, so to avoid accidentally relicensing our api and sdk modules, we will stop running the copywrite script until such time that it can do the right thing. * License protobuf API as MPL The protobuf API files are used to generate the SDK, since we want the SDK to be MPL, the protobuf files must also be MPL. This extends to all protobuf files whose sources end up in the SDK directory, including: - internal/proto/controller/api - internal/proto/controller/custom_options - internal/proto/plugin It also includes the generated service directory in internal/gen/controller/api/services, since they are generated from the protobuf sources used for the SDK. * Change website license file name This makes the naming scheme consistent with the rest of the repository. * Add note on licensing to README * Set copywrite license to BUSL * Revert "Add note on licensing to README" This reverts commit 4d865a91a7a4f11605f0dc4609355d7e3c3cfffe. --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com> Co-authored-by: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com>
200 lines
7.1 KiB
Go
200 lines
7.1 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
package handlers
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"sync"
|
|
|
|
"github.com/hashicorp/boundary/internal/errors"
|
|
pbs "github.com/hashicorp/boundary/internal/gen/controller/servers/services"
|
|
"github.com/hashicorp/boundary/internal/observability/event"
|
|
"github.com/hashicorp/boundary/internal/util"
|
|
"github.com/hashicorp/nodeenrollment"
|
|
"github.com/hashicorp/nodeenrollment/types"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
"google.golang.org/protobuf/proto"
|
|
)
|
|
|
|
var upstreamMessageHandler sync.Map
|
|
|
|
// UpstreamMessageHandler defines a handler for an UpstreamMessageRequest(s).
|
|
//
|
|
// See controllerUpstreamMessageServiceServer.UpstreamMessage for how this is
|
|
// used to handle an UpstreamMessageRequest via registered upstream message
|
|
// handlers.
|
|
type UpstreamMessageHandler interface {
|
|
// Handler for the request. All errors returned must be a error created
|
|
// using google.golang.org/grpc/status
|
|
Handler(ctx context.Context, request proto.Message) (response proto.Message, statusErr error)
|
|
|
|
UpstreamMessageTypeSpecifier
|
|
}
|
|
|
|
// RegisterUpstreamMessageHandler will register an UpstreamMessageHandler for
|
|
// the specified msg name.
|
|
//
|
|
// See controllerUpstreamMessageServiceServer.UpstreamMessage for how this is
|
|
// used to handle an UpstreamMessageRequest via registered upstream message
|
|
// handlers.
|
|
func RegisterUpstreamMessageHandler(ctx context.Context, msgType pbs.MsgType, h UpstreamMessageHandler) error {
|
|
const op = "handlers.RegisterUpstreamMessageHandler"
|
|
switch {
|
|
case msgType == pbs.MsgType_MSG_TYPE_UNSPECIFIED:
|
|
return errors.New(ctx, errors.InvalidParameter, op, "missing msg type")
|
|
case util.IsNil(h):
|
|
return errors.New(ctx, errors.InvalidParameter, op, "missing handler")
|
|
}
|
|
upstreamMessageHandler.Store(msgType, h)
|
|
if err := registerUpstreamMessageTypeSpecifier(ctx, msgType, h); err != nil {
|
|
return errors.Wrap(ctx, err, op)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func getUpstreamMessageHandler(ctx context.Context, msgType pbs.MsgType) (UpstreamMessageHandler, bool) {
|
|
const op = "handlers.getUpstreamMessageHandler"
|
|
switch {
|
|
case msgType == pbs.MsgType_MSG_TYPE_UNSPECIFIED:
|
|
event.WriteError(ctx, op, fmt.Errorf("missing msg type"))
|
|
return nil, false
|
|
}
|
|
v, ok := upstreamMessageHandler.Load(msgType)
|
|
if !ok {
|
|
return nil, false
|
|
}
|
|
|
|
h, ok := v.(UpstreamMessageHandler)
|
|
if !ok {
|
|
event.WriteError(ctx, op, fmt.Errorf("malformed handler type %q registered as incorrect type %T", msgType.String(), v))
|
|
return nil, false
|
|
}
|
|
return h, true
|
|
}
|
|
|
|
// controllerUpstreamMessageServiceServer implements the
|
|
// UpstreamMessageServiceServer for OSS controllers
|
|
type controllerUpstreamMessageServiceServer struct {
|
|
pbs.UnimplementedUpstreamMessageServiceServer
|
|
storage nodeenrollment.Storage
|
|
}
|
|
|
|
var _ pbs.UpstreamMessageServiceServer = (*controllerUpstreamMessageServiceServer)(nil)
|
|
|
|
// NewControllerUpstreamMessageServiceServer creates a new service implementing
|
|
// UpstreamMessageServiceServer, storing values used for the implementing
|
|
// functions.
|
|
func NewControllerUpstreamMessageServiceServer(
|
|
ctx context.Context,
|
|
storage nodeenrollment.Storage,
|
|
) (pbs.UpstreamMessageServiceServer, error) {
|
|
const op = "handlers.NewControllerUpstreamMessageServiceServer"
|
|
switch {
|
|
case util.IsNil(storage):
|
|
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing storage")
|
|
}
|
|
|
|
return &controllerUpstreamMessageServiceServer{
|
|
storage: storage,
|
|
}, nil
|
|
}
|
|
|
|
// UpstreamMessage implements the grpc service of the same name for controllers.
|
|
// It decrypts the request using the originating workers ID using its
|
|
// types.NodeCredentials. If there's a registered UpstreamMessageHandler for the
|
|
// underlying message's protoreflect.FullName; then the msg is handled by the
|
|
// handler otherwise an codes.Unimplemented status error is returned
|
|
func (s *controllerUpstreamMessageServiceServer) UpstreamMessage(ctx context.Context, req *pbs.UpstreamMessageRequest) (*pbs.UpstreamMessageResponse, error) {
|
|
const op = "handlers.(controllerUpstreamMessageServiceServer).UpstreamMessage"
|
|
switch {
|
|
case req == nil:
|
|
return nil, status.Errorf(codes.Internal, "%s: missing request", op)
|
|
case req.GetOriginatingWorkerKeyId() == "":
|
|
return nil, status.Errorf(codes.Internal, "%s: missing originating worker id", op)
|
|
case req.GetMsgType() == pbs.MsgType_MSG_TYPE_UNSPECIFIED:
|
|
return nil, status.Errorf(codes.Internal, "%s: msg type %q is unsupported", op, req.GetMsgType().String())
|
|
}
|
|
|
|
h, ok := getUpstreamMessageHandler(ctx, req.GetMsgType())
|
|
if !ok {
|
|
return nil, status.Errorf(codes.Unimplemented, "upstream message handler for %q is not implemented", req.GetMsgType().String())
|
|
}
|
|
|
|
msg := h.AllocRequest()
|
|
var nodeInfo *types.NodeInformation
|
|
|
|
switch {
|
|
case h.Encrypted() == false:
|
|
if err := proto.Unmarshal(req.GetPt(), msg); err != nil {
|
|
return nil, status.Errorf(codes.Internal, "%s: error marshaling request message: %v", op, err)
|
|
}
|
|
default:
|
|
var err error
|
|
nodeInfo, err = types.LoadNodeInformation(ctx, s.storage, req.OriginatingWorkerKeyId)
|
|
if err != nil {
|
|
return nil, status.Errorf(codes.Internal, "%s: error loading node information: %v", op, err)
|
|
}
|
|
if nodeInfo == nil {
|
|
return nil, status.Errorf(codes.Internal, "%s: no error loading worker pki auth creds but nil creds, returning error", op)
|
|
}
|
|
if err := nodeenrollment.DecryptMessage(ctx, req.GetCt(), nodeInfo, msg); err != nil {
|
|
return nil, status.Errorf(codes.Internal, "%s: error decrypting request message: %v", op, err)
|
|
}
|
|
}
|
|
clonedMsg := proto.Clone(msg)
|
|
if err := event.WriteAudit(ctx, "handlers.(controllerUpstreamMessageServiceServer).UpstreamMessage",
|
|
event.WithRequest(
|
|
&event.Request{
|
|
DetailsUpstreamMessage: &event.UpstreamMessage{
|
|
Message: clonedMsg,
|
|
Type: string(clonedMsg.ProtoReflect().Descriptor().Name()),
|
|
},
|
|
},
|
|
)); err != nil {
|
|
// error was NOT event'd above...
|
|
_ = errors.Wrap(ctx, err, op, errors.WithCode(errors.Internal), errors.WithMsg("error writing upstream message unwrapped details request"))
|
|
}
|
|
respMsg, respStatusErr := h.Handler(ctx, msg)
|
|
if respStatusErr != nil {
|
|
return nil, respStatusErr
|
|
}
|
|
clonedResp := proto.Clone(respMsg)
|
|
if err := event.WriteAudit(ctx, "handlers.(controllerUpstreamMessageServiceServer).UpstreamMessage",
|
|
event.WithResponse(
|
|
&event.Response{
|
|
DetailsUpstreamMessage: &event.UpstreamMessage{
|
|
Message: clonedResp,
|
|
Type: string(clonedResp.ProtoReflect().Descriptor().Name()),
|
|
},
|
|
},
|
|
)); err != nil {
|
|
// error was NOT event'd above...
|
|
_ = errors.Wrap(ctx, err, op, errors.WithCode(errors.Internal), errors.WithMsg("error writing upstream message unwrapped details response"))
|
|
}
|
|
switch {
|
|
case h.Encrypted() == false:
|
|
pt, err := proto.Marshal(respMsg)
|
|
if err != nil {
|
|
return nil, status.Errorf(codes.Internal, "%s: error marshaling response: %v", op, err)
|
|
}
|
|
return &pbs.UpstreamMessageResponse{
|
|
Message: &pbs.UpstreamMessageResponse_Pt{
|
|
Pt: pt,
|
|
},
|
|
}, nil
|
|
default:
|
|
ct, err := nodeenrollment.EncryptMessage(ctx, respMsg, nodeInfo)
|
|
if err != nil {
|
|
return nil, status.Errorf(codes.Internal, "%s: error encrypting response: %v", op, err)
|
|
}
|
|
return &pbs.UpstreamMessageResponse{
|
|
Message: &pbs.UpstreamMessageResponse_Ct{
|
|
Ct: ct,
|
|
},
|
|
}, nil
|
|
}
|
|
}
|