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

Add webhook management for repositories and organizations #154

Merged

Conversation

gabriel-samfira
Copy link
Member

@gabriel-samfira gabriel-samfira commented Aug 15, 2023

This change adds the ability to install or uninstall the GitHub web hook in repositories and organizations (no enterprise support yet) that is needed by GARM to operate normally.

Currently, users have to manually configure a web hook in GitHub that points back to GARM. This is needed in order for garm to receive notifications about jobs, and react by creating or removing runners. In the current implementation, the API endpoint for webhooks is /webhooks.

This change makes it possible for GARM to also receive webhooks on /webhooks/{controllerID}, where {controllerID} is the UUID4 of the current controller.

The old /webhook endpoint still works and can be used for manual configuration. When installing or removing webhooks, GARM will only use the API endpoint that includes the controller ID. This way, we don't accidentally remove a user-defined web-hook.

Or course, users can also manually configure web-hooks using the controllerID suffixed web-hook API endpoint if they wish, but in that case, GARM may remove the webhook if requested via CLI.

To use this feature, the PAT associated to a repo or org needs to have access to add/remove webhooks. A new setting will also be added that enables the API endpoints needed to manage web hooks.

Example usage:

You will have to add the webhook base URL in your config.toml file under the [DEFAULT] section and enable the webhook management feature:

[DEFAULT]
webhook_url = "https://garm.example.com/webhooks"
enable_webhook_management = true

Then you can install/uninstall webhooks.

Installing the webhook:

garm-cli repo webhook install <REPO_ID>

Installing the webhook and disabling TLS certificate verification (not recommended):

garm-cli repo webhook install --insecure <REPO_ID>

Uninstalling:

garm-cli repo webhook uninstall <REPO_ID>

Show current status:

ubuntu@garm:~/garm$ garm-cli org webhook show 23ff4c81-b998-42cf-a4d5-f0c095f3dc4f
+--------------+----------------------------------------------------------------------------+
| FIELD        | VALUE                                                                      |
+--------------+----------------------------------------------------------------------------+
| ID           | 429050643                                                                  |
| URL          | https://garm.example.com/webhooks/543d1288-29de-41b5-8200-f9d3f20502d5     |
| Events       | [workflow_job]                                                             |
| Active       | true                                                                       |
| Insecure SSL | false                                                                      |
+--------------+----------------------------------------------------------------------------+

To view your current controller info and configured URLs, you can run:

ubuntu@garm:~/garm$ garm-cli controller-info show
+------------------------+----------------------------------------------------------------------------+
| FIELD                  | VALUE                                                                      |
+------------------------+----------------------------------------------------------------------------+
| Controller ID          | 543d1288-29de-41b5-8200-f9d3f20502d5                                       |
| Hostname               | garm                                                                       |
| Metadata URL           | https://garm.example.com/api/v1/metadata                                   |
| Callback URL           | https://garm.example.com/api/v1/callbacks                                  |
| Webhook URL Base       | https://garm.example.com/webhooks                                          |
| Controller Webhook URL | https://garm.example.com/webhooks/543d1288-29de-41b5-8200-f9d3f20502d5     |
+------------------------+----------------------------------------------------------------------------+

Important notes

When removing a repo or an organization, by default garm will also cleanup any webhooks that may have been created. GARM will only ever touch webhooks that exactly match both the base URL configured in webhook_url and the controllerID suffix.

IF we assume that the controller ID that was automatically generated when you initialized GARM is 5eb2fc8d-b69b-4690-8e09-1c813b99bbf5 and your webhook URL is https://garm.example.com/webhooks, then GARM will only cleanup webhooks that exactly match https://garm.example.com/webhooks/5eb2fc8d-b69b-4690-8e09-1c813b99bbf5.

This behavior can be disabled by adding ?keepWebhook=1 to the DELETE request or by using the --keep-webhook=true flag for the org delete and repo delete CLI commands.

@gabriel-samfira gabriel-samfira marked this pull request as ready for review August 16, 2023 10:55
@gabriel-samfira gabriel-samfira changed the title Add webhook configuration Add webhook management for repositories and organizations Aug 16, 2023
  * Add a new middleware that tests for admin access
  * Add a new controller ID suffixed webhook endpoint. This will be used
    to accept webhook events on a webhook URL that is suffixed with our own
    controller ID.

Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
  * Added a webhook show command. This gives us info about the webhook and
    if it is installed.
  * Return webhook info when installing the webhook
  * Small typo fixes.

Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
  * When removing a repo or org, we uninstall the webhook as well.
  * Upgrade cobra command and mark "webhook-secret" and "random-webhook-secret"
    as MarkFlagsOneRequired()

Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
The user can opt to not delete the webhook (if installed) when removing
the entity from garm. Garm will only ever try to remove a webhook that
exactly matches the URL that is composed of the base webhook URL configured
in the config.toml file and the unique controller ID that is generated
when the controller is first installed. It should be safe to remove the
webhook when the entity is removed.

Of course, this behavior can be disabled.

Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
@gabriel-samfira
Copy link
Member Author

Merging as is for now. Integration tests are forthcoming.

@gabriel-samfira gabriel-samfira merged commit 9a7fbde into cloudbase:main Aug 22, 2023
4 checks passed
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.

1 participant