vault.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. "context"
  14. "fmt"
  15. "time"
  16. apierrors "k8s.io/apimachinery/pkg/api/errors"
  17. "k8s.io/apimachinery/pkg/types"
  18. "k8s.io/apimachinery/pkg/util/wait"
  19. // nolint
  20. . "github.com/onsi/ginkgo/v2"
  21. . "github.com/onsi/gomega"
  22. v1 "k8s.io/api/core/v1"
  23. "github.com/external-secrets/external-secrets-e2e/framework"
  24. "github.com/external-secrets/external-secrets-e2e/suites/provider/cases/common"
  25. esapi "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  26. )
  27. const (
  28. withTokenAuth = "with token auth"
  29. withTokenAuthAndMTLS = "with token auth and mTLS"
  30. withCertAuth = "with cert auth"
  31. withApprole = "with approle auth"
  32. withV1 = "with v1 provider"
  33. withJWT = "with jwt provider"
  34. withJWTK8s = "with jwt k8s provider"
  35. withK8s = "with kubernetes provider"
  36. withReferentAuth = "with referent provider"
  37. withReferentAuthAndMTLS = "with referent provider and mTLS"
  38. )
  39. var _ = Describe("[vault]", Label("vault"), func() {
  40. f := framework.New("eso-vault")
  41. prov := newVaultProvider(f)
  42. DescribeTable("sync secrets",
  43. framework.TableFuncWithExternalSecret(f, prov),
  44. // uses token auth
  45. framework.Compose(withTokenAuth, f, common.FindByName, useTokenAuth),
  46. framework.Compose(withTokenAuth, f, common.FindByNameAndRewrite, useTokenAuth),
  47. framework.Compose(withTokenAuth, f, common.JSONDataFromSync, useTokenAuth),
  48. framework.Compose(withTokenAuth, f, common.JSONDataFromRewrite, useTokenAuth),
  49. framework.Compose(withTokenAuth, f, common.JSONDataWithProperty, useTokenAuth),
  50. framework.Compose(withTokenAuth, f, common.JSONDataWithTemplate, useTokenAuth),
  51. framework.Compose(withTokenAuth, f, common.DataPropertyDockerconfigJSON, useTokenAuth),
  52. framework.Compose(withTokenAuth, f, common.JSONDataWithoutTargetName, useTokenAuth),
  53. framework.Compose(withTokenAuth, f, common.SyncV1Alpha1, useTokenAuth),
  54. framework.Compose(withTokenAuth, f, common.DecodingPolicySync, useTokenAuth),
  55. framework.Compose(withTokenAuth, f, common.JSONDataWithTemplateFromLiteral, useTokenAuth),
  56. framework.Compose(withTokenAuth, f, common.TemplateFromConfigmaps, useTokenAuth),
  57. // use cert auth
  58. framework.Compose(withCertAuth, f, common.FindByName, useCertAuth),
  59. framework.Compose(withCertAuth, f, common.FindByNameAndRewrite, useCertAuth),
  60. framework.Compose(withCertAuth, f, common.JSONDataFromSync, useCertAuth),
  61. framework.Compose(withCertAuth, f, common.JSONDataFromRewrite, useCertAuth),
  62. framework.Compose(withCertAuth, f, common.JSONDataWithProperty, useCertAuth),
  63. framework.Compose(withCertAuth, f, common.JSONDataWithTemplate, useCertAuth),
  64. framework.Compose(withCertAuth, f, common.DataPropertyDockerconfigJSON, useCertAuth),
  65. framework.Compose(withCertAuth, f, common.JSONDataWithoutTargetName, useCertAuth),
  66. // use approle auth
  67. framework.Compose(withApprole, f, common.FindByName, useApproleAuth),
  68. framework.Compose(withApprole, f, common.FindByNameAndRewrite, useApproleAuth),
  69. framework.Compose(withApprole, f, common.JSONDataFromSync, useApproleAuth),
  70. framework.Compose(withApprole, f, common.JSONDataFromRewrite, useApproleAuth),
  71. framework.Compose(withApprole, f, common.JSONDataWithProperty, useApproleAuth),
  72. framework.Compose(withApprole, f, common.JSONDataWithTemplate, useApproleAuth),
  73. framework.Compose(withApprole, f, common.DataPropertyDockerconfigJSON, useApproleAuth),
  74. framework.Compose(withApprole, f, common.JSONDataWithoutTargetName, useApproleAuth),
  75. // use v1 provider
  76. framework.Compose(withV1, f, common.JSONDataFromSync, useV1Provider),
  77. framework.Compose(withV1, f, common.JSONDataFromRewrite, useV1Provider),
  78. framework.Compose(withV1, f, common.JSONDataWithProperty, useV1Provider),
  79. framework.Compose(withV1, f, common.JSONDataWithTemplate, useV1Provider),
  80. framework.Compose(withV1, f, common.DataPropertyDockerconfigJSON, useV1Provider),
  81. framework.Compose(withV1, f, common.JSONDataWithoutTargetName, useV1Provider),
  82. // use jwt provider
  83. framework.Compose(withJWT, f, common.FindByName, useJWTProvider),
  84. framework.Compose(withJWT, f, common.FindByNameAndRewrite, useJWTProvider),
  85. framework.Compose(withJWT, f, common.JSONDataFromSync, useJWTProvider),
  86. framework.Compose(withJWT, f, common.JSONDataFromRewrite, useJWTProvider),
  87. framework.Compose(withJWT, f, common.JSONDataWithProperty, useJWTProvider),
  88. framework.Compose(withJWT, f, common.JSONDataWithTemplate, useJWTProvider),
  89. framework.Compose(withJWT, f, common.DataPropertyDockerconfigJSON, useJWTProvider),
  90. framework.Compose(withJWT, f, common.JSONDataWithoutTargetName, useJWTProvider),
  91. // use jwt k8s provider
  92. framework.Compose(withJWTK8s, f, common.JSONDataFromSync, useJWTK8sProvider),
  93. framework.Compose(withJWTK8s, f, common.JSONDataFromRewrite, useJWTK8sProvider),
  94. framework.Compose(withJWTK8s, f, common.JSONDataWithProperty, useJWTK8sProvider),
  95. framework.Compose(withJWTK8s, f, common.JSONDataWithTemplate, useJWTK8sProvider),
  96. framework.Compose(withJWTK8s, f, common.DataPropertyDockerconfigJSON, useJWTK8sProvider),
  97. framework.Compose(withJWTK8s, f, common.JSONDataWithoutTargetName, useJWTK8sProvider),
  98. // use kubernetes provider
  99. framework.Compose(withK8s, f, common.FindByName, useKubernetesProvider),
  100. framework.Compose(withK8s, f, common.FindByNameAndRewrite, useKubernetesProvider),
  101. framework.Compose(withK8s, f, common.JSONDataFromSync, useKubernetesProvider),
  102. framework.Compose(withK8s, f, common.JSONDataFromRewrite, useKubernetesProvider),
  103. framework.Compose(withK8s, f, common.JSONDataWithProperty, useKubernetesProvider),
  104. framework.Compose(withK8s, f, common.JSONDataWithTemplate, useKubernetesProvider),
  105. framework.Compose(withK8s, f, common.DataPropertyDockerconfigJSON, useKubernetesProvider),
  106. framework.Compose(withK8s, f, common.JSONDataWithoutTargetName, useKubernetesProvider),
  107. // use referent auth
  108. framework.Compose(withReferentAuth, f, common.JSONDataFromSync, useReferentAuth),
  109. // vault-specific test cases
  110. Entry("secret value via data without property should return json-encoded string", Label("json"), testJSONWithoutProperty),
  111. Entry("secret value via data with property should return json-encoded string", Label("json"), testJSONWithProperty),
  112. Entry("dataFrom without property should extract key/value pairs", Label("json"), testDataFromJSONWithoutProperty),
  113. Entry("dataFrom with property should extract key/value pairs", Label("json"), testDataFromJSONWithProperty),
  114. )
  115. })
  116. var _ = Describe("[vault] with mTLS", Label("vault", "vault-mtls"), func() {
  117. f := framework.New("eso-vault")
  118. prov := newVaultProvider(f)
  119. DescribeTable("sync secrets",
  120. framework.TableFuncWithExternalSecret(f, prov),
  121. // uses token auth
  122. framework.Compose(withTokenAuthAndMTLS, f, common.FindByName, useMTLSAndTokenAuth),
  123. // use referent auth
  124. framework.Compose(withReferentAuthAndMTLS, f, common.JSONDataFromSync, useMTLSAndReferentAuth),
  125. // vault-specific test cases
  126. Entry("store without clientTLS configuration should not be valid", Label("vault-invalid-store"), testInvalidMtlsStore),
  127. )
  128. })
  129. func useTokenAuth(tc *framework.TestCase) {
  130. tc.ExternalSecret.Spec.SecretStoreRef.Name = tc.Framework.Namespace.Name
  131. }
  132. func useMTLSAndTokenAuth(tc *framework.TestCase) {
  133. tc.ExternalSecret.Spec.SecretStoreRef.Name = tc.Framework.Namespace.Name + mtlsSuffix
  134. }
  135. func useCertAuth(tc *framework.TestCase) {
  136. tc.ExternalSecret.Spec.SecretStoreRef.Name = certAuthProviderName
  137. }
  138. func useApproleAuth(tc *framework.TestCase) {
  139. tc.ExternalSecret.Spec.SecretStoreRef.Name = appRoleAuthProviderName
  140. }
  141. func useV1Provider(tc *framework.TestCase) {
  142. tc.ExternalSecret.Spec.SecretStoreRef.Name = kvv1ProviderName
  143. }
  144. func useJWTProvider(tc *framework.TestCase) {
  145. tc.ExternalSecret.Spec.SecretStoreRef.Name = jwtProviderName
  146. }
  147. func useJWTK8sProvider(tc *framework.TestCase) {
  148. tc.ExternalSecret.Spec.SecretStoreRef.Name = jwtK8sProviderName
  149. }
  150. func useKubernetesProvider(tc *framework.TestCase) {
  151. tc.ExternalSecret.Spec.SecretStoreRef.Name = kubernetesProviderName
  152. }
  153. func useReferentAuth(tc *framework.TestCase) {
  154. tc.ExternalSecret.Spec.SecretStoreRef.Name = referentSecretStoreName(tc.Framework)
  155. tc.ExternalSecret.Spec.SecretStoreRef.Kind = esapi.ClusterSecretStoreKind
  156. }
  157. func useMTLSAndReferentAuth(tc *framework.TestCase) {
  158. tc.ExternalSecret.Spec.SecretStoreRef.Name = referentSecretStoreName(tc.Framework) + mtlsSuffix
  159. tc.ExternalSecret.Spec.SecretStoreRef.Kind = esapi.ClusterSecretStoreKind
  160. }
  161. const jsonVal = `{"foo":{"nested":{"bar":"mysecret","baz":"bang"}}}`
  162. // when no property is set it should return the json-encoded at path.
  163. func testJSONWithoutProperty(tc *framework.TestCase) {
  164. secretKey := fmt.Sprintf("%s-%s", tc.Framework.Namespace.Name, "json")
  165. tc.Secrets = map[string]framework.SecretEntry{
  166. secretKey: {Value: jsonVal},
  167. }
  168. tc.ExpectedSecret = &v1.Secret{
  169. Type: v1.SecretTypeOpaque,
  170. Data: map[string][]byte{
  171. secretKey: []byte(jsonVal),
  172. },
  173. }
  174. tc.ExternalSecret.Spec.Data = []esapi.ExternalSecretData{
  175. {
  176. SecretKey: secretKey,
  177. RemoteRef: esapi.ExternalSecretDataRemoteRef{
  178. Key: secretKey,
  179. },
  180. },
  181. }
  182. }
  183. // when property is set it should return the json-encoded at path.
  184. func testJSONWithProperty(tc *framework.TestCase) {
  185. secretKey := fmt.Sprintf("%s-%s", tc.Framework.Namespace.Name, "json")
  186. expectedVal := `{"bar":"mysecret","baz":"bang"}`
  187. tc.Secrets = map[string]framework.SecretEntry{
  188. secretKey: {Value: jsonVal},
  189. }
  190. tc.ExpectedSecret = &v1.Secret{
  191. Type: v1.SecretTypeOpaque,
  192. Data: map[string][]byte{
  193. secretKey: []byte(expectedVal),
  194. },
  195. }
  196. tc.ExternalSecret.Spec.Data = []esapi.ExternalSecretData{
  197. {
  198. SecretKey: secretKey,
  199. RemoteRef: esapi.ExternalSecretDataRemoteRef{
  200. Key: secretKey,
  201. Property: "foo.nested",
  202. },
  203. },
  204. }
  205. }
  206. // when no property is set it should extract the key/value pairs at the given path
  207. // note: it should json-encode if a value contains nested data
  208. func testDataFromJSONWithoutProperty(tc *framework.TestCase) {
  209. secretKey := fmt.Sprintf("%s-%s", tc.Framework.Namespace.Name, "json")
  210. tc.Secrets = map[string]framework.SecretEntry{
  211. secretKey: {Value: jsonVal},
  212. }
  213. tc.ExpectedSecret = &v1.Secret{
  214. Type: v1.SecretTypeOpaque,
  215. Data: map[string][]byte{
  216. "foo": []byte(`{"nested":{"bar":"mysecret","baz":"bang"}}`),
  217. },
  218. }
  219. tc.ExternalSecret.Spec.DataFrom = []esapi.ExternalSecretDataFromRemoteRef{
  220. {
  221. Extract: &esapi.ExternalSecretDataRemoteRef{
  222. Key: secretKey,
  223. },
  224. },
  225. }
  226. }
  227. // when property is set it should extract values with dataFrom at the given path.
  228. func testDataFromJSONWithProperty(tc *framework.TestCase) {
  229. secretKey := fmt.Sprintf("%s-%s", tc.Framework.Namespace.Name, "json")
  230. tc.Secrets = map[string]framework.SecretEntry{
  231. secretKey: {Value: jsonVal},
  232. }
  233. tc.ExpectedSecret = &v1.Secret{
  234. Type: v1.SecretTypeOpaque,
  235. Data: map[string][]byte{
  236. "bar": []byte(`mysecret`),
  237. "baz": []byte(`bang`),
  238. },
  239. }
  240. tc.ExternalSecret.Spec.DataFrom = []esapi.ExternalSecretDataFromRemoteRef{
  241. {
  242. Extract: &esapi.ExternalSecretDataRemoteRef{
  243. Key: secretKey,
  244. Property: "foo.nested",
  245. },
  246. },
  247. }
  248. }
  249. func testInvalidMtlsStore(tc *framework.TestCase) {
  250. tc.ExternalSecret = nil
  251. tc.ExpectedSecret = nil
  252. err := wait.PollUntilContextTimeout(context.Background(), time.Second*10, time.Minute, true, func(context context.Context) (bool, error) {
  253. var ss esapi.SecretStore
  254. err := tc.Framework.CRClient.Get(context, types.NamespacedName{
  255. Namespace: tc.Framework.Namespace.Name,
  256. Name: tc.Framework.Namespace.Name + invalidMtlSuffix,
  257. }, &ss)
  258. if apierrors.IsNotFound(err) {
  259. return false, nil
  260. }
  261. if len(ss.Status.Conditions) == 0 {
  262. return false, nil
  263. }
  264. Expect(string(ss.Status.Conditions[0].Type)).Should(Equal("Ready"))
  265. Expect(string(ss.Status.Conditions[0].Status)).Should(Equal("False"))
  266. Expect(ss.Status.Conditions[0].Reason).Should(Equal("ValidationFailed"))
  267. Expect(ss.Status.Conditions[0].Message).Should(ContainSubstring("unable to validate store"))
  268. return true, nil
  269. })
  270. Expect(err).ToNot(HaveOccurred())
  271. }