当前位置:网站首页>Harbor webhook从原理到构建
Harbor webhook从原理到构建
2022-07-01 11:00:00 【huzai9527】
1. 什么是webhook
- 以下内容来自wiki


- 总结一下: webhook可以看作是一个钩子,例如当我们从harbor上拉取镜像时,harbor会将请求转换成一个可读的JSON格式,并且转发给webhook endpoint(钩子的实现)进行处理。下面就是harbor对pull请求的json概括,包括了镜像的具体信息,用户的具体信息以及项目信息等等。当endpoint拿到这些信息之后,就可以根据信息进行相关的操作。例如对pull的镜像进行恶意文件检查,如果检查不通过就阻止pull的操作。
{
"type":"PULL_ARTIFACT",
"occur_at":1656324256,
"operator":"admin",
"event_data":{
"resources":[
{
"digest":"sha256:f841a2abd0422364ec94bb633a56707a38c330179f2bbccebd95f9aff4a36808",
"tag":"sha256:f841a2abd0422364ec94bb633a56707a38c330179f2bbccebd95f9aff4a36808",
"resource_url":"10.9.33.98/library/[email protected]:f841a2abd0422364ec94bb633a56707a38c330179f2bbccebd95f9aff4a36808"
}
],
"repository":{
"date_created":1656315229,
"name":"java",
"namespace":"library",
"repo_full_name":"library/java",
"repo_type":"private"
}
}
}
2. harbor如何使用webhook
- 官方文档

- 就跟我上面总结的一样,webhook endpoint 根据 harbor 传过来的信息对镜像进行相关的扫描操作,最后根据扫描结果执行相关操作。
3. 如何开启harbor webhook
- harbor配置,在你的项目中选择webhook,直接配置就行,这里要注意endpoint地址是我们自己实现的一个httpserver。

- 在全局配置中开启webhook检测,在配置管理中的系统设置中

4. 构建一个webhook endpoint
- 这里的webhook endpoint实际上就是一个httpserver,这里我们使用gin构建一个httpserver,并将harbor穿过来的信息打印出来,这里只要看api即可
func main() {
r := gin.Default()
r.POST("/push_image", func(c *gin.Context) {
postData := &PushImage{
}
data, _ := ioutil.ReadAll(c.Request.Body)
fmt.Println("string => ", string(data))
if err := json.Unmarshal(data, &postData); err != nil {
fmt.Println(err)
}
fmt.Printf("ctx.Request.body: %s", postData.EventData.Resources[0].Digest)
})
r.POST("/pullimage", func(c *gin.Context) {
postData := &PullImage{
}
data, _ := ioutil.ReadAll(c.Request.Body)
if err := json.Unmarshal(data, &postData); err != nil {
fmt.Println(err)
}
fmt.Println("api TYPE => ", postData.Type)
fmt.Println("iamge name => ", postData.EventData.Resources[0].ResourceURL)
})
r.POST("/api", func(c *gin.Context) {
var body map[string]interface{
}
data, _ := ioutil.ReadAll(c.Request.Body)
if err := json.Unmarshal(data, &body); err != nil {
fmt.Println(err)
}
fmt.Println("body data => ", string(data))
for k, v := range c.Request.Header {
fmt.Println(k, v)
}
})
r.Run() // listen and serve on 0.0.0.0:8080
}
- 接下来启动server并从harbor中pull镜像,我们可以在server终端看到如下的信息

- 当然,我们也可以将请求的json转成go中的对象,然后从对象中获取信息
type PushImage struct {
Type string `json:"type"`
OccurAt int `json:"occur_at"`
Operator string `json:"operator"`
EventData struct {
Resources []struct {
Digest string `json:"digest"`
Tag string `json:"tag"`
ResourceURL string `json:"resource_url"`
} `json:"resources"`
Repository struct {
DateCreated int `json:"date_created"`
Name string `json:"name"`
Namespace string `json:"namespace"`
RepoFullName string `json:"repo_full_name"`
RepoType string `json:"repo_type"`
} `json:"repository"`
} `json:"event_data"`
}
type PullImage struct {
Type string `json:"type"`
OccurAt int `json:"occur_at"`
Operator string `json:"operator"`
EventData struct {
Resources []struct {
Digest string `json:"digest"`
Tag string `json:"tag"`
ResourceURL string `json:"resource_url"`
} `json:"resources"`
Repository struct {
DateCreated int `json:"date_created"`
Name string `json:"name"`
Namespace string `json:"namespace"`
RepoFullName string `json:"repo_full_name"`
RepoType string `json:"repo_type"`
} `json:"repository"`
} `json:"event_data"`
}

