fake.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 fake
  14. import (
  15. "context"
  16. "errors"
  17. "net/url"
  18. btwcutil "github.com/external-secrets/external-secrets/providers/v1/beyondtrustworkloadcredentials/util"
  19. )
  20. type BeyondtrustWorkloadCredentialsClient struct {
  21. getSecret func(ctx context.Context, name string, folderPath *string) (*btwcutil.KV, error)
  22. getSecrets func(ctx context.Context, folderPath *string, recursive bool) ([]btwcutil.KVListItem, error)
  23. generateDynamicSecret func(ctx context.Context, name string, folderPath *string) (*btwcutil.GeneratedSecret, error)
  24. getCalls int
  25. }
  26. func (c *BeyondtrustWorkloadCredentialsClient) BaseURL() *url.URL {
  27. return &url.URL{Scheme: "", Host: ""}
  28. }
  29. func (c *BeyondtrustWorkloadCredentialsClient) SetBaseURL(urlStr string) error {
  30. return nil
  31. }
  32. func (c *BeyondtrustWorkloadCredentialsClient) CheckSession(ctx context.Context) error {
  33. // By default, fake client returns success for session check
  34. return nil
  35. }
  36. func (c *BeyondtrustWorkloadCredentialsClient) Authenticate() error {
  37. return nil
  38. }
  39. func (c *BeyondtrustWorkloadCredentialsClient) GetSecret(ctx context.Context, name string, folderPath *string) (*btwcutil.KV, error) {
  40. if c.getSecret == nil {
  41. return nil, errors.New("GetSecret not configured in fake client")
  42. }
  43. return c.getSecret(ctx, name, folderPath)
  44. }
  45. func (c *BeyondtrustWorkloadCredentialsClient) GetSecrets(ctx context.Context, folderPath *string, recursive bool) ([]btwcutil.KVListItem, error) {
  46. if c.getSecrets == nil {
  47. return nil, errors.New("GetSecrets not configured in fake client")
  48. }
  49. return c.getSecrets(ctx, folderPath, recursive)
  50. }
  51. func (c *BeyondtrustWorkloadCredentialsClient) GenerateDynamicSecret(ctx context.Context, name string, folderPath *string) (*btwcutil.GeneratedSecret, error) {
  52. if c.generateDynamicSecret == nil {
  53. return nil, errors.New("GenerateDynamicSecret not implemented in fake")
  54. }
  55. return c.generateDynamicSecret(ctx, name, folderPath)
  56. }
  57. // WithValues sets up the fake client to return specific values or errors for GetSecret and GetSecrets calls.
  58. func (c *BeyondtrustWorkloadCredentialsClient) WithValues(
  59. ctx context.Context,
  60. name, folderPath *string,
  61. getResponse *btwcutil.KV,
  62. getAllResponse []btwcutil.KVListItem,
  63. getErrMsg, listErrMsg *string,
  64. ) {
  65. if c == nil {
  66. return
  67. }
  68. c.getSecret = func(ctxIn context.Context, nameIn string, folderPathIn *string) (*btwcutil.KV, error) {
  69. // Check for nil/non-nil mismatches and value mismatches
  70. if ctxIn != ctx ||
  71. (name != nil && nameIn != *name) ||
  72. (folderPathIn == nil) != (folderPath == nil) ||
  73. (folderPathIn != nil && folderPath != nil && *folderPathIn != *folderPath) {
  74. return nil, errors.New("unexpected test argument getSecret")
  75. }
  76. if getErrMsg == nil {
  77. return getResponse, nil
  78. }
  79. return nil, errors.New(*getErrMsg)
  80. }
  81. c.getSecrets = func(ctxIn context.Context, folderPathIn *string, recursive bool) ([]btwcutil.KVListItem, error) {
  82. // Check for nil/non-nil mismatches and value mismatches
  83. if ctxIn != ctx ||
  84. (folderPathIn == nil) != (folderPath == nil) ||
  85. (folderPathIn != nil && folderPath != nil && *folderPathIn != *folderPath) {
  86. return nil, errors.New("unexpected test argument getSecrets")
  87. }
  88. if listErrMsg == nil {
  89. return getAllResponse, nil
  90. }
  91. return nil, errors.New(*listErrMsg)
  92. }
  93. }
  94. // WithMultiValues sets up the fake client to return multiple responses for GetSecret calls in sequence, or errors for GetSecret and GetSecrets calls.
  95. func (c *BeyondtrustWorkloadCredentialsClient) WithMultiValues(
  96. ctx context.Context,
  97. names []string,
  98. folderPath *string,
  99. getResponses []btwcutil.KV,
  100. getAllResponse []btwcutil.KVListItem,
  101. getErrMsg, listErrMsg *string,
  102. ) {
  103. if c == nil {
  104. return
  105. }
  106. c.getSecret = func(ctxIn context.Context, nameIn string, folderPathIn *string) (*btwcutil.KV, error) {
  107. // Bounds check for names slice access
  108. if len(names) > 0 && c.getCalls >= len(names) {
  109. return nil, errors.New("getSecret called more times than configured responses")
  110. }
  111. // Check for nil/non-nil mismatches and value mismatches
  112. if ctxIn != ctx ||
  113. (len(names) > 0 && names[c.getCalls] != nameIn) ||
  114. (folderPathIn == nil) != (folderPath == nil) ||
  115. (folderPathIn != nil && folderPath != nil && *folderPathIn != *folderPath) {
  116. return nil, errors.New("unexpected test argument in getSecret")
  117. }
  118. if getErrMsg == nil {
  119. // Bounds check for getResponses slice access
  120. if c.getCalls >= len(getResponses) {
  121. return nil, errors.New("getSecret called more times than configured responses")
  122. }
  123. c.getCalls++
  124. return &getResponses[c.getCalls-1], nil
  125. }
  126. return nil, errors.New(*getErrMsg)
  127. }
  128. c.getSecrets = func(ctxIn context.Context, folderPathIn *string, recursive bool) ([]btwcutil.KVListItem, error) {
  129. // Check for nil/non-nil mismatches and value mismatches
  130. if ctxIn != ctx ||
  131. (folderPathIn == nil) != (folderPath == nil) ||
  132. (folderPathIn != nil && folderPath != nil && *folderPathIn != *folderPath) {
  133. return nil, errors.New("unexpected test argument in getSecrets")
  134. }
  135. if listErrMsg == nil {
  136. return getAllResponse, nil
  137. }
  138. return nil, errors.New(*listErrMsg)
  139. }
  140. }
  141. func (c *BeyondtrustWorkloadCredentialsClient) WithGenerateDynamicSecret(fn func(ctx context.Context, name string, folderPath *string) (*btwcutil.GeneratedSecret, error)) {
  142. if c == nil {
  143. return
  144. }
  145. c.generateDynamicSecret = fn
  146. }