Enhanced rollback description length

Signed-off-by: MrJack <36191829+biagiopietro@users.noreply.github.com>
pull/31580/head
MrJack 1 month ago
parent 6df3f3a593
commit 9fee687b40

@ -31,6 +31,8 @@ import (
"helm.sh/helm/v4/pkg/storage/driver"
)
const MaxDescriptionLength = 256
// Rollback is the action for rolling back to a given release.
//
// It provides the implementation of 'helm rollback'.
@ -118,6 +120,10 @@ func (r *Rollback) prepareRollback(name string) (*release.Release, *release.Rele
return nil, nil, false, errInvalidRevision
}
if len(r.Description) > MaxDescriptionLength {
return nil, nil, false, fmt.Errorf("description must be %d characters or less, got %d", MaxDescriptionLength, len(r.Description))
}
currentReleasei, err := r.cfg.Releases.Last(name)
if err != nil {
return nil, nil, false, err

@ -20,6 +20,7 @@ import (
"context"
"errors"
"io"
"strings"
"testing"
"github.com/stretchr/testify/assert"
@ -212,3 +213,66 @@ func TestRollback_EmptyDescription(t *testing.T) {
// Verify the default description was used for empty string
is.Equal("Rollback to 1", newRelease.Info.Description)
}
func TestRollback_DescriptionTooLong(t *testing.T) {
req := require.New(t)
rollAction := rollbackAction(t)
rel1 := releaseStub()
rel1.Name = "test-release-desc-long"
rel1.Version = 1
rel1.Info.Status = common.StatusSuperseded
rel1.ApplyMethod = "csa"
req.NoError(rollAction.cfg.Releases.Create(rel1))
rel2 := releaseStub()
rel2.Name = "test-release-desc-long"
rel2.Version = 2
rel2.Info.Status = common.StatusDeployed
rel2.ApplyMethod = "csa"
req.NoError(rollAction.cfg.Releases.Create(rel2))
rollAction.Description = strings.Repeat("a", MaxDescriptionLength+1)
rollAction.Version = 1
rollAction.ServerSideApply = "false"
err := rollAction.Run("test-release-desc-long")
req.Error(err)
req.Contains(err.Error(), "description must be")
}
func TestRollback_DescriptionAtMaxLength(t *testing.T) {
is := assert.New(t)
req := require.New(t)
rollAction := rollbackAction(t)
rel1 := releaseStub()
rel1.Name = "test-release-desc-max"
rel1.Version = 1
rel1.Info.Status = common.StatusSuperseded
rel1.ApplyMethod = "csa"
req.NoError(rollAction.cfg.Releases.Create(rel1))
rel2 := releaseStub()
rel2.Name = "test-release-desc-max"
rel2.Version = 2
rel2.Info.Status = common.StatusDeployed
rel2.ApplyMethod = "csa"
req.NoError(rollAction.cfg.Releases.Create(rel2))
rollAction.Description = strings.Repeat("a", MaxDescriptionLength)
rollAction.Version = 1
rollAction.ServerSideApply = "false"
err := rollAction.Run("test-release-desc-max")
req.NoError(err)
newReleasei, err := rollAction.cfg.Releases.Get("test-release-desc-max", 3)
req.NoError(err)
newRelease, err := releaserToV1Release(newReleasei)
req.NoError(err)
is.Equal(strings.Repeat("a", MaxDescriptionLength), newRelease.Info.Description)
}

@ -38,9 +38,6 @@ second is a revision (version) number. If this argument is omitted or set to
To see revision numbers, run 'helm history RELEASE'.
`
// maxDescriptionLength is the maximum length allowed for a rollback description
const maxDescriptionLength = 256
func newRollbackCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
client := action.NewRollback(cfg)
@ -70,8 +67,8 @@ func newRollbackCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
}
// Validate description length
if len(client.Description) > maxDescriptionLength {
return fmt.Errorf("description must be %d characters or less, got %d", maxDescriptionLength, len(client.Description))
if len(client.Description) > action.MaxDescriptionLength {
return fmt.Errorf("description must be %d characters or less, got %d", action.MaxDescriptionLength, len(client.Description))
}
dryRunStrategy, err := cmdGetDryRunFlagStrategy(cmd, false)
@ -90,7 +87,7 @@ func newRollbackCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
}
f := cmd.Flags()
f.StringVar(&client.Description, "description", "", fmt.Sprintf("add a custom description for the rollback (max %d characters)", maxDescriptionLength))
f.StringVar(&client.Description, "description", "", fmt.Sprintf("add a custom description for the rollback (max %d characters)", action.MaxDescriptionLength))
f.BoolVar(&client.ForceReplace, "force-replace", false, "force resource updates by replacement")
f.BoolVar(&client.ForceReplace, "force", false, "deprecated")
f.MarkDeprecated("force", "use --force-replace instead")

@ -22,6 +22,7 @@ import (
"strings"
"testing"
"helm.sh/helm/v4/pkg/action"
chart "helm.sh/helm/v4/pkg/chart/v2"
"helm.sh/helm/v4/pkg/release/common"
release "helm.sh/helm/v4/pkg/release/v1"
@ -198,13 +199,12 @@ func TestRollbackDescriptionTooLong(t *testing.T) {
}
}
// Create a description that exceeds the 512 character limit
longDescription := strings.Repeat("a", 513)
longDescription := strings.Repeat("a", action.MaxDescriptionLength+1)
_, _, err := executeActionCommandC(storage, fmt.Sprintf("rollback %s 1 --description '%s'", releaseName, longDescription))
if err == nil {
t.Error("expected error for description exceeding max length, got success")
}
if err != nil && !strings.Contains(err.Error(), "description must be 512 characters or less") {
if err != nil && !strings.Contains(err.Error(), fmt.Sprintf("description must be %d characters or less", action.MaxDescriptionLength)) {
t.Errorf("expected error about description length, got: %v", err)
}
}

Loading…
Cancel
Save