-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmin.go
377 lines (332 loc) · 10.9 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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
// 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"
)
type adminApiFactory func(srv *Server, root *apiHandler, reqNotifier *requestNotifier) interface{}
// adminApiV0 implements the API that a client first sees when connecting to
// the API. We start serving a different API once the user has logged in.
type adminApiV0 struct {
admin *adminV0
}
// 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
}
type adminV0 struct {
*admin
}
func newAdminApiV0(srv *Server, root *apiHandler, reqNotifier *requestNotifier) interface{} {
return &adminApiV0{
admin: &adminV0{
&admin{
srv: srv,
root: root,
reqNotifier: reqNotifier,
},
},
}
}
// Admin returns an object that provides API access to methods that can be
// called even when not authenticated.
func (r *adminApiV0) Admin(id string) (*adminV0, error) {
if id != "" {
// Safeguard id for possible future use.
return nil, common.ErrBadId
}
return r.admin, nil
}
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")
// Login logs in with the provided credentials. All subsequent requests on the
// connection will act as the authenticated user.
func (a *adminV0) Login(c params.Creds) (params.LoginResult, error) {
var fail params.LoginResult
resultV1, err := a.doLogin(params.LoginRequest{
AuthTag: c.AuthTag,
Credentials: c.Password,
Nonce: c.Nonce,
})
if err != nil {
return fail, err
}
resultV0 := params.LoginResult{
Servers: resultV1.Servers,
EnvironTag: resultV1.EnvironTag,
Facades: resultV1.Facades,
}
if resultV1.UserInfo != nil {
resultV0.LastConnection = resultV1.UserInfo.LastConnection
}
return resultV0, nil
}
func (a *admin) doLogin(req params.LoginRequest) (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, try again later")
return fail, common.ErrTryAgain
}
defer a.srv.limiter.Release()
} else {
isUser = true
}
entity, err := doCheckCreds(a.root.state, req)
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 {
lastConnection := getAndUpdateLastLoginForEntity(entity)
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
}
a.root.rpcConn.ServeFinder(authedApi, serverError)
return params.LoginResultV1{
Servers: params.FromNetworkHostsPorts(hostPorts),
EnvironTag: environ.Tag().String(),
Facades: DescribeFacades(),
UserInfo: maybeUserInfo,
}, 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)
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
func checkCreds(st *state.State, req params.LoginRequest) (state.Entity, error) {
tag, err := names.ParseTag(req.AuthTag)
if err != nil {
return 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, common.ErrBadCreds
}
if err != nil {
return nil, errors.Trace(err)
}
authenticator, err := authentication.FindEntityAuthenticator(entity)
if err != nil {
return nil, err
}
if err = authenticator.Authenticate(entity, req.Credentials, req.Nonce); err != nil {
logger.Debugf("bad credentials")
return nil, err
}
// For user logins, ensure the user is allowed to access the environment.
if user, ok := entity.Tag().(names.UserTag); ok {
_, err := st.EnvironmentUser(user)
if err != nil {
return nil, errors.Wrap(err, common.ErrBadCreds)
}
}
return entity, nil
}
func getAndUpdateLastLoginForEntity(entity state.Entity) *time.Time {
if user, ok := entity.(*state.User); ok {
result := user.LastLogin()
user.UpdateLastLogin()
return result
}
return 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)
err = root.getResources().RegisterNamed("pingTimeout", pingTimeout)
if err != nil {
return err
}
return nil
}
// 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
}