From 518c5795f4078988b63a9912fd9690afd71612bc Mon Sep 17 00:00:00 2001 From: decentral1se Date: Wed, 24 Jul 2024 16:07:08 +0200 Subject: [PATCH] fix: avoid overwriting non version env vars See https://git.coopcloud.tech/coop-cloud/organising/issues/630 --- cli/app/deploy.go | 2 +- cli/app/new.go | 2 +- cli/app/rollback.go | 2 +- cli/app/upgrade.go | 2 +- pkg/app/app.go | 12 ++++++--- pkg/envfile/envfile_test.go | 27 ++++++++++++++----- pkg/test/test.go | 5 ++-- .../servers/evil.corp/ecloud.env | 1 + 8 files changed, 36 insertions(+), 17 deletions(-) diff --git a/cli/app/deploy.go b/cli/app/deploy.go index 7802dd0c..8e8867a1 100644 --- a/cli/app/deploy.go +++ b/cli/app/deploy.go @@ -258,7 +258,7 @@ EXAMPLE: app.Recipe.Version = chaosVersion } log.Debugf("choosing %s as version to save to env file", app.Recipe.Version) - if err := app.WriteRecipeVersion(app.Recipe.Version); err != nil { + if err := app.WriteRecipeVersion(app.Recipe.Version, false); err != nil { log.Fatalf("writing new recipe version in env file: %s", err) } diff --git a/cli/app/new.go b/cli/app/new.go index bc2614c9..67d586c7 100644 --- a/cli/app/new.go +++ b/cli/app/new.go @@ -219,7 +219,7 @@ var appNewCommand = cli.Command{ } log.Debugf("choosing %s as version to save to env file", version) - if err := app.WriteRecipeVersion(version); err != nil { + if err := app.WriteRecipeVersion(version, false); err != nil { log.Fatalf("writing new recipe version in env file: %s", err) } diff --git a/cli/app/rollback.go b/cli/app/rollback.go index 879af6a0..bb714993 100644 --- a/cli/app/rollback.go +++ b/cli/app/rollback.go @@ -225,7 +225,7 @@ EXAMPLE: app.Recipe.Version = chosenDowngrade log.Debugf("choosing %s as version to save to env file", app.Recipe.Version) - if err := app.WriteRecipeVersion(app.Recipe.Version); err != nil { + if err := app.WriteRecipeVersion(app.Recipe.Version, false); err != nil { log.Fatalf("writing new recipe version in env file: %s", err) } diff --git a/cli/app/upgrade.go b/cli/app/upgrade.go index 5e36b584..807110af 100644 --- a/cli/app/upgrade.go +++ b/cli/app/upgrade.go @@ -286,7 +286,7 @@ EXAMPLE: app.Recipe.Version = chosenUpgrade log.Debugf("choosing %s as version to save to env file", app.Recipe.Version) - if err := app.WriteRecipeVersion(app.Recipe.Version); err != nil { + if err := app.WriteRecipeVersion(app.Recipe.Version, false); err != nil { log.Fatalf("writing new recipe version in env file: %s", err) } diff --git a/pkg/app/app.go b/pkg/app/app.go index b11047ae..25c00a99 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -569,7 +569,7 @@ func ReadAbraShCmdNames(abraSh string) ([]string, error) { return cmdNames, nil } -func (a App) WriteRecipeVersion(version string) error { +func (a App) WriteRecipeVersion(version string, dryRun bool) error { file, err := os.Open(a.Path) if err != nil { return err @@ -581,7 +581,7 @@ func (a App) WriteRecipeVersion(version string) error { lines := []string{} for scanner.Scan() { line := scanner.Text() - if !strings.Contains(line, "RECIPE=") && !strings.Contains(line, "TYPE") { + if !strings.HasPrefix(line, "RECIPE=") && !strings.HasPrefix(line, "TYPE=") { lines = append(lines, line) continue } @@ -606,8 +606,12 @@ func (a App) WriteRecipeVersion(version string) error { log.Fatal(err) } - if err := os.WriteFile(a.Path, []byte(strings.Join(lines, "\n")), os.ModePerm); err != nil { - log.Fatal(err) + if !dryRun { + if err := os.WriteFile(a.Path, []byte(strings.Join(lines, "\n")), os.ModePerm); err != nil { + log.Fatal(err) + } + } else { + log.Debugf("skipping writing version %s because dry run", version) } if !skipped { diff --git a/pkg/envfile/envfile_test.go b/pkg/envfile/envfile_test.go index ffab3ee4..3fca108e 100644 --- a/pkg/envfile/envfile_test.go +++ b/pkg/envfile/envfile_test.go @@ -11,6 +11,7 @@ import ( "coopcloud.tech/abra/pkg/envfile" "coopcloud.tech/abra/pkg/recipe" testPkg "coopcloud.tech/abra/pkg/test" + "github.com/stretchr/testify/assert" ) func TestGetAllFoldersInDirectory(t *testing.T) { @@ -43,13 +44,7 @@ func TestReadEnv(t *testing.T) { t.Fatal(err) } if !reflect.DeepEqual(env, testPkg.ExpectedAppEnv) { - t.Fatalf( - "did not get expected application settings. Expected: DOMAIN=%s RECIPE=%s; Got: DOMAIN=%s RECIPE=%s", - testPkg.ExpectedAppEnv["DOMAIN"], - testPkg.ExpectedAppEnv["RECIPE"], - env["DOMAIN"], - env["RECIPE"], - ) + t.Fatal("did not get expected application settings") } } @@ -228,3 +223,21 @@ func TestEnvVarModifiersIncluded(t *testing.T) { } } } + +func TestNoOverwriteNonVersionEnvVars(t *testing.T) { + app, err := appPkg.GetApp(testPkg.ExpectedAppFiles, testPkg.AppName) + if err != nil { + t.Fatal(err) + } + + if err := app.WriteRecipeVersion("1.3.12", true); err != nil { + t.Fatal(err) + } + + app, err = appPkg.GetApp(testPkg.ExpectedAppFiles, testPkg.AppName) + if err != nil { + t.Fatal(err) + } + + assert.NotEqual(t, app.Env["SMTP_AUTHTYPE"], "login:1.3.12") +} diff --git a/pkg/test/test.go b/pkg/test/test.go index 1109a5dd..92e88656 100644 --- a/pkg/test/test.go +++ b/pkg/test/test.go @@ -27,8 +27,9 @@ var ( ) var ExpectedAppEnv = envfile.AppEnv{ - "DOMAIN": "ecloud.evil.corp", - "RECIPE": "ecloud", + "DOMAIN": "ecloud.evil.corp", + "RECIPE": "ecloud", + "SMTP_AUTHTYPE": "login", } var ExpectedApp = appPkg.App{ diff --git a/tests/resources/valid_abra_config/servers/evil.corp/ecloud.env b/tests/resources/valid_abra_config/servers/evil.corp/ecloud.env index feb7610c..8c6b5068 100644 --- a/tests/resources/valid_abra_config/servers/evil.corp/ecloud.env +++ b/tests/resources/valid_abra_config/servers/evil.corp/ecloud.env @@ -1,2 +1,3 @@ RECIPE=ecloud DOMAIN=ecloud.evil.corp +SMTP_AUTHTYPE=login