-
Notifications
You must be signed in to change notification settings - Fork 420
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Throw Error Message and Bad Request Code if event Body isn't json.
- Loading branch information
1 parent
f84479a
commit f90ab96
Showing
4 changed files
with
150 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
Copyright 2021 The Tekton Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package sink | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
) | ||
|
||
func (r Sink) IsValidPayload(eventHandler http.Handler) http.Handler { | ||
return http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) { | ||
payload, err := ioutil.ReadAll(request.Body) | ||
request.Body = ioutil.NopCloser(bytes.NewBuffer(payload)) | ||
if err != nil { | ||
r.Logger.Errorf("Error reading event body: %s", err) | ||
response.WriteHeader(http.StatusInternalServerError) | ||
return | ||
} | ||
var event map[string]interface{} | ||
if err := json.Unmarshal([]byte(payload), &event); err != nil { | ||
errMsg := fmt.Sprintf("Invalid event body format format: %s", err) | ||
r.Logger.Error(errMsg) | ||
response.WriteHeader(http.StatusBadRequest) | ||
response.Header().Set("Content-Type", "application/json") | ||
body := Response{ | ||
EventListener: r.EventListenerName, | ||
Namespace: r.EventListenerNamespace, | ||
ErrorMessage: errMsg, | ||
} | ||
if err := json.NewEncoder(response).Encode(body); err != nil { | ||
r.Logger.Errorf("failed to write back sink response: %v", err) | ||
} | ||
return | ||
} | ||
eventHandler.ServeHTTP(response, request) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
Copyright 2021 The Tekton Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package sink | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
triggersv1 "github.com/tektoncd/triggers/pkg/apis/triggers/v1alpha1" | ||
"github.com/tektoncd/triggers/test" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
func TestSink_IsValidPayload(t *testing.T) { | ||
const defaultELName = "test-el" | ||
for _, tc := range []struct { | ||
name string | ||
testResources test.Resources | ||
eventBody []byte | ||
wantStatusCode int | ||
}{{ | ||
name: "event with Json Body", | ||
testResources: test.Resources{ | ||
EventListeners: []*triggersv1.EventListener{{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: defaultELName, | ||
Namespace: namespace, | ||
}, | ||
Spec: triggersv1.EventListenerSpec{ | ||
Triggers: []triggersv1.EventListenerTrigger{{ | ||
TriggerRef: "test", | ||
}}, | ||
}, | ||
}}, | ||
}, | ||
eventBody: json.RawMessage(`{"head_commit": {"id": "testrevision"}, "repository": {"url": "testurl"}}`), | ||
wantStatusCode: http.StatusAccepted, | ||
}, { | ||
name: "event with non Json Body", | ||
testResources: test.Resources{ | ||
EventListeners: []*triggersv1.EventListener{{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: defaultELName, | ||
Namespace: namespace, | ||
}, | ||
Spec: triggersv1.EventListenerSpec{ | ||
Triggers: []triggersv1.EventListenerTrigger{{ | ||
TriggerRef: "test", | ||
}}, | ||
}, | ||
}}, | ||
}, | ||
eventBody: []byte(`<test>xml</test>`), | ||
wantStatusCode: http.StatusBadRequest, | ||
}} { | ||
t.Run(tc.name, func(t *testing.T) { | ||
elName := defaultELName | ||
if len(tc.testResources.EventListeners) > 0 { | ||
elName = tc.testResources.EventListeners[0].Name | ||
} | ||
sink, _ := getSinkAssets(t, tc.testResources, elName, nil) | ||
|
||
ts := httptest.NewServer(sink.IsValidPayload(http.HandlerFunc(sink.HandleEvent))) | ||
defer ts.Close() | ||
|
||
resp, err := http.Post(ts.URL, "application/json", bytes.NewReader(tc.eventBody)) | ||
if err != nil { | ||
t.Fatalf("error making request to eventListener: %s", err) | ||
} | ||
if resp.StatusCode != tc.wantStatusCode { | ||
t.Fatalf("Status code mismatch: got %d, want %d", resp.StatusCode, http.StatusInternalServerError) | ||
} | ||
}) | ||
} | ||
} |