From 07395ffce53035b44a79ee2ce0163b840e1f3bb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edu=20G=C3=B3mez=20Escandell?= Date: Fri, 20 Oct 2023 13:00:41 +0200 Subject: [PATCH 1/3] Landscape config is now a a multi-line string --- windows-agent/internal/config/config.go | 3 ++- .../config/registry/registry_windows.go | 26 +++++-------------- 2 files changed, 8 insertions(+), 21 deletions(-) diff --git a/windows-agent/internal/config/config.go b/windows-agent/internal/config/config.go index 514ebdf14..3c36e451f 100644 --- a/windows-agent/internal/config/config.go +++ b/windows-agent/internal/config/config.go @@ -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 @@ -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) } diff --git a/windows-agent/internal/config/registry/registry_windows.go b/windows-agent/internal/config/registry/registry_windows.go index 128ff3561..26c922fc3 100644 --- a/windows-agent/internal/config/registry/registry_windows.go +++ b/windows-agent/internal/config/registry/registry_windows.go @@ -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")) } From cccb54fad222acb7113c47365a9b249e64d16747 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edu=20G=C3=B3mez=20Escandell?= Date: Fri, 20 Oct 2023 13:00:52 +0200 Subject: [PATCH 2/3] Update mocks --- .../internal/config/registry/registry_linux.go | 7 ++++++- .../internal/config/registry/registry_mock.go | 13 +++++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/windows-agent/internal/config/registry/registry_linux.go b/windows-agent/internal/config/registry/registry_linux.go index 27eff8292..6f80eebba 100644 --- a/windows-agent/internal/config/registry/registry_linux.go +++ b/windows-agent/internal/config/registry/registry_linux.go @@ -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") +} diff --git a/windows-agent/internal/config/registry/registry_mock.go b/windows-agent/internal/config/registry/registry_mock.go index 15e692c17..9f121127c 100644 --- a/windows-agent/internal/config/registry/registry_mock.go +++ b/windows-agent/internal/config/registry/registry_mock.go @@ -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") } @@ -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 From 2ebb11ca73256e4ba83500b2a9c179d3a020c07e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edu=20G=C3=B3mez=20Escandell?= Date: Fri, 20 Oct 2023 13:01:18 +0200 Subject: [PATCH 3/3] Update workspace sum --- go.work.sum | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/go.work.sum b/go.work.sum index 6d0d515f9..72301c1f7 100644 --- a/go.work.sum +++ b/go.work.sum @@ -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= @@ -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=