From 5eb60fc94fd061e0bafff9ab88d9d07885175fbb Mon Sep 17 00:00:00 2001 From: greg stone Date: Sun, 26 Feb 2023 13:12:08 +0000 Subject: [PATCH] adding a basic unlock client rpc command. --- cmd/indra/seed_rpc.go | 65 ++++++++++++++++++++ cmd/indra/seed_serve.go | 103 ++++++++++++-------------------- pkg/rpc/examples/unix_unlock.go | 33 ++++++++++ pkg/storage/service.go | 33 +++++++--- pkg/storage/service_unlock.go | 12 ++-- 5 files changed, 168 insertions(+), 78 deletions(-) create mode 100644 pkg/rpc/examples/unix_unlock.go diff --git a/cmd/indra/seed_rpc.go b/cmd/indra/seed_rpc.go index e90668a0..43791366 100644 --- a/cmd/indra/seed_rpc.go +++ b/cmd/indra/seed_rpc.go @@ -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 + } + }, } diff --git a/cmd/indra/seed_serve.go b/cmd/indra/seed_serve.go index 85c5348a..ec35d176 100644 --- a/cmd/indra/seed_serve.go +++ b/cmd/indra/seed_serve.go @@ -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 diff --git a/pkg/rpc/examples/unix_unlock.go b/pkg/rpc/examples/unix_unlock.go new file mode 100644 index 00000000..643789ea --- /dev/null +++ b/pkg/rpc/examples/unix_unlock.go @@ -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 + } +} diff --git a/pkg/storage/service.go b/pkg/storage/service.go index 0926e005..ba61ded2 100644 --- a/pkg/storage/service.go +++ b/pkg/storage/service.go @@ -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 diff --git a/pkg/storage/service_unlock.go b/pkg/storage/service_unlock.go index 477d5aec..b4c03c0b 100644 --- a/pkg/storage/service_unlock.go +++ b/pkg/storage/service_unlock.go @@ -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), }