Skip to content

Add EventBridge webhook endpoint for OSS events with configurable CDN refresh#8

Merged
daflyinbed merged 9 commits intoaliyunfrom
copilot/implement-aliyun-event-handler
Jan 17, 2026
Merged

Add EventBridge webhook endpoint for OSS events with configurable CDN refresh#8
daflyinbed merged 9 commits intoaliyunfrom
copilot/implement-aliyun-event-handler

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Jan 16, 2026

  • Understand the issue requirements and existing CDN refresh code
  • Research Aliyun OSS EventBridge event format
  • Create demo.jsonl with sample OSS event payloads
  • Implement event structures in Rust (EventBridge payload, OSS data)
  • Implement configurable bucket-to-URL mapping via bucket_url_map in AliyunConfig
  • Create POST /api/aliyun/events handler
  • Integrate with existing CDN refresh client
  • Update routing and OpenAPI documentation
  • Test the implementation with cargo build and cargo clippy
  • Request code review (passed with no issues)
  • Add JWT authentication via x-eventbridge-signature-token header
  • Change handler to accept serde_json::Value and log raw payload for debugging
  • Address code review feedback:
    • Made Claims.sub optional to match validation behavior
    • Changed logging to debug level to avoid exposing sensitive data
    • Added URL encoding for object keys
    • Added OpenAPI security scheme for eventbridge_token
    • Updated route comment for clarity
    • Replaced hardcoded bucket mapping with configurable bucket_url_map HashMap
  • Complete implementation

Key Features

Configurable Bucket Mapping: The bucket-to-URL mapping is now fully configurable through the bucket_url_map field in the Aliyun configuration section. Administrators can add or modify bucket mappings without code changes by editing the configuration file.

URL Template Support: URL templates support the {object_key} placeholder which is dynamically replaced with the actual percent-encoded object key. Example configuration:

[aliyun.bucket_url_map]
prts-static = "https://static.prts.wiki/{object_key}"
ak-media = "https://media.prts.wiki/{object_key}"

Security: Custom JWT authentication via x-eventbridge-signature-token header with proper OpenAPI documentation.

Debugging: Raw event payload logging at debug level for troubleshooting without exposing sensitive data in production logs.

Original prompt

This section details on the original issue you should resolve

<issue_title>通过阿里云 event_bridge 对 OSS 上传/删除事件触发对应域名 CDN 刷新</issue_title>
<issue_description>## 需求描述
aliyun 分支中,实现一个接口 POST /api/aliyun/events,用于接收来自阿里云 event_bridge 的 OSS 上传与删除事件,并根据对象存储桶触发对应 CDN 域名刷新。

事件参数

参数示例详见 demo.jsonl,事件包括但不限于:

  • ObjectCreated:AppendObject
  • ObjectCreated:CompleteMultipartUpload
  • ObjectCreated:CopyObject
  • ObjectCreated:PostObject
  • ObjectCreated:PutObject
  • ObjectCreated:PutSymlink
  • ObjectRemoved:DeleteObject
  • ObjectRemoved:AbortMultipartUpload
demo.jsonl
{
    "datacontenttype": "application/json;charset=utf-8",
    "aliyunaccountid": "164901546557****",
    "data": {
        "region": "cn-beijing",
        "eventVersion": "1.0",
        "eventSource": "acs:oss",
        "eventName": "ObjectCreated:AppendObject",
        "eventTime": "2021-08-13T06:45:43.000Z",
        "requestParameters": {
            "sourceIPAddress": "118.31.XX.XX"
        },
        "userIdentity": {
            "principalId": "28815334868278****"
        },
        "responseElements": {
            "requestId": "61161517B258223732BC****"
        },
        "oss": {
            "bucket": {
                "name": "oss-source-bucket1-cn-beijing",
                "arn": "acs:oss:cn-beijing:164901546557****:oss-source-bucket1-cn-beijing",
                "ownerIdentity": "164901546557****"
            },
            "ossSchemaVersion": "1.0",
            "object": {
                "size": 9,
                "deltaSize": 9,
                "eTag": "F0F18C2C66AE1DD512BDCD4366F7****",
                "key": "objectname",
                "position": 0
            }
        }
    },
    "subject": "acs:oss:cn-beijing:164901546557****:oss-source-bucket1-cn-beijing/1628837143916",
    "aliyunoriginalaccountid": "164901546557****",
    "source": "acs.oss",
    "type": "oss:ObjectCreated:AppendObject",
    "aliyunpublishtime": "2021-08-13T06:45:43.986Z",
    "specversion": "1.0",
    "aliyuneventbusname": "default",
    "id": "61161517B258223732BC****",
    "time": "2021-08-13T06:45:43Z",
    "aliyunregionid": "cn-beijing"
}


