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