1
0
Fork 0
mirror of https://github.com/external-secrets/external-secrets.git synced 2024-12-14 11:57:59 +00:00

Demonstrate new slices/maps packages (#3839)

This commit is contained in:
Tom Godkin 2024-08-27 21:20:41 +01:00 committed by GitHub
parent 34a1a50609
commit bc97ae06f6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 23 additions and 48 deletions

View file

@ -29,6 +29,7 @@ import (
"net/http"
"os"
"path/filepath"
"slices"
"sync"
"time"
@ -107,18 +108,9 @@ type CertInfo struct {
CAName string
}
func contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
log := r.Log.WithValues("CustomResourceDefinition", req.NamespacedName)
if contains(r.CrdResources, req.NamespacedName.Name) {
if slices.Contains(r.CrdResources, req.NamespacedName.Name) {
err := r.updateCRD(ctx, req)
if err != nil {
log.Error(err, "failed to inject conversion webhook")

View file

@ -19,6 +19,8 @@ import (
"encoding/json"
"errors"
"fmt"
"maps"
"slices"
"strings"
"time"
@ -474,11 +476,8 @@ func getManagedDataKeys(secret *v1.Secret, fieldOwner string) ([]string, error)
if !ok {
return nil
}
var keys []string
for k := range df {
keys = append(keys, k)
}
return keys
return slices.Collect(maps.Keys(df))
})
}
@ -639,12 +638,9 @@ func isSecretValid(existingSecret v1.Secret) bool {
// computeDataHashAnnotation generate a hash of the secret data combining the old key with the new keys to add or override.
func (r *Reconciler) computeDataHashAnnotation(existing, secret *v1.Secret) string {
data := make(map[string][]byte)
for k, v := range existing.Data {
data[k] = v
}
for k, v := range secret.Data {
data[k] = v
}
maps.Insert(data, maps.All(existing.Data))
maps.Insert(data, maps.All(secret.Data))
return utils.ObjectHash(data)
}

View file

@ -17,6 +17,7 @@ package externalsecret
import (
"context"
"fmt"
"maps"
v1 "k8s.io/api/core/v1"
@ -44,9 +45,7 @@ func (r *Reconciler) applyTemplate(ctx context.Context, es *esv1beta1.ExternalSe
}
// Merge Policy should merge secrets
if es.Spec.Target.Template.MergePolicy == esv1beta1.MergePolicyMerge {
for k, v := range dataMap {
secret.Data[k] = v
}
maps.Insert(secret.Data, maps.All(dataMap))
}
execute, err := template.EngineForVersion(es.Spec.Target.Template.EngineVersion)
if err != nil {

View file

@ -18,6 +18,7 @@ import (
"context"
"errors"
"fmt"
"maps"
"strings"
"time"
@ -235,9 +236,7 @@ func mergeSecretState(newMap, old esapi.SyncedPushSecretsMap) esapi.SyncedPushSe
if !ok {
out[k] = make(map[string]esapi.PushSecretData)
}
for kk, vv := range v {
out[k][kk] = vv
}
maps.Insert(out[k], maps.All(v))
}
return out
}

View file

@ -19,6 +19,7 @@ import (
"encoding/json"
"errors"
"fmt"
"slices"
"strings"
"github.com/aws/aws-sdk-go/aws"
@ -239,12 +240,9 @@ func (pm *ParameterStore) PushSecret(ctx context.Context, secret *corev1.Secret,
}
func isManagedByESO(tags []*ssm.Tag) bool {
for _, tag := range tags {
if *tag.Key == managedBy && *tag.Value == externalSecrets {
return true
}
}
return false
return slices.ContainsFunc(tags, func(tag *ssm.Tag) bool {
return *tag.Key == managedBy && *tag.Value == externalSecrets
})
}
func (pm *ParameterStore) setManagedRemoteParameter(ctx context.Context, secretRequest ssm.PutParameterInput, createManagedByTags bool) error {

View file

@ -18,6 +18,7 @@ import (
"context"
"errors"
"fmt"
"slices"
authv1 "k8s.io/api/authorization/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@ -89,20 +90,11 @@ func (c *Client) Validate() (esv1beta1.ValidationResult, error) {
return esv1beta1.ValidationResultUnknown, fmt.Errorf("could not verify if client is valid: %w", err)
}
for _, rev := range authReview.Status.ResourceRules {
if (contains("secrets", rev.Resources) || contains("*", rev.Resources)) &&
(contains("get", rev.Verbs) || contains("*", rev.Verbs)) &&
(len(rev.APIGroups) == 0 || (contains("", rev.APIGroups) || contains("*", rev.APIGroups))) {
if (slices.Contains(rev.Resources, "secrets") || slices.Contains(rev.Resources, "*")) &&
(slices.Contains(rev.Verbs, "get") || slices.Contains(rev.Verbs, "*")) &&
(len(rev.APIGroups) == 0 || (slices.Contains(rev.APIGroups, "") || slices.Contains(rev.APIGroups, "*"))) {
return esv1beta1.ValidationResultReady, nil
}
}
return esv1beta1.ValidationResultError, errors.New("client is not allowed to get secrets")
}
func contains(sub string, args []string) bool {
for _, k := range args {
if k == sub {
return true
}
}
return false
}

View file

@ -20,6 +20,7 @@ import (
"encoding/json"
"errors"
"fmt"
"maps"
corev1 "k8s.io/api/core/v1"
@ -107,9 +108,7 @@ func (c *client) PushSecret(ctx context.Context, secret *corev1.Secret, data esv
return nil
}
}
for k, v := range vaultSecret {
secretVal[k] = v
}
maps.Insert(secretVal, maps.All(vaultSecret))
// Secret got from vault is already on map[string]string format
secretVal[data.GetProperty()] = string(value)
} else {