{
    "datacontenttype": "application/json;charset=utf-8",
    "aliyunaccountid": "164901546557****",
    "data": {
        "region": "cn-beijing",
        "eventVersion": "1.0",
        "eventSource": "acs:oss",
        "eventName": "ObjectCreated:CompleteMultipartUpload",
        "eventTime": "2021-08-13T06:45:43.000Z",
        "requestParameters": {
            "sourceIPAddress": "118.31.XX.XX"
        },
        "userIdentity": {
            "principalId": "28815334868278****"
        },
        "responseElements": {
            "requestId": "61161517B258223732BC****"
        },
        "oss": {
            "bucket": {
                "name": "oss-source-bucket1-cn-beijing",
                "arn": "acs:oss:cn-beijing:164901546557****:oss-source-bucket1-cn-beijing",
                "ownerIdentity": "164901546557****"
            },
            "ossSchemaVersion": "1.0",
            "object": {
                "size": 9,
                "deltaSize": 9,
                "eTag": "F0F18C2C66AE1DD512BDCD4366F7****",
                "key": "objectname"
            }
        }
    },
    "subject": "acs:oss:cn-beijing:164901546557****:oss-source-bucket1-cn-beijing/1628837143916",
    "aliyunoriginalaccountid": "164901546557****",
    "source": "acs.oss",
    "type": "oss:ObjectCreated:CompleteMultipartUpload",
    "aliyunpublishtime": "2021-08-13T06:45:43.986Z",
    "specversion": "1.0",
    "aliyuneventbusname": "default",
    "id": "61161517B258223732BC****",
    "time": "2021-08-13T06:45:43Z",
    "aliyunregionid": "cn-beijing"
}


