mirror of
https://github.com/mitchell/selfpass.git
synced 2025-12-13 21:07:22 +00:00
Implemented encryption functionality of spc and password generation; refactors on spc and server
This commit is contained in:
parent
c289eecd54
commit
cd24f6e848
26 changed files with 1151 additions and 1522 deletions
115
credentials/repositories/grpc_client.go
Normal file
115
credentials/repositories/grpc_client.go
Normal file
|
|
@ -0,0 +1,115 @@
|
|||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/mitchell/selfpass/credentials/endpoints"
|
||||
"github.com/mitchell/selfpass/credentials/protobuf"
|
||||
"github.com/mitchell/selfpass/credentials/transport"
|
||||
"github.com/mitchell/selfpass/credentials/types"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/credentials"
|
||||
)
|
||||
|
||||
func NewCredentialServiceClient(ctx context.Context, target, ca, cert, key string) (types.CredentialClient, error) {
|
||||
keypair, err := tls.X509KeyPair([]byte(cert), []byte(key))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
capool := x509.NewCertPool()
|
||||
capool.AppendCertsFromPEM([]byte(ca))
|
||||
|
||||
creds := credentials.NewTLS(&tls.Config{
|
||||
RootCAs: capool,
|
||||
Certificates: []tls.Certificate{keypair},
|
||||
})
|
||||
|
||||
conn, err := grpc.DialContext(ctx, target, grpc.WithTransportCredentials(creds), grpc.WithBlock())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return CredentialServiceClient{
|
||||
client: protobuf.NewCredentialServiceClient(conn),
|
||||
}, nil
|
||||
}
|
||||
|
||||
type CredentialServiceClient struct {
|
||||
client protobuf.CredentialServiceClient
|
||||
}
|
||||
|
||||
func (c CredentialServiceClient) GetAllMetadata(ctx context.Context, sourceHost string) (output <-chan types.Metadata, errch chan error) {
|
||||
pbmdch := make(chan protobuf.Metadata, 1)
|
||||
errch = make(chan error, 1)
|
||||
|
||||
stream, err := transport.DecodeMetdataStreamResponse(ctx, transport.ProtobufMetadataStream{
|
||||
Metadata: pbmdch,
|
||||
Errors: errch,
|
||||
})
|
||||
|
||||
srv, err := c.client.GetAllMetadata(ctx, &protobuf.GetAllMetadataRequest{SourceHost: sourceHost})
|
||||
if err != nil {
|
||||
errch <- err
|
||||
return nil, errch
|
||||
}
|
||||
|
||||
go func() {
|
||||
defer close(pbmdch)
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
errch <- fmt.Errorf("context timeout")
|
||||
return
|
||||
default:
|
||||
}
|
||||
|
||||
pbmd, err := srv.Recv()
|
||||
if err == io.EOF {
|
||||
return
|
||||
} else if err != nil {
|
||||
errch <- err
|
||||
return
|
||||
}
|
||||
|
||||
pbmdch <- *pbmd
|
||||
}
|
||||
}()
|
||||
|
||||
return stream.Metadata, stream.Errors
|
||||
}
|
||||
|
||||
func (c CredentialServiceClient) Get(ctx context.Context, id string) (output types.Credential, err error) {
|
||||
req := transport.EncodeIdRequest(endpoints.IDRequest{ID: id})
|
||||
|
||||
res, err := c.client.Get(ctx, &req)
|
||||
if err != nil {
|
||||
return output, err
|
||||
}
|
||||
|
||||
return transport.DecodeCredential(*res)
|
||||
}
|
||||
|
||||
func (c CredentialServiceClient) Create(ctx context.Context, ci types.CredentialInput) (output types.Credential, err error) {
|
||||
req := transport.EncodeCredentialRequest(ci)
|
||||
|
||||
res, err := c.client.Create(ctx, &req)
|
||||
if err != nil {
|
||||
return output, err
|
||||
}
|
||||
|
||||
return transport.DecodeCredential(*res)
|
||||
}
|
||||
|
||||
func (c CredentialServiceClient) Update(ctx context.Context, id string, ci types.CredentialInput) (output types.Credential, err error) {
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (c CredentialServiceClient) Delete(ctx context.Context, id string) (err error) {
|
||||
panic("implement me")
|
||||
}
|
||||
|
|
@ -7,18 +7,11 @@ import (
|
|||
"github.com/mitchell/selfpass/credentials/types"
|
||||
)
|
||||
|
||||
func NewRedisConn(cfg ConnConfig) (c RedisConn, err error) {
|
||||
p, err := radix.NewPool(cfg.NetworkType, cfg.Address, int(cfg.Size), cfg.Options...)
|
||||
func NewRedisConn(networkType, address string, connCount uint, options ...radix.PoolOpt) (c RedisConn, err error) {
|
||||
p, err := radix.NewPool(networkType, address, int(connCount), options...)
|
||||
return RedisConn{p: p}, err
|
||||
}
|
||||
|
||||
type ConnConfig struct {
|
||||
NetworkType string
|
||||
Address string
|
||||
Size uint
|
||||
Options []radix.PoolOpt
|
||||
}
|
||||
|
||||
type RedisConn struct {
|
||||
p *radix.Pool
|
||||
}
|
||||
|
|
@ -30,22 +23,23 @@ 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: sourceHost + star})
|
||||
scr := radix.NewScanner(conn.p, radix.ScanOpts{Command: scan, Pattern: types.TypePrefixCred + dash + sourceHost + star})
|
||||
|
||||
for scr.Next(&key) {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
default:
|
||||
var md types.Metadata
|
||||
|
||||
if err := conn.p.Do(radix.Cmd(&md, hGetAll, key)); err != nil {
|
||||
errch <- err
|
||||
return
|
||||
}
|
||||
|
||||
mdch <- md
|
||||
}
|
||||
|
||||
var md types.Metadata
|
||||
|
||||
if err := conn.p.Do(radix.Cmd(&md, hGetAll, key)); err != nil {
|
||||
errch <- err
|
||||
return
|
||||
}
|
||||
|
||||
mdch <- md
|
||||
}
|
||||
}()
|
||||
|
||||
|
|
@ -53,41 +47,17 @@ func (conn RedisConn) GetAllMetadata(ctx context.Context, sourceHost string, err
|
|||
}
|
||||
|
||||
func (conn RedisConn) Get(ctx context.Context, id string) (output types.Credential, err error) {
|
||||
var key string
|
||||
scr := radix.NewScanner(conn.p, radix.ScanOpts{Command: scan, Pattern: star + id, Count: 1})
|
||||
|
||||
if !scr.Next(&key) {
|
||||
return output, nil
|
||||
}
|
||||
|
||||
if err = scr.Close(); err != nil {
|
||||
return output, err
|
||||
}
|
||||
|
||||
err = conn.p.Do(radix.Cmd(&output, hGetAll, key))
|
||||
|
||||
err = conn.p.Do(radix.Cmd(&output, hGetAll, id))
|
||||
return output, err
|
||||
}
|
||||
|
||||
func (conn RedisConn) Put(ctx context.Context, c types.Credential) (err error) {
|
||||
err = conn.p.Do(radix.FlatCmd(nil, hMSet, c.SourceHost+dash+c.ID, c))
|
||||
err = conn.p.Do(radix.FlatCmd(nil, hMSet, c.ID, c))
|
||||
return err
|
||||
}
|
||||
|
||||
func (conn RedisConn) Delete(ctx context.Context, id string) (err error) {
|
||||
var key string
|
||||
scr := radix.NewScanner(conn.p, radix.ScanOpts{Command: scan, Pattern: star + id, Count: 1})
|
||||
|
||||
if !scr.Next(&key) {
|
||||
return nil
|
||||
}
|
||||
|
||||
if err = scr.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = conn.p.Do(radix.Cmd(nil, del, key))
|
||||
|
||||
err = conn.p.Do(radix.Cmd(nil, del, id))
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue