2023-05-08 17:26:19 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-05-08 17:26:19 +00:00
|
|
|
|
|
|
|
package resource
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/acl"
|
2023-08-24 15:49:46 +00:00
|
|
|
"github.com/hashicorp/consul/internal/resource"
|
2023-05-08 17:26:19 +00:00
|
|
|
"github.com/hashicorp/consul/proto-public/pbresource"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *Server) ListByOwner(ctx context.Context, req *pbresource.ListByOwnerRequest) (*pbresource.ListByOwnerResponse, error) {
|
2023-11-03 20:03:07 +00:00
|
|
|
reg, err := s.ensureListByOwnerRequestValid(req)
|
2023-08-24 15:49:46 +00:00
|
|
|
if err != nil {
|
2023-05-08 17:26:19 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-08-24 15:49:46 +00:00
|
|
|
// Convert v2 request tenancy to v1 for ACL subsystem.
|
|
|
|
entMeta := v2TenancyToV1EntMeta(req.Owner.Tenancy)
|
|
|
|
token := tokenFromContext(ctx)
|
|
|
|
|
|
|
|
// Fill entMeta with token tenancy when empty.
|
|
|
|
authz, authzContext, err := s.getAuthorizer(token, entMeta)
|
2023-05-08 17:26:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-08-24 15:49:46 +00:00
|
|
|
// Handle defaulting empty tenancy units from request.
|
|
|
|
v1EntMetaToV2Tenancy(reg, entMeta, req.Owner.Tenancy)
|
|
|
|
|
|
|
|
// Check list ACL before verifying tenancy exists to not leak tenancy existence.
|
|
|
|
err = reg.ACLs.List(authz, authzContext)
|
|
|
|
switch {
|
|
|
|
case acl.IsErrPermissionDenied(err):
|
|
|
|
return nil, status.Error(codes.PermissionDenied, err.Error())
|
|
|
|
case err != nil:
|
|
|
|
return nil, status.Errorf(codes.Internal, "failed list acl: %v", err)
|
2023-05-08 17:26:19 +00:00
|
|
|
}
|
|
|
|
|
2023-10-20 18:49:54 +00:00
|
|
|
// Check tenancy exists for the v2 resource.
|
|
|
|
if err = tenancyExists(reg, s.TenancyBridge, req.Owner.Tenancy, codes.InvalidArgument); err != nil {
|
2023-05-08 17:26:19 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-08-24 15:49:46 +00:00
|
|
|
// Get owned resources.
|
|
|
|
children, err := s.Backend.ListByOwner(ctx, req.Owner)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "failed list by owner: %v", err)
|
|
|
|
}
|
|
|
|
|
2023-05-08 17:26:19 +00:00
|
|
|
result := make([]*pbresource.Resource, 0)
|
|
|
|
for _, child := range children {
|
2023-08-24 15:49:46 +00:00
|
|
|
// Retrieve child type's registration to access read ACL hook.
|
|
|
|
childReg, err := s.resolveType(child.Id.Type)
|
2023-05-08 17:26:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-08-24 15:49:46 +00:00
|
|
|
// Rebuild authorizer if tenancy not identical between owner and child (child scope
|
|
|
|
// may be narrower).
|
|
|
|
childAuthz := authz
|
|
|
|
childAuthzContext := authzContext
|
|
|
|
if !resource.EqualTenancy(req.Owner.Tenancy, child.Id.Tenancy) {
|
|
|
|
childEntMeta := v2TenancyToV1EntMeta(child.Id.Tenancy)
|
|
|
|
childAuthz, childAuthzContext, err = s.getAuthorizer(token, childEntMeta)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter out children that fail real ACL.
|
2023-09-22 14:53:55 +00:00
|
|
|
err = childReg.ACLs.Read(childAuthz, childAuthzContext, child.Id, child)
|
2023-05-08 17:26:19 +00:00
|
|
|
switch {
|
|
|
|
case acl.IsErrPermissionDenied(err):
|
|
|
|
continue
|
|
|
|
case err != nil:
|
|
|
|
return nil, status.Errorf(codes.Internal, "failed read acl: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
result = append(result, child)
|
|
|
|
}
|
|
|
|
return &pbresource.ListByOwnerResponse{Resources: result}, nil
|
|
|
|
}
|
|
|
|
|
2023-11-03 20:03:07 +00:00
|
|
|
func (s *Server) ensureListByOwnerRequestValid(req *pbresource.ListByOwnerRequest) (*resource.Registration, error) {
|
2023-05-08 17:26:19 +00:00
|
|
|
if req.Owner == nil {
|
2023-08-24 15:49:46 +00:00
|
|
|
return nil, status.Errorf(codes.InvalidArgument, "owner is required")
|
2023-05-08 17:26:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := validateId(req.Owner, "owner"); err != nil {
|
2023-08-24 15:49:46 +00:00
|
|
|
return nil, err
|
2023-05-08 17:26:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if req.Owner.Uid == "" {
|
2023-08-24 15:49:46 +00:00
|
|
|
return nil, status.Errorf(codes.InvalidArgument, "owner uid is required")
|
2023-05-08 17:26:19 +00:00
|
|
|
}
|
2023-08-24 15:49:46 +00:00
|
|
|
|
|
|
|
reg, err := s.resolveType(req.Owner.Type)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-10-27 13:55:02 +00:00
|
|
|
if err = checkV2Tenancy(s.UseV2Tenancy, req.Owner.Type); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-08-24 15:49:46 +00:00
|
|
|
// Error when partition scoped and namespace not empty.
|
|
|
|
if reg.Scope == resource.ScopePartition && req.Owner.Tenancy.Namespace != "" {
|
|
|
|
return nil, status.Errorf(
|
|
|
|
codes.InvalidArgument,
|
|
|
|
"partition scoped type %s cannot have a namespace. got: %s",
|
|
|
|
resource.ToGVK(req.Owner.Type),
|
|
|
|
req.Owner.Tenancy.Namespace,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return reg, nil
|
2023-05-08 17:26:19 +00:00
|
|
|
}
|