github.com/cyverse/go-irodsclient@v0.13.2/irods/types/cs_negotiation.go (about)

     1  package types
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  )
     7  
     8  // CSNegotiationRequire defines Negotiation request
     9  type CSNegotiationRequire string
    10  
    11  const (
    12  	// CSNegotiationRequireTCP requires Plain TCP connection
    13  	CSNegotiationRequireTCP CSNegotiationRequire = "CS_NEG_REFUSE"
    14  	// CSNegotiationRequireSSL requires SSL connection
    15  	CSNegotiationRequireSSL CSNegotiationRequire = "CS_NEG_REQUIRE"
    16  	// CSNegotiationDontCare requires any of TCP or SSL connection
    17  	CSNegotiationDontCare CSNegotiationRequire = "CS_NEG_DONT_CARE"
    18  )
    19  
    20  // GetCSNegotiationRequire returns CSNegotiationRequire value from string
    21  func GetCSNegotiationRequire(require string) (CSNegotiationRequire, error) {
    22  	csNegotiationPolicy := CSNegotiationRequireTCP
    23  	var err error = nil
    24  	switch strings.TrimSpace(strings.ToUpper(require)) {
    25  	case string(CSNegotiationRequireTCP), "TCP":
    26  		csNegotiationPolicy = CSNegotiationRequireTCP
    27  	case string(CSNegotiationRequireSSL), "SSL":
    28  		csNegotiationPolicy = CSNegotiationRequireSSL
    29  	case string(CSNegotiationDontCare), "DONT_CARE", "":
    30  		csNegotiationPolicy = CSNegotiationDontCare
    31  	default:
    32  		csNegotiationPolicy = CSNegotiationRequireTCP
    33  		err = fmt.Errorf("cannot parse string %s", require)
    34  	}
    35  
    36  	return csNegotiationPolicy, err
    37  }
    38  
    39  // CSNegotiationPolicy defines Negotiation result
    40  type CSNegotiationPolicy string
    41  
    42  const (
    43  	// CSNegotiationFailure presents negotiation is failed
    44  	CSNegotiationFailure CSNegotiationPolicy = "CS_NEG_FAILURE"
    45  	// CSNegotiationUseTCP uses Plain TCP connection
    46  	CSNegotiationUseTCP CSNegotiationPolicy = "CS_NEG_USE_TCP"
    47  	// CSNegotiationUseSSL uses SSL connection
    48  	CSNegotiationUseSSL CSNegotiationPolicy = "CS_NEG_USE_SSL"
    49  )
    50  
    51  // GetCSNegotiationPolicy returns CSNegotiationPolicy value from string
    52  func GetCSNegotiationPolicy(policy string) (CSNegotiationPolicy, error) {
    53  	csNegotiationPolicy := CSNegotiationFailure
    54  	var err error = nil
    55  	switch strings.TrimSpace(strings.ToUpper(policy)) {
    56  	case string(CSNegotiationUseTCP), "TCP":
    57  		csNegotiationPolicy = CSNegotiationUseTCP
    58  	case string(CSNegotiationUseSSL), "SSL":
    59  		csNegotiationPolicy = CSNegotiationUseSSL
    60  	default:
    61  		csNegotiationPolicy = CSNegotiationFailure
    62  		err = fmt.Errorf("cannot parse string %s", policy)
    63  	}
    64  
    65  	return csNegotiationPolicy, err
    66  }
    67  
    68  // PerformCSNegotiation performs CSNegotiation and returns the policy determined
    69  func PerformCSNegotiation(clientRequest CSNegotiationRequire, serverRequest CSNegotiationRequire) CSNegotiationPolicy {
    70  	if serverRequest == CSNegotiationDontCare {
    71  		switch clientRequest {
    72  		case CSNegotiationDontCare, CSNegotiationRequireTCP:
    73  			return CSNegotiationUseTCP
    74  		case CSNegotiationRequireSSL:
    75  			return CSNegotiationUseSSL
    76  		default:
    77  			return CSNegotiationFailure
    78  		}
    79  	}
    80  
    81  	if clientRequest == serverRequest {
    82  		switch clientRequest {
    83  		case CSNegotiationRequireTCP:
    84  			return CSNegotiationUseTCP
    85  		case CSNegotiationRequireSSL:
    86  			return CSNegotiationUseSSL
    87  		default:
    88  			return CSNegotiationFailure
    89  		}
    90  	}
    91  	return CSNegotiationFailure
    92  }