Skip to content

Commit 2e2665a

Browse files
xbhouseMichaelMraka
authored andcommitted
RHINENG-22863: limit systems to 1000 in update and delete template systems apis
1 parent af01b40 commit 2e2665a

5 files changed

Lines changed: 93 additions & 1 deletion

File tree

manager/controllers/template_systems_delete.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ func TemplateSystemsDeleteHandler(c *gin.Context) {
3232
return
3333
}
3434

35+
if err := checkTemplateSystemsLimit(len(req.Systems), c); err != nil {
36+
return
37+
}
38+
3539
db := middlewares.DBFromContext(c)
3640

3741
err := checkTemplateSystems(c, db, account, nil, req.Systems, groups)

manager/controllers/template_systems_delete_test.go

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,19 @@ package controllers
33
import (
44
"app/base/core"
55
"app/base/database"
6+
"app/base/utils"
67
"bytes"
8+
"fmt"
79
"net/http"
10+
"net/http/httptest"
811
"testing"
912

1013
"github.com/bytedance/sonic"
14+
"github.com/google/uuid"
1115
"github.com/stretchr/testify/assert"
1216
)
1317

14-
func testTemplateSystemsDelete(t *testing.T, body TemplateSystemsUpdateRequest, status int) {
18+
func testTemplateSystemsDelete(t *testing.T, body TemplateSystemsUpdateRequest, status int) *httptest.ResponseRecorder {
1519
bodyJSON, err := sonic.Marshal(&body)
1620
if err != nil {
1721
panic(err)
@@ -21,6 +25,7 @@ func testTemplateSystemsDelete(t *testing.T, body TemplateSystemsUpdateRequest,
2125
TemplateSystemsDeleteHandler, templateAccount)
2226

2327
assert.Equal(t, status, w.Code)
28+
return w
2429
}
2530

2631
func TestTemplateSystemsDeleteDefault(t *testing.T) {
@@ -60,3 +65,42 @@ func TestTemplateSystemsDeleteInvalid(t *testing.T) {
6065
testTemplateSystemsDelete(t, TemplateSystemsUpdateRequest{
6166
Systems: []string{"c0ffeec0-ffee-c0ff-eec0-ffeec0ffee00"}}, http.StatusNotFound)
6267
}
68+
69+
func TestTemplateSystemsDeleteTooManySystems(t *testing.T) {
70+
core.SetupTest(t)
71+
72+
systems := make([]string, 0, TemplateSystemsUpdateLimit+1)
73+
for i := 0; i < TemplateSystemsUpdateLimit; i++ {
74+
systems = append(systems, uuid.NewString())
75+
}
76+
77+
database.CreateTemplate(t, templateAccount, templateUUID, systems)
78+
defer database.DeleteTemplate(t, templateAccount, templateUUID)
79+
80+
// Add one more system to the template so we can try to delete more than the limit
81+
additionalSystem := "00000000-0000-0000-0000-000000000004"
82+
putBody := TemplateSystemsUpdateRequest{
83+
Systems: []string{additionalSystem},
84+
}
85+
86+
putBodyJSON, err := sonic.Marshal(&putBody)
87+
if err != nil {
88+
panic(err)
89+
}
90+
91+
w := CreateRequestRouterWithParams("PUT", templatePath, templateUUID, "", bytes.NewBuffer(putBodyJSON), "",
92+
TemplateSystemsUpdateHandler, templateAccount)
93+
assert.Equal(t, http.StatusOK, w.Code)
94+
95+
systems = append(systems, additionalSystem)
96+
97+
req := TemplateSystemsUpdateRequest{
98+
Systems: systems,
99+
}
100+
101+
res := testTemplateSystemsDelete(t, req, http.StatusBadRequest)
102+
103+
var errResp utils.ErrorResponse
104+
CheckResponse(t, res, http.StatusBadRequest, &errResp)
105+
assert.Equal(t, fmt.Sprintf("Cannot process more than %d systems at once", TemplateSystemsUpdateLimit), errResp.Error)
106+
}

manager/controllers/template_systems_update.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
var candlepinClient = candlepin.CreateCandlepinClient()
2626

2727
const InvalidInventoryIDsErr = "invalid list of inventory IDs"
28+
const TemplateSystemsUpdateLimit = 1000
2829

2930
type TemplateSystemsUpdateRequest struct {
3031
// List of inventory IDs to have templates removed
@@ -61,6 +62,10 @@ func TemplateSystemsUpdateHandler(c *gin.Context) {
6162
return
6263
}
6364

65+
if err := checkTemplateSystemsLimit(len(req.Systems), c); err != nil {
66+
return
67+
}
68+
6469
db := middlewares.DBFromContext(c)
6570
template, err := getTemplate(c, db, account, templateUUID)
6671
if err != nil {

manager/controllers/template_systems_update_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"net/http"
1111
"testing"
1212

13+
"github.com/bytedance/sonic"
14+
"github.com/google/uuid"
1315
"github.com/stretchr/testify/assert"
1416
)
1517

@@ -245,3 +247,30 @@ func TestUpdateTemplateSystemsCandlepin404(t *testing.T) {
245247
database.CheckTemplateSystems(t, templateAccount, templateUUID,
246248
[]string{"00000000-0000-0000-0000-000000000007"})
247249
}
250+
251+
func TestUpdateTemplateTooManySystems(t *testing.T) {
252+
core.SetupTest(t)
253+
254+
database.CreateTemplate(t, templateAccount, templateUUID, templateSystems)
255+
defer database.DeleteTemplate(t, templateAccount, templateUUID)
256+
257+
systems := make([]string, 0, TemplateSystemsUpdateLimit+1)
258+
for i := 0; i < TemplateSystemsUpdateLimit+1; i++ {
259+
systems = append(systems, uuid.NewString())
260+
}
261+
body := map[string][]string{
262+
"systems": systems,
263+
}
264+
265+
bodyJSON, err := sonic.Marshal(body)
266+
if err != nil {
267+
panic(err)
268+
}
269+
270+
w := CreateRequestRouterWithParams("PUT", templatePath, templateUUID, "", bytes.NewBuffer(bodyJSON), "",
271+
TemplateSystemsUpdateHandler, templateAccount)
272+
273+
var errResp utils.ErrorResponse
274+
CheckResponse(t, w, http.StatusBadRequest, &errResp)
275+
assert.Equal(t, fmt.Sprintf("Cannot process more than %d systems at once", TemplateSystemsUpdateLimit), errResp.Error)
276+
}

manager/controllers/utils.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,3 +588,13 @@ func isFilterInURLValid(c *gin.Context) bool {
588588
}
589589
return true
590590
}
591+
592+
func checkTemplateSystemsLimit(numSystems int, c *gin.Context) error {
593+
if numSystems > TemplateSystemsUpdateLimit {
594+
msg := fmt.Sprintf("Cannot process more than %d systems at once", TemplateSystemsUpdateLimit)
595+
err := errors.New(msg)
596+
utils.LogAndRespBadRequest(c, err, msg)
597+
return err
598+
}
599+
return nil
600+
}

0 commit comments

Comments
 (0)