adding a basic unlock client rpc command.

This commit is contained in:
greg stone
2023-02-26 13:12:08 +00:00
parent 68a4b37af7
commit 5eb60fc94f
5 changed files with 168 additions and 78 deletions

View File

@@ -1,7 +1,16 @@
package main
import (
"context"
"fmt"
"git-indra.lan/indra-labs/indra/pkg/rpc"
"git-indra.lan/indra-labs/indra/pkg/storage"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"golang.org/x/term"
"google.golang.org/grpc"
"os"
"syscall"
)
func init() {
@@ -13,13 +22,69 @@ func init() {
//rpc.InitFlags(seedServeCommand)
seedCommand.AddCommand(seedRPCCmd)
initUnlock(unlockRPCCmd)
seedRPCCmd.AddCommand(unlockRPCCmd)
}
var seedRPCCmd = &cobra.Command{
Use: "rpc",
Short: "A list of commands for interacting with a seed",
Long: `A list of commands for interacting with a seed.`,
}
var (
unlockTargetFlag = "target"
)
var (
unlockTarget string
)
func initUnlock(cmd *cobra.Command) {
cmd.PersistentFlags().StringVarP(&unlockTarget, unlockTargetFlag, "",
"unix:///tmp/indra.sock",
"the url of the rpc server",
)
viper.BindPFlag(unlockTargetFlag, cmd.PersistentFlags().Lookup(unlockTargetFlag))
}
var unlockRPCCmd = &cobra.Command{
Use: "unlock",
Short: "unlocks the encrypted storage",
Long: `unlocks the encrypted storage.`,
Run: func(cmd *cobra.Command, args []string) {
var err error
var conn *grpc.ClientConn
conn, err = rpc.Dial(viper.GetString("target"))
if err != nil {
check(err)
os.Exit(1)
}
var password []byte
fmt.Print("Enter Encryption Key: ")
password, err = term.ReadPassword(int(syscall.Stdin))
fmt.Println()
u := storage.NewUnlockServiceClient(conn)
_, err = u.Unlock(context.Background(), &storage.UnlockRequest{
Key: string(password),
})
if err != nil {
check(err)
return
}
},
}

View File

@@ -6,18 +6,12 @@ import (
"git-indra.lan/indra-labs/indra/pkg/interrupt"
log2 "git-indra.lan/indra-labs/indra/pkg/proc/log"
"git-indra.lan/indra-labs/indra/pkg/rpc"
"git-indra.lan/indra-labs/indra/pkg/rpc/examples"
"git-indra.lan/indra-labs/indra/pkg/seed"
"git-indra.lan/indra-labs/indra/pkg/storage"
"github.com/davecgh/go-spew/spew"
"github.com/dgraph-io/badger/v3"
"github.com/multiformats/go-multiaddr"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/tutorialedge/go-grpc-tutorial/chat"
"google.golang.org/grpc"
"os"
"time"
)
var (
@@ -79,68 +73,45 @@ var seedServeCommand = &cobra.Command{
os.Exit(0)
}
storage.Txn(func(txn *badger.Txn) error {
txn.Set([]byte("hello"), []byte("world"))
txn.Commit()
return nil
}, true)
storage.Txn(func(txn *badger.Txn) error {
item, _ := txn.Get([]byte("hello"))
spew.Dump(item.String())
item.Value(func(val []byte) error {
spew.Dump(string(val))
return nil
})
return nil
}, false)
//
// RPC
////
//// RPC
////
//
rpc.RunWith(ctx, func(srv *grpc.Server) {
chat.RegisterChatServiceServer(srv, &chat.Server{})
})
select {
case <-rpc.CantStart():
log.I.Ln("issues starting the rpc server")
log.I.Ln("attempting a graceful shutdown")
ctx, cancel = context.WithTimeout(context.Background(), 30*time.Second)
rpc.Shutdown(ctx)
select {
case <-ctx.Done():
log.I.Ln("can't shutdown gracefully, exiting.")
os.Exit(1)
default:
log.I.Ln("graceful shutdown complete")
os.Exit(0)
}
case <-rpc.IsReady():
log.I.Ln("rpc server is ready")
}
examples.TunnelHello(ctx)
//rpc.RunWith(ctx, func(srv *grpc.Server) {
// chat.RegisterChatServiceServer(srv, &chat.Server{})
//})
//
//select {
//case <-rpc.CantStart():
//
// log.I.Ln("issues starting the rpc server")
// log.I.Ln("attempting a graceful shutdown")
//
// ctx, cancel = context.WithTimeout(context.Background(), 30*time.Second)
//
// rpc.Shutdown(ctx)
//
// select {
// case <-ctx.Done():
//
// log.I.Ln("can't shutdown gracefully, exiting.")
//
// os.Exit(1)
//
// default:
//
// log.I.Ln("graceful shutdown complete")
//
// os.Exit(0)
// }
//
//case <-rpc.IsReady():
//
// log.I.Ln("rpc server is ready")
//}
//
//examples.TunnelHello(ctx)
//
// P2P

View File

@@ -0,0 +1,33 @@
package examples
import (
"context"
"git-indra.lan/indra-labs/indra/pkg/rpc"
"git-indra.lan/indra-labs/indra/pkg/storage"
"google.golang.org/grpc"
"os"
)
func UnixUnlock(ctx context.Context) {
var err error
var conn *grpc.ClientConn
conn, err = rpc.Dial("unix:///tmp/indra.sock")
if err != nil {
check(err)
os.Exit(1)
}
u := storage.NewUnlockServiceClient(conn)
_, err = u.Unlock(ctx, &storage.UnlockRequest{
Key: "979nrx9ry9Re6UqWXYaGqLEne8NS7TzgHFiS8KARABV8",
})
if err != nil {
check(err)
return
}
}

View File

@@ -70,23 +70,40 @@ func Run(ctx context.Context) {
if isRPCUnlockable {
var unlockService = NewUnlockService()
var unlock = NewUnlockService()
go rpc.RunWith(ctx, func(srv *grpc.Server) {
RegisterUnlockServiceServer(srv, unlockService)
RegisterUnlockServiceServer(srv, unlock)
})
select {
case <-IsReady():
return
case <-ctx.Done():
rpc.Shutdown(context.Background())
return
for {
select {
case <-rpc.IsReady():
log.I.Ln("waiting for unlock")
case <-unlock.IsSuccessful():
log.I.Ln("storage successfully unlocked")
isReady <- true
case <-ctx.Done():
Shutdown()
return
}
}
}
var err error
opts.EncryptionKey = key.Bytes()
if db, err = badger.Open(opts); check(err) {
startupErrors <- err
return
}
log.I.Ln("running storage")
isReady <- true

View File

@@ -3,8 +3,6 @@ package storage
import (
"context"
"github.com/dgraph-io/badger/v3"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
type Service struct {
@@ -21,18 +19,24 @@ func (s *Service) Unlock(ctx context.Context, req *UnlockRequest) (res *UnlockRe
key.Decode(req.Key)
opts.EncryptionKey = key.Bytes()
if db, err = badger.Open(opts); check(err) {
return &UnlockResponse{
Success: false,
}, err
}
return nil, status.Errorf(codes.Unimplemented, "method Unlock not implemented")
s.success <- true
return &UnlockResponse{
Success: true,
}, nil
}
func (s *Service) mustEmbedUnimplementedUnlockServiceServer() {}
func NewUnlockService() UnlockServiceServer {
func NewUnlockService() *Service {
return &Service{
success: make(chan bool, 1),
}