making an unlock service.

This commit is contained in:
greg stone
2023-02-26 11:54:21 +00:00
parent 6b76df204b
commit 68a4b37af7
6 changed files with 429 additions and 12 deletions

View File

@@ -11,8 +11,9 @@ import (
)
var (
isNewKey bool
key Key
isNewKey bool
isRPCUnlockable bool
key Key
)
func configure() {
@@ -67,7 +68,18 @@ func configureKey() {
return
}
log.I.Ln("no keyfile found, checking for user prompt")
log.I.Ln("no keyfile found, checking for rpc unlock")
if viper.GetBool(storeKeyRPCFlag) {
log.I.Ln("attempting rpc unlock")
isRPCUnlockable = true
return
}
log.I.Ln("rpc unlock disabled, checking for a user prompt")
if viper.GetBool(storeAskPassFlag) {

View File

@@ -2,8 +2,11 @@ package storage
import (
"context"
"git-indra.lan/indra-labs/indra/pkg/rpc"
"github.com/dgraph-io/badger/v3"
"github.com/spf13/viper"
"google.golang.org/grpc"
"sync"
)
var (
@@ -49,26 +52,43 @@ func Txn(tx func(txn *badger.Txn) error, update bool) error {
return tx(txn)
}
var (
running sync.Mutex
)
func Run(ctx context.Context) {
if !running.TryLock() {
return
}
configure()
log.I.Ln("running storage")
var err error
opts = badger.DefaultOptions(viper.GetString(storeFilePathFlag))
opts.EncryptionKey = key.Bytes()
opts.IndexCacheSize = 128 << 20
opts.Logger = nil
db, err = badger.Open(opts)
if isRPCUnlockable {
if err != nil {
startupErrors <- err
return
var unlockService = NewUnlockService()
go rpc.RunWith(ctx, func(srv *grpc.Server) {
RegisterUnlockServiceServer(srv, unlockService)
})
select {
case <-IsReady():
return
case <-ctx.Done():
rpc.Shutdown(context.Background())
return
}
}
opts.EncryptionKey = key.Bytes()
log.I.Ln("running storage")
isReady <- true
select {

View File

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

222
pkg/storage/unlock.pb.go Normal file
View File

@@ -0,0 +1,222 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.28.1
// protoc v3.21.12
// source: unlock.proto
package storage
import (
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
reflect "reflect"
sync "sync"
)
const (
// Verify that this generated code is sufficiently up-to-date.
_ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
// Verify that runtime/protoimpl is sufficiently up-to-date.
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
)
type UnlockRequest struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Key string `protobuf:"bytes,1,opt,name=key,proto3" json:"key,omitempty"`
}
func (x *UnlockRequest) Reset() {
*x = UnlockRequest{}
if protoimpl.UnsafeEnabled {
mi := &file_unlock_proto_msgTypes[0]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *UnlockRequest) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*UnlockRequest) ProtoMessage() {}
func (x *UnlockRequest) ProtoReflect() protoreflect.Message {
mi := &file_unlock_proto_msgTypes[0]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use UnlockRequest.ProtoReflect.Descriptor instead.
func (*UnlockRequest) Descriptor() ([]byte, []int) {
return file_unlock_proto_rawDescGZIP(), []int{0}
}
func (x *UnlockRequest) GetKey() string {
if x != nil {
return x.Key
}
return ""
}
type UnlockResponse struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Success bool `protobuf:"varint,1,opt,name=success,proto3" json:"success,omitempty"`
Message *string `protobuf:"bytes,2,opt,name=message,proto3,oneof" json:"message,omitempty"`
}
func (x *UnlockResponse) Reset() {
*x = UnlockResponse{}
if protoimpl.UnsafeEnabled {
mi := &file_unlock_proto_msgTypes[1]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *UnlockResponse) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*UnlockResponse) ProtoMessage() {}
func (x *UnlockResponse) ProtoReflect() protoreflect.Message {
mi := &file_unlock_proto_msgTypes[1]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use UnlockResponse.ProtoReflect.Descriptor instead.
func (*UnlockResponse) Descriptor() ([]byte, []int) {
return file_unlock_proto_rawDescGZIP(), []int{1}
}
func (x *UnlockResponse) GetSuccess() bool {
if x != nil {
return x.Success
}
return false
}
func (x *UnlockResponse) GetMessage() string {
if x != nil && x.Message != nil {
return *x.Message
}
return ""
}
var File_unlock_proto protoreflect.FileDescriptor
var file_unlock_proto_rawDesc = []byte{
0x0a, 0x0c, 0x75, 0x6e, 0x6c, 0x6f, 0x63, 0x6b, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x03,
0x72, 0x70, 0x63, 0x22, 0x21, 0x0a, 0x0d, 0x55, 0x6e, 0x6c, 0x6f, 0x63, 0x6b, 0x52, 0x65, 0x71,
0x75, 0x65, 0x73, 0x74, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28,
0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x22, 0x55, 0x0a, 0x0e, 0x55, 0x6e, 0x6c, 0x6f, 0x63, 0x6b,
0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x73, 0x75, 0x63, 0x63,
0x65, 0x73, 0x73, 0x18, 0x01, 0x20, 0x01, 0x28, 0x08, 0x52, 0x07, 0x73, 0x75, 0x63, 0x63, 0x65,
0x73, 0x73, 0x12, 0x1d, 0x0a, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x18, 0x02, 0x20,
0x01, 0x28, 0x09, 0x48, 0x00, 0x52, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x88, 0x01,
0x01, 0x42, 0x0a, 0x0a, 0x08, 0x5f, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x32, 0x44, 0x0a,
0x0d, 0x55, 0x6e, 0x6c, 0x6f, 0x63, 0x6b, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x33,
0x0a, 0x06, 0x55, 0x6e, 0x6c, 0x6f, 0x63, 0x6b, 0x12, 0x12, 0x2e, 0x72, 0x70, 0x63, 0x2e, 0x55,
0x6e, 0x6c, 0x6f, 0x63, 0x6b, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x13, 0x2e, 0x72,
0x70, 0x63, 0x2e, 0x55, 0x6e, 0x6c, 0x6f, 0x63, 0x6b, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73,
0x65, 0x22, 0x00, 0x42, 0x07, 0x5a, 0x05, 0x2e, 0x2f, 0x72, 0x70, 0x63, 0x62, 0x06, 0x70, 0x72,
0x6f, 0x74, 0x6f, 0x33,
}
var (
file_unlock_proto_rawDescOnce sync.Once
file_unlock_proto_rawDescData = file_unlock_proto_rawDesc
)
func file_unlock_proto_rawDescGZIP() []byte {
file_unlock_proto_rawDescOnce.Do(func() {
file_unlock_proto_rawDescData = protoimpl.X.CompressGZIP(file_unlock_proto_rawDescData)
})
return file_unlock_proto_rawDescData
}
var file_unlock_proto_msgTypes = make([]protoimpl.MessageInfo, 2)
var file_unlock_proto_goTypes = []interface{}{
(*UnlockRequest)(nil), // 0: rpc.UnlockRequest
(*UnlockResponse)(nil), // 1: rpc.UnlockResponse
}
var file_unlock_proto_depIdxs = []int32{
0, // 0: rpc.UnlockService.Unlock:input_type -> rpc.UnlockRequest
1, // 1: rpc.UnlockService.Unlock:output_type -> rpc.UnlockResponse
1, // [1:2] is the sub-list for method output_type
0, // [0:1] is the sub-list for method input_type
0, // [0:0] is the sub-list for extension type_name
0, // [0:0] is the sub-list for extension extendee
0, // [0:0] is the sub-list for field type_name
}
func init() { file_unlock_proto_init() }
func file_unlock_proto_init() {
if File_unlock_proto != nil {
return
}
if !protoimpl.UnsafeEnabled {
file_unlock_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*UnlockRequest); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_unlock_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*UnlockResponse); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
}
file_unlock_proto_msgTypes[1].OneofWrappers = []interface{}{}
type x struct{}
out := protoimpl.TypeBuilder{
File: protoimpl.DescBuilder{
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: file_unlock_proto_rawDesc,
NumEnums: 0,
NumMessages: 2,
NumExtensions: 0,
NumServices: 1,
},
GoTypes: file_unlock_proto_goTypes,
DependencyIndexes: file_unlock_proto_depIdxs,
MessageInfos: file_unlock_proto_msgTypes,
}.Build()
File_unlock_proto = out.File
file_unlock_proto_rawDesc = nil
file_unlock_proto_goTypes = nil
file_unlock_proto_depIdxs = nil
}

19
pkg/storage/unlock.proto Normal file
View File

@@ -0,0 +1,19 @@
syntax = "proto3";
option go_package = ".";
package rpc;
message UnlockRequest {
string key = 1;
}
message UnlockResponse {
bool success = 1;
optional string message = 2;
}
service UnlockService {
rpc Unlock(UnlockRequest) returns (UnlockResponse) {}
}

View File

@@ -0,0 +1,105 @@
// Code generated by protoc-gen-go-grpc. DO NOT EDIT.
// versions:
// - protoc-gen-go-grpc v1.2.0
// - protoc v3.21.12
// source: unlock.proto
package storage
import (
context "context"
grpc "google.golang.org/grpc"
codes "google.golang.org/grpc/codes"
status "google.golang.org/grpc/status"
)
// This is a compile-time assertion to ensure that this generated file
// is compatible with the grpc package it is being compiled against.
// Requires gRPC-Go v1.32.0 or later.
const _ = grpc.SupportPackageIsVersion7
// UnlockServiceClient is the client API for UnlockService service.
//
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.
type UnlockServiceClient interface {
Unlock(ctx context.Context, in *UnlockRequest, opts ...grpc.CallOption) (*UnlockResponse, error)
}
type unlockServiceClient struct {
cc grpc.ClientConnInterface
}
func NewUnlockServiceClient(cc grpc.ClientConnInterface) UnlockServiceClient {
return &unlockServiceClient{cc}
}
func (c *unlockServiceClient) Unlock(ctx context.Context, in *UnlockRequest, opts ...grpc.CallOption) (*UnlockResponse, error) {
out := new(UnlockResponse)
err := c.cc.Invoke(ctx, "/rpc.UnlockService/Unlock", in, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
// UnlockServiceServer is the server API for UnlockService service.
// All implementations must embed UnimplementedUnlockServiceServer
// for forward compatibility
type UnlockServiceServer interface {
Unlock(context.Context, *UnlockRequest) (*UnlockResponse, error)
mustEmbedUnimplementedUnlockServiceServer()
}
// UnimplementedUnlockServiceServer must be embedded to have forward compatible implementations.
type UnimplementedUnlockServiceServer struct {
}
func (UnimplementedUnlockServiceServer) Unlock(context.Context, *UnlockRequest) (*UnlockResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method Unlock not implemented")
}
func (UnimplementedUnlockServiceServer) mustEmbedUnimplementedUnlockServiceServer() {}
// UnsafeUnlockServiceServer may be embedded to opt out of forward compatibility for this service.
// Use of this interface is not recommended, as added methods to UnlockServiceServer will
// result in compilation errors.
type UnsafeUnlockServiceServer interface {
mustEmbedUnimplementedUnlockServiceServer()
}
func RegisterUnlockServiceServer(s grpc.ServiceRegistrar, srv UnlockServiceServer) {
s.RegisterService(&UnlockService_ServiceDesc, srv)
}
func _UnlockService_Unlock_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(UnlockRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(UnlockServiceServer).Unlock(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: "/rpc.UnlockService/Unlock",
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(UnlockServiceServer).Unlock(ctx, req.(*UnlockRequest))
}
return interceptor(ctx, in, info, handler)
}
// UnlockService_ServiceDesc is the grpc.ServiceDesc for UnlockService service.
// It's only intended for direct use with grpc.RegisterService,
// and not to be introspected or modified (even as a copy)
var UnlockService_ServiceDesc = grpc.ServiceDesc{
ServiceName: "rpc.UnlockService",
HandlerType: (*UnlockServiceServer)(nil),
Methods: []grpc.MethodDesc{
{
MethodName: "Unlock",
Handler: _UnlockService_Unlock_Handler,
},
},
Streams: []grpc.StreamDesc{},
Metadata: "unlock.proto",
}