@@ -134,7 +134,21 @@ func (s *sessionRpcServer) AddSession(_ context.Context,
134134 return nil , err
135135 }
136136
137- var permissions []bakery.Op
137+ // Store the entity-action permission pairs in a map in order to
138+ // de-dup any repeat perms.
139+ permissions := make (map [string ]map [string ]struct {})
140+
141+ // addPerm is a closure that can be used to add entity-action pairs to
142+ // the permissions map.
143+ addPerm := func (entity , action string ) {
144+ _ , ok := permissions [entity ]
145+ if ! ok {
146+ permissions [entity ] = make (map [string ]struct {})
147+ }
148+
149+ permissions [entity ][action ] = struct {}{}
150+ }
151+
138152 switch typ {
139153 // For the default session types we use empty caveats and permissions,
140154 // the macaroons are baked correctly when creating the session.
@@ -152,10 +166,7 @@ func (s *sessionRpcServer) AddSession(_ context.Context,
152166
153167 for _ , op := range req .MacaroonCustomPermissions {
154168 if op .Entity != macaroons .PermissionEntityCustomURI {
155- permissions = append (permissions , bakery.Op {
156- Entity : op .Entity ,
157- Action : op .Action ,
158- })
169+ addPerm (op .Entity , op .Action )
159170
160171 continue
161172 }
@@ -169,7 +180,9 @@ func (s *sessionRpcServer) AddSession(_ context.Context,
169180 true ,
170181 )
171182
172- permissions = append (permissions , readPerms ... )
183+ for _ , p := range readPerms {
184+ addPerm (p .Entity , p .Action )
185+ }
173186
174187 continue
175188 }
@@ -181,12 +194,7 @@ func (s *sessionRpcServer) AddSession(_ context.Context,
181194 // the matching URIs returned from the
182195 // permissions' manager.
183196 for _ , uri := range uris {
184- permissions = append (
185- permissions , bakery.Op {
186- Entity : op .Entity ,
187- Action : uri ,
188- },
189- )
197+ addPerm (op .Entity , uri )
190198 }
191199 continue
192200 }
@@ -199,10 +207,7 @@ func (s *sessionRpcServer) AddSession(_ context.Context,
199207 "LiT" , op .Action )
200208 }
201209
202- permissions = append (permissions , bakery.Op {
203- Entity : op .Entity ,
204- Action : op .Action ,
205- })
210+ addPerm (op .Entity , op .Action )
206211 }
207212
208213 // No other types are currently supported.
@@ -211,9 +216,20 @@ func (s *sessionRpcServer) AddSession(_ context.Context,
211216 "readonly and custom macaroon types supported in LiT" )
212217 }
213218
219+ // Collect the de-duped permissions.
220+ var perms []bakery.Op
221+ for entity , actions := range permissions {
222+ for action := range actions {
223+ perms = append (perms , bakery.Op {
224+ Entity : entity ,
225+ Action : action ,
226+ })
227+ }
228+ }
229+
214230 sess , err := session .NewSession (
215231 req .Label , typ , expiry , req .MailboxServerAddr , req .DevServer ,
216- permissions , nil ,
232+ perms , nil ,
217233 )
218234 if err != nil {
219235 return nil , fmt .Errorf ("error creating new session: %v" , err )
0 commit comments