externalsecret_controller_validation_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. Copyright © The ESO Authors
  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 externalsecret
  14. import (
  15. "strings"
  16. "testing"
  17. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  18. )
  19. func TestValidateFetchedSecretValue(t *testing.T) {
  20. t.Parallel()
  21. tests := []struct {
  22. name string
  23. policy esv1.ExternalSecretNullBytePolicy
  24. key string
  25. value []byte
  26. wantErr string
  27. }{
  28. {
  29. name: "zero value policy behaves like ignore",
  30. policy: "",
  31. key: "payload",
  32. value: []byte(nullByteSecretVal),
  33. },
  34. {
  35. name: "ignores null bytes when policy is not fail",
  36. policy: esv1.ExternalSecretNullBytePolicyIgnore,
  37. key: "payload",
  38. value: []byte(nullByteSecretVal),
  39. },
  40. {
  41. name: "allows nil values",
  42. policy: esv1.ExternalSecretNullBytePolicyFail,
  43. key: "payload",
  44. value: nil,
  45. },
  46. {
  47. name: "allows fetched data without null bytes",
  48. policy: esv1.ExternalSecretNullBytePolicyFail,
  49. key: "payload",
  50. value: []byte("QQBC"),
  51. },
  52. {
  53. name: "fails on fetched data containing null bytes",
  54. policy: esv1.ExternalSecretNullBytePolicyFail,
  55. key: "payload",
  56. value: []byte(nullByteSecretVal),
  57. wantErr: `fetched secret value for key "payload" contains null bytes`,
  58. },
  59. }
  60. for _, tt := range tests {
  61. t.Run(tt.name, func(t *testing.T) {
  62. t.Parallel()
  63. assertFetchedSecretValidationError(t, validateFetchedSecretValue(tt.policy, tt.key, tt.value), tt.wantErr)
  64. })
  65. }
  66. }
  67. func TestValidateFetchedSecretMap(t *testing.T) {
  68. t.Parallel()
  69. tests := []struct {
  70. name string
  71. policy esv1.ExternalSecretNullBytePolicy
  72. data map[string][]byte
  73. wantErr string
  74. }{
  75. {
  76. name: "allows nil secret data map",
  77. policy: esv1.ExternalSecretNullBytePolicyFail,
  78. data: nil,
  79. },
  80. {
  81. name: "reports the first offending key in sorted order",
  82. policy: esv1.ExternalSecretNullBytePolicyFail,
  83. data: map[string][]byte{
  84. "zeta": []byte(nullByteSecretVal),
  85. "alpha": []byte("C\x00D"),
  86. },
  87. wantErr: `fetched secret value for key "alpha" contains null bytes`,
  88. },
  89. }
  90. for _, tt := range tests {
  91. t.Run(tt.name, func(t *testing.T) {
  92. t.Parallel()
  93. assertFetchedSecretValidationError(t, validateFetchedSecretMap(tt.policy, tt.data), tt.wantErr)
  94. })
  95. }
  96. }
  97. func assertFetchedSecretValidationError(t *testing.T, err error, wantErr string) {
  98. t.Helper()
  99. if wantErr == "" {
  100. if err != nil {
  101. t.Fatalf("unexpected error = %v", err)
  102. }
  103. return
  104. }
  105. if err == nil {
  106. t.Fatalf("error = nil, want substring %q", wantErr)
  107. }
  108. if got := err.Error(); !strings.Contains(got, wantErr) {
  109. t.Fatalf("error = %q, want substring %q", got, wantErr)
  110. }
  111. }