-
Notifications
You must be signed in to change notification settings - Fork 162
/
factory.go
63 lines (50 loc) · 1.31 KB
/
factory.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
package uaa
import (
"fmt"
"net/url"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshhttp "github.com/cloudfoundry/bosh-utils/httpclient"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
)
type Factory struct {
logTag string
logger boshlog.Logger
}
func NewFactory(logger boshlog.Logger) Factory {
return Factory{
logTag: "uaa.Factory",
logger: logger,
}
}
func (f Factory) New(config Config) (UAA, error) {
err := config.Validate()
if err != nil {
return UAAImpl{}, bosherr.WrapErrorf(
err, "Validating UAA connection config")
}
client, err := f.httpClient(config)
if err != nil {
return UAAImpl{}, err
}
return UAAImpl{client: client}, nil
}
func (f Factory) httpClient(config Config) (Client, error) {
certPool, err := config.CACertPool()
if err != nil {
return Client{}, err
}
if certPool == nil {
f.logger.Debug(f.logTag, "Using default root CAs")
} else {
f.logger.Debug(f.logTag, "Using custom root CAs")
}
rawClient := boshhttp.CreateDefaultClient(certPool)
httpClient := boshhttp.NewHTTPClient(rawClient, f.logger)
endpoint := url.URL{
Scheme: "https",
Host: fmt.Sprintf("%s:%d", config.Host, config.Port),
User: url.UserPassword(config.Client, config.ClientSecret),
Path: config.Path,
}
return NewClient(endpoint.String(), httpClient, f.logger), nil
}