From 78afe26fb89c2b80e9d829578ab4c6fa91cdbaf0 Mon Sep 17 00:00:00 2001 From: Kurtis Michie Date: Mon, 14 Nov 2022 14:42:45 -0700 Subject: [PATCH 1/7] Added catch for missing POST body when posted to profileparameters --- traffic_ops/testing/api/conf/traffic-ops-test.conf | 6 +++--- traffic_ops/testing/api/v4/profile_parameters_test.go | 5 +++++ traffic_ops/traffic_ops_golang/api/shared_handlers.go | 5 +++++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/traffic_ops/testing/api/conf/traffic-ops-test.conf b/traffic_ops/testing/api/conf/traffic-ops-test.conf index 1143dc0979..ed438375b7 100644 --- a/traffic_ops/testing/api/conf/traffic-ops-test.conf +++ b/traffic_ops/testing/api/conf/traffic-ops-test.conf @@ -15,7 +15,7 @@ "use_ims": true, "role_based_permissions": false, "trafficOps": { - "URL": "https://localhost:6443", + "URL": "https://localhost:8443", "password": "twelve", "users": { "disallowed": "disallowed", @@ -28,10 +28,10 @@ } }, "trafficOpsDB": { - "dbname": "traffic_ops_development", + "dbname": "to_test", "description": "dev database traffic_ops_development", "hostname": "localhost", - "password": "twelve12", + "password": "twelve", "port": "5432", "type": "Pg", "user": "traffic_ops" diff --git a/traffic_ops/testing/api/v4/profile_parameters_test.go b/traffic_ops/testing/api/v4/profile_parameters_test.go index 152aa75e7f..a27f49e32c 100644 --- a/traffic_ops/testing/api/v4/profile_parameters_test.go +++ b/traffic_ops/testing/api/v4/profile_parameters_test.go @@ -76,6 +76,11 @@ func TestProfileParameters(t *testing.T) { }, Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)), }, + "BAD REQUEST when EMPTY BODY PROVIDED": { + ClientSession: TOSession, + RequestBody: map[string]interface{}{}, + Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)), + }, "BAD REQUEST when MISSING PROFILEID field": { ClientSession: TOSession, RequestBody: map[string]interface{}{ diff --git a/traffic_ops/traffic_ops_golang/api/shared_handlers.go b/traffic_ops/traffic_ops_golang/api/shared_handlers.go index 3fe6e786d7..2f7e139801 100644 --- a/traffic_ops/traffic_ops_golang/api/shared_handlers.go +++ b/traffic_ops/traffic_ops_golang/api/shared_handlers.go @@ -495,6 +495,11 @@ func CreateHandler(creator Creator) http.HandlerFunc { return } + if len(data) == 0 { + HandleErr(w, r, inf.Tx.Tx, http.StatusBadRequest, errors.New("no request body supplied"), nil) + return + } + objSlice, err := parseMultipleCreates(data, objectType, inf) if err != nil { HandleErr(w, r, inf.Tx.Tx, http.StatusInternalServerError, nil, err) From 87e4686f6ee66ab582afbdcf8e8f502b460fbb4a Mon Sep 17 00:00:00 2001 From: Kurtis Michie Date: Mon, 14 Nov 2022 15:23:22 -0700 Subject: [PATCH 2/7] Added test for missing POST body to profileparameters into v3 and v5, and added change into changelog.md --- CHANGELOG.md | 1 + traffic_ops/testing/api/v3/profile_parameters_test.go | 5 +++++ traffic_ops/testing/api/v5/profile_parameters_test.go | 5 +++++ 3 files changed, 11 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index fffe51c255..2461f45a9d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/). - [#7037](https://github.com/apache/trafficcontrol/pull/7037) *Traffic Router* Uses Traffic Ops API 4.0 by default ### Fixed +- [#4428](https://github.com/apache/trafficcontrol/issues/4428) *Traffic Ops* Fixed Internal Server Error with POST to `profileparameters` when POST body is empty - [#7179](https://github.com/apache/trafficcontrol/issues/7179) *Traffic Portal* Fixed search filter for Delivery Service Table - [#7174](https://github.com/apache/trafficcontrol/issues/7174) *Traffic Portal* Fixed topologies sort (table and Delivery Service's form) - [#5970](https://github.com/apache/trafficcontrol/issues/5970) *Traffic Portal* Fixed numeric sort in Delivery Service's form for DSCP diff --git a/traffic_ops/testing/api/v3/profile_parameters_test.go b/traffic_ops/testing/api/v3/profile_parameters_test.go index 1dc360bf0a..e8460e8575 100644 --- a/traffic_ops/testing/api/v3/profile_parameters_test.go +++ b/traffic_ops/testing/api/v3/profile_parameters_test.go @@ -75,6 +75,11 @@ func TestProfileParameters(t *testing.T) { }}, Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)), }, + "BAD REQUEST when EMPTY BODY PROVIDED": { + ClientSession: TOSession, + RequestBody: []tc.ProfileParameter{{}}, + Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)), + }, "BAD REQUEST when MISSING PROFILEID field": { ClientSession: TOSession, RequestBody: []tc.ProfileParameter{{ diff --git a/traffic_ops/testing/api/v5/profile_parameters_test.go b/traffic_ops/testing/api/v5/profile_parameters_test.go index 088e10ceaf..513aefe4ba 100644 --- a/traffic_ops/testing/api/v5/profile_parameters_test.go +++ b/traffic_ops/testing/api/v5/profile_parameters_test.go @@ -76,6 +76,11 @@ func TestProfileParameters(t *testing.T) { }, Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)), }, + "BAD REQUEST when EMPTY BODY PROVIDED": { + ClientSession: TOSession, + RequestBody: map[string]interface{}{}, + Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)), + }, "BAD REQUEST when MISSING PROFILEID field": { ClientSession: TOSession, RequestBody: map[string]interface{}{ From 754a27a63cb3f8d97a73357f62d2956ee4b00c58 Mon Sep 17 00:00:00 2001 From: Kurtis Michie <49660055+kdamichie@users.noreply.github.com> Date: Tue, 15 Nov 2022 13:36:34 -0700 Subject: [PATCH 3/7] Delete traffic-ops-test.conf --- .../testing/api/conf/traffic-ops-test.conf | 40 ------------------- 1 file changed, 40 deletions(-) delete mode 100644 traffic_ops/testing/api/conf/traffic-ops-test.conf diff --git a/traffic_ops/testing/api/conf/traffic-ops-test.conf b/traffic_ops/testing/api/conf/traffic-ops-test.conf deleted file mode 100644 index ed438375b7..0000000000 --- a/traffic_ops/testing/api/conf/traffic-ops-test.conf +++ /dev/null @@ -1,40 +0,0 @@ -{ - "default": { - "logLocations": { - "debug": "stdout", - "error": "stdout", - "event": "stdout", - "info": "stdout", - "warning": "stdout" - }, - "session": { - "timeoutInSecs": 60 - }, - "includeSystemTests": false - }, - "use_ims": true, - "role_based_permissions": false, - "trafficOps": { - "URL": "https://localhost:8443", - "password": "twelve", - "users": { - "disallowed": "disallowed", - "operations": "operations", - "admin": "admin", - "federation": "federation", - "portal": "portal", - "readOnly": "readOnly", - "extension": "extension" - } - }, - "trafficOpsDB": { - "dbname": "to_test", - "description": "dev database traffic_ops_development", - "hostname": "localhost", - "password": "twelve", - "port": "5432", - "type": "Pg", - "user": "traffic_ops" - }, - "noISO": false -} From cd2b3fa4b40e0e39a53fcaa2d8b34f3d157b665e Mon Sep 17 00:00:00 2001 From: Kurtis Michie Date: Tue, 15 Nov 2022 16:03:25 -0700 Subject: [PATCH 4/7] Accidentally deleted. Meant to revert. Adding back in. --- traffic_ops/testing/api/conf/traffic-ops-test.conf | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/traffic_ops/testing/api/conf/traffic-ops-test.conf b/traffic_ops/testing/api/conf/traffic-ops-test.conf index ed438375b7..3c73e1b9f8 100644 --- a/traffic_ops/testing/api/conf/traffic-ops-test.conf +++ b/traffic_ops/testing/api/conf/traffic-ops-test.conf @@ -15,7 +15,7 @@ "use_ims": true, "role_based_permissions": false, "trafficOps": { - "URL": "https://localhost:8443", + "URL": "https://localhost:6443", "password": "twelve", "users": { "disallowed": "disallowed", @@ -28,13 +28,13 @@ } }, "trafficOpsDB": { - "dbname": "to_test", + "dbname": "traffic_ops_development", "description": "dev database traffic_ops_development", "hostname": "localhost", - "password": "twelve", + "password": "twelve12", "port": "5432", "type": "Pg", "user": "traffic_ops" }, "noISO": false -} +} \ No newline at end of file From 903e613ea6e461d80124d072751e015a9975ce71 Mon Sep 17 00:00:00 2001 From: Kurtis Michie <49660055+kdamichie@users.noreply.github.com> Date: Tue, 15 Nov 2022 16:15:25 -0700 Subject: [PATCH 5/7] Update traffic-ops-test.conf Added new line to end of file --- traffic_ops/testing/api/conf/traffic-ops-test.conf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/traffic_ops/testing/api/conf/traffic-ops-test.conf b/traffic_ops/testing/api/conf/traffic-ops-test.conf index 3c73e1b9f8..1143dc0979 100644 --- a/traffic_ops/testing/api/conf/traffic-ops-test.conf +++ b/traffic_ops/testing/api/conf/traffic-ops-test.conf @@ -37,4 +37,4 @@ "user": "traffic_ops" }, "noISO": false -} \ No newline at end of file +} From 5fc7ec284ff4a82d4c110223b66a7256fcad393e Mon Sep 17 00:00:00 2001 From: Kurtis Michie Date: Fri, 18 Nov 2022 13:38:18 -0700 Subject: [PATCH 6/7] Created one off test to check POST for an empty JSON body --- .../testing/api/v3/profile_parameters_test.go | 17 ++++++++++++----- .../testing/api/v4/profile_parameters_test.go | 17 ++++++++++++----- .../testing/api/v5/profile_parameters_test.go | 17 ++++++++++++----- 3 files changed, 36 insertions(+), 15 deletions(-) diff --git a/traffic_ops/testing/api/v3/profile_parameters_test.go b/traffic_ops/testing/api/v3/profile_parameters_test.go index e8460e8575..508420b348 100644 --- a/traffic_ops/testing/api/v3/profile_parameters_test.go +++ b/traffic_ops/testing/api/v3/profile_parameters_test.go @@ -34,6 +34,8 @@ const queryParamFormat = "?profileId=%s¶meterId=%s" func TestProfileParameters(t *testing.T) { WithObjs(t, []TCObj{CDNs, Types, Parameters, Profiles, ProfileParameters}, func() { + // This is a one off test to check POST with an empty JSON body + TestPostWithEmptyBody(t) currentTime := time.Now().UTC().Add(-15 * time.Second) tomorrow := currentTime.AddDate(0, 0, 1).Format(time.RFC1123) @@ -75,11 +77,6 @@ func TestProfileParameters(t *testing.T) { }}, Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)), }, - "BAD REQUEST when EMPTY BODY PROVIDED": { - ClientSession: TOSession, - RequestBody: []tc.ProfileParameter{{}}, - Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)), - }, "BAD REQUEST when MISSING PROFILEID field": { ClientSession: TOSession, RequestBody: []tc.ProfileParameter{{ @@ -168,6 +165,16 @@ func TestProfileParameters(t *testing.T) { }) } +func TestPostWithEmptyBody(t *testing.T) { + resp, err := TOSession.Client.Post(TOSession.URL+"/api/5.0/profileparameters", "application/json", nil) + if err != nil { + t.Fatalf("error sending post to create profile parameter with an empty body: %v", err) + } + if resp.StatusCode != http.StatusBadRequest { + t.Errorf("expected to get a 400 error code, but received %d instead", resp.StatusCode) + } +} + func CreateTestProfileParameters(t *testing.T) { for _, profile := range testData.Profiles { profileID := GetProfileID(t, profile.Name)() diff --git a/traffic_ops/testing/api/v4/profile_parameters_test.go b/traffic_ops/testing/api/v4/profile_parameters_test.go index a27f49e32c..1903389e11 100644 --- a/traffic_ops/testing/api/v4/profile_parameters_test.go +++ b/traffic_ops/testing/api/v4/profile_parameters_test.go @@ -33,6 +33,8 @@ import ( func TestProfileParameters(t *testing.T) { WithObjs(t, []TCObj{CDNs, Types, Parameters, Profiles, ProfileParameters}, func() { + // This is a one off test to check POST with an empty JSON body + TestPostWithEmptyBody(t) currentTime := time.Now().UTC().Add(-15 * time.Second) tomorrow := currentTime.AddDate(0, 0, 1).Format(time.RFC1123) @@ -76,11 +78,6 @@ func TestProfileParameters(t *testing.T) { }, Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)), }, - "BAD REQUEST when EMPTY BODY PROVIDED": { - ClientSession: TOSession, - RequestBody: map[string]interface{}{}, - Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)), - }, "BAD REQUEST when MISSING PROFILEID field": { ClientSession: TOSession, RequestBody: map[string]interface{}{ @@ -177,6 +174,16 @@ func TestProfileParameters(t *testing.T) { }) } +func TestPostWithEmptyBody(t *testing.T) { + resp, err := TOSession.Client.Post(TOSession.URL+"/api/5.0/profileparameters", "application/json", nil) + if err != nil { + t.Fatalf("error sending post to create profile parameter with an empty body: %v", err) + } + if resp.StatusCode != http.StatusBadRequest { + t.Errorf("expected to get a 400 error code, but received %d instead", resp.StatusCode) + } +} + func TestProfileParameter(t *testing.T) { WithObjs(t, []TCObj{CDNs, Types, Parameters, Profiles}, func() { diff --git a/traffic_ops/testing/api/v5/profile_parameters_test.go b/traffic_ops/testing/api/v5/profile_parameters_test.go index 513aefe4ba..7c2a15ebab 100644 --- a/traffic_ops/testing/api/v5/profile_parameters_test.go +++ b/traffic_ops/testing/api/v5/profile_parameters_test.go @@ -33,6 +33,8 @@ import ( func TestProfileParameters(t *testing.T) { WithObjs(t, []TCObj{CDNs, Types, Parameters, Profiles, ProfileParameters}, func() { + // This is a one off test to check POST with an empty JSON body + TestPostWithEmptyBody(t) currentTime := time.Now().UTC().Add(-15 * time.Second) tomorrow := currentTime.AddDate(0, 0, 1).Format(time.RFC1123) @@ -76,11 +78,6 @@ func TestProfileParameters(t *testing.T) { }, Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)), }, - "BAD REQUEST when EMPTY BODY PROVIDED": { - ClientSession: TOSession, - RequestBody: map[string]interface{}{}, - Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)), - }, "BAD REQUEST when MISSING PROFILEID field": { ClientSession: TOSession, RequestBody: map[string]interface{}{ @@ -177,6 +174,16 @@ func TestProfileParameters(t *testing.T) { }) } +func TestPostWithEmptyBody(t *testing.T) { + resp, err := TOSession.Client.Post(TOSession.URL+"/api/5.0/profileparameters", "application/json", nil) + if err != nil { + t.Fatalf("error sending post to create profile parameter with an empty body: %v", err) + } + if resp.StatusCode != http.StatusBadRequest { + t.Errorf("expected to get a 400 error code, but received %d instead", resp.StatusCode) + } +} + func TestProfileParameter(t *testing.T) { WithObjs(t, []TCObj{CDNs, Types, Parameters, Profiles}, func() { From 8d05b3b69b3ca71d0c1914d4e633ba96b0109fd2 Mon Sep 17 00:00:00 2001 From: Kurtis Michie Date: Mon, 21 Nov 2022 09:32:58 -0700 Subject: [PATCH 7/7] Corrected correct URL Path for versions 3.0 and 4.0 --- traffic_ops/testing/api/v3/profile_parameters_test.go | 2 +- traffic_ops/testing/api/v4/profile_parameters_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/traffic_ops/testing/api/v3/profile_parameters_test.go b/traffic_ops/testing/api/v3/profile_parameters_test.go index b8a90c103e..fe73aa262b 100644 --- a/traffic_ops/testing/api/v3/profile_parameters_test.go +++ b/traffic_ops/testing/api/v3/profile_parameters_test.go @@ -166,7 +166,7 @@ func TestProfileParameters(t *testing.T) { } func TestPostWithEmptyBody(t *testing.T) { - resp, err := TOSession.Client.Post(TOSession.URL+"/api/5.0/profileparameters", "application/json", nil) + resp, err := TOSession.Client.Post(TOSession.URL+"/api/3.0/profileparameters", "application/json", nil) if err != nil { t.Fatalf("error sending post to create profile parameter with an empty body: %v", err) } diff --git a/traffic_ops/testing/api/v4/profile_parameters_test.go b/traffic_ops/testing/api/v4/profile_parameters_test.go index 8198ce1776..23365c12f0 100644 --- a/traffic_ops/testing/api/v4/profile_parameters_test.go +++ b/traffic_ops/testing/api/v4/profile_parameters_test.go @@ -175,7 +175,7 @@ func TestProfileParameters(t *testing.T) { } func TestPostWithEmptyBody(t *testing.T) { - resp, err := TOSession.Client.Post(TOSession.URL+"/api/5.0/profileparameters", "application/json", nil) + resp, err := TOSession.Client.Post(TOSession.URL+"/api/4.0/profileparameters", "application/json", nil) if err != nil { t.Fatalf("error sending post to create profile parameter with an empty body: %v", err) }