mirror of
https://github.com/1Password/onepassword-operator.git
synced 2025-10-21 15:08:06 +00:00

* Add missing improvements from Operator SDK 1.34.1 These were not mentioned in the upgrade documentation for version 1.34.x (https://sdk.operatorframework.io/docs/upgrading-sdk-version/v1.34.0/), but I've found them by compating the release with the previous one (https://github.com/operator-framework/operator-sdk/compare/v1.33.0...v1.34.1). * Upgrade to Operator SDK 1.36.0 Source of upgrade steps: https://sdk.operatorframework.io/docs/upgrading-sdk-version/v1.36.0/ Key differences: - Go packages `k8s.io/*` are already at a version higher than the one in the upgrade. - `ENVTEST_K8S_VERSION` is at a version higher than the one in the upgrade - We didn't have the golangci-lint make command before, thus we only needed to add things. * Upgrade to Operator SDK 1.38.0 Source of upgrade steps: https://sdk.operatorframework.io/docs/upgrading-sdk-version/v1.38.0/ * Upgrade to Operator SDK 1.39.0 Source of upgrade steps: https://sdk.operatorframework.io/docs/upgrading-sdk-version/v1.39.0/ * Upgrade to Operator SDK 1.40.0 Source of upgrade steps: https://sdk.operatorframework.io/docs/upgrading-sdk-version/v1.40.0/ I didn't do the "Add app.kubernetes.io/name label to your manifests" since it seems that we have it already, and it's customized. * Address lint errors * Update golangci-lint version used to support Go 1.24 * Improve workflows - Make workflow targets more specific. - Make build workflow only build (i.e. remove test part of it). - Rearrange steps and improve naming for build workflow. * Add back deleted test Initially the test has been removed due to lint saying that it was duplicate code, but it falsely errored since the values are different. * Improve code and add missing upgrade pieces * Upgrade to Operator SDK 1.41.1 Source of upgrade steps: https://sdk.operatorframework.io/docs/upgrading-sdk-version/v1.41.0/ Upgrading to 1.41.1 from 1.40.0 doesn't have any migration steps. Key elements: - Upgrade to golangci-lint v2 - Made the manifests using the updated controller tools * Address linter errors golanci-lint v2 seems to be more robust than the previous one, which is beneficial. Thus, we address the linter errors thrown by v2 and improve our code even further. * Add Makefile improvements These were brought in by comparing the Makefile of a freshly created operator using the latest operator-sdk with ours. * Add missing default kustomization for 1.40.0 upgrade * Bring default kustomization to latest version This is done by putting the file's content from a newly-generated operator. * Switch metrics-bind-address default value back to 8080 This ensures that the upgrade is backwards-compatible. * Add webhook-related scaffolding This enables us to easily add support for webhooks by running `operator-sdk create webhook` whenever we want to add them. * Fix typo
317 lines
9.6 KiB
Go
317 lines
9.6 KiB
Go
package kubernetessecrets
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
"testing"
|
|
|
|
corev1 "k8s.io/api/core/v1"
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
"k8s.io/apimachinery/pkg/types"
|
|
kubeValidate "k8s.io/apimachinery/pkg/util/validation"
|
|
"sigs.k8s.io/controller-runtime/pkg/client/fake"
|
|
|
|
"github.com/1Password/onepassword-operator/pkg/onepassword/model"
|
|
)
|
|
|
|
const (
|
|
restartDeploymentAnnotation = "false"
|
|
testNamespace = "test"
|
|
testItemUUID = "h46bb3jddvay7nxopfhvlwg35q"
|
|
testVaultUUID = "hfnjvi6aymbsnfc2xeeoheizda"
|
|
)
|
|
|
|
func TestCreateKubernetesSecretFromOnePasswordItem(t *testing.T) {
|
|
ctx := context.Background()
|
|
secretName := "test-secret-name"
|
|
namespace := testNamespace
|
|
|
|
item := model.Item{}
|
|
item.Fields = generateFields(5)
|
|
item.Version = 123
|
|
item.VaultID = testVaultUUID
|
|
item.ID = testItemUUID
|
|
|
|
kubeClient := fake.NewClientBuilder().Build()
|
|
secretLabels := map[string]string{}
|
|
secretType := ""
|
|
|
|
err := CreateKubernetesSecretFromItem(ctx, kubeClient, secretName, namespace, &item, restartDeploymentAnnotation,
|
|
secretLabels, secretType, nil)
|
|
if err != nil {
|
|
t.Errorf("Unexpected error: %v", err)
|
|
}
|
|
createdSecret := &corev1.Secret{}
|
|
err = kubeClient.Get(ctx, types.NamespacedName{Name: secretName, Namespace: namespace}, createdSecret)
|
|
|
|
if err != nil {
|
|
t.Errorf("Secret was not created: %v", err)
|
|
}
|
|
compareFields(item.Fields, createdSecret.Data, t)
|
|
compareAnnotationsToItem(createdSecret.Annotations, item, t)
|
|
}
|
|
|
|
func TestKubernetesSecretFromOnePasswordItemOwnerReferences(t *testing.T) {
|
|
ctx := context.Background()
|
|
secretName := "test-secret-name"
|
|
namespace := testNamespace
|
|
|
|
item := model.Item{}
|
|
item.Fields = generateFields(5)
|
|
item.Version = 123
|
|
item.VaultID = testVaultUUID
|
|
item.ID = testItemUUID
|
|
|
|
kubeClient := fake.NewClientBuilder().Build()
|
|
secretLabels := map[string]string{}
|
|
secretType := ""
|
|
|
|
ownerRef := &metav1.OwnerReference{
|
|
Kind: "Deployment",
|
|
APIVersion: "apps/v1",
|
|
Name: "test-deployment",
|
|
UID: types.UID("test-uid"),
|
|
}
|
|
err := CreateKubernetesSecretFromItem(ctx, kubeClient, secretName, namespace, &item, restartDeploymentAnnotation,
|
|
secretLabels, secretType, ownerRef)
|
|
if err != nil {
|
|
t.Errorf("Unexpected error: %v", err)
|
|
}
|
|
createdSecret := &corev1.Secret{}
|
|
err = kubeClient.Get(ctx, types.NamespacedName{Name: secretName, Namespace: namespace}, createdSecret)
|
|
if err != nil {
|
|
t.Errorf("Unexpected error: %v", err)
|
|
}
|
|
|
|
// Check owner references.
|
|
gotOwnerRefs := createdSecret.OwnerReferences
|
|
if len(gotOwnerRefs) != 1 {
|
|
t.Errorf("Expected owner references length: 1 but got: %d", len(gotOwnerRefs))
|
|
}
|
|
|
|
expOwnerRef := metav1.OwnerReference{
|
|
Kind: "Deployment",
|
|
APIVersion: "apps/v1",
|
|
Name: "test-deployment",
|
|
UID: types.UID("test-uid"),
|
|
}
|
|
gotOwnerRef := gotOwnerRefs[0]
|
|
if gotOwnerRef != expOwnerRef {
|
|
t.Errorf("Expected owner reference value: %v but got: %v", expOwnerRef, gotOwnerRef)
|
|
}
|
|
}
|
|
|
|
func TestUpdateKubernetesSecretFromOnePasswordItem(t *testing.T) {
|
|
ctx := context.Background()
|
|
secretName := "test-secret-update"
|
|
namespace := testNamespace
|
|
|
|
item := model.Item{}
|
|
item.Fields = generateFields(5)
|
|
item.Version = 123
|
|
item.VaultID = testVaultUUID
|
|
item.ID = testItemUUID
|
|
|
|
kubeClient := fake.NewClientBuilder().Build()
|
|
secretLabels := map[string]string{}
|
|
secretType := ""
|
|
|
|
err := CreateKubernetesSecretFromItem(ctx, kubeClient, secretName, namespace, &item, restartDeploymentAnnotation,
|
|
secretLabels, secretType, nil)
|
|
|
|
if err != nil {
|
|
t.Errorf("Unexpected error: %v", err)
|
|
}
|
|
|
|
// Updating kubernetes secret with new item
|
|
newItem := model.Item{}
|
|
newItem.Fields = generateFields(6)
|
|
newItem.Version = 456
|
|
newItem.VaultID = testVaultUUID
|
|
newItem.ID = testItemUUID
|
|
err = CreateKubernetesSecretFromItem(ctx, kubeClient, secretName, namespace, &newItem, restartDeploymentAnnotation,
|
|
secretLabels, secretType, nil)
|
|
if err != nil {
|
|
t.Errorf("Unexpected error: %v", err)
|
|
}
|
|
updatedSecret := &corev1.Secret{}
|
|
err = kubeClient.Get(ctx, types.NamespacedName{Name: secretName, Namespace: namespace}, updatedSecret)
|
|
|
|
if err != nil {
|
|
t.Errorf("Secret was not found: %v", err)
|
|
}
|
|
compareFields(newItem.Fields, updatedSecret.Data, t)
|
|
compareAnnotationsToItem(updatedSecret.Annotations, newItem, t)
|
|
}
|
|
func TestBuildKubernetesSecretData(t *testing.T) {
|
|
fields := generateFields(5)
|
|
|
|
secretData := BuildKubernetesSecretData(fields, nil)
|
|
if len(secretData) != len(fields) {
|
|
t.Errorf("Unexpected number of secret fields returned. Expected 3, got %v", len(secretData))
|
|
}
|
|
compareFields(fields, secretData, t)
|
|
}
|
|
|
|
func TestBuildKubernetesSecretFromOnePasswordItem(t *testing.T) {
|
|
annotationKey := "annotationKey"
|
|
annotationValue := "annotationValue"
|
|
name := "someName"
|
|
namespace := "someNamespace"
|
|
annotations := map[string]string{
|
|
annotationKey: annotationValue,
|
|
}
|
|
item := model.Item{}
|
|
item.Fields = generateFields(5)
|
|
labels := map[string]string{}
|
|
secretType := ""
|
|
|
|
kubeSecret := BuildKubernetesSecretFromOnePasswordItem(name, namespace, annotations, labels, secretType, item, nil)
|
|
if kubeSecret.Name != strings.ToLower(name) {
|
|
t.Errorf("Expected name value: %v but got: %v", name, kubeSecret.Name)
|
|
}
|
|
if kubeSecret.Namespace != namespace {
|
|
t.Errorf("Expected namespace value: %v but got: %v", namespace, kubeSecret.Namespace)
|
|
}
|
|
if kubeSecret.Annotations[annotationKey] != annotations[annotationKey] {
|
|
t.Errorf("Expected namespace value: %v but got: %v", namespace, kubeSecret.Namespace)
|
|
}
|
|
compareFields(item.Fields, kubeSecret.Data, t)
|
|
}
|
|
|
|
func TestBuildKubernetesSecretFixesInvalidLabels(t *testing.T) {
|
|
name := "inV@l1d k8s secret%name"
|
|
expectedName := "inv-l1d-k8s-secret-name"
|
|
namespace := "someNamespace"
|
|
annotations := map[string]string{
|
|
"annotationKey": "annotationValue",
|
|
}
|
|
labels := map[string]string{}
|
|
item := model.Item{}
|
|
secretType := ""
|
|
|
|
item.Fields = []model.ItemField{
|
|
{
|
|
Label: "label w%th invalid ch!rs-",
|
|
Value: "value1",
|
|
},
|
|
{
|
|
Label: strings.Repeat("x", kubeValidate.DNS1123SubdomainMaxLength+1),
|
|
Value: "name exceeds max length",
|
|
},
|
|
}
|
|
|
|
kubeSecret := BuildKubernetesSecretFromOnePasswordItem(name, namespace, annotations, labels, secretType, item, nil)
|
|
|
|
// Assert Secret's meta.name was fixed
|
|
if kubeSecret.Name != expectedName {
|
|
t.Errorf("Expected name value: %v but got: %v", name, kubeSecret.Name)
|
|
}
|
|
if kubeSecret.Namespace != namespace {
|
|
t.Errorf("Expected namespace value: %v but got: %v", namespace, kubeSecret.Namespace)
|
|
}
|
|
|
|
// assert labels were fixed for each data key
|
|
for key := range kubeSecret.Data {
|
|
if !validLabel(key) {
|
|
t.Errorf("Expected valid kubernetes label, got %s", key)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestCreateKubernetesTLSSecretFromOnePasswordItem(t *testing.T) {
|
|
ctx := context.Background()
|
|
secretName := "tls-test-secret-name"
|
|
namespace := testNamespace
|
|
|
|
item := model.Item{}
|
|
item.Fields = generateFields(5)
|
|
item.Version = 123
|
|
item.VaultID = testVaultUUID
|
|
item.ID = testItemUUID
|
|
|
|
kubeClient := fake.NewClientBuilder().Build()
|
|
secretLabels := map[string]string{}
|
|
secretType := "kubernetes.io/tls"
|
|
|
|
err := CreateKubernetesSecretFromItem(ctx, kubeClient, secretName, namespace, &item, restartDeploymentAnnotation,
|
|
secretLabels, secretType, nil)
|
|
if err != nil {
|
|
t.Errorf("Unexpected error: %v", err)
|
|
}
|
|
createdSecret := &corev1.Secret{}
|
|
err = kubeClient.Get(ctx, types.NamespacedName{Name: secretName, Namespace: namespace}, createdSecret)
|
|
|
|
if err != nil {
|
|
t.Errorf("Secret was not created: %v", err)
|
|
}
|
|
|
|
if createdSecret.Type != corev1.SecretTypeTLS {
|
|
t.Errorf("Expected secretType to be of tyype corev1.SecretTypeTLS, got %s", string(createdSecret.Type))
|
|
}
|
|
}
|
|
|
|
func compareAnnotationsToItem(annotations map[string]string, item model.Item, t *testing.T) {
|
|
actualVaultId, actualItemId, err := ParseVaultIdAndItemIdFromPath(annotations[ItemPathAnnotation])
|
|
if err != nil {
|
|
t.Errorf("Was unable to parse Item Path")
|
|
}
|
|
if actualVaultId != item.VaultID {
|
|
t.Errorf("Expected annotation vault id to be %v but was %v", item.VaultID, actualVaultId)
|
|
}
|
|
if actualItemId != item.ID {
|
|
t.Errorf("Expected annotation item id to be %v but was %v", item.ID, actualItemId)
|
|
}
|
|
if annotations[VersionAnnotation] != fmt.Sprint(item.Version) {
|
|
t.Errorf("Expected annotation version to be %v but was %v", item.Version, annotations[VersionAnnotation])
|
|
}
|
|
|
|
if annotations[RestartDeploymentsAnnotation] != "false" {
|
|
t.Errorf("Expected restart deployments annotation to be %v but was %v",
|
|
restartDeploymentAnnotation, RestartDeploymentsAnnotation,
|
|
)
|
|
}
|
|
}
|
|
|
|
func compareFields(actualFields []model.ItemField, secretData map[string][]byte, t *testing.T) {
|
|
for i := 0; i < len(actualFields); i++ {
|
|
value, found := secretData[actualFields[i].Label]
|
|
if !found {
|
|
t.Errorf("Expected key %v is missing from secret data", actualFields[i].Label)
|
|
}
|
|
if string(value) != actualFields[i].Value {
|
|
t.Errorf("Expected value %v but got %v", actualFields[i].Value, value)
|
|
}
|
|
}
|
|
}
|
|
|
|
func generateFields(numToGenerate int) []model.ItemField {
|
|
fields := []model.ItemField{}
|
|
for i := 0; i < numToGenerate; i++ {
|
|
fields = append(fields, model.ItemField{
|
|
Label: "key" + fmt.Sprint(i),
|
|
Value: "value" + fmt.Sprint(i),
|
|
})
|
|
}
|
|
return fields
|
|
}
|
|
|
|
func ParseVaultIdAndItemIdFromPath(path string) (string, string, error) {
|
|
splitPath := strings.Split(path, "/")
|
|
if len(splitPath) == 4 && splitPath[0] == "vaults" && splitPath[2] == "items" {
|
|
return splitPath[1], splitPath[3], nil
|
|
}
|
|
return "", "", fmt.Errorf(
|
|
"%q is not an acceptable path for One Password item. Must be of the format: `vaults/{vault_id}/items/{item_id}`",
|
|
path,
|
|
)
|
|
}
|
|
|
|
func validLabel(v string) bool {
|
|
if err := kubeValidate.IsConfigMapKey(v); len(err) > 0 {
|
|
return false
|
|
}
|
|
return true
|
|
}
|