forked from juju/juju
-
Notifications
You must be signed in to change notification settings - Fork 0
/
applicationofferuser.go
197 lines (178 loc) · 5.62 KB
/
applicationofferuser.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
// Copyright 2017 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package state
import (
"github.com/juju/collections/set"
"github.com/juju/errors"
"github.com/juju/mgo/v2/bson"
"github.com/juju/mgo/v2/txn"
"github.com/juju/names/v4"
"github.com/juju/juju/core/permission"
)
// GetOfferAccess gets the access permission for the specified user on an offer.
func (st *State) GetOfferAccess(offerUUID string, user names.UserTag) (permission.Access, error) {
perm, err := st.userPermission(applicationOfferKey(offerUUID), userGlobalKey(userAccessID(user)))
if err != nil {
return "", errors.Trace(err)
}
return perm.access(), nil
}
// GetOfferUsers gets the access permissions on an offer.
func (st *State) GetOfferUsers(offerUUID string) (map[string]permission.Access, error) {
perms, err := st.usersPermissions(applicationOfferKey(offerUUID))
if err != nil {
return nil, errors.Trace(err)
}
result := make(map[string]permission.Access)
for _, p := range perms {
result[userIDFromGlobalKey(p.doc.SubjectGlobalKey)] = p.access()
}
return result, nil
}
// CreateOfferAccess creates a new access permission for a user on an offer.
func (st *State) CreateOfferAccess(offer names.ApplicationOfferTag, user names.UserTag, access permission.Access) error {
if err := permission.ValidateOfferAccess(access); err != nil {
return errors.Trace(err)
}
// Local users must exist.
if user.IsLocal() {
_, err := st.User(user)
if err != nil {
if errors.IsNotFound(err) {
return errors.Annotatef(err, "user %q does not exist locally", user.Name())
}
return errors.Trace(err)
}
}
offerUUID, err := applicationOfferUUID(st, offer.Name)
if err != nil {
return errors.Annotate(err, "creating offer access")
}
op := createPermissionOp(applicationOfferKey(offerUUID), userGlobalKey(userAccessID(user)), access)
err = st.db().RunTransaction([]txn.Op{op})
if err == txn.ErrAborted {
err = errors.AlreadyExistsf("permission for user %q for offer %q", user.Id(), offer.Name)
}
return errors.Trace(err)
}
// UpdateOfferAccess changes the user's access permissions on an offer.
func (st *State) UpdateOfferAccess(offer names.ApplicationOfferTag, user names.UserTag, access permission.Access) error {
if err := permission.ValidateOfferAccess(access); err != nil {
return errors.Trace(err)
}
offerUUID, err := applicationOfferUUID(st, offer.Name)
if err != nil {
return errors.Trace(err)
}
buildTxn := func(int) ([]txn.Op, error) {
_, err := st.GetOfferAccess(offerUUID, user)
if err != nil {
return nil, errors.Trace(err)
}
isAdmin, err := st.isControllerOrModelAdmin(user)
if err != nil {
return nil, errors.Trace(err)
}
ops := []txn.Op{updatePermissionOp(applicationOfferKey(offerUUID), userGlobalKey(userAccessID(user)), access)}
if !isAdmin && access != permission.ConsumeAccess && access != permission.AdminAccess {
suspendOps, err := st.suspendRevokedRelationsOps(offerUUID, user.Id())
if err != nil {
return nil, errors.Trace(err)
}
ops = append(ops, suspendOps...)
}
return ops, nil
}
err = st.db().Run(buildTxn)
return errors.Trace(err)
}
// suspendRevokedRelationsOps suspends any relations the given user has against
// the specified offer.
func (st *State) suspendRevokedRelationsOps(offerUUID, userId string) ([]txn.Op, error) {
conns, err := st.OfferConnections(offerUUID)
if err != nil {
return nil, errors.Trace(err)
}
var ops []txn.Op
relIdsToSuspend := make(set.Ints)
for _, oc := range conns {
if oc.UserName() == userId {
rel, err := st.Relation(oc.RelationId())
if err != nil {
return nil, errors.Trace(err)
}
if rel.Suspended() {
continue
}
relIdsToSuspend.Add(rel.Id())
suspendOp := txn.Op{
C: relationsC,
Id: rel.doc.DocID,
Assert: txn.DocExists,
Update: bson.D{{"$set", bson.D{{"suspended", true}}}},
}
ops = append(ops, suspendOp)
}
}
// Add asserts that the relations against the offered application don't change.
// This is broader than what we need but it's all that's possible.
ao := NewApplicationOffers(st)
offer, err := ao.ApplicationOfferForUUID(offerUUID)
if err != nil {
return nil, errors.Trace(err)
}
app, err := st.Application(offer.ApplicationName)
if err != nil {
return nil, errors.Trace(err)
}
relations, err := app.Relations()
if err != nil {
return nil, errors.Trace(err)
}
sameRelCount := bson.D{{"relationcount", len(relations)}}
ops = append(ops, txn.Op{
C: applicationsC,
Id: app.doc.DocID,
Assert: sameRelCount,
})
// Ensure any relations not being updated still exist.
for _, r := range relations {
if relIdsToSuspend.Contains(r.Id()) {
continue
}
ops = append(ops, txn.Op{
C: relationsC,
Id: r.doc.DocID,
Assert: txn.DocExists,
})
}
return ops, nil
}
// RemoveOfferAccess removes the access permission for a user on an offer.
func (st *State) RemoveOfferAccess(offer names.ApplicationOfferTag, user names.UserTag) error {
offerUUID, err := applicationOfferUUID(st, offer.Name)
if err != nil {
return errors.Trace(err)
}
buildTxn := func(int) ([]txn.Op, error) {
_, err := st.GetOfferAccess(offerUUID, user)
if err != nil {
return nil, err
}
isAdmin, err := st.isControllerOrModelAdmin(user)
if err != nil {
return nil, errors.Trace(err)
}
ops := []txn.Op{removePermissionOp(applicationOfferKey(offerUUID), userGlobalKey(userAccessID(user)))}
if !isAdmin {
suspendOps, err := st.suspendRevokedRelationsOps(offerUUID, user.Id())
if err != nil {
return nil, errors.Trace(err)
}
ops = append(ops, suspendOps...)
}
return ops, nil
}
err = st.db().Run(buildTxn)
return errors.Trace(err)
}