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 21:25:27 +00:00
|
|
|
package resource
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2023-04-11 11:10:14 +00:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/acl"
|
2023-08-15 21:57:59 +00:00
|
|
|
"github.com/hashicorp/consul/internal/resource"
|
2023-03-27 21:25:27 +00:00
|
|
|
"github.com/hashicorp/consul/internal/storage"
|
|
|
|
"github.com/hashicorp/consul/proto-public/pbresource"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *Server) List(ctx context.Context, req *pbresource.ListRequest) (*pbresource.ListResponse, error) {
|
2023-08-15 21:57:59 +00:00
|
|
|
reg, err := s.validateListRequest(req)
|
2023-04-11 11:10:14 +00:00
|
|
|
if err != nil {
|
2023-03-27 21:25:27 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-08-15 21:57:59 +00:00
|
|
|
// v1 ACL subsystem is "wildcard" aware so just pass on through.
|
|
|
|
entMeta := v2TenancyToV1EntMeta(req.Tenancy)
|
|
|
|
token := tokenFromContext(ctx)
|
|
|
|
authz, authzContext, err := s.getAuthorizer(token, entMeta)
|
2023-03-27 21:25:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-08-15 21:57:59 +00:00
|
|
|
// Check ACLs.
|
|
|
|
err = reg.ACLs.List(authz, authzContext)
|
2023-04-11 11:10:14 +00:00
|
|
|
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-08-15 21:57:59 +00:00
|
|
|
// Ensure we're defaulting correctly when request tenancy units are empty.
|
|
|
|
v1EntMetaToV2Tenancy(reg, entMeta, req.Tenancy)
|
|
|
|
|
2023-04-11 11:10:14 +00:00
|
|
|
resources, err := s.Backend.List(
|
|
|
|
ctx,
|
|
|
|
readConsistencyFrom(ctx),
|
|
|
|
storage.UnversionedTypeFrom(req.Type),
|
|
|
|
req.Tenancy,
|
|
|
|
req.NamePrefix,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "failed list: %v", err)
|
|
|
|
}
|
|
|
|
|
2023-03-27 21:25:27 +00:00
|
|
|
result := make([]*pbresource.Resource, 0)
|
|
|
|
for _, resource := range resources {
|
2023-08-15 21:57:59 +00:00
|
|
|
// Filter out non-matching GroupVersion.
|
2023-04-11 11:10:14 +00:00
|
|
|
if resource.Id.Type.GroupVersion != req.Type.GroupVersion {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-08-15 21:57:59 +00:00
|
|
|
// Need to rebuild authorizer per resource since wildcard inputs may
|
|
|
|
// result in different tenancies. Consider caching per tenancy if this
|
|
|
|
// is deemed expensive.
|
|
|
|
entMeta = v2TenancyToV1EntMeta(resource.Id.Tenancy)
|
|
|
|
authz, authzContext, err = s.getAuthorizer(token, entMeta)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter out items that don't pass read ACLs.
|
2023-09-22 14:53:55 +00:00
|
|
|
err = reg.ACLs.Read(authz, authzContext, resource.Id, resource)
|
2023-04-11 11:10:14 +00:00
|
|
|
switch {
|
|
|
|
case acl.IsErrPermissionDenied(err):
|
|
|
|
continue
|
|
|
|
case err != nil:
|
|
|
|
return nil, status.Errorf(codes.Internal, "failed read acl: %v", err)
|
2023-03-27 21:25:27 +00:00
|
|
|
}
|
2023-04-11 11:10:14 +00:00
|
|
|
result = append(result, resource)
|
2023-03-27 21:25:27 +00:00
|
|
|
}
|
|
|
|
return &pbresource.ListResponse{Resources: result}, nil
|
|
|
|
}
|
2023-05-10 09:38:48 +00:00
|
|
|
|
2023-08-15 21:57:59 +00:00
|
|
|
func (s *Server) validateListRequest(req *pbresource.ListRequest) (*resource.Registration, error) {
|
2023-05-10 09:38:48 +00:00
|
|
|
var field string
|
|
|
|
switch {
|
|
|
|
case req.Type == nil:
|
|
|
|
field = "type"
|
|
|
|
case req.Tenancy == nil:
|
|
|
|
field = "tenancy"
|
|
|
|
}
|
2023-08-15 21:57:59 +00:00
|
|
|
|
|
|
|
if field != "" {
|
|
|
|
return nil, status.Errorf(codes.InvalidArgument, "%s is required", field)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check type exists.
|
|
|
|
reg, err := s.resolveType(req.Type)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-10-16 17:55:30 +00:00
|
|
|
if err := validateWildcardTenancy(req.Tenancy, req.NamePrefix); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-08-15 21:57:59 +00:00
|
|
|
|
|
|
|
// Error when partition scoped and namespace not empty.
|
|
|
|
if reg.Scope == resource.ScopePartition && req.Tenancy.Namespace != "" {
|
|
|
|
return nil, status.Errorf(
|
|
|
|
codes.InvalidArgument,
|
|
|
|
"partition scoped type %s cannot have a namespace. got: %s",
|
|
|
|
resource.ToGVK(req.Type),
|
|
|
|
req.Tenancy.Namespace,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return reg, nil
|
2023-05-10 09:38:48 +00:00
|
|
|
}
|