vault.go 7.9 KB

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