diff --git a/configuration/raw.go b/configuration/raw.go index 3659c6de..e89d7e49 100644 --- a/configuration/raw.go +++ b/configuration/raw.go @@ -180,7 +180,7 @@ func (c *client) PostRawConfiguration(config *string, version int64, skipVersion w := bufio.NewWriter(tmp) if !skipVersionCheck { - _, _ = w.WriteString(fmt.Sprintf("# _version=%v\n%v", version, *config)) + _, _ = w.WriteString(fmt.Sprintf("# _version=%v\n%v", version, c.dropVersionFromRaw(*config))) } else { _, _ = w.WriteString(*config) } @@ -204,6 +204,29 @@ func (c *client) PostRawConfiguration(config *string, version int64, skipVersion return nil } +// dropVersionFromRaw is used when force pushing a raw configuration with version check: +// if the provided user input has already a version metadata it must be withdrawn. +func (c *client) dropVersionFromRaw(input string) *string { + scanner := bufio.NewScanner(strings.NewReader(input)) + + var sanitized strings.Builder + + for scanner.Scan() { + t := scanner.Bytes() + + if bytes.HasPrefix(t, []byte("# _version=")) { + continue + } + + sanitized.Write(t) + sanitized.WriteByte('\n') + } + + str := sanitized.String() + + return &str +} + func (c *client) validateConfigFile(confFile string) error { // #nosec G204 cmd := exec.Command(c.Haproxy) diff --git a/configuration/version_test.go b/configuration/version_test.go index c819c307..b81c3c8e 100644 --- a/configuration/version_test.go +++ b/configuration/version_test.go @@ -18,6 +18,9 @@ package configuration import ( "io/ioutil" "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func generateConfig(config string) (string, error) { @@ -32,6 +35,35 @@ func generateConfig(config string) (string, error) { return f.Name(), nil } +func TestClient_PostRawConfiguration(t *testing.T) { + fVersion, err := generateConfig("") + require.NoError(t, err, "generateConfig") + + t.Cleanup(func() { + assert.NoError(t, deleteTestFile(fVersion), "clean-up") + }) + + c, err := prepareClient(fVersion) + require.NoError(t, err, "prepareClient") + + v, vErr := c.GetVersion("") + assert.Equal(t, int64(1), v, "initialized configuration must be 1") + // The user is providing a raw configuration with a wrong version such as metadata: + // this must be ignored and removed by Client Native + configWithVersion := ` +# _version=123 +global + daemon` + + err = c.PostRawConfiguration(&configWithVersion, 1, false) + require.NoError(t, err, "PostRawConfiguration") + + v, vErr = c.GetVersion("") + require.NoError(t, vErr, "GetVersion") + + assert.Equal(t, int64(2), v, "123 should be dropped, and version bumped") +} + func TestClient_GetConfigurationVersion(t *testing.T) { configWithVersion := `# _version=10 global diff --git a/go.mod b/go.mod index 3ba55f6a..8968a40e 100644 --- a/go.mod +++ b/go.mod @@ -13,7 +13,7 @@ require ( github.com/google/go-cmp v0.6.0 github.com/google/renameio v1.0.1 github.com/google/uuid v1.6.0 - github.com/haproxytech/config-parser/v5 v5.1.6-0.20240923121611-21c538dab47a + github.com/haproxytech/config-parser/v5 v5.1.6 github.com/json-iterator/go v1.1.12 github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 github.com/mitchellh/mapstructure v1.5.0 diff --git a/go.sum b/go.sum index b06e9cea..bb914278 100644 --- a/go.sum +++ b/go.sum @@ -32,8 +32,8 @@ github.com/google/renameio v1.0.1 h1:Lh/jXZmvZxb0BBeSY5VKEfidcbcbenKjZFzM/q0fSeU github.com/google/renameio v1.0.1/go.mod h1:t/HQoYBZSsWSNK35C6CO/TpPLDVWvxOHboWUAweKUpk= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/haproxytech/config-parser/v5 v5.1.6-0.20240923121611-21c538dab47a h1:zrHz2vatWGa6lHVSt+daH8erytvuKhre2EFpXXopJa0= -github.com/haproxytech/config-parser/v5 v5.1.6-0.20240923121611-21c538dab47a/go.mod h1:16+1AbS+AvMZkDScIhergz2dqecQuEmjwV4Xt5ncS9s= +github.com/haproxytech/config-parser/v5 v5.1.6 h1:64KHffpOGoyjFuMmq9dLOvcRCEzg/2GAnPwhmue1uVY= +github.com/haproxytech/config-parser/v5 v5.1.6/go.mod h1:16+1AbS+AvMZkDScIhergz2dqecQuEmjwV4Xt5ncS9s= github.com/haproxytech/go-logger v1.1.0 h1:HgGtYaI1ApkvbQdsm7f9AzQQoxTB7w37criTflh7IQE= github.com/haproxytech/go-logger v1.1.0/go.mod h1:OekUd8HCb7ubxMplzHUPBTHNxZmddOWfOjWclZsqIeM= github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=