/
modeluser.go
190 lines (165 loc) · 5.34 KB
/
modeluser.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
// Copyright 2018 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package stateauthenticator
import (
"time"
"github.com/juju/errors"
"github.com/juju/names/v5"
"github.com/juju/juju/apiserver/common"
"github.com/juju/juju/core/permission"
"github.com/juju/juju/state"
stateerrors "github.com/juju/juju/state/errors"
)
// modelUserEntityFinder implements state.EntityFinder by returning
// an Entity value for model users, ensuring that the user exists in
// the state's current model, while also supporting external users.
type modelUserEntityFinder struct {
st *state.State
}
// FindEntity implements state.EntityFinder.
func (f modelUserEntityFinder) FindEntity(tag names.Tag) (state.Entity, error) {
utag, ok := tag.(names.UserTag)
if !ok {
return f.st.FindEntity(tag)
}
model, err := f.st.Model()
if err != nil {
return nil, errors.Trace(err)
}
modelAccess, err := f.st.UserAccess(utag, model.ModelTag())
if err != nil && !errors.IsNotFound(err) {
return nil, errors.Trace(err)
}
// No model user found, so see if the user has been granted
// access to the controller.
if permission.IsEmptyUserAccess(modelAccess) {
controllerAccess, err := state.ControllerAccess(f.st, utag)
if err != nil && !errors.IsNotFound(err) {
return nil, errors.Trace(err)
}
// TODO(perrito666) remove the following section about everyone group
// when groups are implemented, this accounts only for the lack of a local
// ControllerUser when logging in from an external user that has not been granted
// permissions on the controller but there are permissions for the special
// everyone group.
if permission.IsEmptyUserAccess(controllerAccess) && !utag.IsLocal() {
everyoneTag := names.NewUserTag(common.EveryoneTagName)
controllerAccess, err = f.st.UserAccess(everyoneTag, f.st.ControllerTag())
if err != nil && !errors.IsNotFound(err) {
return nil, errors.Annotatef(err, "obtaining ControllerUser for everyone group")
}
}
if permission.IsEmptyUserAccess(controllerAccess) {
return nil, errors.NotFoundf("model or controller user")
}
}
u := &modelUserEntity{
st: f.st,
modelAccess: modelAccess,
tag: utag,
}
if utag.IsLocal() {
user, err := f.st.User(utag)
if err != nil {
return nil, errors.Trace(err)
}
u.user = user
}
return u, nil
}
// modelUserEntity encapsulates a model user
// and, if the user is local, the local state user
// as well. This enables us to implement FindEntity
// in such a way that the authentication mechanisms
// can work without knowing these details.
type modelUserEntity struct {
st *state.State
modelAccess permission.UserAccess
tag names.Tag
// user is nil for external users.
user *state.User
}
// Refresh implements state.Authenticator.Refresh.
func (u *modelUserEntity) Refresh() error {
if u.user == nil {
return nil
}
return u.user.Refresh()
}
// SetPassword implements state.Authenticator.SetPassword
// by setting the password on the local user.
func (u *modelUserEntity) SetPassword(pass string) error {
if u.user == nil {
return errors.New("cannot set password on external user")
}
return u.user.SetPassword(pass)
}
// PasswordValid implements state.Authenticator.PasswordValid.
func (u *modelUserEntity) PasswordValid(pass string) bool {
if u.user == nil {
return false
}
return u.user.PasswordValid(pass)
}
// Tag implements state.Entity.Tag.
func (u *modelUserEntity) Tag() names.Tag {
return u.tag
}
// LastLogin implements loginEntity.LastLogin.
func (u *modelUserEntity) LastLogin() (time.Time, error) {
// The last connection for the model takes precedence over
// the local user last login time.
var err error
var t time.Time
model, err := u.st.Model()
if err != nil {
return t, errors.Trace(err)
}
if !permission.IsEmptyUserAccess(u.modelAccess) {
t, err = model.LastModelConnection(u.modelAccess.UserTag)
} else {
err = stateerrors.NewNeverConnectedError("controller user")
}
if stateerrors.IsNeverConnectedError(err) || permission.IsEmptyUserAccess(u.modelAccess) {
if u.user != nil {
// There's a global user, so use that login time instead.
return u.user.LastLogin()
}
// Since we're implementing LastLogin, we need
// to implement LastLogin error semantics too.
err = stateerrors.NewNeverLoggedInError(err.Error())
}
return t, errors.Trace(err)
}
// UpdateLastLogin implements loginEntity.UpdateLastLogin.
func (u *modelUserEntity) UpdateLastLogin() error {
updateLastLogin := func() error {
// If user is nil, don't attempt to perform the update and exit early.
if u.user == nil {
return nil
}
if err := u.user.UpdateLastLogin(); err != nil {
return errors.Trace(err)
}
return nil
}
if !permission.IsEmptyUserAccess(u.modelAccess) {
if u.modelAccess.Object.Kind() != names.ModelTagKind {
return errors.NotValidf("%s as model user", u.modelAccess.Object.Kind())
}
model, err := u.st.Model()
if err != nil {
return errors.Trace(err)
}
if err := model.UpdateLastModelConnection(u.modelAccess.UserTag); err != nil {
// Attempt to update the users last login data, if the update
// fails, then just report it as a log message and return the
// original error message.
if err := updateLastLogin(); err != nil {
logger.Warningf("Unable to update last login with %s", err.Error())
}
return errors.Trace(err)
}
}
return updateLastLogin()
}