From 1529431e3105ea37ff499390cdeb4b4d578a7248 Mon Sep 17 00:00:00 2001 From: Mike Date: Thu, 3 Oct 2024 14:18:10 -0700 Subject: [PATCH] working on test for keyring --- go.mod | 1 + go.sum | 3 +- pkg/security/storage.go | 42 ++++++++++++++++---------- pkg/security/storage_test.go | 57 ++++++++++++++++++++++++++++++++++++ pkg/security/types.go | 17 ++++++++++- 5 files changed, 102 insertions(+), 18 deletions(-) create mode 100644 pkg/security/storage_test.go diff --git a/go.mod b/go.mod index 2e1e3e41..26f4963d 100644 --- a/go.mod +++ b/go.mod @@ -16,6 +16,7 @@ require ( github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2 // indirect github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c // indirect github.com/mtibben/percent v0.2.1 // indirect + github.com/stretchr/objx v0.5.2 // indirect golang.org/x/sys v0.18.0 // indirect golang.org/x/term v0.3.0 // indirect ) diff --git a/go.sum b/go.sum index f07e6848..ae04dbea 100644 --- a/go.sum +++ b/go.sum @@ -7,8 +7,6 @@ github.com/danieljoos/wincred v1.1.2/go.mod h1:GijpziifJoIBfYh+S7BbkdUTU4LfM+QnG github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/dvsekhvalnov/jose2go v1.5.0 h1:3j8ya4Z4kMCwT5nXIKFSV84YS+HdqSSO0VsTQxaLAeM= -github.com/dvsekhvalnov/jose2go v1.5.0/go.mod h1:QsHjhyTlD/lAVqn/NSbVZmSCGeDehTB/mPZadG+mhXU= github.com/dvsekhvalnov/jose2go v1.7.0 h1:bnQc8+GMnidJZA8zc6lLEAb4xNrIqHwO+9TzqvtQZPo= github.com/dvsekhvalnov/jose2go v1.7.0/go.mod h1:QsHjhyTlD/lAVqn/NSbVZmSCGeDehTB/mPZadG+mhXU= github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2 h1:ZpnhV/YsD2/4cESfV5+Hoeu/iUR3ruzNvZ+yQfO03a0= @@ -32,6 +30,7 @@ github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY= +github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= diff --git a/pkg/security/storage.go b/pkg/security/storage.go index 225648ba..96e8e2dd 100644 --- a/pkg/security/storage.go +++ b/pkg/security/storage.go @@ -4,37 +4,49 @@ import ( "github.com/99designs/keyring" ) -func NewStorage(serviceName string) Storage { +// Storage struct to hold the service name and keyring interface + +// NewStorage function to create a new Storage instance with a keyring interface +func NewStorage(serviceName string, kr Keyring) Storage { return Storage{ ServiceName: serviceName, + Keyring: kr, } } +// SetKeyValue method to set a key-value pair in the keyring func (s Storage) SetKeyValue(key, value string) error { - kr, err := keyring.Open(keyring.Config{ - ServiceName: s.ServiceName, - }) - if err != nil { - return err - } - - err = kr.Set(keyring.Item{ + err := s.Keyring.Set(keyring.Item{ Key: key, Data: []byte(value), }) - return err } +// GetKeyValue method to get a value from the keyring by key func (s Storage) GetKeyValue(key string) (string, error) { + data, err := s.Keyring.Get(key) + return string(data.Data), err +} + +// Set method to set a key-value pair in the real keyring +func (r RealKeyring) Set(item keyring.Item) error { kr, err := keyring.Open(keyring.Config{ - ServiceName: s.ServiceName, + ServiceName: r.ServiceName, }) if err != nil { - return "", err + return err } + return kr.Set(item) +} - data, err := kr.Get(key) - - return string(data.Data), err +// Get method to get a value from the real keyring by key +func (r RealKeyring) Get(key string) (keyring.Item, error) { + kr, err := keyring.Open(keyring.Config{ + ServiceName: r.ServiceName, + }) + if err != nil { + return keyring.Item{}, err + } + return kr.Get(key) } diff --git a/pkg/security/storage_test.go b/pkg/security/storage_test.go new file mode 100644 index 00000000..101a2ce9 --- /dev/null +++ b/pkg/security/storage_test.go @@ -0,0 +1,57 @@ +package security_test + +import ( + "testing" + + "github.com/open-amt-cloud-toolkit/go-wsman-messages/v2/pkg/security" + + "github.com/99designs/keyring" + "github.com/stretchr/testify/mock" +) + +// MockKeyring to mock the keyring interface for unit testing +type MockKeyring struct { + mock.Mock +} + +func (m *MockKeyring) Set(item keyring.Item) error { + args := m.Called(item) + return args.Error(0) +} + +func (m *MockKeyring) Get(key string) (keyring.Item, error) { + args := m.Called(key) + return args.Get(0).(keyring.Item), args.Error(1) +} + +func TestSetKeyValue(t *testing.T) { + mockKeyring := new(MockKeyring) + storage := security.NewStorage("testService", mockKeyring) + + mockKeyring.On("Set", keyring.Item{Key: "testKey", Data: []byte("testValue")}).Return(nil) + + err := storage.SetKeyValue("testKey", "testValue") + if err != nil { + t.Errorf("Expected no error, got %v", err) + } + + mockKeyring.AssertExpectations(t) +} + +func TestGetKeyValue(t *testing.T) { + mockKeyring := new(MockKeyring) + storage := security.NewStorage("testService", mockKeyring) + + mockKeyring.On("Get", "testKey").Return(keyring.Item{Key: "testKey", Data: []byte("testValue")}, nil) + + value, err := storage.GetKeyValue("testKey") + if err != nil { + t.Errorf("Expected no error, got %v", err) + } + + if value != "testValue" { + t.Errorf("Expected value 'testValue', got %v", value) + } + + mockKeyring.AssertExpectations(t) +} diff --git a/pkg/security/types.go b/pkg/security/types.go index 6e83bbdb..59777d92 100644 --- a/pkg/security/types.go +++ b/pkg/security/types.go @@ -1,6 +1,9 @@ package security -import "github.com/open-amt-cloud-toolkit/go-wsman-messages/v2/pkg/config" +import ( + "github.com/99designs/keyring" + "github.com/open-amt-cloud-toolkit/go-wsman-messages/v2/pkg/config" +) type Cryptor interface { Decrypt(cipherText string, key []byte) ([]byte, error) @@ -18,4 +21,16 @@ type Storager interface { type Storage struct { ServiceName string + Keyring Keyring +} + +// Keyring interface to abstract the keyring operations +type Keyring interface { + Set(item keyring.Item) error + Get(key string) (keyring.Item, error) +} + +// RealKeyring struct to implement the Keyring interface using the real keyring package +type RealKeyring struct { + ServiceName string }