-
Notifications
You must be signed in to change notification settings - Fork 270
Adding KAS-O helpers and assertions #2349
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| // Copied from KAS-O test/library/encryption/assertion.go: | ||
| // https://github.com/openshift/cluster-kube-apiserver-operator/blob/main/test/library/encryption/assertion.go | ||
| package encryption | ||
|
|
||
| import ( | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
|
|
||
| corev1 "k8s.io/api/core/v1" | ||
| "k8s.io/apimachinery/pkg/runtime" | ||
| "k8s.io/apimachinery/pkg/runtime/schema" | ||
|
|
||
| configv1 "github.com/openshift/api/config/v1" | ||
| ) | ||
|
|
||
| var DefaultTargetGRs = []schema.GroupResource{ | ||
| {Group: "", Resource: "secrets"}, | ||
| {Group: "", Resource: "configmaps"}, | ||
| } | ||
|
|
||
| func AssertSecretOfLifeEncrypted(t testing.TB, clientSet ClientSet, resource runtime.Object) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's open a new PR and move this function to the existing
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we merge first this pr after your PR merged |
||
| t.Helper() | ||
| secret, ok := resource.(*corev1.Secret) | ||
| if !ok { | ||
| t.Fatalf("expected *corev1.Secret, got %T", resource) | ||
| } | ||
| rawValue := GetRawSecretOfLife(t, clientSet, secret.Namespace) | ||
| if strings.Contains(rawValue, string(secret.Data["quote"])) { | ||
| t.Errorf("secret not encrypted, etcd value contains quote in plain text") | ||
| } | ||
| } | ||
|
|
||
| func AssertSecretOfLifeNotEncrypted(t testing.TB, clientSet ClientSet, resource runtime.Object) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| t.Helper() | ||
| secret, ok := resource.(*corev1.Secret) | ||
| if !ok { | ||
| t.Fatalf("expected *corev1.Secret, got %T", resource) | ||
| } | ||
| rawValue := GetRawSecretOfLife(t, clientSet, secret.Namespace) | ||
| if !strings.Contains(rawValue, string(secret.Data["quote"])) { | ||
| t.Errorf("secret not decrypted, etcd value does not contain quote in plain text") | ||
| } | ||
| } | ||
|
|
||
| func AssertSecretsAndConfigMaps(t testing.TB, clientSet ClientSet, expectedMode configv1.EncryptionType, namespace, labelSelector string) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| t.Helper() | ||
| assertSecrets(t, clientSet.Etcd, string(expectedMode)) | ||
| assertConfigMaps(t, clientSet.Etcd, string(expectedMode)) | ||
| AssertLastMigratedKey(t, clientSet.Kube, DefaultTargetGRs, namespace, labelSelector) | ||
| } | ||
|
|
||
| func assertSecrets(t testing.TB, etcdClient EtcdClient, expectedMode string) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this can be moved as is. |
||
| t.Logf("Checking if all Secrets where encrypted/decrypted for %q mode", expectedMode) | ||
| totalSecrets, err := VerifyResources(t, etcdClient, "/kubernetes.io/secrets/", expectedMode, false) | ||
| t.Logf("Verified %d Secrets", totalSecrets) | ||
| require.NoError(t, err) | ||
| } | ||
|
|
||
| func assertConfigMaps(t testing.TB, etcdClient EtcdClient, expectedMode string) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this can be moved as is. |
||
| t.Logf("Checking if all ConfigMaps where encrypted/decrypted for %q mode", expectedMode) | ||
| totalConfigMaps, err := VerifyResources(t, etcdClient, "/kubernetes.io/configmaps/", expectedMode, false) | ||
| t.Logf("Verified %d ConfigMaps", totalConfigMaps) | ||
| require.NoError(t, err) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| // Copied from KAS-O test/library/encryption/helpers.go: | ||
| // https://github.com/openshift/cluster-kube-apiserver-operator/blob/main/test/library/encryption/helpers.go | ||
| package encryption | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
|
|
||
| corev1 "k8s.io/api/core/v1" | ||
| "k8s.io/apimachinery/pkg/api/errors" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/runtime" | ||
| ) | ||
|
|
||
| const secretOfLifeName = "secret-of-life" | ||
|
|
||
| func CreateAndStoreSecretOfLife(t testing.TB, clientSet ClientSet, namespace string) runtime.Object { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's open a new PR and move this function to the existing |
||
| t.Helper() | ||
| ctx := context.TODO() | ||
|
|
||
| oldSecret, err := clientSet.Kube.CoreV1().Secrets(namespace).Get(ctx, secretOfLifeName, metav1.GetOptions{}) | ||
| if err != nil && !errors.IsNotFound(err) { | ||
| t.Fatalf("Failed to check if the secret already exists: %v", err) | ||
| } | ||
| if oldSecret != nil && len(oldSecret.Name) > 0 { | ||
| t.Log("The secret already exists, removing it first") | ||
| require.NoError(t, clientSet.Kube.CoreV1().Secrets(namespace).Delete(ctx, oldSecret.Name, metav1.DeleteOptions{})) | ||
| } | ||
|
|
||
| t.Logf("Creating %q in %s namespace", secretOfLifeName, namespace) | ||
| rawSecret := SecretOfLife(t, namespace) | ||
| secret, err := clientSet.Kube.CoreV1().Secrets(namespace).Create(ctx, rawSecret.(*corev1.Secret), metav1.CreateOptions{}) | ||
| require.NoError(t, err) | ||
| return secret | ||
| } | ||
|
|
||
| func SecretOfLife(_ testing.TB, namespace string) runtime.Object { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| return &corev1.Secret{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: secretOfLifeName, | ||
| Namespace: namespace, | ||
| }, | ||
| Data: map[string][]byte{ | ||
| "quote": []byte("I have no special talents. I am only passionately curious"), | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func GetRawSecretOfLife(t testing.TB, clientSet ClientSet, namespace string) string { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| t.Helper() | ||
| timeout, cancel := context.WithTimeout(context.Background(), 10*time.Minute) | ||
| defer cancel() | ||
|
|
||
| secretOfLifeKey := fmt.Sprintf("/kubernetes.io/secrets/%s/%s", namespace, secretOfLifeName) | ||
| resp, err := clientSet.Etcd.Get(timeout, secretOfLifeKey) | ||
| require.NoError(t, err) | ||
| require.Len(t, resp.Kvs, 1, "expected exactly one key from etcd for secret-of-life") | ||
|
|
||
| return string(resp.Kvs[0].Value) | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WellKnownKASTargetGRs?