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
208 lines
6.4 KiB
Go
208 lines
6.4 KiB
Go
package kubernetessecrets
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"reflect"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"github.com/1Password/onepassword-operator/pkg/onepassword/model"
|
|
"github.com/1Password/onepassword-operator/pkg/utils"
|
|
corev1 "k8s.io/api/core/v1"
|
|
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
"k8s.io/apimachinery/pkg/types"
|
|
kubeValidate "k8s.io/apimachinery/pkg/util/validation"
|
|
|
|
kubernetesClient "sigs.k8s.io/controller-runtime/pkg/client"
|
|
logf "sigs.k8s.io/controller-runtime/pkg/log"
|
|
)
|
|
|
|
const OnepasswordPrefix = "operator.1password.io"
|
|
const NameAnnotation = OnepasswordPrefix + "/item-name"
|
|
const VersionAnnotation = OnepasswordPrefix + "/item-version"
|
|
const ItemPathAnnotation = OnepasswordPrefix + "/item-path"
|
|
const RestartDeploymentsAnnotation = OnepasswordPrefix + "/auto-restart"
|
|
|
|
var ErrCannotUpdateSecretType = errors.New("cannot change secret type: secret type is immutable")
|
|
|
|
var log = logf.Log
|
|
|
|
func CreateKubernetesSecretFromItem(
|
|
ctx context.Context,
|
|
kubeClient kubernetesClient.Client,
|
|
secretName, namespace string,
|
|
item *model.Item,
|
|
autoRestart string,
|
|
labels map[string]string,
|
|
secretType string,
|
|
ownerRef *metav1.OwnerReference,
|
|
) error {
|
|
itemVersion := fmt.Sprint(item.Version)
|
|
secretAnnotations := map[string]string{
|
|
VersionAnnotation: itemVersion,
|
|
ItemPathAnnotation: fmt.Sprintf("vaults/%v/items/%v", item.VaultID, item.ID),
|
|
}
|
|
|
|
if autoRestart != "" {
|
|
_, err := utils.StringToBool(autoRestart)
|
|
if err != nil {
|
|
return fmt.Errorf("error parsing %v annotation on Secret %v. Must be true or false. Defaulting to false",
|
|
RestartDeploymentsAnnotation, secretName,
|
|
)
|
|
}
|
|
secretAnnotations[RestartDeploymentsAnnotation] = autoRestart
|
|
}
|
|
|
|
// "Opaque" and "" secret types are treated the same by Kubernetes.
|
|
secret := BuildKubernetesSecretFromOnePasswordItem(secretName, namespace, secretAnnotations, labels,
|
|
secretType, *item, ownerRef)
|
|
|
|
currentSecret := &corev1.Secret{}
|
|
err := kubeClient.Get(ctx, types.NamespacedName{Name: secret.Name, Namespace: secret.Namespace}, currentSecret)
|
|
if err != nil && apierrors.IsNotFound(err) {
|
|
log.Info(fmt.Sprintf("Creating Secret %v at namespace '%v'", secret.Name, secret.Namespace))
|
|
return kubeClient.Create(ctx, secret)
|
|
} else if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Check if the secret types are being changed on the update.
|
|
// Avoid Opaque and "" are treated as different on check.
|
|
wantSecretType := secretType
|
|
if wantSecretType == "" {
|
|
wantSecretType = string(corev1.SecretTypeOpaque)
|
|
}
|
|
currentSecretType := string(currentSecret.Type)
|
|
if currentSecretType == "" {
|
|
currentSecretType = string(corev1.SecretTypeOpaque)
|
|
}
|
|
if currentSecretType != wantSecretType {
|
|
return ErrCannotUpdateSecretType
|
|
}
|
|
|
|
currentAnnotations := currentSecret.Annotations
|
|
currentLabels := currentSecret.Labels
|
|
if !reflect.DeepEqual(currentAnnotations, secretAnnotations) || !reflect.DeepEqual(currentLabels, labels) {
|
|
log.Info(fmt.Sprintf("Updating Secret %v at namespace '%v'", secret.Name, secret.Namespace))
|
|
currentSecret.Annotations = secretAnnotations
|
|
currentSecret.Labels = labels
|
|
currentSecret.Data = secret.Data
|
|
if err := kubeClient.Update(ctx, currentSecret); err != nil {
|
|
return fmt.Errorf("kubernetes secret update failed: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
log.Info(fmt.Sprintf("Secret with name %v and version %v already exists",
|
|
secret.Name, secret.Annotations[VersionAnnotation],
|
|
))
|
|
return nil
|
|
}
|
|
|
|
func BuildKubernetesSecretFromOnePasswordItem(
|
|
name, namespace string,
|
|
annotations map[string]string,
|
|
labels map[string]string,
|
|
secretType string,
|
|
item model.Item,
|
|
ownerRef *metav1.OwnerReference,
|
|
) *corev1.Secret {
|
|
var ownerRefs []metav1.OwnerReference
|
|
if ownerRef != nil {
|
|
ownerRefs = []metav1.OwnerReference{*ownerRef}
|
|
}
|
|
|
|
return &corev1.Secret{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Name: formatSecretName(name),
|
|
Namespace: namespace,
|
|
Annotations: annotations,
|
|
Labels: labels,
|
|
OwnerReferences: ownerRefs,
|
|
},
|
|
Data: BuildKubernetesSecretData(item.Fields, item.Files),
|
|
Type: corev1.SecretType(secretType),
|
|
}
|
|
}
|
|
|
|
func BuildKubernetesSecretData(fields []model.ItemField, files []model.File) map[string][]byte {
|
|
secretData := map[string][]byte{}
|
|
for i := 0; i < len(fields); i++ {
|
|
if fields[i].Value != "" {
|
|
key := formatSecretDataName(fields[i].Label)
|
|
secretData[key] = []byte(fields[i].Value)
|
|
}
|
|
}
|
|
|
|
// populate unpopulated fields from files
|
|
for _, file := range files {
|
|
content, err := file.Content()
|
|
if err != nil {
|
|
log.Error(err, fmt.Sprintf("Could not load contents of file %s", file.Name))
|
|
continue
|
|
}
|
|
if content != nil {
|
|
key := file.Name
|
|
if secretData[key] == nil {
|
|
secretData[key] = content
|
|
} else {
|
|
log.Info(fmt.Sprintf("File '%s' ignored because of a field with the same name", file.Name))
|
|
}
|
|
}
|
|
}
|
|
return secretData
|
|
}
|
|
|
|
// formatSecretName rewrites a value to be a valid Secret name.
|
|
//
|
|
// The Secret meta.name and data keys must be valid DNS subdomain names
|
|
// (https://kubernetes.io/docs/concepts/configuration/secret/#overview-of-secrets)
|
|
func formatSecretName(value string) string {
|
|
if errs := kubeValidate.IsDNS1123Subdomain(value); len(errs) == 0 {
|
|
return value
|
|
}
|
|
return createValidSecretName(value)
|
|
}
|
|
|
|
// formatSecretDataName rewrites a value to be a valid Secret data key.
|
|
//
|
|
// The Secret data keys must consist of alphanumeric numbers, `-`, `_` or `.`
|
|
// (https://kubernetes.io/docs/concepts/configuration/secret/#overview-of-secrets)
|
|
func formatSecretDataName(value string) string {
|
|
if errs := kubeValidate.IsConfigMapKey(value); len(errs) == 0 {
|
|
return value
|
|
}
|
|
return createValidSecretDataName(value)
|
|
}
|
|
|
|
var invalidDNS1123Chars = regexp.MustCompile("[^a-z0-9-.]+")
|
|
|
|
func createValidSecretName(value string) string {
|
|
result := strings.ToLower(value)
|
|
result = invalidDNS1123Chars.ReplaceAllString(result, "-")
|
|
|
|
if len(result) > kubeValidate.DNS1123SubdomainMaxLength {
|
|
result = result[0:kubeValidate.DNS1123SubdomainMaxLength]
|
|
}
|
|
|
|
// first and last character MUST be alphanumeric
|
|
return strings.Trim(result, "-.")
|
|
}
|
|
|
|
var invalidDataChars = regexp.MustCompile("[^a-zA-Z0-9-._]+")
|
|
var invalidStartEndChars = regexp.MustCompile("(^[^a-zA-Z0-9-._]+|[^a-zA-Z0-9-._]+$)")
|
|
|
|
func createValidSecretDataName(value string) string {
|
|
result := invalidStartEndChars.ReplaceAllString(value, "")
|
|
result = invalidDataChars.ReplaceAllString(result, "-")
|
|
|
|
if len(result) > kubeValidate.DNS1123SubdomainMaxLength {
|
|
result = result[0:kubeValidate.DNS1123SubdomainMaxLength]
|
|
}
|
|
|
|
return result
|
|
}
|