/
lb_args_handler.go
84 lines (69 loc) · 1.87 KB
/
lb_args_handler.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
package commands
import (
"encoding/base64"
"errors"
"fmt"
"github.com/cloudfoundry/bosh-bootloader/certs"
"github.com/cloudfoundry/bosh-bootloader/storage"
)
type LBArgsHandler struct {
certificateValidator certificateValidator
}
type LBArgs struct {
LBType string
CertPath string
KeyPath string
ChainPath string
Domain string
}
func NewLBArgsHandler(certificateValidator certificateValidator) LBArgsHandler {
return LBArgsHandler{
certificateValidator: certificateValidator,
}
}
func (l LBArgsHandler) GetLBState(iaas string, args LBArgs) (storage.LB, error) {
if args.LBType == "" {
return storage.LB{}, nil
}
var certData certs.CertData
var err error
if iaas == "azure" && args.LBType == "cf" {
certData, err = l.certificateValidator.ReadAndValidatePKCS12(args.CertPath, args.KeyPath)
if err != nil {
return storage.LB{}, fmt.Errorf("Validate certificate: %s", err)
}
return storage.LB{
Type: args.LBType,
Cert: base64.StdEncoding.EncodeToString(certData.Cert),
Key: string(certData.Key),
Domain: args.Domain,
}, nil
}
if args.LBType != "concourse" {
certData, err = l.certificateValidator.ReadAndValidate(args.CertPath, args.KeyPath, args.ChainPath)
if err != nil {
return storage.LB{}, fmt.Errorf("Validate certificate: %s", err)
}
}
if args.LBType == "concourse" && args.Domain != "" {
return storage.LB{}, errors.New("domain is not implemented for concourse load balancers. Remove the --lb-domain flag and try again.")
}
return storage.LB{
Type: args.LBType,
Cert: string(certData.Cert),
Key: string(certData.Key),
Chain: string(certData.Chain),
Domain: args.Domain,
}, nil
}
func (l LBArgsHandler) Merge(new storage.LB, old storage.LB) storage.LB {
if old.Type != "" {
if new.Domain == "" {
new.Domain = old.Domain
}
if new.Type == "" {
new.Type = old.Type
}
}
return new
}