vault.go 9.0 KB

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