2023-03-28 22:48:58 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-28 22:48:58 +00:00
|
|
|
|
2023-03-27 15:35:39 +00:00
|
|
|
package resource
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
|
2023-04-11 11:10:14 +00:00
|
|
|
"github.com/hashicorp/consul/acl"
|
2023-08-07 21:37:03 +00:00
|
|
|
"github.com/hashicorp/consul/internal/resource"
|
2023-03-27 15:35:39 +00:00
|
|
|
"github.com/hashicorp/consul/internal/storage"
|
|
|
|
"github.com/hashicorp/consul/proto-public/pbresource"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *Server) Read(ctx context.Context, req *pbresource.ReadRequest) (*pbresource.ReadResponse, error) {
|
2023-08-07 21:37:03 +00:00
|
|
|
// Light first pass validation based on what user passed in and not much more.
|
2023-11-03 20:03:07 +00:00
|
|
|
reg, err := s.ensureReadRequestValid(req)
|
2023-04-11 11:10:14 +00:00
|
|
|
if err != nil {
|
2023-03-27 21:25:27 +00:00
|
|
|
return nil, err
|
2023-03-27 15:35:39 +00:00
|
|
|
}
|
|
|
|
|
2023-08-07 21:37:03 +00:00
|
|
|
// acl.EnterpriseMeta acl.AuthorizerContext follow rules for V1 resources since they integrate with the V1 acl subsystem.
|
|
|
|
// pbresource.Tenacy follows rules for V2 resources and the Resource service.
|
|
|
|
// Example:
|
|
|
|
//
|
2023-08-22 14:46:03 +00:00
|
|
|
// A CE namespace scoped resource:
|
2023-08-07 21:37:03 +00:00
|
|
|
// V1: EnterpriseMeta{}
|
|
|
|
// V2: Tenancy {Partition: "default", Namespace: "default"}
|
|
|
|
//
|
|
|
|
// An ENT namespace scoped resource:
|
|
|
|
// V1: EnterpriseMeta{Partition: "default", Namespace: "default"}
|
|
|
|
// V2: Tenancy {Partition: "default", Namespace: "default"}
|
|
|
|
//
|
|
|
|
// It is necessary to convert back and forth depending on which component supports which version, V1 or V2.
|
|
|
|
entMeta := v2TenancyToV1EntMeta(req.Id.Tenancy)
|
|
|
|
authz, authzContext, err := s.getAuthorizer(tokenFromContext(ctx), entMeta)
|
2023-03-27 15:35:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-04-11 11:10:14 +00:00
|
|
|
|
2023-08-07 21:37:03 +00:00
|
|
|
v1EntMetaToV2Tenancy(reg, entMeta, req.Id.Tenancy)
|
|
|
|
|
2023-09-22 14:53:55 +00:00
|
|
|
// ACL check usually comes before tenancy existence checks to not leak
|
|
|
|
// tenancy "existence", unless the ACL check requires the data payload
|
|
|
|
// to function.
|
|
|
|
authzNeedsData := false
|
|
|
|
err = reg.ACLs.Read(authz, authzContext, req.Id, nil)
|
2023-04-11 11:10:14 +00:00
|
|
|
switch {
|
2023-10-13 23:16:26 +00:00
|
|
|
case errors.Is(err, resource.ErrNeedResource):
|
2023-09-22 14:53:55 +00:00
|
|
|
authzNeedsData = true
|
|
|
|
err = nil
|
2023-04-11 11:10:14 +00:00
|
|
|
case acl.IsErrPermissionDenied(err):
|
|
|
|
return nil, status.Error(codes.PermissionDenied, err.Error())
|
|
|
|
case err != nil:
|
|
|
|
return nil, status.Errorf(codes.Internal, "failed read acl: %v", err)
|
|
|
|
}
|
|
|
|
|
2023-10-20 18:49:54 +00:00
|
|
|
// Check tenancy exists for the V2 resource.
|
|
|
|
if err = tenancyExists(reg, s.TenancyBridge, req.Id.Tenancy, codes.NotFound); err != nil {
|
2023-08-07 21:37:03 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-04-11 11:10:14 +00:00
|
|
|
resource, err := s.Backend.Read(ctx, readConsistencyFrom(ctx), req.Id)
|
|
|
|
switch {
|
|
|
|
case errors.Is(err, storage.ErrNotFound):
|
|
|
|
return nil, status.Error(codes.NotFound, err.Error())
|
|
|
|
case errors.As(err, &storage.GroupVersionMismatchError{}):
|
|
|
|
return nil, status.Error(codes.InvalidArgument, err.Error())
|
2023-09-22 14:53:55 +00:00
|
|
|
case err != nil:
|
2023-04-11 11:10:14 +00:00
|
|
|
return nil, status.Errorf(codes.Internal, "failed read: %v", err)
|
|
|
|
}
|
2023-09-22 14:53:55 +00:00
|
|
|
|
|
|
|
if authzNeedsData {
|
|
|
|
err = reg.ACLs.Read(authz, authzContext, req.Id, resource)
|
|
|
|
switch {
|
|
|
|
case acl.IsErrPermissionDenied(err):
|
|
|
|
return nil, status.Error(codes.PermissionDenied, err.Error())
|
|
|
|
case err != nil:
|
|
|
|
return nil, status.Errorf(codes.Internal, "failed read acl: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &pbresource.ReadResponse{Resource: resource}, nil
|
2023-03-27 15:35:39 +00:00
|
|
|
}
|
2023-04-17 21:33:20 +00:00
|
|
|
|
2023-11-03 20:03:07 +00:00
|
|
|
func (s *Server) ensureReadRequestValid(req *pbresource.ReadRequest) (*resource.Registration, error) {
|
2023-04-17 21:33:20 +00:00
|
|
|
if req.Id == nil {
|
2023-08-07 21:37:03 +00:00
|
|
|
return nil, status.Errorf(codes.InvalidArgument, "id is required")
|
2023-04-17 21:33:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := validateId(req.Id, "id"); err != nil {
|
2023-08-07 21:37:03 +00:00
|
|
|
return nil, err
|
2023-04-17 21:33:20 +00:00
|
|
|
}
|
2023-08-07 21:37:03 +00:00
|
|
|
|
|
|
|
// Check type exists.
|
|
|
|
reg, err := s.resolveType(req.Id.Type)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-10-27 13:55:02 +00:00
|
|
|
if err = checkV2Tenancy(s.UseV2Tenancy, req.Id.Type); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-08-07 21:37:03 +00:00
|
|
|
// Check scope
|
2023-12-13 15:06:39 +00:00
|
|
|
if err = validateScopedTenancy(reg.Scope, req.Id.Type, req.Id.Tenancy, false); err != nil {
|
2023-11-03 20:03:07 +00:00
|
|
|
return nil, err
|
2023-10-10 18:30:23 +00:00
|
|
|
}
|
2023-11-03 20:03:07 +00:00
|
|
|
|
2023-08-07 21:37:03 +00:00
|
|
|
return reg, nil
|
2023-04-17 21:33:20 +00:00
|
|
|
}
|