github.com/jasonkeene/cli@v6.14.1-0.20160816203908-ca5715166dfb+incompatible/cf/configuration/coreconfig/api_config_refresher.go (about)

     1  package coreconfig
     2  
     3  import (
     4  	"fmt"
     5  	"regexp"
     6  	"strings"
     7  
     8  	. "github.com/cloudfoundry/cli/cf/i18n"
     9  )
    10  
    11  //go:generate counterfeiter . EndpointRepository
    12  
    13  type EndpointRepository interface {
    14  	GetCCInfo(string) (*CCInfo, string, error)
    15  }
    16  
    17  type APIConfigRefresher struct {
    18  	EndpointRepo EndpointRepository
    19  	Config       ReadWriter
    20  	Endpoint     string
    21  }
    22  
    23  func (a APIConfigRefresher) Refresh() (Warning, error) {
    24  	ccInfo, endpoint, err := a.EndpointRepo.GetCCInfo(a.Endpoint)
    25  	if err != nil {
    26  		return nil, err
    27  	}
    28  
    29  	if endpoint != a.Config.APIEndpoint() {
    30  		a.Config.ClearSession()
    31  	}
    32  
    33  	a.Config.SetAPIEndpoint(endpoint)
    34  	a.Config.SetAPIVersion(ccInfo.APIVersion)
    35  	a.Config.SetAuthenticationEndpoint(ccInfo.AuthorizationEndpoint)
    36  	a.Config.SetSSHOAuthClient(ccInfo.SSHOAuthClient)
    37  	a.Config.SetMinCLIVersion(ccInfo.MinCLIVersion)
    38  	a.Config.SetMinRecommendedCLIVersion(ccInfo.MinRecommendedCLIVersion)
    39  	a.Config.SetLoggregatorEndpoint(a.LoggregatorEndpoint(ccInfo, endpoint))
    40  
    41  	//* 3/5/15: loggregator endpoint will be renamed to doppler eventually,
    42  	//          we just have to use the loggregator endpoint as doppler for now
    43  	a.Config.SetDopplerEndpoint(strings.Replace(a.Config.LoggregatorEndpoint(), "loggregator", "doppler", 1))
    44  	a.Config.SetRoutingAPIEndpoint(ccInfo.RoutingAPIEndpoint)
    45  
    46  	if !strings.HasPrefix(endpoint, "https://") {
    47  		return new(insecureWarning), nil
    48  	}
    49  	return nil, nil
    50  }
    51  
    52  func (a APIConfigRefresher) LoggregatorEndpoint(ccInfo *CCInfo, endpoint string) string {
    53  	if ccInfo.LoggregatorEndpoint == "" {
    54  		var endpointDomainRegex = regexp.MustCompile(`^http(s?)://[^\.]+\.([^:]+)`)
    55  
    56  		matches := endpointDomainRegex.FindStringSubmatch(endpoint)
    57  		url := fmt.Sprintf("ws%s://loggregator.%s", matches[1], matches[2])
    58  		if url[0:3] == "wss" {
    59  			return url + ":443"
    60  		}
    61  		return url + ":80"
    62  	}
    63  	return ccInfo.LoggregatorEndpoint
    64  }
    65  
    66  type Warning interface {
    67  	Warn() string
    68  }
    69  
    70  type insecureWarning struct{}
    71  
    72  func (w insecureWarning) Warn() string {
    73  	return T("Warning: Insecure http API endpoint detected: secure https API endpoints are recommended\n")
    74  }