/
cmdctrl.go
176 lines (158 loc) · 4.94 KB
/
cmdctrl.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package cmdctrl
import (
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"net"
"time"
pb "github.com/pandemicsyn/cmdctrl/api"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
type CmdCtrl interface {
Start() (err error)
Stop() (err error)
Exit() (err error)
Reload() (err error)
Restart() (err error)
RingUpdate(version int64, ringBytes []byte) (newversion int64)
SelfUpgrade(version string, bindiff []byte, hash []byte) (status bool, msg string)
Stats() (encoded []byte)
HealthCheck() (status bool, msg string)
SoftwareVersion() (version string)
}
type CCServer struct {
cmdctrl CmdCtrl
cfg *ConfigOpts
}
type ConfigOpts struct {
ListenAddress string
CAFile string
CertFile string
KeyFile string
UseTLS bool
Enabled bool
MutualTLS bool
InsecureSkipVerify bool
}
func NewCCServer(c CmdCtrl, cfg *ConfigOpts) *CCServer {
return &CCServer{
cmdctrl: c,
cfg: cfg,
}
}
func mutualTLS(c ConfigOpts) (*tls.Config, error) {
tlsConf := &tls.Config{}
if c.MutualTLS {
caCert, err := ioutil.ReadFile(c.CAFile)
if err != nil {
return nil, fmt.Errorf("Unable to load ca cert %s: %s", c.CAFile, err.Error())
}
clientCertPool := x509.NewCertPool()
if ok := clientCertPool.AppendCertsFromPEM(caCert); !ok {
return nil, fmt.Errorf("Unable to append cert %s to pool.", c.CAFile)
}
strictness := tls.RequireAndVerifyClientCert
tlsConf = &tls.Config{
ClientAuth: strictness,
ClientCAs: clientCertPool,
CipherSuites: []uint16{tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384},
PreferServerCipherSuites: true,
MinVersion: uint16(tls.VersionTLS12),
}
tlsConf.BuildNameToCertificate()
}
cert, err := tls.LoadX509KeyPair(c.CertFile, c.KeyFile)
if err != nil {
return nil, err
}
tlsConf.Certificates = []tls.Certificate{cert}
tlsConf.InsecureSkipVerify = c.InsecureSkipVerify
return tlsConf, nil
}
func (cc *CCServer) Serve() error {
l, err := net.Listen("tcp", cc.cfg.ListenAddress)
if err != nil {
return err
}
var opts []grpc.ServerOption
if cc.cfg.UseTLS {
if cc.cfg.MutualTLS {
mtc, err := mutualTLS(*cc.cfg)
if err != nil {
return err
}
creds := credentials.NewTLS(mtc)
opts = []grpc.ServerOption{grpc.Creds(creds)}
} else {
creds, err := credentials.NewServerTLSFromFile(cc.cfg.CertFile, cc.cfg.KeyFile)
if err != nil {
return err
}
opts = []grpc.ServerOption{grpc.Creds(creds)}
}
}
s := grpc.NewServer(opts...)
pb.RegisterCmdCtrlServer(s, cc)
return s.Serve(l)
}
func (cc *CCServer) RingUpdate(c context.Context, r *pb.Ring) (*pb.RingUpdateResult, error) {
res := pb.RingUpdateResult{}
res.Newversion = cc.cmdctrl.RingUpdate(r.Version, r.Ring)
return &res, nil
}
func (cc *CCServer) Start(c context.Context, r *pb.EmptyMsg) (*pb.StatusMsg, error) {
err := cc.cmdctrl.Start()
if err != nil {
return &pb.StatusMsg{Status: false, Msg: err.Error()}, nil
}
return &pb.StatusMsg{Status: true, Msg: ""}, nil
}
func (cc *CCServer) Stop(c context.Context, r *pb.EmptyMsg) (*pb.StatusMsg, error) {
err := cc.cmdctrl.Stop()
if err != nil {
return &pb.StatusMsg{Status: false, Msg: err.Error()}, nil
}
return &pb.StatusMsg{Status: true, Msg: ""}, nil
}
func (cc *CCServer) Restart(c context.Context, r *pb.EmptyMsg) (*pb.StatusMsg, error) {
err := cc.cmdctrl.Restart()
if err != nil {
return &pb.StatusMsg{Status: false, Msg: err.Error()}, nil
}
return &pb.StatusMsg{Status: true, Msg: ""}, nil
}
func (cc *CCServer) Reload(c context.Context, r *pb.EmptyMsg) (*pb.StatusMsg, error) {
err := cc.cmdctrl.Reload()
if err != nil {
return &pb.StatusMsg{Status: false, Msg: err.Error()}, nil
}
return &pb.StatusMsg{Status: true, Msg: ""}, nil
}
func (cc *CCServer) Exit(c context.Context, r *pb.EmptyMsg) (*pb.StatusMsg, error) {
err := cc.cmdctrl.Exit()
if err != nil {
return &pb.StatusMsg{Status: false, Msg: err.Error()}, nil
}
return &pb.StatusMsg{Status: true, Msg: ""}, nil
}
func (cc *CCServer) Stats(c context.Context, r *pb.EmptyMsg) (*pb.StatsMsg, error) {
return &pb.StatsMsg{Statsjson: cc.cmdctrl.Stats()}, nil
}
func (cc *CCServer) HealthCheck(c context.Context, r *pb.EmptyMsg) (*pb.HealthCheckMsg, error) {
hm := &pb.HealthCheckMsg{Ts: time.Now().Unix()}
hm.Status, hm.Msg = cc.cmdctrl.HealthCheck()
return hm, nil
}
//SelfUpgrade will have the instance upgrade to the provided version via the cmdctrl.GithubUpdater
func (cc *CCServer) SelfUpgrade(c context.Context, r *pb.SelfUpgradeMsg) (*pb.StatusMsg, error) {
sm := &pb.StatusMsg{}
sm.Status, sm.Msg = cc.cmdctrl.SelfUpgrade(r.Version, r.Bindiff, r.Checksum)
return sm, nil
}
//SoftwareVersion returns the currently running version
func (cc *CCServer) SoftwareVersion(c context.Context, r *pb.EmptyMsg) (*pb.SoftwareVersionMsg, error) {
return &pb.SoftwareVersionMsg{Version: cc.cmdctrl.SoftwareVersion()}, nil
}