Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -248,3 +248,4 @@ Must be one of the following structs:
| url | string | The URL where notification event will be sent to. | Yes |
| signatureKey | string | The HTTP header key used to store the configured signature in each event. Default is "PipeCD-Signature". | No |
| signatureValue | string | The value of signature included in header of each event request. It can be used to verify the received events. | No |
| signatureValueFile | string | The path to the signature value file. | No |
8 changes: 7 additions & 1 deletion pkg/app/piped/notifier/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,13 @@ func (w *webhook) sendEvent(ctx context.Context, event model.NotificationEvent)
return
}

req.Header.Add(w.config.SignatureKey, w.config.SignatureValue)
signature, err := w.config.LoadSignatureValue()
if err != nil {
w.logger.Error("unable to load webhook signature value", zap.Error(err))
return
}

req.Header.Add(w.config.SignatureKey, signature)

resp, err := w.httpClient.Do(req)
if err != nil {
Expand Down
25 changes: 22 additions & 3 deletions pkg/config/piped.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"errors"
"fmt"
"os"
"strings"

"github.com/pipe-cd/pipecd/pkg/model"
)
Expand Down Expand Up @@ -610,9 +611,27 @@ type NotificationReceiverSlack struct {
}

type NotificationReceiverWebhook struct {
URL string `json:"url"`
SignatureKey string `json:"signatureKey" default:"PipeCD-Signature"`
SignatureValue string `json:"signatureValue"`
URL string `json:"url"`
SignatureKey string `json:"signatureKey" default:"PipeCD-Signature"`
SignatureValue string `json:"signatureValue"`
SignatureValueFile string `json:"signatureValueFile"`
}

func (n *NotificationReceiverWebhook) LoadSignatureValue() (string, error) {
if n.SignatureValue != "" && n.SignatureValueFile != "" {
return "", errors.New("only either signatureValue or signatureValueFile can be set")
}
if n.SignatureValue != "" {
return n.SignatureValue, nil
}
if n.SignatureValueFile != "" {
val, err := os.ReadFile(n.SignatureValueFile)
if err != nil {
return "", err
}
return strings.TrimSuffix(string(val), "\n"), nil
}
return "", nil
}

type SecretManagement struct {
Expand Down
45 changes: 45 additions & 0 deletions pkg/config/piped_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,3 +319,48 @@ func TestPipedEventWatcherValidate(t *testing.T) {
})
}
}

func TestNotificationReceiverWebhook_LoadSignatureValue(t *testing.T) {
testcase := []struct {
name string
webhook *NotificationReceiverWebhook
want string
wantErr bool
}{
{
name: "set signatureValue",
webhook: &NotificationReceiverWebhook{
URL: "https://example.com",
SignatureValue: "foo",
},
want: "foo",
wantErr: false,
},
{
name: "set signatureValueFile",
webhook: &NotificationReceiverWebhook{
URL: "https://example.com",
SignatureValueFile: "testdata/piped/notification-receiver-webhook",
},
want: "foo",
wantErr: false,
},
{
name: "set both of them",
webhook: &NotificationReceiverWebhook{
URL: "https://example.com",
SignatureValue: "foo",
SignatureValueFile: "testdata/piped/notification-receiver-webhook",
},
want: "",
wantErr: true,
},
}
for _, tc := range testcase {
t.Run(tc.name, func(t *testing.T) {
got, err := tc.webhook.LoadSignatureValue()
assert.Equal(t, tc.wantErr, err != nil)
assert.Equal(t, tc.want, got)
})
}
}
1 change: 1 addition & 0 deletions pkg/config/testdata/piped/notification-receiver-webhook
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo