Add 2 migrations for ids and boltdb; minor refactor to creds service

This commit is contained in:
mitchell 2019-07-20 04:12:58 -04:00
parent cf90993d4e
commit a67be14fcb
13 changed files with 203 additions and 58 deletions

View file

@ -4,6 +4,7 @@ import (
"context"
"github.com/go-kit/kit/endpoint"
"github.com/mitchell/selfpass/services/credentials/types"
)
@ -48,10 +49,6 @@ func MakeUpdateEndpoint(svc types.Service) endpoint.Endpoint {
}
}
type DumpResponse struct {
Contents []byte
}
type IDRequest struct {
ID string
}

View file

@ -5,6 +5,7 @@ import (
"time"
"github.com/go-kit/kit/log"
"github.com/mitchell/selfpass/services/credentials/types"
)
@ -97,18 +98,3 @@ func (svc ServiceLogger) Delete(ctx context.Context, id string) (err error) {
err = svc.next.Delete(ctx, id)
return err
}
func (svc ServiceLogger) DumpDB(ctx context.Context) (output []byte, err error) {
defer func(begin time.Time) {
_ = svc.l.Log(
"service", "Credentials",
"method", "Dump",
"output", output,
"err", err,
"took", time.Since(begin),
)
}(time.Now())
output, err = svc.next.DumpDB(ctx)
return output, err
}

View file

@ -4,6 +4,7 @@ import (
"context"
"github.com/mediocregopher/radix/v3"
"github.com/mitchell/selfpass/services/credentials/types"
)
@ -23,7 +24,7 @@ func (conn RedisConn) GetAllMetadata(ctx context.Context, sourceHost string, err
defer close(mdch)
var key string
scr := radix.NewScanner(conn.p, radix.ScanOpts{Command: scan, Pattern: types.TypePrefixCred + dash + sourceHost + star})
scr := radix.NewScanner(conn.p, radix.ScanAllKeys)
for scr.Next(&key) {
select {
@ -39,7 +40,9 @@ func (conn RedisConn) GetAllMetadata(ctx context.Context, sourceHost string, err
return
}
mdch <- md
if sourceHost == "" || sourceHost == md.SourceHost {
mdch <- md
}
}
}()
@ -61,20 +64,7 @@ func (conn RedisConn) Delete(ctx context.Context, id string) (err error) {
return err
}
func (conn RedisConn) DumpDB(ctx context.Context) (bs []byte, err error) {
bs = []byte{}
if err := conn.p.Do(radix.Cmd(&bs, "DUMP")); err != nil {
return nil, err
}
return bs, nil
}
const (
dash = "-"
star = "*"
scan = "SCAN"
hGetAll = "HGETALL"
hMSet = "HMSET"
del = "DEL"

View file

@ -4,6 +4,7 @@ import (
"context"
"encoding/base64"
"fmt"
"math/rand"
"time"
"github.com/mitchell/selfpass/services/credentials/types"
@ -37,10 +38,12 @@ func (svc Credentials) Create(ctx context.Context, ci types.CredentialInput) (ou
return output, err
}
now := time.Now()
var c types.Credential
c.ID = generateID(ci)
c.CreatedAt = time.Now()
c.UpdatedAt = time.Now()
c.ID = generateID()
c.CreatedAt = now
c.UpdatedAt = now
c.Primary = ci.Primary
c.LoginURL = ci.LoginURL
c.SourceHost = ci.SourceHost
@ -78,15 +81,19 @@ func validateCredentialInput(c types.CredentialInput) (err error) {
return err
}
func generateID(ci types.CredentialInput) string {
idFormat := types.TypePrefixCred + "-%s-%s"
func generateID() string {
const idLen = 8
const alphanumerics = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ0123456789"
const alphaLen = len(alphanumerics)
if ci.Tag != "" {
idFormat += "-%s"
return fmt.Sprintf(idFormat, ci.SourceHost, ci.Primary, ci.Tag)
rand.Seed(time.Now().UnixNano())
id := make([]byte, idLen)
for index := range id {
id[index] = alphanumerics[rand.Int63()%int64(alphaLen)]
}
return fmt.Sprintf(idFormat, ci.SourceHost, ci.Primary)
return fmt.Sprintf("%s-%s", types.KeyCredential, string(id))
}
func (svc Credentials) Update(ctx context.Context, id string, ci types.CredentialInput) (output types.Credential, err error) {
@ -103,7 +110,6 @@ func (svc Credentials) Update(ctx context.Context, id string, ci types.Credentia
return output, err
}
c.ID = generateID(ci)
c.UpdatedAt = time.Now()
c.Primary = ci.Primary
c.LoginURL = ci.LoginURL
@ -114,12 +120,6 @@ func (svc Credentials) Update(ctx context.Context, id string, ci types.Credentia
c.Username = ci.Username
c.Tag = ci.Tag
if c.ID != id {
if err = svc.repo.Delete(ctx, id); err != nil {
return output, err
}
}
return c, svc.repo.Put(ctx, c)
}
@ -129,7 +129,3 @@ func (svc Credentials) Delete(ctx context.Context, id string) (err error) {
}
return svc.repo.Delete(ctx, id)
}
func (svc Credentials) DumpDB(ctx context.Context) (bs []byte, err error) {
return svc.repo.DumpDB(ctx)
}

View file

@ -5,7 +5,7 @@ import (
"time"
)
const TypePrefixCred = "cred"
const KeyCredential = "cred"
type Credential struct {
Metadata

View file

@ -8,7 +8,6 @@ type Service interface {
Create(ctx context.Context, ci CredentialInput) (output Credential, err error)
Update(ctx context.Context, id string, ci CredentialInput) (output Credential, err error)
Delete(ctx context.Context, id string) (err error)
DumpDB(ctx context.Context) (bs []byte, err error)
}
type CredentialsRepo interface {
@ -16,7 +15,6 @@ type CredentialsRepo interface {
Get(ctx context.Context, id string) (output Credential, err error)
Put(ctx context.Context, c Credential) (err error)
Delete(ctx context.Context, id string) (err error)
DumpDB(ctx context.Context) (bs []byte, err error)
}
type CredentialsClientInit func(ctx context.Context, target, ca, cert, key string) (c CredentialsClient, err error)