vault.go 10 KB

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