forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
factory_config.go
91 lines (69 loc) · 1.71 KB
/
factory_config.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package director
import (
"crypto/x509"
gonet "net"
gourl "net/url"
"strconv"
"strings"
"github.com/cloudfoundry/bosh-utils/crypto"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
)
type Config struct {
Host string
Port int
// CA certificate is not required
CACert string
Client string
ClientSecret string
TokenFunc func(bool) (string, error)
}
func NewConfigFromURL(url string) (Config, error) {
if len(url) == 0 {
return Config{}, bosherr.Error("Expected non-empty Director URL")
}
parsedURL, err := gourl.Parse(url)
if err != nil {
return Config{}, bosherr.WrapErrorf(err, "Parsing Director URL '%s'", url)
}
host := parsedURL.Host
port := 25555
if len(host) == 0 {
host = url
}
if strings.Contains(host, ":") {
var portStr string
host, portStr, err = gonet.SplitHostPort(host)
if err != nil {
return Config{}, bosherr.WrapErrorf(
err, "Extracting host/port from URL '%s'", parsedURL.Host)
}
port, err = strconv.Atoi(portStr)
if err != nil {
return Config{}, bosherr.WrapErrorf(
err, "Extracting port from URL '%s'", parsedURL.Host)
}
}
if len(host) == 0 {
return Config{}, bosherr.Errorf("Expected to extract host from URL '%s'", url)
}
return Config{Host: host, Port: port}, nil
}
func (c Config) Validate() error {
if len(c.Host) == 0 {
return bosherr.Error("Missing 'Host'")
}
if c.Port == 0 {
return bosherr.Error("Missing 'Port'")
}
if _, err := c.CACertPool(); err != nil {
return err
}
// Don't validate credentials since Info call does not require authentication.
return nil
}
func (c Config) CACertPool() (*x509.CertPool, error) {
if len(c.CACert) == 0 {
return nil, nil
}
return crypto.CertPoolFromPEM([]byte(c.CACert))
}