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

Config: Fix config version downgrade #1770

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
5 changes: 4 additions & 1 deletion cmd/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ func main() {
if cmd == "encrypt" && isEncrypted {
fatal("Error: File is already encrypted")
}
if cmd == "decrypt" && !isEncrypted {
fatal("Error: File is already decrypted")
}

if len(key) == 0 && (isEncrypted || cmd == "encrypt") {
if key, err = config.PromptForConfigKey(cmd == "encrypt"); err != nil {
Expand All @@ -82,7 +85,7 @@ func main() {
usage(fs)
os.Exit(3)
}
version = -1
version = versions.LatestVersion
}
if data, err = versions.Manager.Deploy(context.Background(), data, version); err != nil {
fatal("Unable to " + cmd + " config; Error: " + err.Error())
Expand Down
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -1512,7 +1512,7 @@ func (c *Config) readConfig(d io.Reader) error {
}
}

if j, err = versions.Manager.Deploy(context.Background(), j, -1); err != nil {
if j, err = versions.Manager.Deploy(context.Background(), j, versions.LatestVersion); err != nil {
return err
}

Expand Down
8 changes: 5 additions & 3 deletions config/versions/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
"github.com/thrasher-corp/gocryptotrader/common"
)

const LatestVersion = -1

Check failure on line 29 in config/versions/versions.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported const LatestVersion should have comment or be unexported (revive)

var (
errMissingVersion = errors.New("missing version")
errVersionIncompatible = errors.New("version does not implement ConfigVersion or ExchangeVersion")
Expand Down Expand Up @@ -59,7 +61,7 @@
var Manager = &manager{}

// Deploy upgrades or downgrades the config between versions
// version param -1 defaults to the latest version
// version param LatestVersion defaults to the latest version
gbjk marked this conversation as resolved.
Show resolved Hide resolved
// Prints an error an exits if the config file version or version param is not registered
func (m *manager) Deploy(ctx context.Context, j []byte, version int) ([]byte, error) {
if err := m.checkVersions(); err != nil {
Expand All @@ -72,7 +74,7 @@
}

target := latest
if version != -1 {
if version != LatestVersion {
target = version
}

Expand All @@ -83,7 +85,7 @@
current := int(current64)
switch {
case errors.Is(err, jsonparser.KeyPathNotFoundError):
current = -1
current = LatestVersion
case err != nil:
return j, fmt.Errorf("%w `version`: %w", common.ErrGettingField, err)
case target == current:
Expand Down
20 changes: 10 additions & 10 deletions config/versions/versions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,43 +13,43 @@ import (
func TestDeploy(t *testing.T) {
t.Parallel()
m := manager{}
_, err := m.Deploy(context.Background(), []byte(``), -1)
_, err := m.Deploy(context.Background(), []byte(``), LatestVersion)
assert.ErrorIs(t, err, errNoVersions)

m.registerVersion(1, &TestVersion1{})
_, err = m.Deploy(context.Background(), []byte(``), -1)
_, err = m.Deploy(context.Background(), []byte(``), LatestVersion)
require.ErrorIs(t, err, errVersionIncompatible)

m = manager{}

m.registerVersion(0, &Version0{})
_, err = m.Deploy(context.Background(), []byte(`not an object`), -1)
_, err = m.Deploy(context.Background(), []byte(`not an object`), LatestVersion)
require.ErrorIs(t, err, jsonparser.KeyPathNotFoundError, "Must throw the correct error trying to add version to bad json")
require.ErrorIs(t, err, common.ErrSettingField, "Must throw the correct error trying to add version to bad json")
require.ErrorContains(t, err, "version", "Must throw the correct error trying to add version to bad json")

_, err = m.Deploy(context.Background(), []byte(`{"version":"not an int"}`), -1)
_, err = m.Deploy(context.Background(), []byte(`{"version":"not an int"}`), LatestVersion)
require.ErrorIs(t, err, common.ErrGettingField, "Must throw the correct error trying to get version from bad json")

in := []byte(`{"version":0,"exchanges":[{"name":"Juan"}]}`)
j, err := m.Deploy(context.Background(), in, -1)
j, err := m.Deploy(context.Background(), in, LatestVersion)
require.NoError(t, err)
assert.Equal(t, string(in), string(j))

m.registerVersion(1, &Version1{})
j, err = m.Deploy(context.Background(), in, -1)
j, err = m.Deploy(context.Background(), in, LatestVersion)
require.NoError(t, err)
assert.Contains(t, string(j), `"version": 1`)

_, err = m.Deploy(context.Background(), j, 2)
assert.ErrorIs(t, err, errTargetVersion, "Downgrade to a unregistered version should not be allowed")

m.versions = append(m.versions, &TestVersion2{ConfigErr: true, ExchErr: false})
_, err = m.Deploy(context.Background(), j, -1)
_, err = m.Deploy(context.Background(), j, LatestVersion)
require.ErrorIs(t, err, errUpgrade)

m.versions[len(m.versions)-1] = &TestVersion2{ConfigErr: false, ExchErr: true}
_, err = m.Deploy(context.Background(), in, -1)
_, err = m.Deploy(context.Background(), in, LatestVersion)
require.Implements(t, (*ExchangeVersion)(nil), m.versions[1])
require.ErrorIs(t, err, errUpgrade)

Expand All @@ -58,7 +58,7 @@ func TestDeploy(t *testing.T) {
assert.Contains(t, string(j2), `"version": 0`, "Explicit downgrade should work correctly")

m.versions = m.versions[:1]
_, err = m.Deploy(context.Background(), j, -1)
_, err = m.Deploy(context.Background(), j, LatestVersion)
assert.ErrorIs(t, err, errConfigVersion, "Config version ahead of latest version should error")

_, err = m.Deploy(context.Background(), j, 0)
Expand All @@ -70,7 +70,7 @@ func TestDeploy(t *testing.T) {
func TestExchangeDeploy(t *testing.T) {
t.Parallel()
m := manager{}
_, err := m.Deploy(context.Background(), []byte(``), -1)
_, err := m.Deploy(context.Background(), []byte(``), LatestVersion)
assert.ErrorIs(t, err, errNoVersions)

v := &TestVersion2{}
Expand Down
Loading