5. endpoint的结果如何反馈给用户
- 不同于
docker plugin, harbor webhook没有相关的authorization库,能够根绝扫描结果直接返回相关结果(允许/禁止),就像下面这样
func defaultAuthResponse() *authorization.Response {
return &authorization.Response{
Allow: true,
}
}
- 因此,如果需要进行相关的阻断操作,我们需要通过harbor的api来进行相关操作,其实上面的authorization库就是对docker api的封装,当时我还issue 了 harbor webhook的维护者


- 所以,总结一下,首先harbor 将操作请求转成json发送给webhook endpoint,然后enpoint进行相关检查,最后,enpoint可以根据检查结果调用harbor api进行相关操作,关于操作这一部分我还没有考虑好,所以没有示例,待续。。。
边栏推荐
- NC | 肠道细胞和乳酸菌共同作用来防止念珠菌感染
- CVPR 2022 | Virtual Correspondence: Humans as a Cue for Extreme-View Geometry
- NC | intestinal cells and lactic acid bacteria work together to prevent Candida infection
- Packet mode and three streaming modes in SDP protocol
- PHP有哪些优势和劣势
- Half of 2022 has passed, isn't it sudden?
- 内存泄漏定位工具之 valgrind 使用
- Value 1000 graduation project campus information publishing platform website source code
- 12. Gateway new generation gateway
- CRC check
猜你喜欢
![[MPC] ② quadprog solves positive definite, semi positive definite and negative definite quadratic programming](/img/85/56b12fd664726e4776cab69ca91d57.png)
[MPC] ② quadprog solves positive definite, semi positive definite and negative definite quadratic programming

Error: missing revert data in call exception

12款大家都在用的產品管理平臺

Mobile hard drive reads but does not display drive letter

華為設備配置大型網絡WLAN基本業務

How to solve the problem of SQL?

Website source code whole site download website template source code download
![[.NET6]使用ML.NET+ONNX预训练模型整活B站经典《华强买瓜》](/img/b3/b117481fba7257453011e4cdb1eaaa.png)
[.NET6]使用ML.NET+ONNX预训练模型整活B站经典《华强买瓜》

Database experiment report (I)

Matplotlib data visualization Foundation
随机推荐
How does MySQL copy table data from one database to another (two databases are not linked to the same database)
华为HMS Core携手超图为三维GIS注入新动能
使用强大的DBPack处理分布式事务(PHP使用教程)
Mutual conversion of pictures in fluent uint8list format and pictures in file format
Compliance management of fund managers
爬虫(2) - Requests(1) | Requests模块的深度解析
想请教一下,我在广州,到哪里开户比较好?现在网上开户安全么?
12 plateformes de gestion de produits utilisées par tout le monde
The project bar on the left side of CodeBlocks disappears, workspace automatically saves the project, default workspace, open the last workspace, workspace (Graphic tutorial, solved)
Want to open an account, is it safe to open an account of Huatai Securities online?
Oracle和JSON的結合
LeetCode 438. Find all letter ectopic words in the string__ sliding window
转义字符串
[.NET6]使用ML.NET+ONNX预训练模型整活B站经典《华强买瓜》
Database experiment report (II)
Ten years of sharpening a sword: unveiling the secrets of ant group's observability platform antmonitor
The idea runs with an error command line is too long Shorten command line for...
Project0: Games
Ask everyone in the group about the fact that the logminer scheme of flick Oracle CDC has been used to run stably in production
Wireshark TS | confusion between fast retransmission and out of sequence