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

fix(windows-agent): Use registry multiline string for Landscape Config #350

Merged
merged 3 commits into from
Oct 24, 2023
Merged
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
4 changes: 4 additions & 0 deletions go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,9 @@ github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OS
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8=
github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
github.com/phayes/checkstyle v0.0.0-20170904204023-bfd46e6a821d/go.mod h1:3OzsM7FXDQlpCiw2j81fOmAwQLnZnLGXVKUzeKQXIAw=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pkg/sftp v1.10.0/go.mod h1:NxmoDg/QLVWluQDUYG7XBZTLUpKeFa8e3aMf1BfjyHk=
github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZI=
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
Expand All @@ -600,6 +603,7 @@ github.com/sirupsen/logrus v1.9.2/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVs
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/spf13/afero v1.3.3/go.mod h1:5KUK8ByomD5Ti5Artl0RtHeI5pTF7MIDuXL3yY520V4=
github.com/spf13/afero v1.6.0/go.mod h1:Ai8FlHk4v/PARR026UzYexafAt9roJ7LcLMAmO6Z93I=
github.com/spf13/jwalterweatherman v1.1.0/go.mod h1:aNWZUN0dPAAO/Ljvb5BEdw96iTZ0EXowPYD95IqWIGo=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
github.com/stretchr/testify v1.7.5/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
Expand Down
3 changes: 2 additions & 1 deletion windows-agent/internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type Registry interface {
CloseKey(k uintptr)
ReadValue(k uintptr, field string) (value string, err error)
WriteValue(k uintptr, field string, value string) (err error)
WriteMultilineValue(k uintptr, field string, value string) (err error)
}

// Config manages configuration parameters. It is a wrapper around a dictionary
Expand Down Expand Up @@ -323,7 +324,7 @@ func (c *Config) dump() (err error) {
return fmt.Errorf("could not write into registry key: %v", err)
}

if err := c.registry.WriteValue(k, fieldLandscapeClientConfig, c.data.landscapeClientConfig); err != nil {
if err := c.registry.WriteMultilineValue(k, fieldLandscapeClientConfig, c.data.landscapeClientConfig); err != nil {
return fmt.Errorf("could not write into registry key: %v", err)
}

Expand Down
7 changes: 6 additions & 1 deletion windows-agent/internal/config/registry/registry_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ func (Windows) ReadValue(k uintptr, field string) (value string, err error) {
panic("the Windows registry is not available on Linux")
}

// WriteValue writes the provided value into the specified field of key k.
// WriteValue writes the provided single-line string value into the specified field of key k.
func (Windows) WriteValue(k uintptr, field string, value string) (err error) {
panic("the Windows registry is not available on Linux")
}

// WriteMultilineValue writes the provided multi-line string value into the specified field of key k.
func (Windows) WriteMultilineValue(k uintptr, field string, value string) (err error) {
panic("the Windows registry is not available on Linux")
}
13 changes: 11 additions & 2 deletions windows-agent/internal/config/registry/registry_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,7 @@ func (r Mock) ReadValue(k uintptr, field string) (value string, err error) {
return v, nil
}

// WriteValue writes the provided value into the specified field of key k.
func (r *Mock) WriteValue(k uintptr, field string, value string) (err error) {
func (r *Mock) write(k uintptr, field string, value string) (err error) {
if k == 0 {
return errors.New("Null key")
}
Expand All @@ -132,6 +131,16 @@ func (r *Mock) WriteValue(k uintptr, field string, value string) (err error) {
return nil
}

// WriteValue writes the provided value into the specified field of key k.
func (r *Mock) WriteValue(k uintptr, field string, value string) (err error) {
return r.write(k, field, value)
}

// WriteMultilineValue writes the provided multi-line string into the specified field of key k.
func (r *Mock) WriteMultilineValue(k uintptr, field string, value string) (err error) {
return r.write(k, field, value)
}

func isRead(access uint32) bool {
// Bit mask of the lower 16 bits
return (access & READ & 0xffff) != 0
Expand Down
26 changes: 6 additions & 20 deletions windows-agent/internal/config/registry/registry_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,26 +65,12 @@ func (Windows) ReadValue(k uintptr, field string) (string, error) {
return "", acc
}

// WriteValue writes the provided value into the specified field of key k.
// WriteValue writes the provided single-line string value into the specified field of key k.
func (Windows) WriteValue(k uintptr, field string, value string) error {
var acc error

if !strings.ContainsRune(value, '\n') {
// Single line string: we try storing a regular string
// This can fail if this field is already multi-line
if err := registry.Key(k).SetStringValue(field, value); err != nil {
acc = errors.Join(acc, err)
} else {
return nil
}
}

// Multi-line string
if err := registry.Key(k).SetStringsValue(field, strings.Split(value, "\n")); err != nil {
acc = errors.Join(acc, err)
} else {
return nil
}
return registry.Key(k).SetStringValue(field, value)
}

return acc
// WriteMultilineValue writes the provided multi-line string value into the specified field of key k.
func (Windows) WriteMultilineValue(k uintptr, field string, value string) error {
return registry.Key(k).SetStringsValue(field, strings.Split(value, "\n"))
}
Loading