/
admin.go
352 lines (311 loc) · 11.2 KB
/
admin.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
// Copyright 2013, 2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package apiserver
import (
"sync"
"time"
"github.com/juju/errors"
"github.com/juju/names"
"github.com/juju/juju/apiserver/authentication"
"github.com/juju/juju/apiserver/common"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/rpc"
"github.com/juju/juju/state"
"github.com/juju/juju/state/presence"
"github.com/juju/juju/version"
)
type adminApiFactory func(srv *Server, root *apiHandler, reqNotifier *requestNotifier) interface{}
// admin is the only object that unlogged-in clients can access. It holds any
// methods that are needed to log in.
type admin struct {
srv *Server
root *apiHandler
reqNotifier *requestNotifier
mu sync.Mutex
loggedIn bool
}
var UpgradeInProgressError = errors.New("upgrade in progress")
var AboutToRestoreError = errors.New("restore preparation in progress")
var RestoreInProgressError = errors.New("restore in progress")
var MaintenanceNoLoginError = errors.New("login failed - maintenance in progress")
var errAlreadyLoggedIn = errors.New("already logged in")
func (a *admin) doLogin(req params.LoginRequest, loginVersion int) (params.LoginResultV1, error) {
var fail params.LoginResultV1
a.mu.Lock()
defer a.mu.Unlock()
if a.loggedIn {
// This can only happen if Login is called concurrently.
return fail, errAlreadyLoggedIn
}
// authedApi is the API method finder we'll use after getting logged in.
var authedApi rpc.MethodFinder = newApiRoot(a.root.state, a.root.closeState, a.root.resources, a.root)
// Use the login validation function, if one was specified.
if a.srv.validator != nil {
err := a.srv.validator(req)
switch err {
case UpgradeInProgressError:
authedApi = newUpgradingRoot(authedApi)
case AboutToRestoreError:
authedApi = newAboutToRestoreRoot(authedApi)
case RestoreInProgressError:
authedApi = newRestoreInProgressRoot(authedApi)
case nil:
// in this case no need to wrap authed api so we do nothing
default:
return fail, err
}
}
var agentPingerNeeded = true
var isUser bool
kind, err := names.TagKind(req.AuthTag)
if err != nil || kind != names.UserTagKind {
// Users are not rate limited, all other entities are
if !a.srv.limiter.Acquire() {
logger.Debugf("rate limiting for agent %s", req.AuthTag)
return fail, common.ErrTryAgain
}
defer a.srv.limiter.Release()
} else {
isUser = true
}
serverOnlyLogin := loginVersion > 1 && a.root.envUUID == ""
entity, lastConnection, err := doCheckCreds(a.root.state, req, !serverOnlyLogin)
if err != nil {
if a.maintenanceInProgress() {
// An upgrade, restore or similar operation is in
// progress. It is possible for logins to fail until this
// is complete due to incomplete or updating data. Mask
// transitory and potentially confusing errors from failed
// logins with a more helpful one.
return fail, MaintenanceNoLoginError
}
// Here we have a special case. The machine agents that manage
// environments in the state server environment need to be able to
// open API connections to other environments. In those cases, we
// need to look in the state server database to check the creds
// against the machine if and only if the entity tag is a machine tag,
// and the machine exists in the state server environment, and the
// machine has the manage state job. If all those parts are valid, we
// can then check the credentials against the state server environment
// machine.
if kind != names.MachineTagKind {
return fail, err
}
entity, err = a.checkCredsOfStateServerMachine(req)
if err != nil {
return fail, err
}
// If we are here, then the entity will refer to a state server
// machine in the state server environment, and we don't need a pinger
// for it as we already have one running in the machine agent api
// worker for the state server environment.
agentPingerNeeded = false
}
a.root.entity = entity
if a.reqNotifier != nil {
a.reqNotifier.login(entity.Tag().String())
}
// We have authenticated the user; enable the appropriate API
// to serve to them.
a.loggedIn = true
if agentPingerNeeded {
if err := startPingerIfAgent(a.root, entity); err != nil {
return fail, err
}
}
var maybeUserInfo *params.AuthUserInfo
// Send back user info if user
if isUser {
maybeUserInfo = ¶ms.AuthUserInfo{
Identity: entity.Tag().String(),
LastConnection: lastConnection,
}
}
// Fetch the API server addresses from state.
hostPorts, err := a.root.state.APIHostPorts()
if err != nil {
return fail, err
}
logger.Debugf("hostPorts: %v", hostPorts)
environ, err := a.root.state.Environment()
if err != nil {
return fail, err
}
loginResult := params.LoginResultV1{
Servers: params.FromNetworkHostsPorts(hostPorts),
EnvironTag: environ.Tag().String(),
ServerTag: environ.ServerTag().String(),
Facades: DescribeFacades(),
UserInfo: maybeUserInfo,
ServerVersion: version.Current.Number.String(),
}
// For sufficiently modern login versions, stop serving the
// state server environment at the root of the API.
if serverOnlyLogin {
authedApi = newRestrictedRoot(authedApi)
// Remove the EnvironTag from the response as there is no
// environment here.
loginResult.EnvironTag = ""
// Strip out the facades that are not supported from the result.
var facades []params.FacadeVersions
for _, facade := range loginResult.Facades {
if restrictedRootNames.Contains(facade.Name) {
facades = append(facades, facade)
}
}
loginResult.Facades = facades
}
a.root.rpcConn.ServeFinder(authedApi, serverError)
return loginResult, nil
}
// checkCredsOfStateServerMachine checks the special case of a state server
// machine creating an API connection for a different environment so it can
// run API workers for that environment to do things like provisioning
// machines.
func (a *admin) checkCredsOfStateServerMachine(req params.LoginRequest) (state.Entity, error) {
// Check the credentials against the state server environment.
entity, _, err := doCheckCreds(a.srv.state, req, false)
if err != nil {
return nil, err
}
machine, ok := entity.(*state.Machine)
if !ok {
return nil, errors.Errorf("entity should be a machine, but is %T", entity)
}
for _, job := range machine.Jobs() {
if job == state.JobManageEnviron {
return entity, nil
}
}
// The machine does exist in the state server environment, but it
// doesn't manage environments, so reject it.
return nil, common.ErrBadCreds
}
func (a *admin) maintenanceInProgress() bool {
if a.srv.validator == nil {
return false
}
// jujud's login validator will return an error for any user tag
// if jujud is upgrading or restoring. The tag of the entity
// trying to log in can't be used because jujud's login validator
// will always return nil for the local machine agent and here we
// need to know if maintenance is in progress irrespective of the
// the authenticating entity.
//
// TODO(mjs): 2014-09-29 bug 1375110
// This needs improving but I don't have the cycles right now.
req := params.LoginRequest{
AuthTag: names.NewUserTag("arbitrary").String(),
}
return a.srv.validator(req) != nil
}
var doCheckCreds = checkCreds
// checkCreds validates the entities credentials in the current environment.
// If the entity is a user, and lookForEnvUser is true, an env user must exist
// for the environment. In the case of a user logging in to the server, but
// not an environment, there is no env user needed. While we have the env
// user, if we do have it, update the last login time.
func checkCreds(st *state.State, req params.LoginRequest, lookForEnvUser bool) (state.Entity, *time.Time, error) {
tag, err := names.ParseTag(req.AuthTag)
if err != nil {
return nil, nil, err
}
entity, err := st.FindEntity(tag)
if errors.IsNotFound(err) {
// We return the same error when an entity does not exist as for a bad
// password, so that we don't allow unauthenticated users to find
// information about existing entities.
logger.Debugf("entity %q not found", tag)
return nil, nil, common.ErrBadCreds
}
if err != nil {
return nil, nil, errors.Trace(err)
}
authenticator, err := authentication.FindEntityAuthenticator(entity)
if err != nil {
return nil, nil, err
}
if err = authenticator.Authenticate(entity, req.Credentials, req.Nonce); err != nil {
logger.Debugf("bad credentials")
return nil, nil, err
}
// For user logins, update the last login time.
// NOTE: this code path is only for local users. When we support remote
// user logins with bearer tokens, we will need to make sure that we also
// update the last connection times for the environment users there.
var lastLogin *time.Time
if user, ok := entity.(*state.User); ok {
lastLogin = user.LastLogin()
if lookForEnvUser {
envUser, err := st.EnvironmentUser(user.UserTag())
if err != nil {
return nil, nil, errors.Wrap(err, common.ErrBadCreds)
}
// The last connection for the environment takes precedence over
// the local user last login time.
lastLogin = envUser.LastConnection()
envUser.UpdateLastConnection()
}
// Only update the user's last login time if it is a successful
// login, meaning that if we are logging into an environment, make
// sure that there is an environment user in that environment for
// this user.
user.UpdateLastLogin()
}
return entity, lastLogin, nil
}
func checkForValidMachineAgent(entity state.Entity, req params.LoginRequest) error {
// If this is a machine agent connecting, we need to check the
// nonce matches, otherwise the wrong agent might be trying to
// connect.
if machine, ok := entity.(*state.Machine); ok {
if !machine.CheckProvisioned(req.Nonce) {
return errors.NotProvisionedf("machine %v", machine.Id())
}
}
return nil
}
// machinePinger wraps a presence.Pinger.
type machinePinger struct {
*presence.Pinger
}
// Stop implements Pinger.Stop() as Pinger.Kill(), needed at
// connection closing time to properly stop the wrapped pinger.
func (p *machinePinger) Stop() error {
if err := p.Pinger.Stop(); err != nil {
return err
}
return p.Pinger.Kill()
}
func startPingerIfAgent(root *apiHandler, entity state.Entity) error {
// A machine or unit agent has connected, so start a pinger to
// announce it's now alive, and set up the API pinger
// so that the connection will be terminated if a sufficient
// interval passes between pings.
agentPresencer, ok := entity.(presence.Presencer)
if !ok {
return nil
}
pinger, err := agentPresencer.SetAgentPresence()
if err != nil {
return err
}
root.getResources().Register(&machinePinger{pinger})
action := func() {
if err := root.getRpcConn().Close(); err != nil {
logger.Errorf("error closing the RPC connection: %v", err)
}
}
pingTimeout := newPingTimeout(action, maxClientPingInterval)
return root.getResources().RegisterNamed("pingTimeout", pingTimeout)
}
// errRoot implements the API that a client first sees
// when connecting to the API. It exposes the same API as initialRoot, except
// it returns the requested error when the client makes any request.
type errRoot struct {
err error
}
// Admin conforms to the same API as initialRoot, but we'll always return (nil, err)
func (r *errRoot) Admin(id string) (*adminV0, error) {
return nil, r.err
}