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 19:37:54 +00:00
|
|
|
package resource
|
|
|
|
|
|
|
|
import (
|
2023-05-15 11:35:10 +00:00
|
|
|
"errors"
|
|
|
|
|
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-21 20:02:23 +00:00
|
|
|
"github.com/hashicorp/consul/internal/resource"
|
2023-03-27 19:37:54 +00:00
|
|
|
"github.com/hashicorp/consul/internal/storage"
|
|
|
|
"github.com/hashicorp/consul/proto-public/pbresource"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *Server) WatchList(req *pbresource.WatchListRequest, stream pbresource.ResourceService_WatchListServer) error {
|
2023-08-21 20:02:23 +00:00
|
|
|
reg, err := s.validateWatchListRequest(req)
|
2023-04-11 11:10:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-08-21 20:02:23 +00:00
|
|
|
// v1 ACL subsystem is "wildcard" aware so just pass on through.
|
|
|
|
entMeta := v2TenancyToV1EntMeta(req.Tenancy)
|
|
|
|
token := tokenFromContext(stream.Context())
|
|
|
|
authz, authzContext, err := s.getAuthorizer(token, entMeta)
|
2023-04-11 11:10:14 +00:00
|
|
|
if err != nil {
|
2023-03-27 19:37:54 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-08-21 20:02:23 +00:00
|
|
|
// Check list ACL.
|
2023-08-15 21:57:59 +00:00
|
|
|
err = reg.ACLs.List(authz, authzContext)
|
2023-04-11 11:10:14 +00:00
|
|
|
switch {
|
|
|
|
case acl.IsErrPermissionDenied(err):
|
|
|
|
return status.Error(codes.PermissionDenied, err.Error())
|
|
|
|
case err != nil:
|
|
|
|
return status.Errorf(codes.Internal, "failed list acl: %v", err)
|
|
|
|
}
|
|
|
|
|
2023-08-21 20:02:23 +00:00
|
|
|
// Ensure we're defaulting correctly when request tenancy units are empty.
|
|
|
|
v1EntMetaToV2Tenancy(reg, entMeta, req.Tenancy)
|
|
|
|
|
2023-03-27 19:37:54 +00:00
|
|
|
unversionedType := storage.UnversionedTypeFrom(req.Type)
|
2023-04-04 16:30:06 +00:00
|
|
|
watch, err := s.Backend.WatchList(
|
2023-03-27 19:37:54 +00:00
|
|
|
stream.Context(),
|
|
|
|
unversionedType,
|
|
|
|
req.Tenancy,
|
|
|
|
req.NamePrefix,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-03-31 12:24:19 +00:00
|
|
|
defer watch.Close()
|
2023-03-27 19:37:54 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
event, err := watch.Next(stream.Context())
|
2023-05-15 11:35:10 +00:00
|
|
|
switch {
|
|
|
|
case errors.Is(err, storage.ErrWatchClosed):
|
|
|
|
return status.Error(codes.Aborted, "watch closed by the storage backend (possibly due to snapshot restoration)")
|
|
|
|
case err != nil:
|
2023-04-11 11:10:14 +00:00
|
|
|
return status.Errorf(codes.Internal, "failed next: %v", err)
|
2023-03-27 19:37:54 +00:00
|
|
|
}
|
|
|
|
|
2023-04-11 11:10:14 +00:00
|
|
|
// drop group versions that don't match
|
2023-03-27 19:37:54 +00:00
|
|
|
if event.Resource.Id.Type.GroupVersion != req.Type.GroupVersion {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-08-21 20:02:23 +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(event.Resource.Id.Tenancy)
|
|
|
|
authz, authzContext, err = s.getAuthorizer(token, entMeta)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-04-11 11:10:14 +00:00
|
|
|
// filter out items that don't pass read ACLs
|
2023-08-07 21:37:03 +00:00
|
|
|
err = reg.ACLs.Read(authz, authzContext, event.Resource.Id)
|
2023-04-11 11:10:14 +00:00
|
|
|
switch {
|
|
|
|
case acl.IsErrPermissionDenied(err):
|
|
|
|
continue
|
|
|
|
case err != nil:
|
|
|
|
return status.Errorf(codes.Internal, "failed read acl: %v", err)
|
|
|
|
}
|
|
|
|
|
2023-03-27 19:37:54 +00:00
|
|
|
if err = stream.Send(event); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-05-10 09:38:48 +00:00
|
|
|
|
2023-08-21 20:02:23 +00:00
|
|
|
func (s *Server) validateWatchListRequest(req *pbresource.WatchListRequest) (*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-21 20:02:23 +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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lowercase
|
|
|
|
resource.Normalize(req.Tenancy)
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|