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

Remove security enforcement label from namespace in SaaS #531

Merged
merged 2 commits into from
Apr 30, 2024
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
11 changes: 11 additions & 0 deletions go-chaos/internal/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,14 @@ func (c K8Client) CreateStatefulSetWithLabelsAndName(t *testing.T, selector *met

require.NoError(t, err)
}

func (c *K8Client) createSaaSNamespace(t *testing.T) {
namespace := v1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: c.GetCurrentNamespace(),
Labels: map[string]string{"pod-security.kubernetes.io/enforce": "true"},
},
}
_, err := c.Clientset.CoreV1().Namespaces().Create(context.TODO(), &namespace, metav1.CreateOptions{})
require.NoError(t, err)
}
18 changes: 18 additions & 0 deletions go-chaos/internal/k8helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,31 @@ func createK8Client(settings KubernetesSettings) (K8Client, error) {

if client.SaaSEnv {
LogVerbose("Running experiment in SaaS environment.")
err = prepareSaaSTargetCluster(client)
if err != nil {
return K8Client{}, err
}
} else {
LogVerbose("Running experiment in self-managed environment.")
}

return client, nil
}

func prepareSaaSTargetCluster(client K8Client) error {
LogVerbose("Pausing reconciliation preventive.")
err := client.PauseReconciliation()
if err != nil {
return err
}

err = client.disableSaaSNamespaceSecurityLabel()
if err != nil {
return err
}
return nil
}

func internalCreateClient(settings KubernetesSettings) (K8Client, error) {
clientConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&clientcmd.ClientConfigLoadingRules{ExplicitPath: settings.kubeConfigPath},
Expand Down
14 changes: 14 additions & 0 deletions go-chaos/internal/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package internal

import (
"context"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
)
Expand Down Expand Up @@ -84,3 +85,16 @@ func (c K8Client) getWorkerLabels() string {
}
return labels.Set(labelSelector.MatchLabels).String()
}

func (c K8Client) disableSaaSNamespaceSecurityLabel() error {
ns, err := c.Clientset.CoreV1().Namespaces().Get(context.TODO(), c.GetCurrentNamespace(), metav1.GetOptions{})
if err != nil {
return err
}

LogVerbose("Removing namespace label: 'pod-security.kubernetes.io/enforce' to allow further privileges.")
delete(ns.Labels, "pod-security.kubernetes.io/enforce")

_, err = c.Clientset.CoreV1().Namespaces().Update(context.TODO(), ns, metav1.UpdateOptions{})
return err
}
18 changes: 18 additions & 0 deletions go-chaos/internal/labels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
package internal

import (
"context"
"github.com/stretchr/testify/require"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -63,3 +66,18 @@ func Test_shouldGetSaasGatewayLabels(t *testing.T) {
// then
assert.Equal(t, expected, actual, "Labels should be equal")
}

func Test_shouldRemoveNamespaceLabel(t *testing.T) {
// given
k8Client := CreateFakeClient()
k8Client.createSaaSNamespace(t)

// when
err := k8Client.disableSaaSNamespaceSecurityLabel()

// then
require.NoError(t, err)
namespace, err := k8Client.Clientset.CoreV1().Namespaces().Get(context.TODO(), k8Client.GetCurrentNamespace(), metav1.GetOptions{})
require.NoError(t, err)
assert.Empty(t, namespace.Labels)
}
Loading