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-11-03 20:03:07 +00:00
|
|
|
reg, err := s.ensureWatchListRequestValid(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
|
|
|
}
|
|
|
|
|
2024-02-02 21:11:05 +00:00
|
|
|
var resource *pbresource.Resource
|
|
|
|
switch {
|
|
|
|
case event.GetUpsert() != nil:
|
|
|
|
resource = event.GetUpsert().GetResource()
|
|
|
|
case event.GetDelete() != nil:
|
|
|
|
resource = event.GetDelete().GetResource()
|
|
|
|
case event.GetEndOfSnapshot() != nil:
|
|
|
|
// skip the rest and send the event.
|
|
|
|
if err = stream.Send(event); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
default:
|
|
|
|
// skip unknown type of operation
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// From here on out we assume the event is operating on a non-nil resource.
|
|
|
|
|
2023-04-11 11:10:14 +00:00
|
|
|
// drop group versions that don't match
|
2024-02-02 21:11:05 +00:00
|
|
|
if resource.Id.Type.GroupVersion != req.Type.GroupVersion {
|
2023-03-27 19:37:54 +00:00
|
|
|
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.
|
2024-02-02 21:11:05 +00:00
|
|
|
entMeta = v2TenancyToV1EntMeta(resource.Id.Tenancy)
|
2023-08-21 20:02:23 +00:00
|
|
|
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
|
2024-02-02 21:11:05 +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 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-11-03 20:03:07 +00:00
|
|
|
func (s *Server) ensureWatchListRequestValid(req *pbresource.WatchListRequest) (*resource.Registration, error) {
|
|
|
|
if req.Type == nil {
|
|
|
|
return nil, status.Errorf(codes.InvalidArgument, "type is required")
|
2023-08-21 20:02:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check type exists.
|
|
|
|
reg, err := s.resolveType(req.Type)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-02-20 20:42:31 +00:00
|
|
|
// Ignore return value since read ops are allowed but will log a warning if the feature is
|
|
|
|
// not enabled in the license.
|
|
|
|
_ = s.FeatureCheck(reg)
|
|
|
|
|
2023-11-03 20:03:07 +00:00
|
|
|
// if no tenancy is passed defaults to wildcard
|
|
|
|
if req.Tenancy == nil {
|
|
|
|
req.Tenancy = wildcardTenancyFor(reg.Scope)
|
|
|
|
}
|
|
|
|
|
2023-10-27 13:55:02 +00:00
|
|
|
if err = checkV2Tenancy(s.UseV2Tenancy, req.Type); 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-21 20:02:23 +00:00
|
|
|
|
2023-11-03 20:03:07 +00:00
|
|
|
// Check scope
|
2023-12-13 15:06:39 +00:00
|
|
|
if err = validateScopedTenancy(reg.Scope, req.Type, req.Tenancy, true); err != nil {
|
2023-11-03 20:03:07 +00:00
|
|
|
return nil, err
|
2023-08-21 20:02:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return reg, nil
|
2023-05-10 09:38:48 +00:00
|
|
|
}
|
2023-11-03 20:03:07 +00:00
|
|
|
|
|
|
|
func wildcardTenancyFor(scope resource.Scope) *pbresource.Tenancy {
|
|
|
|
var defaultTenancy *pbresource.Tenancy
|
|
|
|
|
|
|
|
switch scope {
|
|
|
|
case resource.ScopeCluster:
|
2024-01-29 20:08:31 +00:00
|
|
|
defaultTenancy = &pbresource.Tenancy{}
|
2023-11-03 20:03:07 +00:00
|
|
|
case resource.ScopePartition:
|
|
|
|
defaultTenancy = &pbresource.Tenancy{
|
|
|
|
Partition: storage.Wildcard,
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
defaultTenancy = &pbresource.Tenancy{
|
|
|
|
Partition: storage.Wildcard,
|
|
|
|
Namespace: storage.Wildcard,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return defaultTenancy
|
|
|
|
}
|