vault.go 12 KB

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