-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from etsy/force-state
feat: not allow state operations without a TF_DEMUX_ALLOW_STATE_COMMANDS environment
- Loading branch information
Showing
7 changed files
with
168 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package wrapper | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/Masterminds/semver/v3" | ||
) | ||
|
||
func checkStateCommand(args []string, version *semver.Version) error { | ||
versionImport, _ := semver.NewConstraint(">= 1.5.0") | ||
versionMoved, _ := semver.NewConstraint(">= 1.1.0") | ||
versionRemoved, _ := semver.NewConstraint(">= 1.7.0") | ||
STATE_COMMAND_VAR := "TF_DEMUX_ALLOW_STATE_COMMANDS" | ||
|
||
errorMsg := func(command string, suggestion string) error { | ||
return fmt.Errorf("refusing to execute '%s' command - use a '%s' configuration block instead, or set %s=true", command, suggestion, STATE_COMMAND_VAR) | ||
} | ||
|
||
if allowStateCommand(STATE_COMMAND_VAR) { | ||
return nil | ||
} | ||
|
||
if checkArgsExists(args, "import") >= 0 && | ||
versionImport.Check(version) { | ||
return errorMsg("import", "import") | ||
} | ||
|
||
if checkArgsExists(args, "state") >= 0 && | ||
checkArgsExists(args, "mv") >= 0 && | ||
versionMoved.Check(version) { | ||
return errorMsg("state mv", "moved") | ||
} | ||
|
||
if checkArgsExists(args, "state") >= 0 && | ||
checkArgsExists(args, "rm") >= 0 && | ||
versionRemoved.Check(version) { | ||
return errorMsg("state rm", "removed") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func checkArgsExists(args []string, cmd string) int { | ||
for i, arg := range args { | ||
if arg == cmd { | ||
return i | ||
} | ||
} | ||
return -1 | ||
} | ||
|
||
func allowStateCommand(envVarName string) bool { | ||
validValues := []string{"1", "true", "yes"} | ||
value := strings.ToLower(os.Getenv(envVarName)) | ||
for _, valid := range validValues { | ||
if value == valid { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package wrapper | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/Masterminds/semver/v3" | ||
) | ||
|
||
func TestCheckStateCommand(t *testing.T) { | ||
STATE_COMMAND_VAR := "TF_DEMUX_ALLOW_STATE_COMMANDS" | ||
t.Run("Valid state import command with TF_DEMUX_ALLOW_STATE_COMMANDS on 1.5.0", func(t *testing.T) { | ||
args := []string{"import", "--force"} | ||
version, _ := semver.NewVersion("1.5.0") | ||
os.Setenv(STATE_COMMAND_VAR, "true") | ||
err := checkStateCommand(args, version) | ||
if err != nil { | ||
t.Errorf("Expected no error, got: %v", err) | ||
} | ||
}) | ||
|
||
t.Run("Valid state import command without TF_DEMUX_ALLOW_STATE_COMMANDS on 1.4.7", func(t *testing.T) { | ||
args := []string{"import"} | ||
version, _ := semver.NewVersion("1.4.7") | ||
os.Setenv(STATE_COMMAND_VAR, "true") | ||
err := checkStateCommand(args, version) | ||
if err != nil { | ||
t.Errorf("Expected no error, got: %v", err) | ||
} | ||
}) | ||
|
||
t.Run("Invalid state import command without TF_DEMUX_ALLOW_STATE_COMMANDS on 1.5.0", func(t *testing.T) { | ||
args := []string{"import"} | ||
version, _ := semver.NewVersion("1.6.0") | ||
os.Setenv(STATE_COMMAND_VAR, "") | ||
err := checkStateCommand(args, version) | ||
if err == nil { | ||
t.Errorf("Expected error, got: %v", err) | ||
} | ||
}) | ||
|
||
t.Run("Valid state mv command with TF_DEMUX_ALLOW_STATE_COMMANDS on 1.6.0", func(t *testing.T) { | ||
args := []string{"state", "mv", "--force"} | ||
version, _ := semver.NewVersion("1.6.0") | ||
os.Setenv(STATE_COMMAND_VAR, "true") | ||
err := checkStateCommand(args, version) | ||
if err != nil { | ||
t.Errorf("Expected no error, got: %v", err) | ||
} | ||
}) | ||
} | ||
|
||
func TestCheckArgsExists(t *testing.T) { | ||
t.Run("Check 'import --force' command", func(t *testing.T) { | ||
args := []string{"import", "--force"} | ||
result := checkArgsExists(args, "import") | ||
if result != 0 { | ||
t.Errorf("Expected 0, got: %v", result) | ||
} | ||
result = checkArgsExists(args, "--force") | ||
if result != 1 { | ||
t.Errorf("Expected 1, got: %v", result) | ||
} | ||
}) | ||
|
||
t.Run("Check 'state moved' command", func(t *testing.T) { | ||
args := []string{"state", "mv"} | ||
result := checkArgsExists(args, "state") | ||
if result != 0 { | ||
t.Errorf("Expected 0, got: %v", result) | ||
} | ||
result = checkArgsExists(args, "mv") | ||
if result != 1 { | ||
t.Errorf("Expected 1, got: %v", result) | ||
} | ||
result = checkArgsExists(args, "--force") | ||
if result != -1 { | ||
t.Errorf("Expected -1, got: %v", result) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters