15
15
package manager
16
16
17
17
import (
18
- "errors"
19
- "fmt"
20
18
"net/http"
21
- "strings"
22
19
23
- "github.com/google/uuid"
24
20
"github.com/rs/zerolog"
25
-
26
- "github.com/stacklok/minder/internal/providers/gitlab/webhooksecret"
27
21
)
28
22
29
23
// GetWebhookHandler implements the ProviderManager interface
30
24
// Note that this is where the whole webhook handler is defined and
31
25
// will live.
32
26
func (m * providerClassManager ) GetWebhookHandler () http.Handler {
33
- return http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
27
+ return http .HandlerFunc (func (_ http.ResponseWriter , r * http.Request ) {
34
28
l := zerolog .Ctx (m .parentContext ).With ().
35
29
Str ("webhook" , "gitlab" ).
36
30
Str ("method" , r .Method ).
@@ -40,57 +34,8 @@ func (m *providerClassManager) GetWebhookHandler() http.Handler {
40
34
Str ("content-type" , r .Header .Get ("Content-Type" )).
41
35
Logger ()
42
36
43
- // Validate the webhook secret
44
- if err := m .validateRequest (r ); err != nil {
45
- l .Error ().Err (err ).Msg ("invalid webhook request" )
46
- http .Error (w , "invalid webhook request" , http .StatusUnauthorized )
47
- return
48
- }
37
+ // TODO: Implement webhook handler
49
38
50
39
l .Debug ().Msg ("received webhook" )
51
40
})
52
41
}
53
-
54
- func (m * providerClassManager ) validateRequest (r * http.Request ) error {
55
- // Validate the webhook secret
56
- gltok := r .Header .Get ("X-Gitlab-Token" )
57
- if gltok == "" {
58
- return errors .New ("missing X-Gitlab-Token header" )
59
- }
60
-
61
- if err := m .validateToken (gltok , r ); err != nil {
62
- return fmt .Errorf ("invalid X-Gitlab-Token header: %w" , err )
63
- }
64
-
65
- return nil
66
- }
67
-
68
- // validateToken validates the incoming GitLab webhook token
69
- // Validation takes the secret from the GitLab webhook configuration
70
- // appens the last element of the path to the URL (which is unique per entity)
71
- func (m * providerClassManager ) validateToken (token string , req * http.Request ) error {
72
- // Extract the unique ID from the URL path
73
- path := req .URL .Path
74
- uniq := path [strings .LastIndex (path , "/" )+ 1 :]
75
-
76
- // uniq must be a valid UUID
77
- _ , err := uuid .Parse (uniq )
78
- if err != nil {
79
- return errors .New ("invalid unique ID" )
80
- }
81
-
82
- // Generate the expected secret
83
- if valid := webhooksecret .Verify (m .currentWebhookSecret , uniq , token ); valid {
84
- // If the secret is valid, we can return
85
- return nil
86
- }
87
-
88
- // Check the previous secrets
89
- for _ , prev := range m .previousWebhookSecrets {
90
- if valid := webhooksecret .Verify (prev , uniq , token ); valid {
91
- return nil
92
- }
93
- }
94
-
95
- return errors .New ("invalid webhook token" )
96
- }
0 commit comments