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

     1  package config
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/kyma-incubator/compass/components/director/pkg/oauth"
     7  	"github.com/pkg/errors"
     8  )
     9  
    10  // InstanceConfig is a service instance config
    11  type InstanceConfig struct {
    12  	ClientID     string
    13  	ClientSecret string
    14  	URL          string
    15  	TokenURL     string
    16  	Cert         string
    17  	Key          string
    18  }
    19  
    20  // validate checks if all required fields are populated based on Oauth Mode.
    21  // In the end, the error message is aggregated by joining all error messages.
    22  func (i *InstanceConfig) validate(oauthMode oauth.AuthMode) error {
    23  	errorMessages := make([]string, 0)
    24  
    25  	if i.ClientID == "" {
    26  		errorMessages = append(errorMessages, "Client ID is missing")
    27  	}
    28  	if i.TokenURL == "" {
    29  		errorMessages = append(errorMessages, "Token URL is missing")
    30  	}
    31  	if i.URL == "" {
    32  		errorMessages = append(errorMessages, "URL is missing")
    33  	}
    34  
    35  	switch oauthMode {
    36  	case oauth.Standard:
    37  		if i.ClientSecret == "" {
    38  			errorMessages = append(errorMessages, "Client Secret is missing")
    39  		}
    40  	case oauth.Mtls:
    41  		if i.Cert == "" {
    42  			errorMessages = append(errorMessages, "Certificate is missing")
    43  		}
    44  		if i.Key == "" {
    45  			errorMessages = append(errorMessages, "Key is missing")
    46  		}
    47  	}
    48  
    49  	errorMsg := strings.Join(errorMessages, ", ")
    50  	if errorMsg != "" {
    51  		return errors.New(errorMsg)
    52  	}
    53  
    54  	return nil
    55  }