vault.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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/v1beta1"
  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. withJWTK8s = "with jwt k8s provider"
  28. withK8s = "with kubernetes provider"
  29. )
  30. var _ = Describe("[vault]", Label("vault"), func() {
  31. f := framework.New("eso-vault")
  32. prov := newVaultProvider(f)
  33. DescribeTable("sync secrets",
  34. framework.TableFunc(f, prov),
  35. // uses token auth
  36. framework.Compose(withTokenAuth, f, common.FindByName, useTokenAuth),
  37. framework.Compose(withTokenAuth, f, common.JSONDataFromSync, useTokenAuth),
  38. framework.Compose(withTokenAuth, f, common.JSONDataWithProperty, useTokenAuth),
  39. framework.Compose(withTokenAuth, f, common.JSONDataWithTemplate, useTokenAuth),
  40. framework.Compose(withTokenAuth, f, common.DataPropertyDockerconfigJSON, useTokenAuth),
  41. framework.Compose(withTokenAuth, f, common.JSONDataWithoutTargetName, useTokenAuth),
  42. framework.Compose(withTokenAuth, f, common.SyncV1Alpha1, useTokenAuth),
  43. // use cert auth
  44. framework.Compose(withCertAuth, f, common.FindByName, useCertAuth),
  45. framework.Compose(withCertAuth, f, common.JSONDataFromSync, useCertAuth),
  46. framework.Compose(withCertAuth, f, common.JSONDataWithProperty, useCertAuth),
  47. framework.Compose(withCertAuth, f, common.JSONDataWithTemplate, useCertAuth),
  48. framework.Compose(withCertAuth, f, common.DataPropertyDockerconfigJSON, useCertAuth),
  49. framework.Compose(withCertAuth, f, common.JSONDataWithoutTargetName, useCertAuth),
  50. // use approle auth
  51. framework.Compose(withApprole, f, common.FindByName, useApproleAuth),
  52. framework.Compose(withApprole, f, common.JSONDataFromSync, useApproleAuth),
  53. framework.Compose(withApprole, f, common.JSONDataWithProperty, useApproleAuth),
  54. framework.Compose(withApprole, f, common.JSONDataWithTemplate, useApproleAuth),
  55. framework.Compose(withApprole, f, common.DataPropertyDockerconfigJSON, useApproleAuth),
  56. framework.Compose(withApprole, f, common.JSONDataWithoutTargetName, useApproleAuth),
  57. // use v1 provider
  58. framework.Compose(withV1, f, common.JSONDataFromSync, useV1Provider),
  59. framework.Compose(withV1, f, common.JSONDataWithProperty, useV1Provider),
  60. framework.Compose(withV1, f, common.JSONDataWithTemplate, useV1Provider),
  61. framework.Compose(withV1, f, common.DataPropertyDockerconfigJSON, useV1Provider),
  62. framework.Compose(withV1, f, common.JSONDataWithoutTargetName, useV1Provider),
  63. // use jwt provider
  64. framework.Compose(withJWT, f, common.FindByName, useJWTProvider),
  65. framework.Compose(withJWT, f, common.JSONDataFromSync, useJWTProvider),
  66. framework.Compose(withJWT, f, common.JSONDataWithProperty, useJWTProvider),
  67. framework.Compose(withJWT, f, common.JSONDataWithTemplate, useJWTProvider),
  68. framework.Compose(withJWT, f, common.DataPropertyDockerconfigJSON, useJWTProvider),
  69. framework.Compose(withJWT, f, common.JSONDataWithoutTargetName, useJWTProvider),
  70. // use jwt k8s provider
  71. framework.Compose(withJWTK8s, f, common.JSONDataFromSync, useJWTK8sProvider),
  72. framework.Compose(withJWTK8s, f, common.JSONDataWithProperty, useJWTK8sProvider),
  73. framework.Compose(withJWTK8s, f, common.JSONDataWithTemplate, useJWTK8sProvider),
  74. framework.Compose(withJWTK8s, f, common.DataPropertyDockerconfigJSON, useJWTK8sProvider),
  75. framework.Compose(withJWTK8s, f, common.JSONDataWithoutTargetName, useJWTK8sProvider),
  76. // use kubernetes provider
  77. framework.Compose(withK8s, f, common.FindByName, useKubernetesProvider),
  78. framework.Compose(withK8s, f, common.JSONDataFromSync, useKubernetesProvider),
  79. framework.Compose(withK8s, f, common.JSONDataWithProperty, useKubernetesProvider),
  80. framework.Compose(withK8s, f, common.JSONDataWithTemplate, useKubernetesProvider),
  81. framework.Compose(withK8s, f, common.DataPropertyDockerconfigJSON, useKubernetesProvider),
  82. framework.Compose(withK8s, f, common.JSONDataWithoutTargetName, useKubernetesProvider),
  83. // vault-specific test cases
  84. Entry("secret value via data without property should return json-encoded string", Label("json"), testJSONWithoutProperty),
  85. Entry("secret value via data with property should return json-encoded string", Label("json"), testJSONWithProperty),
  86. Entry("dataFrom without property should extract key/value pairs", Label("json"), testDataFromJSONWithoutProperty),
  87. Entry("dataFrom with property should extract key/value pairs", Label("json"), testDataFromJSONWithProperty),
  88. )
  89. })
  90. func useTokenAuth(tc *framework.TestCase) {
  91. tc.ExternalSecret.Spec.SecretStoreRef.Name = tc.Framework.Namespace.Name
  92. }
  93. func useCertAuth(tc *framework.TestCase) {
  94. tc.ExternalSecret.Spec.SecretStoreRef.Name = certAuthProviderName
  95. }
  96. func useApproleAuth(tc *framework.TestCase) {
  97. tc.ExternalSecret.Spec.SecretStoreRef.Name = appRoleAuthProviderName
  98. }
  99. func useV1Provider(tc *framework.TestCase) {
  100. tc.ExternalSecret.Spec.SecretStoreRef.Name = kvv1ProviderName
  101. }
  102. func useJWTProvider(tc *framework.TestCase) {
  103. tc.ExternalSecret.Spec.SecretStoreRef.Name = jwtProviderName
  104. }
  105. func useJWTK8sProvider(tc *framework.TestCase) {
  106. tc.ExternalSecret.Spec.SecretStoreRef.Name = jwtK8sProviderName
  107. }
  108. func useKubernetesProvider(tc *framework.TestCase) {
  109. tc.ExternalSecret.Spec.SecretStoreRef.Name = kubernetesProviderName
  110. }
  111. const jsonVal = `{"foo":{"nested":{"bar":"mysecret","baz":"bang"}}}`
  112. // when no property is set it should return the json-encoded at path.
  113. func testJSONWithoutProperty(tc *framework.TestCase) {
  114. secretKey := fmt.Sprintf("%s-%s", tc.Framework.Namespace.Name, "json")
  115. tc.Secrets = map[string]framework.SecretEntry{
  116. secretKey: {Value: jsonVal},
  117. }
  118. tc.ExpectedSecret = &v1.Secret{
  119. Type: v1.SecretTypeOpaque,
  120. Data: map[string][]byte{
  121. secretKey: []byte(jsonVal),
  122. },
  123. }
  124. tc.ExternalSecret.Spec.Data = []esapi.ExternalSecretData{
  125. {
  126. SecretKey: secretKey,
  127. RemoteRef: esapi.ExternalSecretDataRemoteRef{
  128. Key: secretKey,
  129. },
  130. },
  131. }
  132. }
  133. // when property is set it should return the json-encoded at path.
  134. func testJSONWithProperty(tc *framework.TestCase) {
  135. secretKey := fmt.Sprintf("%s-%s", tc.Framework.Namespace.Name, "json")
  136. expectedVal := `{"bar":"mysecret","baz":"bang"}`
  137. tc.Secrets = map[string]framework.SecretEntry{
  138. secretKey: {Value: jsonVal},
  139. }
  140. tc.ExpectedSecret = &v1.Secret{
  141. Type: v1.SecretTypeOpaque,
  142. Data: map[string][]byte{
  143. secretKey: []byte(expectedVal),
  144. },
  145. }
  146. tc.ExternalSecret.Spec.Data = []esapi.ExternalSecretData{
  147. {
  148. SecretKey: secretKey,
  149. RemoteRef: esapi.ExternalSecretDataRemoteRef{
  150. Key: secretKey,
  151. Property: "foo.nested",
  152. },
  153. },
  154. }
  155. }
  156. // when no property is set it should extract the key/value pairs at the given path
  157. // note: it should json-encode if a value contains nested data
  158. func testDataFromJSONWithoutProperty(tc *framework.TestCase) {
  159. secretKey := fmt.Sprintf("%s-%s", tc.Framework.Namespace.Name, "json")
  160. tc.Secrets = map[string]framework.SecretEntry{
  161. secretKey: {Value: jsonVal},
  162. }
  163. tc.ExpectedSecret = &v1.Secret{
  164. Type: v1.SecretTypeOpaque,
  165. Data: map[string][]byte{
  166. "foo": []byte(`{"nested":{"bar":"mysecret","baz":"bang"}}`),
  167. },
  168. }
  169. tc.ExternalSecret.Spec.DataFrom = []esapi.ExternalSecretDataFromRemoteRef{
  170. {
  171. Extract: &esapi.ExternalSecretDataRemoteRef{
  172. Key: secretKey,
  173. },
  174. },
  175. }
  176. }
  177. // when property is set it should extract values with dataFrom at the given path.
  178. func testDataFromJSONWithProperty(tc *framework.TestCase) {
  179. secretKey := fmt.Sprintf("%s-%s", tc.Framework.Namespace.Name, "json")
  180. tc.Secrets = map[string]framework.SecretEntry{
  181. secretKey: {Value: jsonVal},
  182. }
  183. tc.ExpectedSecret = &v1.Secret{
  184. Type: v1.SecretTypeOpaque,
  185. Data: map[string][]byte{
  186. "bar": []byte(`mysecret`),
  187. "baz": []byte(`bang`),
  188. },
  189. }
  190. tc.ExternalSecret.Spec.DataFrom = []esapi.ExternalSecretDataFromRemoteRef{
  191. {
  192. Extract: &esapi.ExternalSecretDataRemoteRef{
  193. Key: secretKey,
  194. Property: "foo.nested",
  195. },
  196. },
  197. }
  198. }