vault.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. limitations under the License.
  10. */
  11. package vault
  12. import (
  13. "fmt"
  14. // nolint
  15. . "github.com/onsi/ginkgo/v2"
  16. v1 "k8s.io/api/core/v1"
  17. esapi "github.com/external-secrets/external-secrets/apis/externalsecrets/v1alpha1"
  18. "github.com/external-secrets/external-secrets/e2e/framework"
  19. "github.com/external-secrets/external-secrets/e2e/suite/common"
  20. )
  21. const (
  22. withTokenAuth = "with token auth"
  23. withCertAuth = "with cert auth"
  24. withApprole = "with approle auth"
  25. withV1 = "with v1 provider"
  26. withJWT = "with jwt provider"
  27. withK8s = "with kubernetes provider"
  28. )
  29. var _ = Describe("[vault]", Label("vault"), func() {
  30. f := framework.New("eso-vault")
  31. prov := newVaultProvider(f)
  32. DescribeTable("sync secrets",
  33. framework.TableFunc(f, prov),
  34. // uses token auth
  35. framework.Compose(withTokenAuth, f, common.JSONDataFromSync, useTokenAuth),
  36. framework.Compose(withTokenAuth, f, common.JSONDataWithProperty, useTokenAuth),
  37. framework.Compose(withTokenAuth, f, common.JSONDataWithTemplate, useTokenAuth),
  38. framework.Compose(withTokenAuth, f, common.DataPropertyDockerconfigJSON, useTokenAuth),
  39. framework.Compose(withTokenAuth, f, common.JSONDataWithoutTargetName, useTokenAuth),
  40. // use cert auth
  41. framework.Compose(withCertAuth, f, common.JSONDataFromSync, useCertAuth),
  42. framework.Compose(withCertAuth, f, common.JSONDataWithProperty, useCertAuth),
  43. framework.Compose(withCertAuth, f, common.JSONDataWithTemplate, useCertAuth),
  44. framework.Compose(withCertAuth, f, common.DataPropertyDockerconfigJSON, useCertAuth),
  45. framework.Compose(withCertAuth, f, common.JSONDataWithoutTargetName, useCertAuth),
  46. // use approle auth
  47. framework.Compose(withApprole, f, common.JSONDataFromSync, useApproleAuth),
  48. framework.Compose(withApprole, f, common.JSONDataWithProperty, useApproleAuth),
  49. framework.Compose(withApprole, f, common.JSONDataWithTemplate, useApproleAuth),
  50. framework.Compose(withApprole, f, common.DataPropertyDockerconfigJSON, useApproleAuth),
  51. framework.Compose(withApprole, f, common.JSONDataWithoutTargetName, useApproleAuth),
  52. // use v1 provider
  53. framework.Compose(withV1, f, common.JSONDataFromSync, useV1Provider),
  54. framework.Compose(withV1, f, common.JSONDataWithProperty, useV1Provider),
  55. framework.Compose(withV1, f, common.JSONDataWithTemplate, useV1Provider),
  56. framework.Compose(withV1, f, common.DataPropertyDockerconfigJSON, useV1Provider),
  57. framework.Compose(withV1, f, common.JSONDataWithoutTargetName, useV1Provider),
  58. // use jwt provider
  59. framework.Compose(withJWT, f, common.JSONDataFromSync, useJWTProvider),
  60. framework.Compose(withJWT, f, common.JSONDataWithProperty, useJWTProvider),
  61. framework.Compose(withJWT, f, common.JSONDataWithTemplate, useJWTProvider),
  62. framework.Compose(withJWT, f, common.DataPropertyDockerconfigJSON, useJWTProvider),
  63. framework.Compose(withJWT, f, common.JSONDataWithoutTargetName, useJWTProvider),
  64. // use kubernetes provider
  65. framework.Compose(withK8s, f, common.JSONDataFromSync, useKubernetesProvider),
  66. framework.Compose(withK8s, f, common.JSONDataWithProperty, useKubernetesProvider),
  67. framework.Compose(withK8s, f, common.JSONDataWithTemplate, useKubernetesProvider),
  68. framework.Compose(withK8s, f, common.DataPropertyDockerconfigJSON, useKubernetesProvider),
  69. framework.Compose(withK8s, f, common.JSONDataWithoutTargetName, useKubernetesProvider),
  70. // vault-specific test cases
  71. Entry("secret value via data without property should return json-encoded string", Label("json"), testJSONWithoutProperty),
  72. Entry("secret value via data with property should return json-encoded string", Label("json"), testJSONWithProperty),
  73. Entry("dataFrom without property should extract key/value pairs", Label("json"), testDataFromJSONWithoutProperty),
  74. Entry("dataFrom with property should extract key/value pairs", Label("json"), testDataFromJSONWithProperty),
  75. )
  76. })
  77. func useTokenAuth(tc *framework.TestCase) {
  78. tc.ExternalSecret.Spec.SecretStoreRef.Name = tc.Framework.Namespace.Name
  79. }
  80. func useCertAuth(tc *framework.TestCase) {
  81. tc.ExternalSecret.Spec.SecretStoreRef.Name = certAuthProviderName
  82. }
  83. func useApproleAuth(tc *framework.TestCase) {
  84. tc.ExternalSecret.Spec.SecretStoreRef.Name = appRoleAuthProviderName
  85. }
  86. func useV1Provider(tc *framework.TestCase) {
  87. tc.ExternalSecret.Spec.SecretStoreRef.Name = kvv1ProviderName
  88. }
  89. func useJWTProvider(tc *framework.TestCase) {
  90. tc.ExternalSecret.Spec.SecretStoreRef.Name = jwtProviderName
  91. }
  92. func useKubernetesProvider(tc *framework.TestCase) {
  93. tc.ExternalSecret.Spec.SecretStoreRef.Name = kubernetesProviderName
  94. }
  95. const jsonVal = `{"foo":{"nested":{"bar":"mysecret","baz":"bang"}}}`
  96. // when no property is set it should return the json-encoded at path.
  97. func testJSONWithoutProperty(tc *framework.TestCase) {
  98. secretKey := fmt.Sprintf("%s-%s", tc.Framework.Namespace.Name, "json")
  99. tc.Secrets = map[string]string{
  100. secretKey: jsonVal,
  101. }
  102. tc.ExpectedSecret = &v1.Secret{
  103. Type: v1.SecretTypeOpaque,
  104. Data: map[string][]byte{
  105. secretKey: []byte(jsonVal),
  106. },
  107. }
  108. tc.ExternalSecret.Spec.Data = []esapi.ExternalSecretData{
  109. {
  110. SecretKey: secretKey,
  111. RemoteRef: esapi.ExternalSecretDataRemoteRef{
  112. Key: secretKey,
  113. },
  114. },
  115. }
  116. }
  117. // when property is set it should return the json-encoded at path.
  118. func testJSONWithProperty(tc *framework.TestCase) {
  119. secretKey := fmt.Sprintf("%s-%s", tc.Framework.Namespace.Name, "json")
  120. expectedVal := `{"bar":"mysecret","baz":"bang"}`
  121. tc.Secrets = map[string]string{
  122. secretKey: jsonVal,
  123. }
  124. tc.ExpectedSecret = &v1.Secret{
  125. Type: v1.SecretTypeOpaque,
  126. Data: map[string][]byte{
  127. secretKey: []byte(expectedVal),
  128. },
  129. }
  130. tc.ExternalSecret.Spec.Data = []esapi.ExternalSecretData{
  131. {
  132. SecretKey: secretKey,
  133. RemoteRef: esapi.ExternalSecretDataRemoteRef{
  134. Key: secretKey,
  135. Property: "foo.nested",
  136. },
  137. },
  138. }
  139. }
  140. // when no property is set it should extract the key/value pairs at the given path
  141. // note: it should json-encode if a value contains nested data
  142. func testDataFromJSONWithoutProperty(tc *framework.TestCase) {
  143. secretKey := fmt.Sprintf("%s-%s", tc.Framework.Namespace.Name, "json")
  144. tc.Secrets = map[string]string{
  145. secretKey: jsonVal,
  146. }
  147. tc.ExpectedSecret = &v1.Secret{
  148. Type: v1.SecretTypeOpaque,
  149. Data: map[string][]byte{
  150. "foo": []byte(`{"nested":{"bar":"mysecret","baz":"bang"}}`),
  151. },
  152. }
  153. tc.ExternalSecret.Spec.DataFrom = []esapi.ExternalSecretDataRemoteRef{
  154. {
  155. Key: secretKey,
  156. },
  157. }
  158. }
  159. // when property is set it should extract values with dataFrom at the given path.
  160. func testDataFromJSONWithProperty(tc *framework.TestCase) {
  161. secretKey := fmt.Sprintf("%s-%s", tc.Framework.Namespace.Name, "json")
  162. tc.Secrets = map[string]string{
  163. secretKey: jsonVal,
  164. }
  165. tc.ExpectedSecret = &v1.Secret{
  166. Type: v1.SecretTypeOpaque,
  167. Data: map[string][]byte{
  168. "bar": []byte(`mysecret`),
  169. "baz": []byte(`bang`),
  170. },
  171. }
  172. tc.ExternalSecret.Spec.DataFrom = []esapi.ExternalSecretDataRemoteRef{
  173. {
  174. Key: secretKey,
  175. Property: "foo.nested",
  176. },
  177. }
  178. }