-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add validation webhook for permissions.rabbitmq.com
- Loading branch information
Showing
10 changed files
with
211 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package v1alpha2 | ||
|
||
import ( | ||
"fmt" | ||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/util/validation/field" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook" | ||
) | ||
|
||
func (p *Permission) SetupWebhookWithManager(mgr ctrl.Manager) error { | ||
return ctrl.NewWebhookManagedBy(mgr). | ||
For(p). | ||
Complete() | ||
} | ||
|
||
// +kubebuilder:webhook:verbs=create;update,path=/validate-rabbitmq-com-v1alpha2-permission,mutating=false,failurePolicy=fail,groups=rabbitmq.com,resources=permissions,versions=v1alpha2,name=vpermission.kb.io,sideEffects=none,admissionReviewVersions=v1 | ||
|
||
var _ webhook.Validator = &Permission{} | ||
|
||
// no validation on update | ||
func (p *Permission) ValidateCreate() error { | ||
return nil | ||
} | ||
|
||
// do not allow updates on spec.vhost, spec.user, and spec.rabbitmqClusterReference | ||
// updates on spec.permissions are allowed | ||
func (p *Permission) ValidateUpdate(old runtime.Object) error { | ||
oldPermission, ok := old.(*Permission) | ||
if !ok { | ||
return apierrors.NewBadRequest(fmt.Sprintf("expected a permission but got a %T", old)) | ||
} | ||
|
||
detailMsg := "updates on user, vhost and rabbitmqClusterReference are all forbidden" | ||
if p.Spec.User != oldPermission.Spec.User { | ||
return apierrors.NewForbidden(p.GroupResource(), p.Name, | ||
field.Forbidden(field.NewPath("spec", "user"), detailMsg)) | ||
} | ||
|
||
if p.Spec.Vhost != oldPermission.Spec.Vhost { | ||
return apierrors.NewForbidden(p.GroupResource(), p.Name, | ||
field.Forbidden(field.NewPath("spec", "vhost"), detailMsg)) | ||
} | ||
|
||
if p.Spec.RabbitmqClusterReference != oldPermission.Spec.RabbitmqClusterReference { | ||
return apierrors.NewForbidden(p.GroupResource(), p.Name, | ||
field.Forbidden(field.NewPath("spec", "rabbitmqClusterReference"), detailMsg)) | ||
} | ||
return nil | ||
} | ||
|
||
// no validation on delete | ||
func (p *Permission) ValidateDelete() error { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package v1alpha2 | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
var _ = Describe("permission webhook", func() { | ||
var permission = Permission{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test", | ||
}, | ||
Spec: PermissionSpec{ | ||
User: "test-user", | ||
Vhost: "/a-vhost", | ||
Permissions: VhostPermissions{ | ||
Configure: ".*", | ||
Read: ".*", | ||
Write: ".*", | ||
}, | ||
RabbitmqClusterReference: RabbitmqClusterReference{ | ||
Name: "a-cluster", | ||
}, | ||
}, | ||
} | ||
|
||
It("does not allow updates on user", func() { | ||
newPermission := permission.DeepCopy() | ||
newPermission.Spec.User = "new-user" | ||
Expect(apierrors.IsForbidden(newPermission.ValidateUpdate(&permission))).To(BeTrue()) | ||
}) | ||
|
||
It("does not allow updates on vhost", func() { | ||
newPermission := permission.DeepCopy() | ||
newPermission.Spec.Vhost = "new-vhost" | ||
Expect(apierrors.IsForbidden(newPermission.ValidateUpdate(&permission))).To(BeTrue()) | ||
}) | ||
|
||
It("does not allow updates on RabbitmqClusterReference", func() { | ||
newPermission := permission.DeepCopy() | ||
newPermission.Spec.RabbitmqClusterReference = RabbitmqClusterReference{ | ||
Name: "new-cluster", | ||
} | ||
Expect(apierrors.IsForbidden(newPermission.ValidateUpdate(&permission))).To(BeTrue()) | ||
}) | ||
|
||
It("allows updates on permission.spec.permissions.configure", func() { | ||
newPermission := permission.DeepCopy() | ||
newPermission.Spec.Permissions.Configure = "?" | ||
Expect(newPermission.ValidateUpdate(&permission)).To(Succeed()) | ||
}) | ||
|
||
It("allows updates on permission.spec.permissions.read", func() { | ||
newPermission := permission.DeepCopy() | ||
newPermission.Spec.Permissions.Read = "?" | ||
Expect(newPermission.ValidateUpdate(&permission)).To(Succeed()) | ||
}) | ||
|
||
It("allows updates on permission.spec.permissions.write", func() { | ||
newPermission := permission.DeepCopy() | ||
newPermission.Spec.Permissions.Write = "?" | ||
Expect(newPermission.ValidateUpdate(&permission)).To(Succeed()) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters