2023-03-28 22:48:58 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
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-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-05-10 09:38:48 +00:00
|
|
|
if err := validateWatchListRequest(req); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-27 19:37:54 +00:00
|
|
|
// check type exists
|
2023-04-11 11:10:14 +00:00
|
|
|
reg, err := s.resolveType(req.Type)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
authz, err := s.getAuthorizer(tokenFromContext(stream.Context()))
|
|
|
|
if err != nil {
|
2023-03-27 19:37:54 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-04-11 11:10:14 +00:00
|
|
|
// check acls
|
|
|
|
err = reg.ACLs.List(authz, req.Tenancy)
|
|
|
|
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-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-04-11 11:10:14 +00:00
|
|
|
// filter out items that don't pass read ACLs
|
|
|
|
err = reg.ACLs.Read(authz, event.Resource.Id)
|
|
|
|
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
|
|
|
|
|
|
|
func validateWatchListRequest(req *pbresource.WatchListRequest) error {
|
|
|
|
var field string
|
|
|
|
switch {
|
|
|
|
case req.Type == nil:
|
|
|
|
field = "type"
|
|
|
|
case req.Tenancy == nil:
|
|
|
|
field = "tenancy"
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return status.Errorf(codes.InvalidArgument, "%s is required", field)
|
|
|
|
}
|