2023-04-20 12:40:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2023-04-06 13:58:54 +00:00
|
|
|
package resource
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
|
2023-04-12 21:22:44 +00:00
|
|
|
"github.com/hashicorp/consul/acl"
|
2023-04-06 13:58:54 +00:00
|
|
|
"github.com/hashicorp/consul/internal/storage"
|
|
|
|
"github.com/hashicorp/consul/proto-public/pbresource"
|
|
|
|
)
|
|
|
|
|
2023-04-14 13:18:24 +00:00
|
|
|
// Deletes a resource.
|
|
|
|
// - To delete a resource regardless of the stored version, set Version = ""
|
|
|
|
// - Supports deleting a resource by name, hence Id.Uid may be empty.
|
|
|
|
// - Delete of a previously deleted or non-existent resource is a no-op to support idempotency.
|
|
|
|
// - Errors with Aborted if the requested Version does not match the stored Version.
|
|
|
|
// - Errors with PermissionDenied if ACL check fails
|
2023-04-06 13:58:54 +00:00
|
|
|
//
|
2023-04-14 13:18:24 +00:00
|
|
|
// TODO(spatel): Move docs to the proto file
|
2023-04-06 13:58:54 +00:00
|
|
|
func (s *Server) Delete(ctx context.Context, req *pbresource.DeleteRequest) (*pbresource.DeleteResponse, error) {
|
2023-04-17 21:33:20 +00:00
|
|
|
if err := validateDeleteRequest(req); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-04-06 13:58:54 +00:00
|
|
|
reg, err := s.resolveType(req.Id.Type)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-04-12 21:22:44 +00:00
|
|
|
authz, err := s.getAuthorizer(tokenFromContext(ctx))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = reg.ACLs.Write(authz, req.Id)
|
|
|
|
switch {
|
|
|
|
case acl.IsErrPermissionDenied(err):
|
|
|
|
return nil, status.Error(codes.PermissionDenied, err.Error())
|
|
|
|
case err != nil:
|
|
|
|
return nil, status.Errorf(codes.Internal, "failed write acl: %v", err)
|
|
|
|
}
|
2023-04-06 13:58:54 +00:00
|
|
|
|
2023-04-14 13:18:24 +00:00
|
|
|
// The storage backend requires a Version and Uid to delete a resource based
|
|
|
|
// on CAS semantics. When either are not provided, the resource must be read
|
|
|
|
// with a strongly consistent read to retrieve either or both.
|
|
|
|
//
|
|
|
|
// n.b.: There is a chance DeleteCAS may fail with a storage.ErrCASFailure
|
|
|
|
// if an update occurs between the Read and DeleteCAS. Consider refactoring
|
|
|
|
// to use retryCAS() similar to the Write endpoint to close this gap.
|
|
|
|
deleteVersion := req.Version
|
|
|
|
deleteId := req.Id
|
|
|
|
if deleteVersion == "" || deleteId.Uid == "" {
|
2023-04-06 13:58:54 +00:00
|
|
|
existing, err := s.Backend.Read(ctx, storage.StrongConsistency, req.Id)
|
|
|
|
switch {
|
|
|
|
case err == nil:
|
2023-04-14 13:18:24 +00:00
|
|
|
deleteVersion = existing.Version
|
|
|
|
deleteId = existing.Id
|
2023-04-06 13:58:54 +00:00
|
|
|
case errors.Is(err, storage.ErrNotFound):
|
2023-04-14 13:18:24 +00:00
|
|
|
// Deletes are idempotent so no-op when not found
|
2023-04-06 13:58:54 +00:00
|
|
|
return &pbresource.DeleteResponse{}, nil
|
|
|
|
default:
|
|
|
|
return nil, status.Errorf(codes.Internal, "failed read: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-14 13:18:24 +00:00
|
|
|
err = s.Backend.DeleteCAS(ctx, deleteId, deleteVersion)
|
2023-04-06 13:58:54 +00:00
|
|
|
switch {
|
|
|
|
case err == nil:
|
|
|
|
return &pbresource.DeleteResponse{}, nil
|
|
|
|
case errors.Is(err, storage.ErrCASFailure):
|
|
|
|
return nil, status.Error(codes.Aborted, err.Error())
|
|
|
|
default:
|
|
|
|
return nil, status.Errorf(codes.Internal, "failed delete: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2023-04-17 21:33:20 +00:00
|
|
|
|
|
|
|
func validateDeleteRequest(req *pbresource.DeleteRequest) error {
|
|
|
|
if req.Id == nil {
|
|
|
|
return status.Errorf(codes.InvalidArgument, "id is required")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := validateId(req.Id, "id"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|