Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ssa: Detect immutable errors from CEL and custom webhooks #610

Merged
merged 1 commit into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 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 @@ -284,13 +285,22 @@ func IsKustomization(object *unstructured.Unstructured) bool {
return false
}

var matchImmutableFieldErr = regexp.MustCompile(`.*is\simmutable.*`)

// IsImmutableError checks if the given error is an immutable error.
func IsImmutableError(err error) bool {
// Detect immutability like kubectl does
// https://github.com/kubernetes/kubectl/blob/8165f83007/pkg/cmd/apply/patcher.go#L201
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 matchImmutableFieldErr.MatchString(err.Error()) {
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))
})
}
}
Loading