forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
factory_config.go
93 lines (72 loc) · 1.65 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
92
93
package uaa
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
Path string
Client string
ClientSecret string
CACert string
}
func NewConfigFromURL(url string) (Config, error) {
if len(url) == 0 {
return Config{}, bosherr.Error("Expected non-empty UAA URL")
}
parsedURL, err := gourl.Parse(url)
if err != nil {
return Config{}, bosherr.WrapErrorf(err, "Parsing UAA URL '%s'", url)
}
host := parsedURL.Host
port := 443
path := parsedURL.Path
if len(host) == 0 {
host = url
path = ""
}
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'", url)
}
port, err = strconv.Atoi(portStr)
if err != nil {
return Config{}, bosherr.WrapErrorf(
err, "Extracting port from URL '%s'", url)
}
}
if len(host) == 0 {
return Config{}, bosherr.Errorf("Expected to extract host from URL '%s'", url)
}
return Config{Host: host, Port: port, Path: path}, 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 len(c.Client) == 0 {
return bosherr.Error("Missing 'Client'")
}
if _, err := c.CACertPool(); err != nil {
return err
}
return nil
}
func (c Config) CACertPool() (*x509.CertPool, error) {
if len(c.CACert) == 0 {
return nil, nil
}
return crypto.CertPoolFromPEM([]byte(c.CACert))
}