Browse Source

:sparkles: Support property in fake provider (#2056)

* Support property in fake provider

Signed-off-by: shuheiktgw <s-kitagawa@mercari.com>

* Remove a unused function

Signed-off-by: shuheiktgw <s-kitagawa@mercari.com>

* fix autogen api/spec.md

Signed-off-by: Moritz Johner <moolen@users.noreply.github.com>

---------

Signed-off-by: shuheiktgw <s-kitagawa@mercari.com>
Signed-off-by: Moritz Johner <moolen@users.noreply.github.com>
Co-authored-by: Moritz Johner <moolen@users.noreply.github.com>
Shuhei Kitagawa 3 years ago
parent
commit
c28707aa52
2 changed files with 29 additions and 0 deletions
  1. 11 0
      pkg/provider/fake/fake.go
  2. 18 0
      pkg/provider/fake/fake_test.go

+ 11 - 0
pkg/provider/fake/fake.go

@@ -18,6 +18,7 @@ import (
 	"context"
 	"context"
 	"fmt"
 	"fmt"
 
 
+	"github.com/tidwall/gjson"
 	"sigs.k8s.io/controller-runtime/pkg/client"
 	"sigs.k8s.io/controller-runtime/pkg/client"
 
 
 	esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
 	esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
@@ -134,6 +135,16 @@ func (p *Provider) GetSecret(ctx context.Context, ref esv1beta1.ExternalSecretDa
 	if !ok || data.Version != ref.Version {
 	if !ok || data.Version != ref.Version {
 		return nil, esv1beta1.NoSecretErr
 		return nil, esv1beta1.NoSecretErr
 	}
 	}
+
+	if ref.Property != "" {
+		val := gjson.Get(data.Value, ref.Property)
+		if !val.Exists() {
+			return nil, esv1beta1.NoSecretErr
+		}
+
+		return []byte(val.String()), nil
+	}
+
 	return []byte(data.Value), nil
 	return []byte(data.Value), nil
 }
 }
 
 

+ 18 - 0
pkg/provider/fake/fake_test.go

@@ -124,6 +124,24 @@ func TestGetSecret(t *testing.T) {
 			},
 			},
 			expValue: "bar2",
 			expValue: "bar2",
 		},
 		},
+		{
+			name: "get correct value from multiple properties",
+			input: []esv1beta1.FakeProviderData{
+				{
+					Key:   "junk",
+					Value: "xxxxx",
+				},
+				{
+					Key:   "/foo",
+					Value: `{"p1":"bar","p2":"bar2"}`,
+				},
+			},
+			request: esv1beta1.ExternalSecretDataRemoteRef{
+				Key:      "/foo",
+				Property: "p2",
+			},
+			expValue: "bar2",
+		},
 	}
 	}
 
 
 	for i, row := range tbl {
 	for i, row := range tbl {