* 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>
463 lines
14 KiB
Go
463 lines
14 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
package controller
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"reflect"
|
|
"runtime/debug"
|
|
|
|
grpc_recovery "github.com/grpc-ecosystem/go-grpc-middleware/recovery"
|
|
commonSrv "github.com/hashicorp/boundary/internal/daemon/common"
|
|
"github.com/hashicorp/boundary/internal/daemon/controller/auth"
|
|
"github.com/hashicorp/boundary/internal/daemon/controller/common"
|
|
"github.com/hashicorp/boundary/internal/daemon/controller/handlers"
|
|
"github.com/hashicorp/boundary/internal/errors"
|
|
pb "github.com/hashicorp/boundary/internal/gen/controller/api"
|
|
authpb "github.com/hashicorp/boundary/internal/gen/controller/auth"
|
|
pberrors "github.com/hashicorp/boundary/internal/gen/errors"
|
|
"github.com/hashicorp/boundary/internal/kms"
|
|
"github.com/hashicorp/boundary/internal/observability/event"
|
|
"github.com/hashicorp/boundary/internal/requests"
|
|
"github.com/mr-tron/base58"
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/metadata"
|
|
"google.golang.org/grpc/status"
|
|
"google.golang.org/protobuf/proto"
|
|
)
|
|
|
|
const (
|
|
requestInfoMdKey = "request-info"
|
|
|
|
// domainErrHeader defines an http header for encoded domain errors from the
|
|
// grpc server.
|
|
domainErrHeader = "x-domain-err"
|
|
|
|
// apiErrHeader defines an http header for encoded api errors from the
|
|
// grpc server.
|
|
apiErrHeader = "x-api-err"
|
|
)
|
|
|
|
// customContextServerStream wraps the grpc.ServerStream interface and lets us
|
|
// set a custom context
|
|
type customContextServerStream struct {
|
|
grpc.ServerStream
|
|
customContext context.Context
|
|
}
|
|
|
|
func (c *customContextServerStream) Context() context.Context {
|
|
if c.customContext != nil {
|
|
return c.customContext
|
|
}
|
|
return c.ServerStream.Context()
|
|
}
|
|
|
|
// requestCtxUnaryInterceptor creates an unary server interceptor that pulls
|
|
// grpc metadata into a ctx for the request. The metadata must be set in an
|
|
// upstream http handler/middleware by marshalling a RequestInfo protobuf into
|
|
// the requestInfoMdKey header (see: controller.wrapHandlerWithCommonFuncs).
|
|
func requestCtxUnaryInterceptor(
|
|
ctx context.Context,
|
|
iamRepoFn common.IamRepoFactory,
|
|
authTokenRepoFn common.AuthTokenRepoFactory,
|
|
serversRepoFn common.ServersRepoFactory,
|
|
passwordAuthRepoFn common.PasswordAuthRepoFactory,
|
|
oidcAuthRepoFn common.OidcAuthRepoFactory,
|
|
ldapAuthRepoFn common.LdapAuthRepoFactory,
|
|
kms *kms.Kms,
|
|
ticket string,
|
|
eventer *event.Eventer,
|
|
) (grpc.UnaryServerInterceptor, error) {
|
|
const op = "controller.requestCtxUnaryInterceptor"
|
|
if err := sharedRequestInterceptorValidation(
|
|
ctx,
|
|
op,
|
|
iamRepoFn,
|
|
authTokenRepoFn,
|
|
serversRepoFn,
|
|
kms,
|
|
ticket,
|
|
eventer,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return func(interceptorCtx context.Context,
|
|
req any,
|
|
_ *grpc.UnaryServerInfo,
|
|
handler grpc.UnaryHandler,
|
|
) (any, error) {
|
|
updatedCtx, err := sharedRequestInterceptorLogic(
|
|
interceptorCtx,
|
|
op,
|
|
iamRepoFn,
|
|
authTokenRepoFn,
|
|
serversRepoFn,
|
|
passwordAuthRepoFn,
|
|
oidcAuthRepoFn,
|
|
ldapAuthRepoFn,
|
|
kms,
|
|
ticket,
|
|
eventer,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return handler(updatedCtx, req)
|
|
}, nil
|
|
}
|
|
|
|
// requestCtxStreamInterceptor creates a stream server interceptor that pulls
|
|
// grpc metadata into a ctx for the request. The metadata must be set in an
|
|
// upstream http handler/middleware by marshalling a RequestInfo protobuf into
|
|
// the requestInfoMdKey header (see: controller.wrapHandlerWithCommonFuncs).
|
|
func requestCtxStreamInterceptor(
|
|
ctx context.Context,
|
|
iamRepoFn common.IamRepoFactory,
|
|
authTokenRepoFn common.AuthTokenRepoFactory,
|
|
serversRepoFn common.ServersRepoFactory,
|
|
passwordAuthRepoFn common.PasswordAuthRepoFactory,
|
|
oidcAuthRepoFn common.OidcAuthRepoFactory,
|
|
ldapAuthRepoFn common.LdapAuthRepoFactory,
|
|
kms *kms.Kms,
|
|
ticket string,
|
|
eventer *event.Eventer,
|
|
) (grpc.StreamServerInterceptor, error) {
|
|
const op = "controller.requestCtxStreamInterceptor"
|
|
if err := sharedRequestInterceptorValidation(
|
|
ctx,
|
|
op,
|
|
iamRepoFn,
|
|
authTokenRepoFn,
|
|
serversRepoFn,
|
|
kms,
|
|
ticket,
|
|
eventer,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
return func(srv any,
|
|
ss grpc.ServerStream,
|
|
info *grpc.StreamServerInfo,
|
|
handler grpc.StreamHandler,
|
|
) error {
|
|
updatedCtx, err := sharedRequestInterceptorLogic(
|
|
ss.Context(),
|
|
op,
|
|
iamRepoFn,
|
|
authTokenRepoFn,
|
|
serversRepoFn,
|
|
passwordAuthRepoFn,
|
|
oidcAuthRepoFn,
|
|
ldapAuthRepoFn,
|
|
kms,
|
|
ticket,
|
|
eventer,
|
|
)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
css := &customContextServerStream{
|
|
ServerStream: ss,
|
|
customContext: updatedCtx,
|
|
}
|
|
return handler(srv, css)
|
|
}, nil
|
|
}
|
|
|
|
func sharedRequestInterceptorValidation(
|
|
ctx context.Context,
|
|
op errors.Op,
|
|
iamRepoFn common.IamRepoFactory,
|
|
authTokenRepoFn common.AuthTokenRepoFactory,
|
|
serversRepoFn common.ServersRepoFactory,
|
|
kms *kms.Kms,
|
|
ticket string,
|
|
eventer *event.Eventer,
|
|
) error {
|
|
if iamRepoFn == nil {
|
|
return errors.New(ctx, errors.InvalidParameter, op, "missing iam repo function")
|
|
}
|
|
if authTokenRepoFn == nil {
|
|
return errors.New(ctx, errors.InvalidParameter, op, "missing auth token repo function")
|
|
}
|
|
if serversRepoFn == nil {
|
|
return errors.New(ctx, errors.InvalidParameter, op, "missing server repo function")
|
|
}
|
|
if kms == nil {
|
|
return errors.New(ctx, errors.InvalidParameter, op, "missing kms")
|
|
}
|
|
if ticket == "" {
|
|
return errors.New(ctx, errors.InvalidParameter, op, "missing ticket")
|
|
}
|
|
if eventer == nil {
|
|
return errors.New(ctx, errors.InvalidParameter, op, "missing eventer")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func sharedRequestInterceptorLogic(
|
|
interceptorCtx context.Context,
|
|
op errors.Op,
|
|
iamRepoFn common.IamRepoFactory,
|
|
authTokenRepoFn common.AuthTokenRepoFactory,
|
|
serversRepoFn common.ServersRepoFactory,
|
|
passwordAuthRepoFn common.PasswordAuthRepoFactory,
|
|
oidcAuthRepoFn common.OidcAuthRepoFactory,
|
|
ldapAuthRepoFn common.LdapAuthRepoFactory,
|
|
kms *kms.Kms,
|
|
ticket string,
|
|
eventer *event.Eventer,
|
|
) (context.Context, error) {
|
|
md, ok := metadata.FromIncomingContext(interceptorCtx)
|
|
if !ok {
|
|
return nil, errors.New(interceptorCtx, errors.Internal, op, "No metadata")
|
|
}
|
|
|
|
values := md.Get(requestInfoMdKey)
|
|
if len(values) == 0 {
|
|
return nil, errors.New(interceptorCtx, errors.Internal, op, "Missing request metadata")
|
|
}
|
|
if len(values) > 1 {
|
|
return nil, errors.New(interceptorCtx, errors.Internal, op, fmt.Sprintf("expected 1 value for %s metadata and got %d", requestInfoMdKey, len(values)))
|
|
}
|
|
|
|
decoded, err := base58.FastBase58Decoding(values[0])
|
|
if err != nil {
|
|
return nil, errors.Wrap(interceptorCtx, err, op, errors.WithCode(errors.Internal), errors.WithMsg("unable to decode request info"))
|
|
}
|
|
var requestInfo authpb.RequestInfo
|
|
if err := proto.Unmarshal(decoded, &requestInfo); err != nil {
|
|
return nil, errors.Wrap(interceptorCtx, err, op, errors.WithCode(errors.Internal), errors.WithMsg("unable to unmarshal request info"))
|
|
}
|
|
switch {
|
|
case requestInfo.Ticket == "":
|
|
return nil, errors.New(interceptorCtx, errors.Internal, op, "Invalid context (missing ticket)")
|
|
case requestInfo.Ticket != ticket:
|
|
return nil, errors.New(interceptorCtx, errors.Internal, op, "Invalid context (bad ticket)")
|
|
}
|
|
|
|
interceptorCtx = auth.NewVerifierContextWithAccounts(interceptorCtx, iamRepoFn, authTokenRepoFn, serversRepoFn, passwordAuthRepoFn, oidcAuthRepoFn, ldapAuthRepoFn, kms, &requestInfo)
|
|
|
|
// Add general request information to the context. The information from
|
|
// the auth verifier context is pretty specifically curated to
|
|
// authentication/authorization verification so this is more
|
|
// general-purpose.
|
|
//
|
|
// We could use requests.NewRequestContext but this saves an immediate
|
|
// lookup.
|
|
interceptorCtx = context.WithValue(interceptorCtx, requests.ContextRequestInformationKey, &requests.RequestContext{
|
|
Path: requestInfo.Path,
|
|
Method: requestInfo.Method,
|
|
})
|
|
|
|
// This event request info is required by downstream handlers
|
|
info := &event.RequestInfo{
|
|
EventId: requestInfo.EventId,
|
|
Id: requestInfo.TraceId,
|
|
PublicId: requestInfo.PublicId,
|
|
Method: requestInfo.Method,
|
|
Path: requestInfo.Path,
|
|
ClientIp: requestInfo.ClientIp,
|
|
}
|
|
interceptorCtx, err = event.NewRequestInfoContext(interceptorCtx, info)
|
|
if err != nil {
|
|
return nil, errors.Wrap(interceptorCtx, err, op, errors.WithCode(errors.Internal), errors.WithMsg("unable to create context with request info"))
|
|
}
|
|
interceptorCtx, err = event.NewEventerContext(interceptorCtx, eventer)
|
|
if err != nil {
|
|
return nil, errors.Wrap(interceptorCtx, err, op, errors.WithCode(errors.Internal), errors.WithMsg("unable to create context with eventer"))
|
|
}
|
|
|
|
return interceptorCtx, err // not convinced we want to wrap every error and turn them into domain errors...
|
|
}
|
|
|
|
func errorInterceptor(
|
|
_ context.Context,
|
|
) grpc.UnaryServerInterceptor {
|
|
const op = "controller.errorInterceptor"
|
|
return func(interceptorCtx context.Context,
|
|
req any,
|
|
_ *grpc.UnaryServerInfo,
|
|
handler grpc.UnaryHandler) (any, error,
|
|
) {
|
|
// call the handler...
|
|
h, handlerErr := handler(interceptorCtx, req)
|
|
|
|
// if there's an error and it's a domain err, then encode it into the
|
|
if handlerErr != nil {
|
|
var domainErr *errors.Err
|
|
isDomainErr := errors.As(handlerErr, &domainErr)
|
|
if isDomainErr {
|
|
pbErr := errors.ToPbErrors(domainErr)
|
|
var buf []byte
|
|
var err error
|
|
if buf, err = proto.Marshal(pbErr); err != nil {
|
|
return h, status.Errorf(codes.Internal, "unable to marshal domain error: %s", err)
|
|
}
|
|
if err := grpc.SetHeader(interceptorCtx, metadata.Pairs(domainErrHeader, base58.FastBase58Encoding(buf))); err != nil {
|
|
return h, status.Errorf(codes.Internal, "unable to set domain error header: %s", err)
|
|
}
|
|
return h, handlerErr
|
|
}
|
|
|
|
var apiErr *handlers.ApiError
|
|
isApiError := errors.As(handlerErr, &apiErr)
|
|
if isApiError {
|
|
pbErr := &pberrors.ApiError{
|
|
ApiError: apiErr.Inner,
|
|
Status: apiErr.Status,
|
|
}
|
|
var buf []byte
|
|
var err error
|
|
if buf, err = proto.Marshal(pbErr); err != nil {
|
|
return h, status.Errorf(codes.Internal, "unable to marshal api error: %s", err)
|
|
}
|
|
if err := grpc.SetHeader(interceptorCtx, metadata.Pairs(apiErrHeader, base58.FastBase58Encoding(buf))); err != nil {
|
|
return h, status.Errorf(codes.Internal, "unable to set api error header: %s", err)
|
|
}
|
|
return h, handlerErr
|
|
}
|
|
}
|
|
return h, handlerErr
|
|
}
|
|
}
|
|
|
|
func statusCodeInterceptor(
|
|
_ context.Context,
|
|
) grpc.UnaryServerInterceptor {
|
|
const op = "controller.statusCodeInterceptor"
|
|
return func(interceptorCtx context.Context,
|
|
req any,
|
|
_ *grpc.UnaryServerInfo,
|
|
handler grpc.UnaryHandler) (any, error,
|
|
) {
|
|
// call the handler...
|
|
h, handlerErr := handler(interceptorCtx, req)
|
|
|
|
// if a service handler returns nil, nil then we want to single a 204
|
|
// response to the proxy with no resp msg
|
|
if isNil(h) && handlerErr == nil {
|
|
if err := handlers.SetStatusCode(interceptorCtx, http.StatusNoContent); err != nil {
|
|
return &pb.EmptyResponse{}, err
|
|
}
|
|
return &pb.EmptyResponse{}, nil
|
|
}
|
|
|
|
return h, handlerErr
|
|
}
|
|
}
|
|
|
|
func isNil(i any) bool {
|
|
if i == nil {
|
|
return true
|
|
}
|
|
switch reflect.TypeOf(i).Kind() {
|
|
case reflect.Ptr, reflect.Map, reflect.Array, reflect.Chan, reflect.Slice:
|
|
return reflect.ValueOf(i).IsNil()
|
|
}
|
|
return false
|
|
}
|
|
|
|
func auditRequestInterceptor(
|
|
_ context.Context,
|
|
) grpc.UnaryServerInterceptor {
|
|
const op = "controller.auditRequestInterceptor"
|
|
return func(interceptorCtx context.Context,
|
|
req any,
|
|
_ *grpc.UnaryServerInfo,
|
|
handler grpc.UnaryHandler) (any, error,
|
|
) {
|
|
if msg, ok := req.(proto.Message); ok {
|
|
// Clone the request before writing it to the audit log,
|
|
// in case downstream interceptors modify it.
|
|
clonedMsg := proto.Clone(msg)
|
|
if err := event.WriteAudit(interceptorCtx, op, event.WithRequest(&event.Request{Details: clonedMsg})); err != nil {
|
|
return req, status.Errorf(codes.Internal, "unable to write request msg audit: %s", err)
|
|
}
|
|
}
|
|
|
|
return handler(interceptorCtx, req)
|
|
}
|
|
}
|
|
|
|
func auditResponseInterceptor(
|
|
_ context.Context,
|
|
) grpc.UnaryServerInterceptor {
|
|
const op = "controller.auditResponseInterceptor"
|
|
return func(interceptorCtx context.Context,
|
|
req any,
|
|
_ *grpc.UnaryServerInfo,
|
|
handler grpc.UnaryHandler) (any, error,
|
|
) {
|
|
// call the handler...
|
|
resp, err := handler(interceptorCtx, req)
|
|
|
|
if msg, ok := resp.(proto.Message); ok {
|
|
// Clone the response before writing it to the audit log,
|
|
// in case downstream interceptors modify it.
|
|
clonedMsg := proto.Clone(msg)
|
|
if err := event.WriteAudit(interceptorCtx, op, event.WithResponse(&event.Response{Details: clonedMsg})); err != nil {
|
|
return req, status.Errorf(codes.Internal, "unable to write response msg audit: %s", err)
|
|
}
|
|
}
|
|
|
|
return resp, err
|
|
}
|
|
}
|
|
|
|
func workerRequestInfoInterceptor(ctx context.Context, eventer *event.Eventer) (grpc.UnaryServerInterceptor, error) {
|
|
const op = "worker.requestInfoInterceptor"
|
|
if eventer == nil {
|
|
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing eventer")
|
|
}
|
|
return func(interceptorCtx context.Context,
|
|
req any,
|
|
srvInfo *grpc.UnaryServerInfo,
|
|
handler grpc.UnaryHandler,
|
|
) (any, error) {
|
|
var err error
|
|
id, err := event.NewId(event.IdPrefix)
|
|
if err != nil {
|
|
event.WriteError(interceptorCtx, op, err, event.WithInfoMsg("unable to create id for event", "method", srvInfo.FullMethod))
|
|
return nil, status.Errorf(codes.Internal, "Error creating id for event: %v", err)
|
|
}
|
|
info := &event.RequestInfo{
|
|
EventId: id,
|
|
Id: commonSrv.GeneratedTraceId(interceptorCtx),
|
|
Method: srvInfo.FullMethod,
|
|
}
|
|
interceptorCtx, err = event.NewRequestInfoContext(interceptorCtx, info)
|
|
if err != nil {
|
|
event.WriteError(interceptorCtx, op, err, event.WithInfoMsg("unable to create context with request info", "method", srvInfo.FullMethod))
|
|
return nil, status.Errorf(codes.Internal, "Error creating context with request info: %v", err)
|
|
}
|
|
interceptorCtx, err = event.NewEventerContext(interceptorCtx, eventer)
|
|
if err != nil {
|
|
event.WriteError(interceptorCtx, op, err, event.WithInfoMsg("unable to create context with eventer", "method", srvInfo.FullMethod))
|
|
return nil, status.Errorf(codes.Internal, "Error creating context with eventer: %v", err)
|
|
}
|
|
// call the handler...
|
|
return handler(interceptorCtx, req)
|
|
}, nil
|
|
}
|
|
|
|
func recoveryHandler() grpc_recovery.RecoveryHandlerFuncContext {
|
|
const op = "controller.recoveryHandler"
|
|
return func(ctx context.Context, p any) (err error) {
|
|
event.WriteError(
|
|
ctx,
|
|
op,
|
|
fmt.Errorf("recovered from panic: %v", p),
|
|
event.WithInfo("stack", string(debug.Stack())),
|
|
)
|
|
|
|
return status.Errorf(codes.Internal, "%v", p)
|
|
}
|
|
}
|