Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementing GitHub webhook handler #331

Open
wants to merge 2 commits into
base: fred/approval-service-skeleton-1
Choose a base branch
from

Conversation

doggydogworld
Copy link
Contributor

Created an implementation for the GitHub webhook. It is just a small wrapper around the go-github library.

Made some changes to the main function to use errgroup to run the eventSources concurrently. The errgroup library handles propagating cancellations that result from errors and is similar in functionality to waitgroups. In the event of an error from one of the eventSources, the errgroup will cancel the context which all goroutines in the group can react to appropriately.

@doggydogworld doggydogworld requested a review from a team as a code owner February 14, 2025 06:34
Copy link
Contributor

@fheinecke fheinecke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall LGTM. We might want to merge the base of this PR into master though, or its PR will become massive by the time we're done.

@r0mant, would you rather we file a bunch of smaller broken/development/not building PRs (which you'll need to review as only eng leads are codeowners), or one massive one later?

head.GitHubHookInstallationTargetID = r.Header.Get("X-GitHub-Hook-Installation-Target-ID")
head.HubSignature256 = r.Header.Get("X-Hub-Signature-256")

payload, err := github.ValidatePayload(r, h.secretToken) // If secretToken is empty, the signature will not be verified.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that we should error if the secret token is empty and a signature is provided. If this happens, we've probably misconfigured something.


payload, err := github.ValidatePayload(r, h.secretToken) // If secretToken is empty, the signature will not be verified.
if err != nil {
h.log.Warn("webhook validation failed", "headers", head)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In all these cases we should probably include some information about the request itself in the log, for debugging purposes. Or maybe the handler (or possibly the ingress gateway) should be able to log the entire request if debug logging is enabled?

})
mux.Handle("/webhook", webhook.NewHandler(
eventProcessor,
webhook.WithSecretToken([]byte("secret-token")), // TODO: get from config
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this should always take a string? It's a string when configured with GH, and I don't see when we would want to pass a byte array to this func.

Comment on lines 194 to 196
ghes.srv.Shutdown(context.Background()) // Ignore error - we're already handling one
case <-ctx.Done():
err = ghes.srv.Shutdown(context.Background())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should probably take a time-limited shutdown context stemming from context.Background().

deployReviewChan <- event
return nil
default:
return trace.Errorf("unknown event type: %T", event)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This library only exists for Teleport error handling, so we probably shouldn't be using it anymore outside of the product (here and elsewhere)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants