vault.go 7.7 KB

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