Skip to content

Commit

Permalink
api: validate scale count value is not negative.
Browse files Browse the repository at this point in the history
An operator could submit a scale request including a negative count
value. This negative value caused the Nomad server to panic. The
fix adds validation to the submitted count, returning an error to
the caller if it is negative.
  • Loading branch information
jrasell committed May 8, 2020
1 parent f5c84fc commit 3c6235b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
3 changes: 3 additions & 0 deletions nomad/job_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,9 @@ func (j *Job) Scale(args *structs.JobScaleRequest, reply *structs.JobRegisterRes
if args.Error && args.Count != nil {
return structs.NewErrRPCCoded(400, "scaling action should not contain count if error is true")
}
if args.Count != nil && *args.Count < 0 {
return structs.NewErrRPCCoded(400, "scaling action count can't be negative")
}

// Check for submit-job permissions
if aclObj, err := j.srv.ResolveToken(args.AuthToken); err != nil {
Expand Down
30 changes: 30 additions & 0 deletions nomad/job_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5617,6 +5617,36 @@ func TestJobEndpoint_Scale_NoEval(t *testing.T) {
require.Greater(eventsIndex, jobCreateIndex)
}

func TestJobEndpoint_InvalidCount(t *testing.T) {
t.Parallel()
require := require.New(t)

s1, cleanupS1 := TestServer(t, nil)
defer cleanupS1()
codec := rpcClient(t, s1)
testutil.WaitForLeader(t, s1.RPC)
state := s1.fsm.State()

job := mock.Job()
err := state.UpsertJob(1000, job)
require.Nil(err)

scale := &structs.JobScaleRequest{
JobID: job.ID,
Target: map[string]string{
structs.ScalingTargetGroup: job.TaskGroups[0].Name,
},
Count: helper.Int64ToPtr(int64(-1)),
WriteRequest: structs.WriteRequest{
Region: "global",
Namespace: job.Namespace,
},
}
var resp structs.JobRegisterResponse
err = msgpackrpc.CallWithCodec(codec, "Job.Scale", scale, &resp)
require.Error(err)
}

func TestJobEndpoint_GetScaleStatus(t *testing.T) {
t.Parallel()
require := require.New(t)
Expand Down

0 comments on commit 3c6235b

Please sign in to comment.