当前位置:网站首页>Harbor webhook from principle to construction
Harbor webhook from principle to construction
2022-07-01 11:24:00 【huzai9527】
1. What is? webhook
- The following is from wiki


- To sum up : webhook It can be regarded as a hook , For example, when we start from harbor When you pull up the image ,harbor Will transform the request into a readable JSON Format , And forward it to webhook endpoint( Implementation of hook ) To deal with . The following is harbor Yes pull Requested json Generalization , Including the specific information of the image , User specific information and project information, etc . When endpoint After getting this information , You can carry out relevant operations according to the information . For example pull To check malicious files , If the inspection fails, stop pull The operation of .
{
"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 How to use webhook
- Official documents

- Just like what I summarized above ,webhook endpoint according to harbor The transmitted information scans the image , Finally, perform relevant operations according to the scanning results .
3. How to open harbor webhook
- harbor To configure , Choose from your project webhook, Just configure it directly , Pay attention here endpoint The address is one of our own httpserver.

- Enable in global configuration webhook testing , In system settings in configuration management

4. Construct a webhook endpoint
- there webhook endpoint It's actually a httpserver, Here we use gin Construct a httpserver, And will harbor Print out the information coming through , Just look here api that will do
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
}
- Next launch server And from harbor in pull Mirror image , We can do it in server The terminal sees the following information

- Of course , We can also send the requested json Turn into go Objects in the , Then get the information from the object
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 How to feed back the results to users
- differ
docker plugin, harbor webhook There is no correlation authorization library , It can eliminate the scanning results and directly return relevant results ( allow / prohibit ), It looks like this
func defaultAuthResponse() *authorization.Response {
return &authorization.Response{
Allow: true,
}
}
- therefore , If relevant blocking operation is required , We need to pass harbor Of api To carry out relevant operations , In fact, the above authorization Library is right docker api Encapsulation , I was still issue 了 harbor webhook The maintainer of


- therefore , To sum up , First harbor Turn the operation request into json Send to webhook endpoint, then enpoint Conduct relevant inspections , Last ,enpoint You can call harbor api Carry out relevant operations , I haven't thought about the operation yet , So there are no examples , To be continued ...
边栏推荐
- 分享psd格式怎么预览的方法和psd文件缩略图插件[通俗易懂]
- Get key code
- 今天开户今天能买股票吗?在线开户是很安全么?
- Unittest框架中跳过要执行的测试用例
- No statements may be issued when any streaming result sets are open and in use on a given connection
- 名创拟7月13日上市:最高发行价22.1港元 单季净利下降19%
- Continuous delivery -pipeline getting started
- Mysql的四个隔离级别是如何实现的 (简要)
- sshd_ Discussion on permitrotlogin in config
- 编译调试Net6源码
猜你喜欢

Unittest框架中跳过要执行的测试用例

"Target detection" + "visual understanding" to realize the understanding and translation of the input image (with source code)

CAD如何设置标注小数位

CPI tutorial - asynchronous interface creation and use

Tempest HDMI leak receive 5

Redis的攻击手法

The first anniversary of the data security law, which four major changes are coming?

Skip the test cases to be executed in the unittest framework

技术分享 | Linkis参数介绍

Intel Labs annonce de nouveaux progrès en photonique intégrée
随机推荐
kafuka学习之路(一)kafuka安装和简单使用
Unittest框架中跳过要执行的测试用例
redis常识
金融壹账通拟7月4日香港上市:2年亏近30亿 市值蒸发超90%
No statements may be issued when any streaming result sets are open and in use on a given connection
The developer said, "this doesn't need to be tested, just return to the normal process". What about the testers?
Can servers bundled with flask be safely used in production- Is the server bundled with Flask safe to use in production?
redis中value/SortedSet
Cvpr22 | CMT: efficient combination of CNN and transformer (open source)
Technology sharing | introduction to linkis parameters
Jd.com renewed its cooperation with Tencent: issuing class A shares to Tencent with a maximum value of US $220million
BAIC bluevale: performance under pressure, extremely difficult period
Intel Labs announces new progress in integrated photonics research
Node version manager NVM installation and switching
京东与腾讯续签合作:向腾讯发行A类股 价值最高达2.2亿美元
Network security learning notes 01 network security foundation
Nordic nrf52832 flash download M4 error
Epoll introduction
华泰证券网上开户安全吗?
Openinstall: wechat applet jump to H5 configuration service domain name tutorial