github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/pkg/accessstrategy/executor_provider.go (about)

     1  package accessstrategy
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/kyma-incubator/compass/components/director/pkg/certloader"
     7  )
     8  
     9  // ExecutorProvider defines an interface for access strategy executor provider
    10  //go:generate mockery --name=ExecutorProvider --output=automock --outpkg=automock --case=underscore --disable-version-string
    11  type ExecutorProvider interface {
    12  	Provide(accessStrategyType Type) (Executor, error)
    13  }
    14  
    15  // Provider is responsible to provides an access strategy executors
    16  type Provider struct {
    17  	executors map[Type]Executor
    18  }
    19  
    20  // NewExecutorProvider returns a new access strategy executor provider based on type <-> executor mapping
    21  func NewExecutorProvider(executors map[Type]Executor) *Provider {
    22  	return &Provider{
    23  		executors: executors,
    24  	}
    25  }
    26  
    27  // NewDefaultExecutorProvider returns a new access strategy executor provider with the default static type <-> executor mapping
    28  func NewDefaultExecutorProvider(certCache certloader.Cache, externalClientCertSecretName, extSvcClientCertSecretName string) *Provider {
    29  	return &Provider{
    30  		executors: map[Type]Executor{
    31  			OpenAccessStrategy:    &openAccessStrategyExecutor{},
    32  			CMPmTLSAccessStrategy: NewCMPmTLSAccessStrategyExecutor(certCache, nil, externalClientCertSecretName, extSvcClientCertSecretName),
    33  		},
    34  	}
    35  }
    36  
    37  // NewExecutorProviderWithTenant returns a new access strategy executor provider by given tenant provider function
    38  func NewExecutorProviderWithTenant(certCache certloader.Cache, tenantProviderFunc func(ctx context.Context) (string, error), externalClientCertSecretName, extSvcClientCertSecretName string) *Provider {
    39  	return &Provider{
    40  		executors: map[Type]Executor{
    41  			OpenAccessStrategy:    &openAccessStrategyExecutor{},
    42  			CMPmTLSAccessStrategy: NewCMPmTLSAccessStrategyExecutor(certCache, tenantProviderFunc, externalClientCertSecretName, extSvcClientCertSecretName),
    43  		},
    44  	}
    45  }
    46  
    47  // Provide provides an executor for a given access strategy if supported, UnsupportedErr otherwise
    48  func (p *Provider) Provide(accessStrategyType Type) (Executor, error) {
    49  	executor, ok := p.executors[accessStrategyType]
    50  	if !ok {
    51  		return nil, UnsupportedErr
    52  	}
    53  	return executor, nil
    54  }