vault.go 12 KB

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