| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- /*
- Copyright © The ESO Authors
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- https://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- */
- package fake
- import (
- "context"
- "errors"
- "net/url"
- btwcutil "github.com/external-secrets/external-secrets/providers/v1/beyondtrustworkloadcredentials/util"
- )
- type BeyondtrustWorkloadCredentialsClient struct {
- getSecret func(ctx context.Context, name string, folderPath *string) (*btwcutil.KV, error)
- getSecrets func(ctx context.Context, folderPath *string, recursive bool) ([]btwcutil.KVListItem, error)
- generateDynamicSecret func(ctx context.Context, name string, folderPath *string) (*btwcutil.GeneratedSecret, error)
- getCalls int
- }
- func (c *BeyondtrustWorkloadCredentialsClient) BaseURL() *url.URL {
- return &url.URL{Scheme: "", Host: ""}
- }
- func (c *BeyondtrustWorkloadCredentialsClient) SetBaseURL(urlStr string) error {
- return nil
- }
- func (c *BeyondtrustWorkloadCredentialsClient) CheckSession(ctx context.Context) error {
- // By default, fake client returns success for session check
- return nil
- }
- func (c *BeyondtrustWorkloadCredentialsClient) Authenticate() error {
- return nil
- }
- func (c *BeyondtrustWorkloadCredentialsClient) GetSecret(ctx context.Context, name string, folderPath *string) (*btwcutil.KV, error) {
- if c.getSecret == nil {
- return nil, errors.New("GetSecret not configured in fake client")
- }
- return c.getSecret(ctx, name, folderPath)
- }
- func (c *BeyondtrustWorkloadCredentialsClient) GetSecrets(ctx context.Context, folderPath *string, recursive bool) ([]btwcutil.KVListItem, error) {
- if c.getSecrets == nil {
- return nil, errors.New("GetSecrets not configured in fake client")
- }
- return c.getSecrets(ctx, folderPath, recursive)
- }
- func (c *BeyondtrustWorkloadCredentialsClient) GenerateDynamicSecret(ctx context.Context, name string, folderPath *string) (*btwcutil.GeneratedSecret, error) {
- if c.generateDynamicSecret == nil {
- return nil, errors.New("GenerateDynamicSecret not implemented in fake")
- }
- return c.generateDynamicSecret(ctx, name, folderPath)
- }
- // WithValues sets up the fake client to return specific values or errors for GetSecret and GetSecrets calls.
- func (c *BeyondtrustWorkloadCredentialsClient) WithValues(
- ctx context.Context,
- name, folderPath *string,
- getResponse *btwcutil.KV,
- getAllResponse []btwcutil.KVListItem,
- getErrMsg, listErrMsg *string,
- ) {
- if c == nil {
- return
- }
- c.getSecret = func(ctxIn context.Context, nameIn string, folderPathIn *string) (*btwcutil.KV, error) {
- // Check for nil/non-nil mismatches and value mismatches
- if ctxIn != ctx ||
- (name != nil && nameIn != *name) ||
- (folderPathIn == nil) != (folderPath == nil) ||
- (folderPathIn != nil && folderPath != nil && *folderPathIn != *folderPath) {
- return nil, errors.New("unexpected test argument getSecret")
- }
- if getErrMsg == nil {
- return getResponse, nil
- }
- return nil, errors.New(*getErrMsg)
- }
- c.getSecrets = func(ctxIn context.Context, folderPathIn *string, recursive bool) ([]btwcutil.KVListItem, error) {
- // Check for nil/non-nil mismatches and value mismatches
- if ctxIn != ctx ||
- (folderPathIn == nil) != (folderPath == nil) ||
- (folderPathIn != nil && folderPath != nil && *folderPathIn != *folderPath) {
- return nil, errors.New("unexpected test argument getSecrets")
- }
- if listErrMsg == nil {
- return getAllResponse, nil
- }
- return nil, errors.New(*listErrMsg)
- }
- }
- // WithMultiValues sets up the fake client to return multiple responses for GetSecret calls in sequence, or errors for GetSecret and GetSecrets calls.
- func (c *BeyondtrustWorkloadCredentialsClient) WithMultiValues(
- ctx context.Context,
- names []string,
- folderPath *string,
- getResponses []btwcutil.KV,
- getAllResponse []btwcutil.KVListItem,
- getErrMsg, listErrMsg *string,
- ) {
- if c == nil {
- return
- }
- c.getSecret = func(ctxIn context.Context, nameIn string, folderPathIn *string) (*btwcutil.KV, error) {
- // Bounds check for names slice access
- if len(names) > 0 && c.getCalls >= len(names) {
- return nil, errors.New("getSecret called more times than configured responses")
- }
- // Check for nil/non-nil mismatches and value mismatches
- if ctxIn != ctx ||
- (len(names) > 0 && names[c.getCalls] != nameIn) ||
- (folderPathIn == nil) != (folderPath == nil) ||
- (folderPathIn != nil && folderPath != nil && *folderPathIn != *folderPath) {
- return nil, errors.New("unexpected test argument in getSecret")
- }
- if getErrMsg == nil {
- // Bounds check for getResponses slice access
- if c.getCalls >= len(getResponses) {
- return nil, errors.New("getSecret called more times than configured responses")
- }
- c.getCalls++
- return &getResponses[c.getCalls-1], nil
- }
- return nil, errors.New(*getErrMsg)
- }
- c.getSecrets = func(ctxIn context.Context, folderPathIn *string, recursive bool) ([]btwcutil.KVListItem, error) {
- // Check for nil/non-nil mismatches and value mismatches
- if ctxIn != ctx ||
- (folderPathIn == nil) != (folderPath == nil) ||
- (folderPathIn != nil && folderPath != nil && *folderPathIn != *folderPath) {
- return nil, errors.New("unexpected test argument in getSecrets")
- }
- if listErrMsg == nil {
- return getAllResponse, nil
- }
- return nil, errors.New(*listErrMsg)
- }
- }
- func (c *BeyondtrustWorkloadCredentialsClient) WithGenerateDynamicSecret(fn func(ctx context.Context, name string, folderPath *string) (*btwcutil.GeneratedSecret, error)) {
- if c == nil {
- return
- }
- c.generateDynamicSecret = fn
- }
|