Skip to content

Commit 14e1e85

Browse files
Merge pull request #375 from MateSaary/e2e-fix
Fix interceptor e2e tests
2 parents fb24a79 + a91bb5f commit 14e1e85

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

interceptor/test/e2e.sh

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,40 @@ NC='\033[0m'
99
# Load pd token from vault - needed by interceptor
1010
export VAULT_ADDR="https://vault.devshift.net"
1111
export VAULT_TOKEN="$(vault login -method=oidc -token-only)"
12-
for v in $(vault kv get -format=json osd-sre/configuration-anomaly-detection/cad-testing | jq -r ".data.data|to_entries|map(\"\(.key)=\(.value|tostring)\")|.[]"); do export $v; done
12+
for v in $(vault kv get -format=json osd-sre/configuration-anomaly-detection/cad-testing | jq -r ".data.data|to_entries|map(\"\(.key)=\(.value|tostring)\")|.[]"); do export $v; done
1313
unset VAULT_ADDR VAULT_TOKEN
14-
echo
14+
echo
1515

1616
temp_log_file=$(mktemp)
1717

1818
# Function to send an interceptor request and check the response
1919
function test_interceptor {
20+
21+
local incident_id=$1
22+
local expected_response=$2
23+
2024
# Run the interceptor and print logs to temporary log file
2125
CAD_PD_TOKEN=$(echo $pd_test_token) CAD_SILENT_POLICY=$(echo $pd_test_silence_policy) ./../bin/interceptor > $temp_log_file 2>&1 &
2226
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"
27+
PAYLOAD_BODY="{\\\"__pd_metadata\\\":{\\\"incident\\\":{\\\"id\\\":\\\"$incident_id\\\"}}}"
28+
PAYLOAD_BODY_FORMATTED='{"__pd_metadata":{"incident":{"id":"'$incident_id'"}}}'
29+
SIGN=$(echo -n "$PAYLOAD_BODY_FORMATTED" | sha256hmac -K $PD_SIGNATURE | tr -d "[:space:]-")
2630

2731
# Store the PID of the interceptor process
2832
INTERCEPTOR_PID=$!
2933

34+
# Wrap the webhook originating payload (this is the expected format of the payload sent to the interceptor)
35+
WRAPPED_PAYLOAD="{\"header\":{\"Content-Type\":[\"application/json\"],\"X-PagerDuty-Signature\":[\"v1=$SIGN\"]},\"body\":\"$PAYLOAD_BODY\"}"
36+
3037
# Wait for 1 second to allow the interceptor to start up
3138
sleep 5
3239

33-
local incident_id=$1
34-
local expected_response=$2
3540

3641
# Send an interceptor request to localhost:8080
3742
# See https://pkg.go.dev/github.com/tektoncd/triggers/pkg/apis/triggers/v1alpha1#InterceptorRequest
3843
CURL_EXITCODE=0
3944
CURL_OUTPUT=$(curl -s -X POST -H "X-PagerDuty-Signature:v1=${SIGN}" -H "Content-Type: application/json" \
40-
-d "$PAYLOAD" \
45+
-d "$WRAPPED_PAYLOAD" \
4146
http://localhost:8080) || CURL_EXITCODE=$?
4247

4348
# Check if the curl output matches the expected response
@@ -66,6 +71,7 @@ function test_interceptor {
6671
# See https://github.com/tektoncd/triggers/blob/v0.27.0/pkg/apis/triggers/v1alpha1/interceptor_types.go#L134
6772
EXPECTED_RESPONSE_CONTINUE='{"continue":true,"status":{}}'
6873
EXPECTED_RESPONSE_STOP='{"continue":false,"status":{}}'
74+
EXPECTED_RESPONSE_SIGNATURE_ERROR='failed to verify signature: invalid webhook signature'
6975

7076
echo "========= TESTS ============="
7177
# Test for a pre-existing alert we handle (ClusterProvisioningDelay)
@@ -76,6 +82,7 @@ test_interceptor "Q12WO44XJLR3H3" "$EXPECTED_RESPONSE_CONTINUE"
7682
echo "Test 2: unhandled alerts returns a 'continue: false' response"
7783
test_interceptor "Q3722KGCG12ZWD" "$EXPECTED_RESPONSE_STOP"
7884

85+
# Test for an alert with invalid signature
7986
echo "Test 3: expected failure due to invalid signature"
8087
PD_SIGNATURE="invalid-signature"
81-
test_interceptor "Q12WO44XJLR3H3" "$EXPECTED_RESPONSE_STOP"
88+
test_interceptor "Q12WO44XJLR3H3" "$EXPECTED_RESPONSE_SIGNATURE_ERROR"

0 commit comments

Comments
 (0)