github.com/argoproj/argo-events@v1.9.1/pkg/apis/common/validate.go (about)

     1  package common
     2  
     3  import (
     4  	fmt "fmt"
     5  )
     6  
     7  // ValidateTLSConfig validates a TLS configuration.
     8  func ValidateTLSConfig(tlsConfig *TLSConfig) error {
     9  	if tlsConfig == nil {
    10  		return nil
    11  	}
    12  
    13  	if tlsConfig.InsecureSkipVerify {
    14  		return nil
    15  	}
    16  
    17  	var caCertSet, clientCertSet, clientKeySet bool
    18  
    19  	if tlsConfig.CACertSecret != nil {
    20  		caCertSet = true
    21  	}
    22  
    23  	if tlsConfig.ClientCertSecret != nil {
    24  		clientCertSet = true
    25  	}
    26  
    27  	if tlsConfig.ClientKeySecret != nil {
    28  		clientKeySet = true
    29  	}
    30  
    31  	if !caCertSet && !clientCertSet && !clientKeySet {
    32  		return fmt.Errorf("invalid tls config, please configure either caCertSecret, or clientCertSecret and clientKeySecret, or both")
    33  	}
    34  
    35  	if (clientCertSet || clientKeySet) && (!clientCertSet || !clientKeySet) {
    36  		return fmt.Errorf("invalid tls config, both clientCertSecret and clientKeySecret need to be configured")
    37  	}
    38  	return nil
    39  }
    40  
    41  func ValidateBasicAuth(auth *BasicAuth) error {
    42  	if auth == nil {
    43  		return nil
    44  	}
    45  	if auth.Username == nil {
    46  		return fmt.Errorf("username missing")
    47  	}
    48  	if auth.Password == nil {
    49  		return fmt.Errorf("password missing")
    50  	}
    51  	return nil
    52  }
    53  
    54  func ValidateSASLConfig(saslConfig *SASLConfig) error {
    55  	if saslConfig == nil {
    56  		return nil
    57  	}
    58  
    59  	switch saslConfig.Mechanism {
    60  	case "", "PLAIN", "OAUTHBEARER", "SCRAM-SHA-256", "SCRAM-SHA-512", "GSSAPI":
    61  	default:
    62  		return fmt.Errorf("invalid sasl config. Possible values for SASL Mechanism are `OAUTHBEARER`, `PLAIN`, `SCRAM-SHA-256`, `SCRAM-SHA-512` and `GSSAPI`")
    63  	}
    64  
    65  	// user and password must both be set
    66  	if saslConfig.UserSecret == nil || saslConfig.PasswordSecret == nil {
    67  		return fmt.Errorf("invalid sasl config, both userSecret and passwordSecret must be defined")
    68  	}
    69  
    70  	return nil
    71  }