-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
github-events: Add multisecret support to trampoline webhook.
Adds the ability for the trampoline to accept multiple secrets. This tries to retain the existing behavior of the webhook assuming at least one secret by allowing additional secrets to be specified by the same prefix. Additional secrets must be defined and loaded out of band of the service.
- Loading branch information
Showing
8 changed files
with
315 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package trampoline | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"mime" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/chainguard-dev/clog" | ||
cloudevents "github.com/cloudevents/sdk-go/v2" | ||
"github.com/google/go-github/v61/github" | ||
"github.com/jonboulle/clockwork" | ||
) | ||
|
||
type Server struct { | ||
client cloudevents.Client | ||
secrets [][]byte | ||
clock clockwork.Clock | ||
} | ||
|
||
func NewServer(client cloudevents.Client, secrets [][]byte) *Server { | ||
return &Server{ | ||
client: client, | ||
secrets: secrets, | ||
clock: clockwork.NewRealClock(), | ||
} | ||
} | ||
|
||
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
ctx := r.Context() | ||
log := clog.FromContext(ctx) | ||
|
||
// https://docs.github.com/en/webhooks/using-webhooks/validating-webhook-deliveries | ||
payload, err := ValidatePayload(r, s.secrets) | ||
if err != nil { | ||
log.Errorf("failed to verify webhook: %v", err) | ||
w.WriteHeader(http.StatusForbidden) | ||
fmt.Fprintf(w, "failed to verify webhook: %v", err) | ||
return | ||
} | ||
|
||
// https://docs.github.com/en/webhooks/webhook-events-and-payloads#delivery-headers | ||
t := github.WebHookType(r) | ||
if t == "" { | ||
log.Errorf("missing X-GitHub-Event header") | ||
w.WriteHeader(http.StatusBadRequest) | ||
return | ||
} | ||
t = "dev.chainguard.github." + t | ||
log = log.With("event-type", t) | ||
|
||
var msg struct { | ||
Action string `json:"action"` | ||
Repository struct { | ||
FullName string `json:"full_name"` | ||
} `json:"repository"` | ||
} | ||
if err := json.Unmarshal(payload, &msg); err != nil { | ||
log.Warnf("failed to unmarshal payload; action and subject will be unset: %v", err) | ||
} else { | ||
log = log.With("action", msg.Action, "repo", msg.Repository.FullName) | ||
} | ||
|
||
log.Debugf("forwarding event: %s", t) | ||
|
||
event := cloudevents.NewEvent() | ||
event.SetID(github.DeliveryID(r)) | ||
event.SetType(t) | ||
event.SetSource(r.Host) | ||
event.SetSubject(msg.Repository.FullName) | ||
event.SetExtension("action", msg.Action) | ||
if err := event.SetData(cloudevents.ApplicationJSON, eventData{ | ||
When: s.clock.Now(), | ||
Body: payload, | ||
}); err != nil { | ||
log.Errorf("failed to set data: %v", err) | ||
w.WriteHeader(http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
const retryDelay = 10 * time.Millisecond | ||
const maxRetry = 3 | ||
rctx := cloudevents.ContextWithRetriesExponentialBackoff(context.WithoutCancel(ctx), retryDelay, maxRetry) | ||
if ceresult := s.client.Send(rctx, event); cloudevents.IsUndelivered(ceresult) || cloudevents.IsNACK(ceresult) { | ||
log.Errorf("Failed to deliver event: %v", ceresult) | ||
w.WriteHeader(http.StatusInternalServerError) | ||
} | ||
log.Debugf("event forwarded") | ||
} | ||
|
||
type eventData struct { | ||
When time.Time `json:"when"` | ||
Body json.RawMessage `json:"body"` | ||
} | ||
|
||
// ValidatePayload validates the payload of a webhook request for a given set of secrets. | ||
// If any of the secrets are valid, the payload is returned with no error. | ||
func ValidatePayload(r *http.Request, secrets [][]byte) ([]byte, error) { | ||
// Largely forked from github.ValidatePayload - we can't use this directly to avoid consuming the body. | ||
signature := r.Header.Get(github.SHA256SignatureHeader) | ||
if signature == "" { | ||
signature = r.Header.Get(github.SHA1SignatureHeader) | ||
} | ||
contentType, _, err := mime.ParseMediaType(r.Header.Get("Content-Type")) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
body, err := io.ReadAll(r.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, secret := range secrets { | ||
payload, err := github.ValidatePayloadFromBody(contentType, bytes.NewBuffer(body), signature, secret) | ||
if err == nil { | ||
return payload, nil | ||
} | ||
} | ||
return nil, fmt.Errorf("failed to validate payload") | ||
} |
Oops, something went wrong.