Files
substrate/cmd/ateapi/internal/controlapi/update_actor.go
T
Luiz Oliveira c538b68ba6 Fix TOCTOU race in UpdateActorSnapshotTag (#854)
This is a very similar fix to #829

We're also removing the precondition as the storage layer function
arguments and passing it as a wrapper around the closure functions.

https://github.com/agent-substrate/substrate/issues/763

- [x] Tests pass
- [x] Appropriate changes to documentation are included in the PR
2026-08-12 10:00:22 -04:00

87 lines
3.0 KiB
Go

// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package controlapi
import (
"context"
"errors"
"fmt"
"github.com/agent-substrate/substrate/cmd/ateapi/internal/store"
"github.com/agent-substrate/substrate/internal/fieldmask"
"github.com/agent-substrate/substrate/internal/resources"
"github.com/agent-substrate/substrate/pkg/proto/ateapipb"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"k8s.io/apimachinery/pkg/util/validation/field"
)
// actorMutableFields lists the Actor field paths a client may name in an
// UpdateActor update_mask.
var actorMutableFields = fieldmask.NewMutableFields(
"worker_selector",
"worker_selector.match_labels",
)
func (s *Service) UpdateActor(ctx context.Context, req *ateapipb.UpdateActorRequest) (*ateapipb.Actor, error) {
if errs := validateUpdateActorRequest(req); len(errs) > 0 {
return nil, toGRPCStatusError(errs)
}
in := req.GetActor()
actorRef := resources.ActorRefFromActor(in)
setSpanActorRefAttributes(ctx, actorRef)
storedActor, err := s.persistence.UpdateActor(ctx, actorRef, store.WithPrecondition(in, func(toUpdate *ateapipb.Actor) error {
fieldmask.Apply(toUpdate, in, req.GetUpdateMask())
return nil
}))
if err != nil {
if errors.Is(err, store.ErrVersionConflict) {
return nil, status.Error(codes.Aborted, "concurrent update conflict, please retry")
}
if errors.Is(err, store.ErrUIDConflict) {
return nil, status.Errorf(codes.Aborted, "actor %s/%s not found with uid %s", in.GetMetadata().GetAtespace(), in.GetMetadata().GetName(), in.GetMetadata().GetUid())
}
if errors.Is(err, store.ErrNotFound) {
return nil, status.Errorf(codes.NotFound, "actor %s not found", actorRef)
}
return nil, fmt.Errorf("while updating actor: %w", err)
}
setSpanActorAttributes(ctx, storedActor)
return storedActor, nil
}
func validateUpdateActorRequest(req *ateapipb.UpdateActorRequest) field.ErrorList {
var fldPath *field.Path
var errs field.ErrorList
actor := req.GetActor()
actorPath := fldPath.Child("actor")
if actor == nil {
return field.ErrorList{field.Required(actorPath, "")}
}
errs = append(errs, resources.ValidateResourceMetadataRef(actor.GetMetadata(), actorPath.Child("metadata"))...)
errs = append(errs, fieldmask.Validate(req.GetUpdateMask(), actorMutableFields, fldPath.Child("update_mask"))...)
if selector := actor.GetWorkerSelector(); selector != nil {
errs = append(errs, validateSelector(selector, actorPath.Child("worker_selector"))...)
}
return errs
}