Skip to content

Commit eca56bf

Browse files
Merge pull request #363 from MateSaary/OSD-27752
OSD-27752: Add PD_SIGNATURE verification to interceptor
2 parents 37c7524 + 2918c39 commit eca56bf

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

interceptor/pkg/interceptor/pdinterceptor.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"os"
1212
"time"
1313

14+
"github.com/PagerDuty/go-pagerduty/webhookv3"
1415
investigations "github.com/openshift/configuration-anomaly-detection/pkg/investigations"
1516
"github.com/openshift/configuration-anomaly-detection/pkg/pagerduty"
1617
triggersv1 "github.com/tektoncd/triggers/pkg/apis/triggers/v1beta1"
@@ -39,6 +40,7 @@ func (pdi PagerDutyInterceptor) ServeHTTP(w http.ResponseWriter, r *http.Request
3940
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
4041
}
4142
}
43+
4244
w.Header().Add("Content-Type", "application/json")
4345
if _, err := w.Write(b); err != nil {
4446
pdi.Logger.Errorf("failed to write response: %s", err)
@@ -86,7 +88,17 @@ func (pdi *PagerDutyInterceptor) executeInterceptor(r *http.Request) ([]byte, er
8688
if _, err := io.Copy(&body, r.Body); err != nil {
8789
return nil, internal(fmt.Errorf("failed to read body: %w", err))
8890
}
91+
r.Body = io.NopCloser(bytes.NewReader(body.Bytes()))
92+
8993
var ireq triggersv1.InterceptorRequest
94+
95+
token, _ := os.LookupEnv("PD_SIGNATURE")
96+
97+
err := webhookv3.VerifySignature(r, token)
98+
if err != nil {
99+
return nil, badRequest(fmt.Errorf("failed to verify signature: %w", err))
100+
}
101+
90102
if err := json.Unmarshal(body.Bytes(), &ireq); err != nil {
91103
return nil, badRequest(fmt.Errorf("failed to parse body as InterceptorRequest: %w", err))
92104
}

interceptor/test/e2e.sh

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ temp_log_file=$(mktemp)
1919
function test_interceptor {
2020
# Run the interceptor and print logs to temporary log file
2121
CAD_PD_TOKEN=$(echo $pd_test_token) CAD_SILENT_POLICY=$(echo $pd_test_silence_policy) ./../bin/interceptor > $temp_log_file 2>&1 &
22-
22+
PD_SIGNATURE="test"
23+
PAYLOAD="{\"body\":\"{\\\"__pd_metadata\\\":{\\\"incident\\\":{\\\"id\\\":\\\"$incident_id\\\"}}}\",\"header\":{\"Content-Type\":[\"application/json\"]},\"extensions\":{},\"interceptor_params\":{},\"context\":null}"
24+
SIGN=$(echo -n "$PAYLOAD" | sha256hmac -K $PD_SIGNATURE | tr -d "[:space:]-")
25+
echo "Sign: $SIGN"
26+
2327
# Store the PID of the interceptor process
2428
INTERCEPTOR_PID=$!
2529

@@ -32,8 +36,8 @@ function test_interceptor {
3236
# Send an interceptor request to localhost:8080
3337
# See https://pkg.go.dev/github.com/tektoncd/triggers/pkg/apis/triggers/v1alpha1#InterceptorRequest
3438
CURL_EXITCODE=0
35-
CURL_OUTPUT=$(curl -s -X POST -H "Content-Type: application/json" \
36-
-d "{\"body\":\"{\\\"__pd_metadata\\\":{\\\"incident\\\":{\\\"id\\\":\\\"$incident_id\\\"}}}\",\"header\":{\"Content-Type\":[\"application/json\"]},\"extensions\":{},\"interceptor_params\":{},\"context\":null}" \
39+
CURL_OUTPUT=$(curl -s -X POST -H "X-PagerDuty-Signature:v1=${SIGN}" -H "Content-Type: application/json" \
40+
-d "$PAYLOAD" \
3741
http://localhost:8080) || CURL_EXITCODE=$?
3842

3943
# Check if the curl output matches the expected response
@@ -69,5 +73,9 @@ echo "Test 1: alert with existing handling returns a 'continue: true' response"
6973
test_interceptor "Q12WO44XJLR3H3" "$EXPECTED_RESPONSE_CONTINUE"
7074

7175
# Test for an alert we don't handle (alert called unhandled)
72-
echo "Test 1: unhandled alerts returns a 'continue: false' response"
76+
echo "Test 2: unhandled alerts returns a 'continue: false' response"
7377
test_interceptor "Q3722KGCG12ZWD" "$EXPECTED_RESPONSE_STOP"
78+
79+
echo "Test 3: expected failure due to invalid signature"
80+
PD_SIGNATURE="invalid-signature"
81+
test_interceptor "Q12WO44XJLR3H3" "$EXPECTED_RESPONSE_STOP"

0 commit comments

Comments
 (0)