-
Notifications
You must be signed in to change notification settings - Fork 316
feat: add CloudEvents webhook support for AWS ECR #1370
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
Open
audacioustux
wants to merge
4
commits into
argoproj-labs:master
Choose a base branch
from
audacioustux:feat/cloudevents-webhook
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
aaa02b5
feat: add CloudEvents webhook support for AWS ECR
audacioustux 8d15807
fix(docs): correct typo in GHCR webhook secret flag name (gchr -> ghcr)
audacioustux 08d544e
docs: add inline field comments to WebhookConfig struct
audacioustux 0b0f8c2
docs: clarify that 0 disables rate limiting in WebhookConfig
audacioustux File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,38 @@ | ||
| # CloudEvents Webhook Example | ||
|
|
||
| Example Terraform configuration for setting up AWS EventBridge to send ECR push events to ArgoCD Image Updater via CloudEvents. | ||
|
|
||
| ## Quick Start | ||
|
|
||
| ### 1. Configure EventBridge with Terraform | ||
|
|
||
| ```bash | ||
| cd terraform | ||
|
|
||
| # Set your variables | ||
| export TF_VAR_webhook_url="https://your-domain.com/webhook?type=cloudevents" | ||
| export TF_VAR_webhook_secret="your-webhook-secret" | ||
| export TF_VAR_aws_region="us-east-1" | ||
|
|
||
| # Apply the configuration | ||
| terraform init | ||
| terraform apply | ||
|
|
||
| # Return to parent directory | ||
| cd .. | ||
| ``` | ||
|
|
||
| ### 2. Test the Webhook | ||
|
|
||
| ```bash | ||
| ./test-webhook.sh https://your-webhook-url/webhook?type=cloudevents your-secret | ||
| ``` | ||
|
|
||
| ## Files | ||
|
|
||
| - `terraform/` - EventBridge configuration with input transformer for ECR events | ||
| - `test-webhook.sh` - Script to test the webhook endpoint | ||
|
|
||
| ## Documentation | ||
|
|
||
| For complete setup instructions, see the [webhook documentation](../../../docs/configuration/webhook.md#aws-ecr-via-eventbridge-cloudevents). |
This file contains hidden or 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,145 @@ | ||
| terraform { | ||
| required_version = ">= 1.0" | ||
|
|
||
| required_providers { | ||
| aws = { | ||
| source = "hashicorp/aws" | ||
| version = "~> 5.0" | ||
| } | ||
| } | ||
| } | ||
|
|
||
| provider "aws" { | ||
| region = var.aws_region | ||
| } | ||
|
|
||
| # EventBridge Rule to capture ECR push events | ||
| resource "aws_cloudwatch_event_rule" "ecr_push" { | ||
| name = "argocd-image-updater-ecr-push" | ||
| description = "Capture ECR image push events for ArgoCD Image Updater" | ||
|
|
||
| event_pattern = jsonencode({ | ||
| source = ["aws.ecr"] | ||
| detail-type = ["ECR Image Action"] | ||
| detail = { | ||
| action-type = ["PUSH"] | ||
| result = ["SUCCESS"] | ||
| # Filter for events with image tags (excludes untagged/manifest-only pushes) | ||
| image-tag = [{ | ||
| exists = true | ||
| }] | ||
| # Optional: Filter by specific repositories | ||
| repository-name = length(var.ecr_repository_filter) > 0 ? var.ecr_repository_filter : null | ||
| } | ||
| }) | ||
|
|
||
| tags = var.tags | ||
| } | ||
|
|
||
| # EventBridge Connection for API authentication | ||
| resource "aws_cloudwatch_event_connection" "webhook" { | ||
| name = "argocd-image-updater-webhook" | ||
| description = "Connection to ArgoCD Image Updater webhook" | ||
| authorization_type = "API_KEY" | ||
|
|
||
| auth_parameters { | ||
| api_key { | ||
| key = "X-Webhook-Secret" | ||
| value = var.webhook_secret | ||
| } | ||
| } | ||
| } | ||
|
|
||
| # API Destination pointing to ArgoCD Image Updater webhook | ||
| resource "aws_cloudwatch_event_api_destination" "webhook" { | ||
| name = "argocd-image-updater-webhook" | ||
| description = "ArgoCD Image Updater CloudEvents webhook endpoint" | ||
| invocation_endpoint = var.webhook_url | ||
| http_method = "POST" | ||
| invocation_rate_limit_per_second = 10 | ||
| connection_arn = aws_cloudwatch_event_connection.webhook.arn | ||
| } | ||
|
|
||
| # IAM Role for EventBridge to invoke API Destination | ||
| resource "aws_iam_role" "eventbridge" { | ||
| name = "argocd-image-updater-eventbridge-role" | ||
| assume_role_policy = data.aws_iam_policy_document.eventbridge_assume_role.json | ||
| tags = var.tags | ||
| } | ||
|
|
||
| data "aws_iam_policy_document" "eventbridge_assume_role" { | ||
| statement { | ||
| effect = "Allow" | ||
|
|
||
| principals { | ||
| type = "Service" | ||
| identifiers = ["events.amazonaws.com"] | ||
| } | ||
|
|
||
| actions = ["sts:AssumeRole"] | ||
| } | ||
| } | ||
|
|
||
| # IAM Policy for EventBridge to invoke API Destination | ||
| resource "aws_iam_role_policy" "eventbridge_invoke_api_destination" { | ||
| name = "invoke-api-destination" | ||
| role = aws_iam_role.eventbridge.id | ||
| policy = data.aws_iam_policy_document.eventbridge_invoke_api_destination.json | ||
| } | ||
|
|
||
| data "aws_iam_policy_document" "eventbridge_invoke_api_destination" { | ||
| statement { | ||
| effect = "Allow" | ||
|
|
||
| actions = [ | ||
| "events:InvokeApiDestination" | ||
| ] | ||
|
|
||
| resources = [ | ||
| aws_cloudwatch_event_api_destination.webhook.arn | ||
| ] | ||
| } | ||
| } | ||
|
|
||
| # EventBridge Target with Input Transformer (ECR -> CloudEvents) | ||
| resource "aws_cloudwatch_event_target" "api_destination" { | ||
| rule = aws_cloudwatch_event_rule.ecr_push.name | ||
| target_id = "ArgocdImageUpdaterCloudEvent" | ||
| arn = aws_cloudwatch_event_api_destination.webhook.arn | ||
| role_arn = aws_iam_role.eventbridge.arn | ||
|
|
||
| input_transformer { | ||
| input_paths = { | ||
| id = "$.id" | ||
| time = "$.time" | ||
| account = "$.account" | ||
| region = "$.region" | ||
| repo = "$.detail.repository-name" | ||
| digest = "$.detail.image-digest" | ||
| tag = "$.detail.image-tag" | ||
| } | ||
|
|
||
| input_template = <<-EOF | ||
| { | ||
| "specversion": "1.0", | ||
| "id": "<id>", | ||
| "type": "com.amazon.ecr.image.push", | ||
| "source": "urn:aws:ecr:<region>:<account>:repository/<repo>", | ||
| "subject": "<repo>:<tag>", | ||
| "time": "<time>", | ||
| "datacontenttype": "application/json", | ||
| "data": { | ||
| "repositoryName": "<repo>", | ||
| "imageDigest": "<digest>", | ||
| "imageTag": "<tag>", | ||
| "registryId": "<account>" | ||
| } | ||
| } | ||
| EOF | ||
| } | ||
|
|
||
| retry_policy { | ||
| maximum_event_age_in_seconds = 3600 | ||
| maximum_retry_attempts = 3 | ||
| } | ||
| } |
This file contains hidden or 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,19 @@ | ||
| output "eventbridge_rule_arn" { | ||
| description = "ARN of the EventBridge rule" | ||
| value = aws_cloudwatch_event_rule.ecr_push.arn | ||
| } | ||
|
|
||
| output "api_destination_arn" { | ||
| description = "ARN of the API destination" | ||
| value = aws_cloudwatch_event_api_destination.webhook.arn | ||
| } | ||
|
|
||
| output "eventbridge_role_arn" { | ||
| description = "ARN of the EventBridge IAM role" | ||
| value = aws_iam_role.eventbridge.arn | ||
| } | ||
|
|
||
| output "webhook_endpoint" { | ||
| description = "Configured webhook endpoint URL" | ||
| value = var.webhook_url | ||
| } |
This file contains hidden or 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,32 @@ | ||
| variable "aws_region" { | ||
| description = "AWS region for ECR and EventBridge" | ||
| type = string | ||
| default = "us-east-1" | ||
| } | ||
|
|
||
| variable "webhook_url" { | ||
| description = "ArgoCD Image Updater webhook endpoint URL" | ||
| type = string | ||
| # Example: "https://image-updater-webhook.example.com/webhook?type=cloudevents" | ||
| } | ||
|
|
||
| variable "webhook_secret" { | ||
| description = "Secret for webhook authentication" | ||
| type = string | ||
| sensitive = true | ||
| } | ||
|
|
||
| variable "ecr_repository_filter" { | ||
| description = "List of ECR repository names to monitor (empty list = all repositories)" | ||
| type = list(string) | ||
| default = [] | ||
| } | ||
|
|
||
| variable "tags" { | ||
| description = "Tags to apply to all resources" | ||
| type = map(string) | ||
| default = { | ||
| ManagedBy = "Terraform" | ||
| Purpose = "ArgoCD-Image-Updater" | ||
| } | ||
| } | ||
This file contains hidden or 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,92 @@ | ||
| #!/bin/bash | ||
| # Test script for CloudEvents webhook | ||
| # Usage: ./test-webhook.sh <webhook-url> <secret> | ||
|
|
||
| set -e | ||
|
|
||
| WEBHOOK_URL="${1:-http://localhost:8080/webhook?type=cloudevents}" | ||
| SECRET="${2:-test-secret}" | ||
|
|
||
| echo "Testing CloudEvents webhook..." | ||
| echo "URL: ${WEBHOOK_URL}" | ||
| echo "" | ||
|
|
||
| # Test 1: ECR event | ||
| echo "Test 1: AWS ECR push event" | ||
| curl -X POST "${WEBHOOK_URL}" \ | ||
| -H "Content-Type: application/json" \ | ||
| -H "X-Webhook-Secret: ${SECRET}" \ | ||
| -d '{ | ||
| "specversion": "1.0", | ||
| "id": "test-ecr-event-001", | ||
| "type": "com.amazon.ecr.image.push", | ||
| "source": "urn:aws:ecr:us-east-1:123456789012:repository/my-app", | ||
| "subject": "my-app:v1.2.3", | ||
| "time": "2025-11-27T10:00:00Z", | ||
| "datacontenttype": "application/json", | ||
| "data": { | ||
| "repositoryName": "my-app", | ||
| "imageDigest": "sha256:abcdef1234567890", | ||
| "imageTag": "v1.2.3", | ||
| "registryId": "123456789012" | ||
| } | ||
| }' \ | ||
| -w "\nHTTP Status: %{http_code}\n\n" | ||
|
|
||
| # Test 2: Generic container event | ||
| echo "Test 2: Generic container push event" | ||
| curl -X POST "${WEBHOOK_URL}" \ | ||
| -H "Content-Type: application/cloudevents+json" \ | ||
| -H "X-Webhook-Secret: ${SECRET}" \ | ||
| -d '{ | ||
| "specversion": "1.0", | ||
| "id": "test-generic-event-001", | ||
| "type": "container.image.push", | ||
| "source": "https://registry.example.com", | ||
| "subject": "myapp/backend:v2.0.0", | ||
| "time": "2025-11-27T10:05:00Z", | ||
| "datacontenttype": "application/json", | ||
| "data": { | ||
| "repository": "myapp/backend", | ||
| "tag": "v2.0.0", | ||
| "digest": "sha256:fedcba0987654321", | ||
| "registryUrl": "registry.example.com" | ||
| } | ||
| }' \ | ||
| -w "\nHTTP Status: %{http_code}\n\n" | ||
|
|
||
| # Test 3: Missing secret (should fail) | ||
| echo "Test 3: Missing secret (expected to fail)" | ||
| curl -X POST "${WEBHOOK_URL}" \ | ||
| -H "Content-Type: application/json" \ | ||
| -d '{ | ||
| "specversion": "1.0", | ||
| "id": "test-fail-event-001", | ||
| "type": "com.amazon.ecr.image.push", | ||
| "source": "urn:aws:ecr:us-east-1:123456789012:repository/test", | ||
| "subject": "test:latest", | ||
| "data": { | ||
| "repositoryName": "test", | ||
| "imageTag": "latest", | ||
| "registryId": "123456789012" | ||
| } | ||
| }' \ | ||
| -w "\nHTTP Status: %{http_code}\n\n" || echo "Expected failure" | ||
|
|
||
| # Test 4: Invalid event type (should fail) | ||
| echo "Test 4: Invalid event type (expected to fail)" | ||
| curl -X POST "${WEBHOOK_URL}" \ | ||
| -H "Content-Type: application/json" \ | ||
| -H "X-Webhook-Secret: ${SECRET}" \ | ||
| -d '{ | ||
| "specversion": "1.0", | ||
| "id": "test-invalid-event-001", | ||
| "type": "com.example.database.updated", | ||
| "source": "https://db.example.com", | ||
| "data": { | ||
| "table": "users" | ||
| } | ||
| }' \ | ||
| -w "\nHTTP Status: %{http_code}\n\n" || echo "Expected failure" | ||
|
|
||
| echo "Testing complete!" |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.