/
api.go
229 lines (198 loc) · 6.82 KB
/
api.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// Copyright 2014 The Serviced Authors.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package api
import (
"fmt"
"os"
"path/filepath"
"runtime/pprof"
"strings"
"time"
"github.com/Sirupsen/logrus"
"github.com/control-center/serviced/auth"
"github.com/control-center/serviced/config"
"github.com/control-center/serviced/dao"
"github.com/control-center/serviced/dao/client"
"github.com/control-center/serviced/rpc/agent"
"github.com/control-center/serviced/rpc/master"
"github.com/control-center/serviced/utils"
"github.com/control-center/serviced/validation"
dockerclient "github.com/fsouza/go-dockerclient"
"github.com/zenoss/glog"
)
type api struct {
master master.ClientInterface
agent *agent.Client
docker *dockerclient.Client
dao dao.ControlPlane // Deprecated
}
var hostAuthenticated bool
func New() API {
// let lazy init populate each interface as necessary
return NewAPI(nil, nil, nil, nil)
}
// New creates a new API type
func NewAPI(master master.ClientInterface, agent *agent.Client, docker *dockerclient.Client, dao dao.ControlPlane) API {
return &api{master: master, agent: agent, docker: docker, dao: dao}
}
// Starts the agent or master services on this host
func (a *api) StartServer() error {
options := config.GetOptions()
configureLoggingForLogstash(options.LogstashURL)
log := log.WithFields(logrus.Fields{
"staticips": options.StaticIPs,
})
log.Debug("Starting server")
if err := utils.SetCiphers("http", options.TLSCiphers); err != nil {
return fmt.Errorf("unable to set HTTP TLS Ciphers %v", err)
}
log.WithFields(logrus.Fields{
"ciphers": strings.Join(options.TLSCiphers, ","),
}).Debug("Set supported TLS ciphers for HTTP")
if err := utils.SetMinTLS("http", options.TLSMinVersion); err != nil {
return fmt.Errorf("unable to set minimum HTTP TLS version %v", err)
}
log.WithFields(logrus.Fields{
"minversion": options.TLSMinVersion,
}).Debug("Set minimum TLS version for HTTP")
if err := utils.SetCiphers("mux", options.MUXTLSCiphers); err != nil {
return fmt.Errorf("unable to set MUX TLS Ciphers %v", err)
}
log.WithFields(logrus.Fields{
"ciphers": strings.Join(options.MUXTLSCiphers, ","),
}).Debug("Set supported TLS ciphers for the mux")
if err := utils.SetMinTLS("mux", options.MUXTLSMinVersion); err != nil {
return fmt.Errorf("unable to set minimum MUX TLS version %v", err)
}
log.WithFields(logrus.Fields{
"minversion": options.MUXTLSMinVersion,
}).Debug("Set minimum TLS version for the mux")
if err := utils.SetCiphers("rpc", options.RPCTLSCiphers); err != nil {
return fmt.Errorf("unable to set RPC TLS Ciphers %v", err)
}
log.WithFields(logrus.Fields{
"ciphers": strings.Join(options.RPCTLSCiphers, ","),
}).Debug("Set supported TLS ciphers for RPC")
if err := utils.SetMinTLS("rpc", options.RPCTLSMinVersion); err != nil {
return fmt.Errorf("unable to set minimum RPC TLS version %v", err)
}
log.WithFields(logrus.Fields{
"minversion": options.RPCTLSMinVersion,
}).Debug("Set minimum TLS version for RPC")
if len(options.CPUProfile) > 0 {
f, err := os.Create(options.CPUProfile)
if err != nil {
log.WithFields(logrus.Fields{
"file": options.CPUProfile,
}).WithError(err).Fatal("Unable to create CPU profile file")
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
d, err := newDaemon(options.Endpoint, options.StaticIPs, options.MasterPoolID, time.Duration(options.TokenExpiration)*time.Second)
if err != nil {
return err
}
return d.run()
}
func configureLoggingForLogstash(logstashURL string) {
hostname, err := os.Hostname()
if err != nil {
hostname = "unknown"
}
// TODO: CC-3061: Our use of logrus does not support a similar integration with logstash
glog.SetLogstashType("serviced-" + hostname)
glog.SetLogstashURL(logstashURL)
}
// Opens a connection to the master if not already connected
func (a *api) connectMaster() (master.ClientInterface, error) {
if a.master == nil {
var err error
a.master, err = master.NewClient(config.GetOptions().Endpoint)
if err != nil {
return nil, fmt.Errorf("could not create a client to the master: %s", err)
}
a.authenticateHost()
}
return a.master, nil
}
// Opens a connection to the agent if not already connected
func (a *api) connectAgent(address string) (*agent.Client, error) {
if a.agent == nil {
var err error
a.agent, err = agent.NewClient(address)
if err != nil {
return nil, fmt.Errorf("could not create a client to the agent: %s", err)
}
a.authenticateHost()
}
return a.agent, nil
}
// Opens a connection to docker if not already connected
func (a *api) connectDocker() (*dockerclient.Client, error) {
if a.docker == nil {
const DockerEndpoint string = "unix:///var/run/docker.sock"
var err error
if a.docker, err = dockerclient.NewClient(DockerEndpoint); err != nil {
return nil, fmt.Errorf("could not create a client to docker: %s", err)
}
}
return a.docker, nil
}
// DEPRECATED: Opens a connection to the DAO if not already connected
func (a *api) connectDAO() (dao.ControlPlane, error) {
if a.dao == nil {
var err error
a.dao, err = client.NewControlClient(config.GetOptions().Endpoint)
if err != nil {
return nil, fmt.Errorf("could not create a client to the agent: %s", err)
}
a.authenticateHost()
}
return a.dao, nil
}
// This will authenticate the host once to get a valid token for any CLI commands
// that require it.
func (a *api) authenticateHost() error {
if hostAuthenticated {
return nil
}
options := config.GetOptions()
// Try to load the master keys, fail silently if they don't exist
masterKeyFile := filepath.Join(options.IsvcsPath, auth.MasterKeyFileName)
if err := auth.LoadMasterKeyFile(masterKeyFile); err != nil {
log.WithError(err).Debug("Unable to load master keys")
}
// Load the delegate keys
delegateKeyFile := filepath.Join(options.EtcPath, auth.DelegateKeyFileName)
if err := auth.LoadDelegateKeysFromFile(delegateKeyFile); err != nil {
return err
}
// Get our host ID
myHostID, err := utils.HostID()
if err != nil {
return err
} else if err := validation.ValidHostID(myHostID); err != nil {
return err
}
// Load an auth token once
tokenFile := filepath.Join(options.EtcPath, auth.TokenFileName)
getToken := func() (string, int64, error) {
return a.AuthenticateHost(myHostID)
}
if _, err := auth.RefreshToken(getToken, tokenFile); err != nil {
return err
}
hostAuthenticated = true
return nil
}