providerstore_validator_test.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 v2alpha1
  14. import (
  15. "context"
  16. "strings"
  17. "testing"
  18. )
  19. func TestProviderStoreValidateCreateRejectsCrossNamespaceBackendRef(t *testing.T) {
  20. store := &ProviderStore{
  21. Spec: ProviderStoreSpec{
  22. RuntimeRef: StoreRuntimeRef{Name: "runtime"},
  23. BackendRef: BackendObjectReference{
  24. APIVersion: "provider.aws.external-secrets.io/v2alpha1",
  25. Kind: "SecretsManagerStore",
  26. Name: "team-a-backend",
  27. Namespace: "team-b",
  28. },
  29. },
  30. }
  31. store.Namespace = "team-a"
  32. if _, err := (&ProviderStoreValidator{}).ValidateCreate(context.Background(), store); err == nil {
  33. t.Fatal("expected cross-namespace backendRef to be rejected")
  34. }
  35. }
  36. func TestClusterProviderStoreValidateCreateAllowsOmittedBackendNamespace(t *testing.T) {
  37. store := &ClusterProviderStore{
  38. Spec: ClusterProviderStoreSpec{
  39. RuntimeRef: StoreRuntimeRef{Name: "runtime"},
  40. BackendRef: BackendObjectReference{
  41. APIVersion: "provider.aws.external-secrets.io/v2alpha1",
  42. Kind: "SecretsManagerStore",
  43. Name: "shared-backend",
  44. },
  45. },
  46. }
  47. if _, err := (&ClusterProviderStoreValidator{}).ValidateCreate(context.Background(), store); err != nil {
  48. t.Fatalf("expected omitted backendRef.namespace to be allowed: %v", err)
  49. }
  50. }
  51. func TestClusterProviderStoreValidateCreateRejectsInvalidNamespaceRegex(t *testing.T) {
  52. store := &ClusterProviderStore{
  53. Spec: ClusterProviderStoreSpec{
  54. RuntimeRef: StoreRuntimeRef{Name: "runtime"},
  55. BackendRef: BackendObjectReference{
  56. APIVersion: "provider.aws.external-secrets.io/v2alpha1",
  57. Kind: "SecretsManagerStore",
  58. Name: "shared-backend",
  59. },
  60. Conditions: []StoreNamespaceCondition{
  61. {
  62. NamespaceRegexes: []string{`\1`},
  63. },
  64. },
  65. },
  66. }
  67. _, err := (&ClusterProviderStoreValidator{}).ValidateCreate(context.Background(), store)
  68. if err == nil {
  69. t.Fatal("expected invalid namespace regex to be rejected")
  70. }
  71. if !strings.Contains(err.Error(), "failed to compile 0th namespace regex in 0th condition") {
  72. t.Fatalf("expected regex compilation failure, got: %v", err)
  73. }
  74. }