auth_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 dvls
  14. import (
  15. "context"
  16. "testing"
  17. "github.com/stretchr/testify/assert"
  18. "github.com/stretchr/testify/require"
  19. corev1 "k8s.io/api/core/v1"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. "k8s.io/apimachinery/pkg/runtime"
  22. "sigs.k8s.io/controller-runtime/pkg/client/fake"
  23. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  24. esmeta "github.com/external-secrets/external-secrets/apis/meta/v1"
  25. )
  26. func TestNewDVLSClient_CrossNamespaceSecurityConstraint(t *testing.T) {
  27. otherNamespace := "other-namespace"
  28. tests := []struct {
  29. name string
  30. storeKind string
  31. namespace string
  32. secretNS *string
  33. expectError bool
  34. errorMsg string
  35. }{
  36. {
  37. name: "ClusterSecretStore can access cross-namespace secrets",
  38. storeKind: esv1.ClusterSecretStoreKind,
  39. namespace: testNamespace,
  40. secretNS: &otherNamespace,
  41. expectError: false,
  42. },
  43. {
  44. name: "SecretStore cannot access cross-namespace secrets",
  45. storeKind: esv1.SecretStoreKind,
  46. namespace: testNamespace,
  47. secretNS: &otherNamespace,
  48. expectError: true,
  49. errorMsg: "cannot get Kubernetes secret",
  50. },
  51. {
  52. name: "SecretStore can access same-namespace secrets",
  53. storeKind: esv1.SecretStoreKind,
  54. namespace: testNamespace,
  55. secretNS: nil,
  56. expectError: false,
  57. },
  58. }
  59. for _, tt := range tests {
  60. t.Run(tt.name, func(t *testing.T) {
  61. targetNS := testNamespace
  62. if tt.secretNS != nil {
  63. targetNS = *tt.secretNS
  64. }
  65. secret := &corev1.Secret{
  66. ObjectMeta: metav1.ObjectMeta{
  67. Name: secretName,
  68. Namespace: targetNS,
  69. },
  70. Data: map[string][]byte{
  71. appIDKey: []byte(testAppID),
  72. appSecretKey: []byte(testAppSecret),
  73. },
  74. }
  75. scheme := runtime.NewScheme()
  76. require.NoError(t, corev1.AddToScheme(scheme))
  77. kube := fake.NewClientBuilder().WithScheme(scheme).WithObjects(secret).Build()
  78. provider := &esv1.DVLSProvider{
  79. ServerURL: testServerURL,
  80. Auth: esv1.DVLSAuth{
  81. SecretRef: esv1.DVLSAuthSecretRef{
  82. AppID: esmeta.SecretKeySelector{
  83. Name: secretName,
  84. Key: appIDKey,
  85. Namespace: tt.secretNS,
  86. },
  87. AppSecret: esmeta.SecretKeySelector{
  88. Name: secretName,
  89. Key: appSecretKey,
  90. Namespace: tt.secretNS,
  91. },
  92. },
  93. },
  94. }
  95. client, err := NewDVLSClient(context.Background(), kube, tt.storeKind, tt.namespace, provider)
  96. if tt.expectError {
  97. require.Error(t, err)
  98. require.Nil(t, client)
  99. if tt.errorMsg != "" {
  100. assert.Contains(t, err.Error(), tt.errorMsg)
  101. }
  102. } else if err != nil {
  103. // Verify Kubernetes secret access succeeded (DVLS connection will fail due to fake server).
  104. assert.NotContains(t, err.Error(), "failed to get appId")
  105. assert.NotContains(t, err.Error(), "failed to get appSecret")
  106. assert.NotContains(t, err.Error(), "cannot get Kubernetes secret")
  107. }
  108. })
  109. }
  110. }