Просмотр исходного кода

fixed error messages and switch case in vault provider. Attempted fixes for vault JSON syncing errors

Kian Kordtomeikel 4 лет назад
Родитель
Сommit
723d8b53b6
3 измененных файлов с 25 добавлено и 15 удалено
  1. 10 12
      e2e/suite/common/common.go
  2. 2 0
      e2e/suite/vault/vault.go
  3. 13 3
      pkg/provider/vault/vault.go

+ 10 - 12
e2e/suite/common/common.go

@@ -281,25 +281,23 @@ func DockerJSONConfigVault(f *framework.Framework) (string, func(*framework.Test
 	return "[common] should sync docker configurated json secrets with template", func(tc *framework.TestCase) {
 		cloudSecretName := fmt.Sprintf("%s-%s", f.Namespace.Name, "docker-config-example")
 		//cloudSecretValue := `{"auths":{"https://index.docker.io/v1/": {"auth": "c3R...zE2"}}}`
+		//dockerconfig := "{\"auths\":{\"https://index.docker.io/v1/\": {\"auth\": \"c3R...zE2\"}}}" // so we have the json string that is the final docker config that we want
+		//cloudSecretValue := "{\"dockerconfig\": {\"auths\":{\"https://index.docker.io/v1/\": {\"auth\": \"c3R...zE2\"}}}}"
+		// cloudSecretValue := fmt.Sprintf(`{"dockerconfig": %s}`, dockerconfig)
+		//dockerconfig := "{\"auths\":{\"https://index.docker.io/v1/\": {\"auth\": \"c3R...zE2\"}}}"
+
+		dockerconfigString := `"{\"auths\":{\"https://index.docker.io/v1/\": {\"auth\": \"c3R...zE2\"}}}"`
 		dockerconfig := `{"auths":{"https://index.docker.io/v1/": {"auth": "c3R...zE2"}}}`
-		cloudSecretValue := fmt.Sprintf(`{"dockerconfig": %s}`, dockerconfig)
-		/*cloudSecretValue := `{
-			"dockerconfig": {
-				"auths": {
-					"https://index.docker.io/v1/": {
-						"auth": "c3R...zE2"
-					}
-				}
-			}
-		}`*/
+		cloudSecretValue := fmt.Sprintf(`{"dockerconfig": %s}`, dockerconfigString)
+
 		tc.Secrets = map[string]string{
 			cloudSecretName: cloudSecretValue,
 		}
 
 		tc.ExpectedSecret = &v1.Secret{
-			Type: v1.SecretTypeOpaque,
+			Type: v1.SecretTypeDockerConfigJson, // we forgot to change this type
 			Data: map[string][]byte{
-				".dockerconfigjson": []byte(dockerconfig),
+				cloudSecretName: []byte(dockerconfig),
 			},
 		}
 

+ 2 - 0
e2e/suite/vault/vault.go

@@ -32,5 +32,7 @@ var _ = Describe("[vault] ", func() {
 		Entry(common.JSONDataFromSync(f)),
 		Entry(common.JSONDataWithProperty(f)),
 		Entry(common.JSONDataWithTemplate(f)),
+		//Entry(common.DockerJSONConfig(f)),
+		//Entry(common.DockerJSONConfigVault(f)),
 	)
 })

+ 13 - 3
pkg/provider/vault/vault.go

@@ -17,6 +17,7 @@ package vault
 import (
 	"context"
 	"crypto/x509"
+	"encoding/json"
 	"errors"
 	"fmt"
 	"io/ioutil"
@@ -182,15 +183,17 @@ func (v *client) readSecret(ctx context.Context, path, version string) (map[stri
 
 	secretData := vaultSecret.Data
 	if v.store.Version == esv1alpha1.VaultKVStoreV2 {
+
 		// Vault KV2 has data embedded within sub-field
 		// reference - https://www.vaultproject.io/api/secret/kv/kv-v2#read-secret-version
 		dataInt, ok := vaultSecret.Data["data"]
+
 		if !ok {
-			return nil, errors.New(errVaultData)
+			return nil, errors.New(fmt.Sprintf("failed to find data field: %v", vaultSecret.Data))
 		}
 		secretData, ok = dataInt.(map[string]interface{})
 		if !ok {
-			return nil, errors.New(errVaultData)
+			return nil, errors.New(fmt.Sprintf("failed to unmarshall JSON: %v", dataInt))
 		}
 	}
 
@@ -201,8 +204,15 @@ func (v *client) readSecret(ctx context.Context, path, version string) (map[stri
 			byteMap[k] = []byte(t)
 		case []byte:
 			byteMap[k] = t
+		case map[string]interface{}:
+			jsonString, err := json.Marshal(t)
+			byteMap[k] = jsonString
+			if err != nil {
+				return nil, err
+			}
+
 		default:
-			return nil, errors.New(errVaultData)
+			return nil, errors.New(fmt.Sprintf("Secret data not in expected format: %v", secretData))
 		}
 	}