find_by_name.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. Copyright © 2025 ESO Maintainer Team
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. https://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package common
  14. import (
  15. "fmt"
  16. v1 "k8s.io/api/core/v1"
  17. "github.com/external-secrets/external-secrets-e2e/framework"
  18. esapi "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  19. )
  20. const (
  21. findValue = "{\"foo1\":\"foo1-val\"}"
  22. )
  23. // This case creates multiple secrets with simple key/value pairs and syncs them using multiple .Spec.Data blocks.
  24. func FindByName(f *framework.Framework) (string, func(*framework.TestCase)) {
  25. return "[common] should find secrets by name using .DataFrom[]", func(tc *framework.TestCase) {
  26. const namePrefix = "e2e_find_name_%s_%s"
  27. secretKeyOne := fmt.Sprintf(namePrefix, f.Namespace.Name, "one")
  28. secretKeyTwo := fmt.Sprintf(namePrefix, f.Namespace.Name, "two")
  29. secretKeyThree := fmt.Sprintf(namePrefix, f.Namespace.Name, "three")
  30. secretValue := findValue
  31. tc.Secrets = map[string]framework.SecretEntry{
  32. f.MakeRemoteRefKey(secretKeyOne): {Value: secretValue},
  33. f.MakeRemoteRefKey(secretKeyTwo): {Value: secretValue},
  34. f.MakeRemoteRefKey(secretKeyThree): {Value: secretValue},
  35. }
  36. tc.ExpectedSecret = &v1.Secret{
  37. Type: v1.SecretTypeOpaque,
  38. Data: map[string][]byte{
  39. secretKeyOne: []byte(secretValue),
  40. secretKeyTwo: []byte(secretValue),
  41. secretKeyThree: []byte(secretValue),
  42. },
  43. }
  44. tc.ExternalSecret.Spec.DataFrom = []esapi.ExternalSecretDataFromRemoteRef{
  45. {
  46. Find: &esapi.ExternalSecretFind{
  47. Name: &esapi.FindName{
  48. RegExp: fmt.Sprintf("e2e_find_name_%s.+", f.Namespace.Name),
  49. },
  50. },
  51. },
  52. }
  53. }
  54. }
  55. // This case creates multiple secrets with simple key/value pairs and syncs them using multiple .Spec.Data blocks.
  56. func FindByNameAndRewrite(f *framework.Framework) (string, func(*framework.TestCase)) {
  57. return "[common] should find and rewrite secrets by name using .DataFrom[]", func(tc *framework.TestCase) {
  58. const namePrefix = "e2e_find_and_rewrite_%s_%s"
  59. secretKeyOne := fmt.Sprintf(namePrefix, f.Namespace.Name, "one")
  60. secretKeyTwo := fmt.Sprintf(namePrefix, f.Namespace.Name, "two")
  61. secretKeyThree := fmt.Sprintf(namePrefix, f.Namespace.Name, "three")
  62. expectedKeyOne := fmt.Sprintf("%s_%s", f.Namespace.Name, "one")
  63. expectedKeyTwo := fmt.Sprintf("%s_%s", f.Namespace.Name, "two")
  64. expectedKeyThree := fmt.Sprintf("%s_%s", f.Namespace.Name, "three")
  65. secretValue := findValue
  66. tc.Secrets = map[string]framework.SecretEntry{
  67. f.MakeRemoteRefKey(secretKeyOne): {Value: secretValue},
  68. f.MakeRemoteRefKey(secretKeyTwo): {Value: secretValue},
  69. f.MakeRemoteRefKey(secretKeyThree): {Value: secretValue},
  70. }
  71. tc.ExpectedSecret = &v1.Secret{
  72. Type: v1.SecretTypeOpaque,
  73. Data: map[string][]byte{
  74. expectedKeyOne: []byte(secretValue),
  75. expectedKeyTwo: []byte(secretValue),
  76. expectedKeyThree: []byte(secretValue),
  77. },
  78. }
  79. tc.ExternalSecret.Spec.DataFrom = []esapi.ExternalSecretDataFromRemoteRef{
  80. {
  81. Find: &esapi.ExternalSecretFind{
  82. Name: &esapi.FindName{
  83. RegExp: fmt.Sprintf("e2e_find_and_rewrite_%s.+", f.Namespace.Name),
  84. },
  85. },
  86. Rewrite: []esapi.ExternalSecretRewrite{
  87. {
  88. Regexp: &esapi.ExternalSecretRewriteRegexp{
  89. Source: "e2e_find_and_rewrite_(.*)",
  90. Target: "$1",
  91. },
  92. },
  93. },
  94. },
  95. }
  96. }
  97. }
  98. func FindByNameWithPath(f *framework.Framework) (string, func(*framework.TestCase)) {
  99. return "[common] should find secrets by name with path", func(tc *framework.TestCase) {
  100. secretKeyOne := fmt.Sprintf("e2e-find-name-%s-one", f.Namespace.Name)
  101. secretKeyTwo := fmt.Sprintf("%s-two", f.Namespace.Name)
  102. secretKeythree := fmt.Sprintf("%s-three", f.Namespace.Name)
  103. secretValue := findValue
  104. tc.Secrets = map[string]framework.SecretEntry{
  105. secretKeyOne: {Value: secretValue},
  106. secretKeyTwo: {Value: secretValue},
  107. secretKeythree: {Value: secretValue},
  108. }
  109. tc.ExpectedSecret = &v1.Secret{
  110. Type: v1.SecretTypeOpaque,
  111. Data: map[string][]byte{
  112. fmt.Sprintf("%s-two", f.Namespace.Name): []byte(secretValue),
  113. fmt.Sprintf("%s-three", f.Namespace.Name): []byte(secretValue),
  114. },
  115. }
  116. tc.ExternalSecret.Spec.DataFrom = []esapi.ExternalSecretDataFromRemoteRef{
  117. {
  118. Find: &esapi.ExternalSecretFind{
  119. Path: &f.Namespace.Name,
  120. Name: &esapi.FindName{
  121. RegExp: ".+",
  122. },
  123. },
  124. },
  125. }
  126. }
  127. }