-
Notifications
You must be signed in to change notification settings - Fork 416
/
bitbucket.go
95 lines (79 loc) · 2.85 KB
/
bitbucket.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
Copyright 2020 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 bitbucket
import (
"context"
"fmt"
"net/http"
"google.golang.org/grpc/codes"
corev1lister "k8s.io/client-go/listers/core/v1"
gh "github.com/google/go-github/v31/github"
triggersv1 "github.com/tektoncd/triggers/pkg/apis/triggers/v1beta1"
"github.com/tektoncd/triggers/pkg/interceptors"
"go.uber.org/zap"
)
var _ triggersv1.InterceptorInterface = (*Interceptor)(nil)
type Interceptor struct {
SecretLister corev1lister.SecretLister
Logger *zap.SugaredLogger
}
func NewInterceptor(sl corev1lister.SecretLister, l *zap.SugaredLogger) *Interceptor {
return &Interceptor{
SecretLister: sl,
Logger: l,
}
}
func (w *Interceptor) Process(ctx context.Context, r *triggersv1.InterceptorRequest) *triggersv1.InterceptorResponse {
p := triggersv1.BitbucketInterceptor{}
if err := interceptors.UnmarshalParams(r.InterceptorParams, &p); err != nil {
return interceptors.Failf(codes.InvalidArgument, "failed to parse interceptor params: %v", err)
}
headers := interceptors.Canonical(r.Header)
// Check if the event type is in the allow-list
if p.EventTypes != nil {
actualEvent := http.Header(r.Header).Get("X-Event-Key")
isAllowed := false
for _, allowedEvent := range p.EventTypes {
if actualEvent == allowedEvent {
isAllowed = true
break
}
}
if !isAllowed {
return interceptors.Failf(codes.FailedPrecondition, "event type %s is not allowed", actualEvent)
}
}
// Next validate secrets if set
if p.SecretRef != nil {
// Check the secret to see if it is empty
if p.SecretRef.SecretKey == "" {
return interceptors.Fail(codes.FailedPrecondition, "bitbucket interceptor secretRef.secretKey is empty")
}
header := headers.Get("X-Hub-Signature")
if header == "" {
return interceptors.Fail(codes.InvalidArgument, "no X-Hub-Signature header set")
}
ns, _ := triggersv1.ParseTriggerID(r.Context.TriggerID)
secret, err := w.SecretLister.Secrets(ns).Get(p.SecretRef.SecretName)
if err != nil {
return interceptors.Failf(codes.Internal, fmt.Sprintf("error getting secret: %v", err))
}
secretToken := secret.Data[p.SecretRef.SecretKey]
if err := gh.ValidateSignature(header, []byte(r.Body), secretToken); err != nil {
return interceptors.Failf(codes.FailedPrecondition, err.Error())
}
}
return &triggersv1.InterceptorResponse{
Continue: true,
}
}