forked from canonical/candid
/
bakery.go
43 lines (36 loc) · 1.26 KB
/
bakery.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
// Copyright 2017 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package mgostore
import (
"golang.org/x/net/context"
errgo "gopkg.in/errgo.v1"
"gopkg.in/macaroon-bakery.v2/bakery/mgorootkeystore"
mgo "gopkg.in/mgo.v2"
)
const macaroonCollection = "macaroons"
type rootKeyStore struct {
b *backend
policy mgorootkeystore.Policy
}
// Get implements bakery.RootKeyStore.Get by wrapping mgorootkeystore
// implementation with code to determine the collection.
func (s rootKeyStore) Get(ctx context.Context, id []byte) ([]byte, error) {
coll := s.b.c(ctx, macaroonCollection)
defer coll.Database.Session.Close()
store := s.b.rootKeys.NewStore(coll, s.policy)
return store.Get(ctx, id)
}
// RootKey implements bakery.RootKeyStore.RootKey by wrapping
// mgorootkeystore implementation with code to determine the collection.
func (s rootKeyStore) RootKey(ctx context.Context) ([]byte, []byte, error) {
coll := s.b.c(ctx, macaroonCollection)
defer coll.Database.Session.Close()
store := s.b.rootKeys.NewStore(coll, s.policy)
return store.RootKey(ctx)
}
func ensureBakeryIndexes(rk *mgorootkeystore.RootKeys, db *mgo.Database) error {
if err := rk.EnsureIndex(db.C(macaroonCollection)); err != nil {
return errgo.Mask(err)
}
return nil
}