Skip to content

Commit

Permalink
ssa: Detect immutable errors from CEL and custom webhooks
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Prodan <stefan.prodan@gmail.com>
  • Loading branch information
stefanprodan committed Jul 31, 2023
1 parent 4104c21 commit eb41f80
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
8 changes: 8 additions & 0 deletions ssa/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"encoding/json"
"fmt"
"io"
"regexp"
"strings"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -291,6 +292,13 @@ func IsImmutableError(err error) bool {
if errors.IsConflict(err) || errors.IsInvalid(err) {
return true
}

// Detect immutable errors returned by custom admission webhooks and Kubernetes CEL
// https://kubernetes.io/blog/2022/09/29/enforce-immutability-using-cel/#immutablility-after-first-modification
if match, _ := regexp.MatchString(`.*is\simmutable.*`, err.Error()); match {
return true
}

return false
}

Expand Down
32 changes: 32 additions & 0 deletions ssa/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,35 @@ stringData:
})
}
}

func TestIsImmutableError(t *testing.T) {
testCases := []struct {
name string
err error
match bool
}{
{
name: "CEL immutable error",
err: fmt.Errorf(`the ImmutableSinceFirstWrite "test1" is invalid: value: Invalid value: "string": Value is immutable`),
match: true,
},
{
name: "Custom admission immutable error",
err: fmt.Errorf(`the IAMPolicyMember's spec is immutable: admission webhook "deny-immutable-field-updates.cnrm.cloud.google.com" denied the request: the IAMPolicyMember's spec is immutable`),
match: true,
},
{
name: "Not immutable error",
err: fmt.Errorf(`is not immutable`),
match: false,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
g := NewWithT(t)

g.Expect(IsImmutableError(tc.err)).To(BeIdenticalTo(tc.match))
})
}
}

0 comments on commit eb41f80

Please sign in to comment.