vault.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. "net/http"
  16. vault "github.com/hashicorp/vault/api"
  17. // nolint
  18. . "github.com/onsi/ginkgo"
  19. // nolint
  20. . "github.com/onsi/gomega"
  21. v1 "k8s.io/api/core/v1"
  22. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  23. esv1alpha1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1alpha1"
  24. esmeta "github.com/external-secrets/external-secrets/apis/meta/v1"
  25. "github.com/external-secrets/external-secrets/e2e/framework"
  26. )
  27. var _ = Describe("[vault] ", func() {
  28. f := framework.New("eso-vault")
  29. var secretStore *esv1alpha1.SecretStore
  30. BeforeEach(func() {
  31. By("creating an secret store for vault")
  32. vaultCreds := &v1.Secret{
  33. ObjectMeta: metav1.ObjectMeta{
  34. Name: f.Namespace.Name,
  35. Namespace: f.Namespace.Name,
  36. },
  37. StringData: map[string]string{
  38. "token": "root", // vault dev-mode default token
  39. },
  40. }
  41. err := f.CRClient.Create(context.Background(), vaultCreds)
  42. Expect(err).ToNot(HaveOccurred())
  43. secretStore = &esv1alpha1.SecretStore{
  44. ObjectMeta: metav1.ObjectMeta{
  45. Name: f.Namespace.Name,
  46. Namespace: f.Namespace.Name,
  47. },
  48. Spec: esv1alpha1.SecretStoreSpec{
  49. Provider: &esv1alpha1.SecretStoreProvider{
  50. Vault: &esv1alpha1.VaultProvider{
  51. Version: esv1alpha1.VaultKVStoreV2,
  52. Path: "secret",
  53. Server: "http://vault.default:8200",
  54. Auth: esv1alpha1.VaultAuth{
  55. TokenSecretRef: &esmeta.SecretKeySelector{
  56. Name: f.Namespace.Name,
  57. Key: "token",
  58. },
  59. },
  60. },
  61. },
  62. },
  63. }
  64. err = f.CRClient.Create(context.Background(), secretStore)
  65. Expect(err).ToNot(HaveOccurred())
  66. })
  67. It("should sync secrets", func() {
  68. secretKey := fmt.Sprintf("%s-%s", f.Namespace.Name, "one")
  69. secretProp := "example"
  70. secretValue := "bar"
  71. targetSecret := "target-secret"
  72. By("creating a vault secret")
  73. vc, err := vault.NewClient(&vault.Config{
  74. Address: "http://vault.default:8200",
  75. })
  76. Expect(err).ToNot(HaveOccurred())
  77. vc.SetToken("root") // dev-mode default token
  78. req := vc.NewRequest(http.MethodPost, fmt.Sprintf("/v1/secret/data/%s", secretKey))
  79. err = req.SetJSONBody(map[string]interface{}{
  80. "data": map[string]string{
  81. secretProp: secretValue,
  82. },
  83. })
  84. Expect(err).ToNot(HaveOccurred())
  85. _, err = vc.RawRequestWithContext(context.Background(), req)
  86. Expect(err).ToNot(HaveOccurred())
  87. By("creating ExternalSecret")
  88. err = f.CRClient.Create(context.Background(), &esv1alpha1.ExternalSecret{
  89. ObjectMeta: metav1.ObjectMeta{
  90. Name: "simple-sync",
  91. Namespace: f.Namespace.Name,
  92. },
  93. Spec: esv1alpha1.ExternalSecretSpec{
  94. SecretStoreRef: esv1alpha1.SecretStoreRef{
  95. Name: secretStore.Name,
  96. },
  97. Target: esv1alpha1.ExternalSecretTarget{
  98. Name: targetSecret,
  99. },
  100. Data: []esv1alpha1.ExternalSecretData{
  101. {
  102. SecretKey: secretKey,
  103. RemoteRef: esv1alpha1.ExternalSecretDataRemoteRef{
  104. Key: secretKey,
  105. Property: secretProp,
  106. },
  107. },
  108. },
  109. },
  110. })
  111. Expect(err).ToNot(HaveOccurred())
  112. _, err = f.WaitForSecretValue(f.Namespace.Name, targetSecret, map[string][]byte{
  113. secretKey: []byte(secretValue),
  114. })
  115. Expect(err).ToNot(HaveOccurred())
  116. })
  117. })