2019-07-23 00:24:00 +00:00
|
|
|
package repositories
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/gob"
|
2019-08-01 03:16:20 +00:00
|
|
|
"fmt"
|
2019-07-23 00:24:00 +00:00
|
|
|
"os"
|
|
|
|
|
|
|
|
"go.etcd.io/bbolt"
|
|
|
|
|
|
|
|
"github.com/mitchell/selfpass/services/credentials/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
func OpenBoltDB(file string, mode os.FileMode, opts *bbolt.Options) (out BoltDB, err error) {
|
|
|
|
db, err := bbolt.Open(file, mode, opts)
|
|
|
|
if err != nil {
|
|
|
|
return out, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return BoltDB{bolt: db}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type BoltDB struct {
|
|
|
|
bolt *bbolt.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db BoltDB) GetAllMetadata(ctx context.Context, sourceHost string, errch chan<- error) (output <-chan types.Metadata) {
|
|
|
|
mdch := make(chan types.Metadata, 1)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer close(mdch)
|
|
|
|
|
|
|
|
err := db.bolt.View(func(tx *bbolt.Tx) error {
|
2019-08-01 03:16:20 +00:00
|
|
|
bkt := getCredentialsBucket(tx)
|
|
|
|
if bkt.isEmpty {
|
2019-07-23 00:24:00 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-08-01 03:16:20 +00:00
|
|
|
c := bkt.hostPrimaryIndex.Cursor()
|
|
|
|
|
|
|
|
if sourceHost == "" {
|
|
|
|
for key, value := c.First(); key != nil; key, value = c.Next() {
|
2019-09-07 05:44:43 +00:00
|
|
|
unmarshalAndSendCred(value, mdch, errch)
|
2019-08-01 03:16:20 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
hostBytes := []byte(sourceHost)
|
|
|
|
for key, value := c.Seek(hostBytes); bytes.HasPrefix(key, hostBytes); key, value = c.Next() {
|
2019-09-07 05:44:43 +00:00
|
|
|
unmarshalAndSendCred(value, mdch, errch)
|
2019-08-01 03:16:20 +00:00
|
|
|
}
|
2019-07-23 00:24:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
errch <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return mdch
|
|
|
|
}
|
|
|
|
|
2019-09-07 05:44:43 +00:00
|
|
|
func unmarshalAndSendCred(value []byte, mdch chan<- types.Metadata, errch chan<- error) {
|
2019-08-01 03:16:20 +00:00
|
|
|
var cred types.Credential
|
|
|
|
|
|
|
|
err := gobUnmarshal(value, &cred)
|
|
|
|
if err != nil {
|
|
|
|
errch <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
mdch <- cred.Metadata
|
|
|
|
}
|
|
|
|
|
2019-07-23 00:24:00 +00:00
|
|
|
func (db BoltDB) Get(ctx context.Context, id string) (output types.Credential, err error) {
|
|
|
|
err = db.bolt.View(func(tx *bbolt.Tx) error {
|
2019-08-01 03:16:20 +00:00
|
|
|
bkt := getCredentialsBucket(tx)
|
|
|
|
if bkt.isEmpty {
|
2019-07-23 00:24:00 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
value := bkt.Get([]byte(id))
|
|
|
|
if value == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return gobUnmarshal(value, &output)
|
|
|
|
})
|
|
|
|
|
|
|
|
return output, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db BoltDB) Put(ctx context.Context, c types.Credential) (err error) {
|
2019-08-01 03:16:20 +00:00
|
|
|
return db.bolt.Update(func(tx *bbolt.Tx) error {
|
|
|
|
bkt := getCredentialsBucket(tx)
|
|
|
|
bkt.createIfNotExists()
|
|
|
|
|
|
|
|
value := bkt.Get([]byte(c.ID))
|
|
|
|
if value != nil {
|
|
|
|
var cred types.Credential
|
|
|
|
if err = gobUnmarshal(value, &cred); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = bkt.Delete([]byte(c.ID)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = bkt.hostPrimaryIndex.Delete([]byte(genHostPrimaryIdxKey(cred))); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-07-23 00:24:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
value, err := gobMarshal(c)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-01 03:16:20 +00:00
|
|
|
if err = bkt.hostPrimaryIndex.Put([]byte(genHostPrimaryIdxKey(c)), value); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-07-23 00:24:00 +00:00
|
|
|
return bkt.Put([]byte(c.ID), value)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db BoltDB) Delete(ctx context.Context, id string) (err error) {
|
2019-08-01 03:16:20 +00:00
|
|
|
return db.bolt.Update(func(tx *bbolt.Tx) error {
|
|
|
|
bkt := getCredentialsBucket(tx)
|
|
|
|
if bkt.isEmpty {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
value := bkt.Get([]byte(id))
|
|
|
|
if value == nil {
|
2019-07-23 00:24:00 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-08-01 03:16:20 +00:00
|
|
|
var cred types.Credential
|
|
|
|
if err = gobUnmarshal(value, &cred); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = bkt.hostPrimaryIndex.Delete([]byte(genHostPrimaryIdxKey(cred))); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-07-23 00:24:00 +00:00
|
|
|
return bkt.Delete([]byte(id))
|
|
|
|
})
|
2019-08-01 03:16:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const keyCredentialsBkt = "credentials"
|
|
|
|
const keyHostAndPrimaryIdx = "sourceHost-primary"
|
|
|
|
|
|
|
|
func getCredentialsBucket(tx *bbolt.Tx) credentialsBucket {
|
|
|
|
bkt := credentialsBucket{
|
|
|
|
Bucket: tx.Bucket([]byte(keyCredentialsBkt)),
|
|
|
|
tx: tx,
|
|
|
|
}
|
|
|
|
bkt.isEmpty = bkt.Bucket == nil
|
|
|
|
|
|
|
|
if !bkt.isEmpty {
|
|
|
|
bkt.hostPrimaryIndex = bkt.Bucket.Bucket([]byte(keyHostAndPrimaryIdx))
|
|
|
|
}
|
2019-07-23 00:24:00 +00:00
|
|
|
|
2019-08-01 03:16:20 +00:00
|
|
|
return bkt
|
2019-07-23 00:24:00 +00:00
|
|
|
}
|
|
|
|
|
2019-08-01 03:16:20 +00:00
|
|
|
type credentialsBucket struct {
|
|
|
|
*bbolt.Bucket
|
|
|
|
tx *bbolt.Tx
|
|
|
|
hostPrimaryIndex *bbolt.Bucket
|
|
|
|
isEmpty bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bkt *credentialsBucket) createIfNotExists() {
|
|
|
|
if bkt.isEmpty {
|
|
|
|
bkt.Bucket, _ = bkt.tx.CreateBucket([]byte(keyCredentialsBkt))
|
|
|
|
bkt.hostPrimaryIndex, _ = bkt.CreateBucket([]byte(keyHostAndPrimaryIdx))
|
|
|
|
bkt.isEmpty = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func genHostPrimaryIdxKey(cred types.Credential) string {
|
|
|
|
return fmt.Sprintf("%s-%s-%s", cred.SourceHost, cred.Primary, cred.ID)
|
|
|
|
}
|
2019-07-23 00:24:00 +00:00
|
|
|
|
|
|
|
func gobMarshal(v interface{}) (bs []byte, err error) {
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
err = gob.NewEncoder(buf).Encode(v)
|
|
|
|
return buf.Bytes(), err
|
|
|
|
}
|
|
|
|
|
|
|
|
func gobUnmarshal(bs []byte, v interface{}) error {
|
|
|
|
buf := bytes.NewReader(bs)
|
|
|
|
return gob.NewDecoder(buf).Decode(v)
|
|
|
|
}
|