Skip to content

Commit 840cce8

Browse files
zakiskchmouel
authored andcommitted
feat(incoming): Set default secret key for incoming webhooks
When no secret key is specified in the Repository CR's incoming webhook configuration, the system now defaults to using "secret" as the key name when retrieving the secret value from the Secret resource. Changes: - Add default key fallback logic in incoming webhook adapter - Document the default "secret" key behavior in incoming webhook guide This simplifies configuration by not requiring users to explicitly specify the secret key when using the standard "secret" key name. Signed-off-by: Zaki Shaikh <zashaikh@redhat.com>
1 parent 9a418d7 commit 840cce8

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

docs/content/docs/guide/incoming_webhook.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ spec:
5959
type: webhook-url
6060
```
6161
62+
**Note:** If no secret key is specified in the Repository CR, the default key `secret` will be used to retrieve the secret value from the `repo-incoming-secret` Secret resource.
63+
6264
### Glob Pattern Matching in Targets
6365

6466
The `targets` field supports both exact string matching and glob patterns, allowing you to match multiple branches with a single rule.

pkg/adapter/incoming.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ import (
2323
"go.uber.org/zap"
2424
)
2525

26+
const (
27+
defaultIncomingWebhookSecretKey = "secret"
28+
)
29+
2630
func compareSecret(incomingSecret, secretValue string) bool {
2731
return subtle.ConstantTimeCompare([]byte(incomingSecret), []byte(secretValue)) != 0
2832
}
@@ -122,6 +126,11 @@ func (l *listener) detectIncoming(ctx context.Context, req *http.Request, payloa
122126
Name: hook.Secret.Name,
123127
Key: hook.Secret.Key,
124128
}
129+
130+
if secretOpts.Key == "" {
131+
secretOpts.Key = defaultIncomingWebhookSecretKey
132+
}
133+
125134
secretValue, err := l.kint.GetSecret(ctx, secretOpts)
126135
if err != nil {
127136
return false, nil, fmt.Errorf("error getting secret referenced in incoming-webhook: %w", err)

pkg/adapter/incoming_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,43 @@ func Test_listener_detectIncoming(t *testing.T) {
232232
incomingBody: `{"params":{"the_best_superhero_is":"you"}}`,
233233
},
234234
},
235+
{
236+
name: "good/incoming with default secret key",
237+
want: true,
238+
args: args{
239+
secretResult: map[string]string{"incoming-secret": "verysecrete"},
240+
data: testclient.Data{
241+
Repositories: []*v1alpha1.Repository{
242+
{
243+
ObjectMeta: metav1.ObjectMeta{
244+
Name: "test-default-key",
245+
},
246+
Spec: v1alpha1.RepositorySpec{
247+
URL: goodURL,
248+
Incomings: &[]v1alpha1.Incoming{
249+
{
250+
Targets: []string{"main"},
251+
Secret: v1alpha1.Secret{
252+
Name: "incoming-secret",
253+
// Key is not specified, should default to "secret"
254+
},
255+
},
256+
},
257+
GitProvider: &v1alpha1.GitProvider{
258+
Type: "github",
259+
},
260+
},
261+
},
262+
},
263+
},
264+
method: http.MethodPost,
265+
queryURL: "/incoming",
266+
queryRepository: "test-default-key",
267+
querySecret: "verysecrete",
268+
queryPipelineRun: "pipelinerun1",
269+
queryBranch: "main",
270+
},
271+
},
235272
{
236273
name: "invalid incoming body",
237274
args: args{

0 commit comments

Comments
 (0)