-
Notifications
You must be signed in to change notification settings - Fork 83
/
incoming.go
174 lines (156 loc) · 6.5 KB
/
incoming.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package adapter
import (
"context"
"crypto/subtle"
"fmt"
"net/http"
apincoming "github.com/openshift-pipelines/pipelines-as-code/pkg/apis/incoming"
"github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/v1alpha1"
"github.com/openshift-pipelines/pipelines-as-code/pkg/formatting"
"github.com/openshift-pipelines/pipelines-as-code/pkg/matcher"
"github.com/openshift-pipelines/pipelines-as-code/pkg/params/info"
"github.com/openshift-pipelines/pipelines-as-code/pkg/provider"
"github.com/openshift-pipelines/pipelines-as-code/pkg/provider/bitbucketcloud"
"github.com/openshift-pipelines/pipelines-as-code/pkg/provider/bitbucketserver"
"github.com/openshift-pipelines/pipelines-as-code/pkg/provider/gitea"
"github.com/openshift-pipelines/pipelines-as-code/pkg/provider/github"
"github.com/openshift-pipelines/pipelines-as-code/pkg/provider/github/app"
"github.com/openshift-pipelines/pipelines-as-code/pkg/provider/gitlab"
ktypes "github.com/openshift-pipelines/pipelines-as-code/pkg/secrets/types"
"go.uber.org/zap"
)
func compareSecret(incomingSecret, secretValue string) bool {
return subtle.ConstantTimeCompare([]byte(incomingSecret), []byte(secretValue)) != 0
}
func applyIncomingParams(req *http.Request, payloadBody []byte, params []string) (apincoming.Payload, error) {
if req.Header.Get("Content-Type") != "application/json" {
return apincoming.Payload{}, fmt.Errorf("invalid content type, only application/json is accepted when posting a body")
}
payload, err := apincoming.ParseIncomingPayload(payloadBody)
if err != nil {
return apincoming.Payload{}, fmt.Errorf("error parsing incoming payload, not the expected format?: %w", err)
}
for k := range payload.Params {
allowed := false
for _, allowedP := range params {
if k == allowedP {
allowed = true
break
}
}
if !allowed {
return apincoming.Payload{}, fmt.Errorf("param %s is not allowed in incoming webhook CR", k)
}
}
return payload, nil
}
func (l *listener) detectIncoming(ctx context.Context, req *http.Request, payloadBody []byte) (bool, *v1alpha1.Repository, error) {
repository := req.URL.Query().Get("repository")
querySecret := req.URL.Query().Get("secret")
pipelineRun := req.URL.Query().Get("pipelinerun")
branch := req.URL.Query().Get("branch")
if req.URL.Path != "/incoming" {
return false, nil, nil
}
l.logger.Infof("incoming request has been requested: %v", req.URL)
if pipelineRun == "" || repository == "" || querySecret == "" || branch == "" {
err := fmt.Errorf("missing query URL argument: pipelinerun, branch, repository, secret: '%s' '%s' '%s' '%s'", pipelineRun, branch, repository, querySecret)
return false, nil, err
}
repo, err := matcher.GetRepo(ctx, l.run, repository)
if err != nil {
return false, nil, fmt.Errorf("error getting repo: %w", err)
}
if repo == nil {
return false, nil, fmt.Errorf("cannot find repository %s", repository)
}
if repo.Spec.Incomings == nil {
return false, nil, fmt.Errorf("you need to have incoming webhooks rules in your repo spec, repo: %s", repository)
}
hook := matcher.IncomingWebhookRule(branch, *repo.Spec.Incomings)
if hook == nil {
return false, nil, fmt.Errorf("branch '%s' has not matched any rules in repo incoming webhooks spec: %+v", branch, *repo.Spec.Incomings)
}
// log incoming request
l.logger.Infof("incoming request targeting pipelinerun %s on branch %s for repository %s has been accepted", pipelineRun, branch, repository)
secretOpts := ktypes.GetSecretOpt{
Namespace: repo.Namespace,
Name: hook.Secret.Name,
Key: hook.Secret.Key,
}
secretValue, err := l.kint.GetSecret(ctx, secretOpts)
if err != nil {
return false, nil, fmt.Errorf("error getting secret referenced in incoming-webhook: %w", err)
}
if secretValue == "" {
return false, nil, fmt.Errorf("secret referenced in incoming-webhook %s is empty or key %s is not existent", hook.Secret.Name, hook.Secret.Key)
}
// TODO: move to somewhere common to share between gitlab and here
if !compareSecret(querySecret, secretValue) {
return false, nil, fmt.Errorf("secret passed to the webhook does not match the incoming webhook secret set on repository CR in secret %s", hook.Secret.Name)
}
if repo.Spec.GitProvider == nil || repo.Spec.GitProvider.Type == "" {
gh := github.New()
gh.Run = l.run
ns := info.GetNS(ctx)
ip := app.NewInstallation(req, l.run, repo, gh, ns)
enterpriseURL, token, installationID, err := ip.GetAndUpdateInstallationID(ctx)
if err != nil {
return false, nil, err
}
l.event.Provider.URL = enterpriseURL
l.event.Provider.Token = token
l.event.InstallationID = installationID
// Github app is not installed for provided repository url
if l.event.InstallationID == 0 {
return false, nil, fmt.Errorf("GithubApp is not installed for the provided repository url %s ", repo.Spec.URL)
}
}
// make sure accepted is json
if string(payloadBody) != "" {
if l.event.Event, err = applyIncomingParams(req, payloadBody, hook.Params); err != nil {
return false, nil, err
}
}
// TODO: more than i think about it and more i think triggertarget should be
// eventType and vice versa, but keeping as is for now.
l.event.EventType = "incoming"
l.event.TriggerTarget = "push"
l.event.TargetPipelineRun = pipelineRun
l.event.HeadBranch = branch
l.event.BaseBranch = branch
l.event.Request.Header = req.Header
l.event.Request.Payload = payloadBody
l.event.URL = repo.Spec.URL
l.event.Sender = "incoming"
return true, repo, nil
}
func (l *listener) processIncoming(targetRepo *v1alpha1.Repository) (provider.Interface, *zap.SugaredLogger, error) {
// can a git ssh URL be a Repo URL? I don't think this will even ever work
org, repo, err := formatting.GetRepoOwnerSplitted(targetRepo.Spec.URL)
if err != nil {
return nil, nil, err
}
l.event.Organization = org
l.event.Repository = repo
var provider provider.Interface
if targetRepo.Spec.GitProvider == nil || targetRepo.Spec.GitProvider.Type == "" {
provider = github.New()
} else {
switch targetRepo.Spec.GitProvider.Type {
case "github":
provider = github.New()
case "gitlab":
provider = &gitlab.Provider{}
case "gitea":
provider = &gitea.Provider{}
case "bitbucket-cloud":
provider = &bitbucketcloud.Provider{}
case "bitbucket-server":
provider = &bitbucketserver.Provider{}
default:
return l.processRes(false, nil, l.logger.With("namespace", targetRepo.Namespace), "", fmt.Errorf("no supported Git provider has been detected"))
}
}
return l.processRes(true, provider, l.logger.With("provider", "incoming", "namespace", targetRepo.Namespace), "", nil)
}