client_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 barbican
  14. import (
  15. "context"
  16. "testing"
  17. "github.com/gophercloud/gophercloud/v2"
  18. "github.com/stretchr/testify/assert"
  19. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  20. )
  21. func TestExtractUUIDFromRef(t *testing.T) {
  22. testCases := []struct {
  23. name string
  24. secretRef string
  25. expectedUUID string
  26. }{
  27. {
  28. name: "valid barbican secret ref",
  29. secretRef: "https://barbican.example.com/v1/secrets/12345678-1234-1234-1234-123456789abc",
  30. expectedUUID: "12345678-1234-1234-1234-123456789abc",
  31. },
  32. {
  33. name: "secret ref without protocol",
  34. secretRef: "barbican.example.com/v1/secrets/87654321-4321-4321-4321-cba987654321",
  35. expectedUUID: "87654321-4321-4321-4321-cba987654321",
  36. },
  37. {
  38. name: "empty string",
  39. secretRef: "",
  40. expectedUUID: "",
  41. },
  42. {
  43. name: "trailing slash",
  44. secretRef: "https://barbican.example.com/v1/secrets/12345678-1234-1234-1234-123456789abc/",
  45. expectedUUID: "",
  46. },
  47. }
  48. for _, tc := range testCases {
  49. t.Run(tc.name, func(t *testing.T) {
  50. uuid := extractUUIDFromRef(tc.secretRef)
  51. assert.Equal(t, tc.expectedUUID, uuid)
  52. })
  53. }
  54. }
  55. func TestGetSecretPayloadProperty(t *testing.T) {
  56. testPayload := []byte(`{"username":"admin","password":"secret123","nested":{"key":"value"}}`)
  57. testCases := []struct {
  58. name string
  59. payload []byte
  60. property string
  61. expectError bool
  62. errorMessage string
  63. expectedData []byte
  64. }{
  65. {
  66. name: "empty property returns full payload",
  67. payload: testPayload,
  68. property: "",
  69. expectError: false,
  70. expectedData: testPayload,
  71. },
  72. {
  73. name: "valid property extraction",
  74. payload: testPayload,
  75. property: "username",
  76. expectError: false,
  77. expectedData: []byte(`"admin"`),
  78. },
  79. {
  80. name: "nested property extraction",
  81. payload: testPayload,
  82. property: "nested",
  83. expectError: false,
  84. expectedData: []byte(`{"key":"value"}`),
  85. },
  86. {
  87. name: "property not found",
  88. payload: testPayload,
  89. property: "nonexistent",
  90. expectError: true,
  91. errorMessage: "property nonexistent not found in secret payload",
  92. },
  93. {
  94. name: "invalid JSON",
  95. payload: []byte("invalid-json"),
  96. property: "username",
  97. expectError: true,
  98. errorMessage: "barbican client",
  99. },
  100. }
  101. for _, tc := range testCases {
  102. t.Run(tc.name, func(t *testing.T) {
  103. data, err := getSecretPayloadProperty(tc.payload, tc.property)
  104. if tc.expectError {
  105. assert.Error(t, err)
  106. assert.Contains(t, err.Error(), tc.errorMessage)
  107. assert.Nil(t, data)
  108. } else {
  109. assert.NoError(t, err)
  110. assert.Equal(t, tc.expectedData, data)
  111. }
  112. })
  113. }
  114. }
  115. func TestUnsupportedOperations(t *testing.T) {
  116. client := &Client{
  117. keyManager: &gophercloud.ServiceClient{},
  118. }
  119. // Test PushSecret
  120. err := client.PushSecret(context.Background(), nil, nil)
  121. assert.Error(t, err)
  122. assert.Contains(t, err.Error(), "does not support pushing secrets")
  123. // Test SecretExists
  124. exists, err := client.SecretExists(context.Background(), nil)
  125. assert.Error(t, err)
  126. assert.False(t, exists)
  127. assert.Contains(t, err.Error(), "barbican provider does not pushing secrets with update policy IfNotExists")
  128. // Test DeleteSecret
  129. err = client.DeleteSecret(context.Background(), nil)
  130. assert.Error(t, err)
  131. assert.Contains(t, err.Error(), "does not support deleting secrets")
  132. }
  133. func TestValidateAndClose(t *testing.T) {
  134. client := &Client{
  135. keyManager: &gophercloud.ServiceClient{},
  136. }
  137. // Test Validate
  138. result, err := client.Validate()
  139. assert.NoError(t, err)
  140. assert.Equal(t, esv1.ValidationResultUnknown, result)
  141. // Test Close
  142. err = client.Close(context.Background())
  143. assert.NoError(t, err)
  144. }
  145. func TestGetAllSecretsValidation(t *testing.T) {
  146. client := &Client{
  147. keyManager: &gophercloud.ServiceClient{},
  148. }
  149. testCases := []struct {
  150. name string
  151. findRef esv1.ExternalSecretFind
  152. expectError bool
  153. errorMessage string
  154. }{
  155. {
  156. name: "no name specified should return error",
  157. findRef: esv1.ExternalSecretFind{
  158. Name: nil,
  159. },
  160. expectError: true,
  161. errorMessage: "missing field",
  162. },
  163. {
  164. name: "empty name regex should return error",
  165. findRef: esv1.ExternalSecretFind{
  166. Name: &esv1.FindName{
  167. RegExp: "",
  168. },
  169. },
  170. expectError: true,
  171. errorMessage: "missing field",
  172. },
  173. }
  174. for _, tc := range testCases {
  175. t.Run(tc.name, func(t *testing.T) {
  176. _, err := client.GetAllSecrets(context.Background(), tc.findRef)
  177. if tc.expectError {
  178. assert.Error(t, err)
  179. assert.Contains(t, err.Error(), tc.errorMessage)
  180. } else if err != nil {
  181. assert.Contains(t, err.Error(), "barbican client")
  182. }
  183. })
  184. }
  185. }