forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebhook.go
102 lines (90 loc) · 3.13 KB
/
webhook.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
package buildconfig
import (
"fmt"
"net/http"
"strings"
kapi "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/errors"
buildapi "github.com/openshift/origin/pkg/build/api"
"github.com/openshift/origin/pkg/build/client"
"github.com/openshift/origin/pkg/build/webhook"
"github.com/openshift/origin/pkg/util/rest"
)
// NewWebHookREST returns the webhook handler wrapped in a rest.WebHook object.
func NewWebHookREST(registry Registry, instantiator client.BuildConfigInstantiator, plugins map[string]webhook.Plugin) *rest.WebHook {
hook := &WebHook{
registry: registry,
instantiator: instantiator,
plugins: plugins,
}
return rest.NewWebHook(hook, false)
}
type WebHook struct {
registry Registry
instantiator client.BuildConfigInstantiator
plugins map[string]webhook.Plugin
}
// ServeHTTP implements rest.HookHandler
func (w *WebHook) ServeHTTP(writer http.ResponseWriter, req *http.Request, ctx kapi.Context, name, subpath string) error {
parts := strings.Split(subpath, "/")
if len(parts) != 2 {
return errors.NewBadRequest(fmt.Sprintf("unexpected hook subpath %s", subpath))
}
secret, hookType := parts[0], parts[1]
plugin, ok := w.plugins[hookType]
if !ok {
return errors.NewNotFound(buildapi.Resource("buildconfighook"), hookType)
}
config, err := w.registry.GetBuildConfig(ctx, name)
if err != nil {
// clients should not be able to find information about build configs in
// the system unless the config exists and the secret matches
return errors.NewUnauthorized(fmt.Sprintf("the webhook %q for %q did not accept your secret", hookType, name))
}
revision, envvars, proceed, err := plugin.Extract(config, secret, "", req)
switch err {
case webhook.ErrSecretMismatch, webhook.ErrHookNotEnabled:
return errors.NewUnauthorized(fmt.Sprintf("the webhook %q for %q did not accept your secret", hookType, name))
case nil:
default:
return errors.NewInternalError(fmt.Errorf("hook failed: %v", err))
}
if !proceed {
return nil
}
buildTriggerCauses := generateBuildTriggerInfo(revision, hookType, secret)
request := &buildapi.BuildRequest{
TriggeredBy: buildTriggerCauses,
ObjectMeta: kapi.ObjectMeta{Name: name},
Revision: revision,
Env: envvars,
}
if _, err := w.instantiator.Instantiate(config.Namespace, request); err != nil {
return errors.NewInternalError(fmt.Errorf("could not generate a build: %v", err))
}
return nil
}
func generateBuildTriggerInfo(revision *buildapi.SourceRevision, hookType, secret string) (buildTriggerCauses []buildapi.BuildTriggerCause) {
hiddenSecret := fmt.Sprintf("%s***", secret[:(len(secret)/2)])
switch {
case hookType == "generic":
buildTriggerCauses = append(buildTriggerCauses,
buildapi.BuildTriggerCause{
Message: "Generic WebHook",
GenericWebHook: &buildapi.GenericWebHookCause{
Revision: revision,
Secret: hiddenSecret,
},
})
case hookType == "github":
buildTriggerCauses = append(buildTriggerCauses,
buildapi.BuildTriggerCause{
Message: "GitHub WebHook",
GitHubWebHook: &buildapi.GitHubWebHookCause{
Revision: revision,
Secret: hiddenSecret,
},
})
}
return buildTriggerCauses
}