{
    "datacontenttype": "application/json;charset=utf-8",
    "aliyunaccountid": "164901546557****",
    "data": {
        "region": "cn-beijing",
        "eventVersion": "1.0",
        "eventSource": "acs:oss",
        "eventName": "ObjectCreated:CopyObject",
        "eventTime": "2021-08-13T06:45:43.000Z",
        "requestParameters": {
            "sourceIPAddress": "118.31.XX.XX"
        },
        "userIdentity": {
            "principalId": "28815334868278****"
        },
        "responseElements": {
            "requestId": "61161517B258223732BC****"
        },
        "oss": {
            "bucket": {
                "name": "oss-source-bucket1-cn-beijing",
                "arn": "acs:oss:cn-beijing:164901546557****:oss-source-bucket1-cn-beijing",
                "ownerIdentity": "164901546557****"
            },
            "ossSchemaVersion": "1.0",
            "object": {
                "size": 9,
                "deltaSize": 9,
                "eTag": "F0F18C2C66AE1DD512BDCD4366F7****",
                "key": "objectname"
            }
        }
    },
    "subject": "acs:oss:cn-beijing:164901546557****:oss-source-bucket1-cn-beijing/1628837143916",
    "aliyunoriginalaccountid": "164901546557****",
    "source": "acs.oss",
    "type"...

</details>



<!-- START COPILOT CODING AGENT SUFFIX -->

- Fixes daflyinbed/janus#7

<!-- START COPILOT CODING AGENT TIPS -->
---

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more [Copilot coding agent tips](https://gh.io/copilot-coding-agent-tips) in the docs.

Co-authored-by: daflyinbed <21363956+daflyinbed@users.noreply.github.com>
Copilot AI changed the title [WIP] Add API for handling OSS event triggers Add EventBridge webhook endpoint for OSS events with CDN refresh Jan 16, 2026
Copilot AI requested a review from daflyinbed January 16, 2026 09:30
pub async fn handle_oss_events(
State(state): State<AppState>,
headers: HeaderMap,
Json(payload): Json<OssEventPayload>,
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@copilot 这里直接接受serde_json::Value 在handler里面打印收到的整个json 然后再parse成OssEventPayload

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Updated in commit cf1b76a. The handler now accepts serde_json::Value, logs the entire received JSON with tracing::info!(), then parses it into OssEventPayload.

Co-authored-by: daflyinbed <21363956+daflyinbed@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds a webhook endpoint for Aliyun EventBridge to receive OSS (Object Storage Service) events and automatically trigger CDN cache refresh for the affected objects. The implementation uses custom JWT authentication via the x-eventbridge-signature-token header and maps OSS bucket names to CDN domains.

Changes:

  • Added POST /api/aliyun/events endpoint to handle OSS EventBridge events
  • Restructured router to separate public routes from JWT-protected routes
  • Modified JWT validation to skip standard claim validation for EventBridge tokens
  • Created sample event data in demo.jsonl for testing

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 7 comments.

File Description
src/routes/mod.rs Splits router into public and protected routes, adds EventBridge endpoint to public routes with custom auth
src/routes/aliyun_handlers.rs Implements OSS event handler with data structures, bucket-to-domain mapping, and CDN refresh integration
src/auth.rs Modifies JWT validation to skip required spec claims validation
demo.jsonl Provides sample OSS event payloads for testing various event types

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +228 to +239
#[utoipa::path(
post,
tag = "aliyun",
path = "/aliyun/events",
request_body = OssEventPayload,
responses(
(status = OK, description = "Successfully processed OSS event and triggered CDN refresh", body = OssEventResponse),
(status = UNAUTHORIZED, description = "Missing or invalid x-eventbridge-signature-token"),
(status = BAD_REQUEST, description = "Invalid request or unsupported bucket"),
(status = INTERNAL_SERVER_ERROR, description = "Internal server error")
)
)]
Copy link

Copilot AI Jan 17, 2026

Choose a reason for hiding this comment

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

The OpenAPI documentation for this endpoint doesn't specify a security scheme for the x-eventbridge-signature-token header. This makes the API documentation incomplete and potentially confusing for API consumers. Consider adding a custom security scheme in the SecurityAddon modifier and applying it to this endpoint using the security attribute in the utoipa::path macro, similar to how bearer_auth is used for other endpoints.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@copilot apply changes based on this feedback

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Applied in commit 61c5e20. Added eventbridge_token security scheme to OpenAPI documentation and applied it to the /aliyun/events endpoint.

daflyinbed and others added 2 commits January 17, 2026 14:05
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…add URL encoding, update OpenAPI security

Co-authored-by: daflyinbed <21363956+daflyinbed@users.noreply.github.com>
@daflyinbed
Copy link
Copy Markdown
Collaborator

@copilot 配置的aliyun字段下面添加一个map字段 key是oss的bucket的名字 value是一个字符串作为url的模板,handler里面拿到bucket_name之后查map找到到对应的url模板 用object_key替换里面的变量。

Co-authored-by: daflyinbed <21363956+daflyinbed@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Jan 17, 2026

@copilot 配置的aliyun字段下面添加一个map字段 key是oss的bucket的名字 value是一个字符串作为url的模板,handler里面拿到bucket_name之后查map找到到对应的url模板 ...

Implemented in commit c2f789e. Added bucket_url_map field to AliyunConfig as a HashMap. The handler now looks up the URL template from the config map and replaces {object_key} with the actual encoded object key. Updated example.toml with configuration examples.

Copilot AI changed the title Add EventBridge webhook endpoint for OSS events with CDN refresh Add EventBridge webhook endpoint for OSS events with configurable CDN refresh Jan 17, 2026
@daflyinbed daflyinbed marked this pull request as ready for review January 17, 2026 07:18
@daflyinbed daflyinbed merged commit ec3d564 into aliyun Jan 17, 2026
2 checks passed
@daflyinbed daflyinbed deleted the copilot/implement-aliyun-event-handler branch January 19, 2026 06:17
daflyinbed added a commit that referenced this pull request Jan 19, 2026
…le CDN refresh (#8)

* Initial plan

* Add OSS EventBridge event handler with bucket-to-domain mapping

Co-authored-by: daflyinbed <21363956+daflyinbed@users.noreply.github.com>

* auth

* Accept serde_json::Value and log raw OSS event payload for debugging

Co-authored-by: daflyinbed <21363956+daflyinbed@users.noreply.github.com>

* Update src/routes/mod.rs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Address code review feedback: fix Claims sub field, improve logging, add URL encoding, update OpenAPI security

Co-authored-by: daflyinbed <21363956+daflyinbed@users.noreply.github.com>

* Replace hardcoded bucket mapping with configurable bucket_url_map

Co-authored-by: daflyinbed <21363956+daflyinbed@users.noreply.github.com>

* update

* update

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: daflyinbed <21363956+daflyinbed@users.noreply.github.com>
Co-authored-by: 夕舞八弦 <1677759063@qq.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
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.

3 participants