From 88f043027c92641fa106caf3afa8bc8a74938929 Mon Sep 17 00:00:00 2001 From: Julian Sommer <115516918+juliansommer@users.noreply.github.com> Date: Wed, 1 Jul 2026 14:30:23 +1000 Subject: [PATCH] chore: last cleanup --- spinifex/daemon/daemon_system_dispatch.go | 5 ++-- spinifex/formation/formation.go | 6 ++--- spinifex/handlers/ecs/records.go | 9 ++++--- spinifex/handlers/iam/groups.go | 15 +++--------- spinifex/handlers/iam/service_impl.go | 30 +++++------------------ 5 files changed, 20 insertions(+), 45 deletions(-) diff --git a/spinifex/daemon/daemon_system_dispatch.go b/spinifex/daemon/daemon_system_dispatch.go index 8e6c6c1b6..0ddfa6625 100644 --- a/spinifex/daemon/daemon_system_dispatch.go +++ b/spinifex/daemon/daemon_system_dispatch.go @@ -293,8 +293,9 @@ func respondWithSystemTerminateError(msg *nats.Msg, errMsg string) { // string if subject does not start with prefix. Helper kept local to avoid // touching utils for a one-liner. func splitSubjectTail(subject, prefix string) string { - if len(subject) <= len(prefix) || subject[:len(prefix)] != prefix { + tail, ok := strings.CutPrefix(subject, prefix) + if !ok { return "" } - return subject[len(prefix):] + return tail } diff --git a/spinifex/formation/formation.go b/spinifex/formation/formation.go index 569a1c3ad..1e990c970 100644 --- a/spinifex/formation/formation.go +++ b/spinifex/formation/formation.go @@ -11,6 +11,7 @@ import ( "log/slog" "maps" "net/http" + "strings" "sync" "time" @@ -295,11 +296,10 @@ func (fs *FormationServer) validateToken(r *http.Request) error { return fmt.Errorf("missing Authorization header") } - const prefix = "Bearer " - if len(auth) < len(prefix) || auth[:len(prefix)] != prefix { + token, ok := strings.CutPrefix(auth, "Bearer ") + if !ok { return fmt.Errorf("invalid Authorization header format") } - token := auth[len(prefix):] if subtle.ConstantTimeCompare([]byte(token), []byte(fs.joinToken)) != 1 { return fmt.Errorf("invalid join token") diff --git a/spinifex/handlers/ecs/records.go b/spinifex/handlers/ecs/records.go index f16836d94..67909d67a 100644 --- a/spinifex/handlers/ecs/records.go +++ b/spinifex/handlers/ecs/records.go @@ -2,6 +2,7 @@ package handlers_ecs import ( "fmt" + "strings" "time" "github.com/mulgadc/spinifex/spinifex/handlers/ecs/bus" @@ -66,11 +67,11 @@ func serviceTaskGroup(name string) string { // serviceNameFromGroup returns the service name encoded in a task group, or "" // when the group is not a service group. func serviceNameFromGroup(group string) string { - const p = "service:" - if len(group) > len(p) && group[:len(p)] == p { - return group[len(p):] + name, ok := strings.CutPrefix(group, "service:") + if !ok { + return "" } - return "" + return name } // ClusterRecord is the persisted cluster meta at ClusterMetaKey. diff --git a/spinifex/handlers/iam/groups.go b/spinifex/handlers/iam/groups.go index 29cec97ae..04febc95f 100644 --- a/spinifex/handlers/iam/groups.go +++ b/spinifex/handlers/iam/groups.go @@ -239,21 +239,12 @@ func (s *IAMServiceImpl) RemoveUserFromGroup(accountID string, input *iam.Remove // Operate purely on the membership reference so a dangling pointer to an // already-deleted group is still cleanable; never fetch the group here. - found := false - remaining := make([]string, 0, len(user.Groups)) - for _, name := range user.Groups { - if name == groupName { - found = true - } else { - remaining = append(remaining, name) - } - } - - if !found { + idx := slices.Index(user.Groups, groupName) + if idx < 0 { return nil, errors.New(awserrors.ErrorIAMNoSuchEntity) } - user.Groups = remaining + user.Groups = slices.Delete(user.Groups, idx, idx+1) userData, err := json.Marshal(user) if err != nil { return nil, fmt.Errorf("marshal user: %w", err) diff --git a/spinifex/handlers/iam/service_impl.go b/spinifex/handlers/iam/service_impl.go index 59922b798..11e8d86d3 100644 --- a/spinifex/handlers/iam/service_impl.go +++ b/spinifex/handlers/iam/service_impl.go @@ -529,22 +529,13 @@ func (s *IAMServiceImpl) DeleteAccessKey(accountID string, input *iam.DeleteAcce return nil, err } - found := false - remaining := make([]string, 0, len(user.AccessKeys)) - for _, keyID := range user.AccessKeys { - if keyID == accessKeyID { - found = true - } else { - remaining = append(remaining, keyID) - } - } - - if !found { + idx := slices.Index(user.AccessKeys, accessKeyID) + if idx < 0 { return nil, errors.New(awserrors.ErrorIAMNoSuchEntity) } // Update user record first to avoid orphaning the reference on crash. - user.AccessKeys = remaining + user.AccessKeys = slices.Delete(user.AccessKeys, idx, idx+1) userData, err := json.Marshal(user) if err != nil { return nil, fmt.Errorf("marshal user: %w", err) @@ -1239,21 +1230,12 @@ func (s *IAMServiceImpl) DetachUserPolicy(accountID string, input *iam.DetachUse return nil, err } - found := false - remaining := make([]string, 0, len(user.AttachedPolicies)) - for _, arn := range user.AttachedPolicies { - if arn == policyARN { - found = true - } else { - remaining = append(remaining, arn) - } - } - - if !found { + idx := slices.Index(user.AttachedPolicies, policyARN) + if idx < 0 { return nil, errors.New(awserrors.ErrorIAMNoSuchEntity) } - user.AttachedPolicies = remaining + user.AttachedPolicies = slices.Delete(user.AttachedPolicies, idx, idx+1) userData, err := json.Marshal(user) if err != nil { return nil, fmt.Errorf("marshal user: %w", err)