Skip to content

Commit

Permalink
working on test for keyring
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike committed Oct 3, 2024
1 parent e91ff3e commit 1529431
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 18 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
3 changes: 1 addition & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand All @@ -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=
Expand Down
42 changes: 27 additions & 15 deletions pkg/security/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check failure on line 9 in pkg/security/storage.go

View workflow job for this annotation

GitHub Actions / runner / golangci-lint

[golangci] reported by reviewdog 🐶 Comment should end in a period (godot) Raw Output: pkg/security/storage.go:9:81: Comment should end in a period (godot) // 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

Check failure on line 17 in pkg/security/storage.go

View workflow job for this annotation

GitHub Actions / runner / golangci-lint

[golangci] reported by reviewdog 🐶 Comment should end in a period (godot) Raw Output: pkg/security/storage.go:17:61: Comment should end in a period (godot) // 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

Check failure on line 23 in pkg/security/storage.go

View workflow job for this annotation

GitHub Actions / runner / golangci-lint

[golangci] reported by reviewdog 🐶 return with no blank line before (nlreturn) Raw Output: pkg/security/storage.go:23:2: return with no blank line before (nlreturn) return err ^
}

// GetKeyValue method to get a value from the keyring by key

Check failure on line 26 in pkg/security/storage.go

View workflow job for this annotation

GitHub Actions / runner / golangci-lint

[golangci] reported by reviewdog 🐶 Comment should end in a period (godot) Raw Output: pkg/security/storage.go:26:61: Comment should end in a period (godot) // 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

Check failure on line 29 in pkg/security/storage.go

View workflow job for this annotation

GitHub Actions / runner / golangci-lint

[golangci] reported by reviewdog 🐶 return with no blank line before (nlreturn) Raw Output: pkg/security/storage.go:29:2: return with no blank line before (nlreturn) 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)

Check failure on line 40 in pkg/security/storage.go

View workflow job for this annotation

GitHub Actions / runner / golangci-lint

[golangci] reported by reviewdog 🐶 return with no blank line before (nlreturn) Raw Output: pkg/security/storage.go:40:2: return with no blank line before (nlreturn) 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)
}
57 changes: 57 additions & 0 deletions pkg/security/storage_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package security_test

import (
"testing"

"github.com/open-amt-cloud-toolkit/go-wsman-messages/v2/pkg/security"

Check failure on line 6 in pkg/security/storage_test.go

View workflow job for this annotation

GitHub Actions / runner / golangci-lint

[golangci] reported by reviewdog 🐶 File is not `gci`-ed with --skip-generated -s standard -s default (gci) Raw Output: pkg/security/storage_test.go:6: File is not `gci`-ed with --skip-generated -s standard -s default (gci) "github.com/open-amt-cloud-toolkit/go-wsman-messages/v2/pkg/security"

"github.com/99designs/keyring"
"github.com/stretchr/testify/mock"

Check failure on line 9 in pkg/security/storage_test.go

View workflow job for this annotation

GitHub Actions / runner / golangci-lint

[golangci] reported by reviewdog 🐶 File is not `gci`-ed with --skip-generated -s standard -s default (gci) Raw Output: pkg/security/storage_test.go:9: File is not `gci`-ed with --skip-generated -s standard -s default (gci) "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)
}
17 changes: 16 additions & 1 deletion pkg/security/types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package security

import "github.com/open-amt-cloud-toolkit/go-wsman-messages/v2/pkg/config"
import (
"github.com/99designs/keyring"

Check failure on line 4 in pkg/security/types.go

View workflow job for this annotation

GitHub Actions / runner / golangci-lint

[golangci] reported by reviewdog 🐶 File is not `gci`-ed with --skip-generated -s standard -s default (gci) Raw Output: pkg/security/types.go:4: File is not `gci`-ed with --skip-generated -s standard -s default (gci) "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)
Expand All @@ -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
}

0 comments on commit 1529431

Please sign in to comment.