Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor EncryptedDiskStore to return io.Reader instead of io.ReadCloser #4329

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/pkg/agent/application/info/agent_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type persistentAgentInfo struct {

type ioStore interface {
Save(io.Reader) error
Load() (io.ReadCloser, error)
Load() (io.Reader, error)
}

// updateLogLevel updates log level and persists it to disk.
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/agent/storage/disk_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (d *DiskStore) Save(in io.Reader) error {
}

// Load return a io.ReadCloser for the target file.
func (d *DiskStore) Load() (io.ReadCloser, error) {
func (d *DiskStore) Load() (io.Reader, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you sure this is allright?

fd, err := os.OpenFile(d.target, os.O_RDONLY|os.O_CREATE, perms)
if err != nil {
return nil, errors.New(err,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ func TestEncryptedDiskStorageWindowsLinuxLoad(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer r.Close()

b, err := io.ReadAll(r)
if err != nil {
Expand Down Expand Up @@ -98,7 +97,6 @@ func TestEncryptedDiskStorageWindowsLinuxLoad(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer nr.Close()

b, err = io.ReadAll(nr)
if err != nil {
Expand Down
18 changes: 6 additions & 12 deletions internal/pkg/agent/storage/encrypted_disk_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,31 +167,25 @@ func (d *EncryptedDiskStore) Save(in io.Reader) error {
}

// Load returns an io.ReadCloser for the target.
func (d *EncryptedDiskStore) Load() (rc io.ReadCloser, err error) {
fd, err := os.OpenFile(d.target, os.O_RDONLY, perms)
func (d *EncryptedDiskStore) Load() (rc io.Reader, err error) {
data, err := os.ReadFile(d.target)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
// If file doesn't exist, return empty reader closer
return io.NopCloser(bytes.NewReader([]byte{})), nil
return bytes.NewReader([]byte{}), nil
}
return nil, errors.New(err,
fmt.Sprintf("could not open %s", d.target),
errors.TypeFilesystem,
errors.M(errors.MetaKeyPath, d.target))
}

// Close fd if there is an error upon return
defer func() {
if err != nil && fd != nil {
_ = fd.Close()
}
}()

// Ensure has agent key
err = d.ensureKey(d.ctx)
if err != nil {
return nil, errors.New(err, "failed to ensure key during encrypted disk store Load")
return nil, errors.New(err,
"failed to ensure key during encrypted disk store Load")
}

return crypto.NewReaderWithDefaults(fd, d.key)
return crypto.NewReaderWithDefaults(bytes.NewReader(data), d.key)
}
2 changes: 1 addition & 1 deletion internal/pkg/agent/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type Storage interface {
Store

// Load return an io.ReadCloser for the target store.
Load() (io.ReadCloser, error)
Load() (io.Reader, error)

// Exists checks if the store exists.
Exists() (bool, error)
Expand Down
1 change: 0 additions & 1 deletion internal/pkg/agent/storage/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ func TestDiskStore(t *testing.T) {
d := NewDiskStore(target)
r, err := d.Load()
require.NoError(t, err)
defer r.Close()

content, err := io.ReadAll(r)
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/agent/storage/store/state_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type store interface {

type storeLoad interface {
store
Load() (io.ReadCloser, error)
Load() (io.Reader, error)
}

type action = fleetapi.Action
Expand Down
Loading