当前位置:网站首页>harbor scanner 从原理到构建
harbor scanner 从原理到构建
2022-07-21 03:53:00 【huzai9527】
harbor scanner 从原理到构建
什么是harbor scanner
- scanner 故名思义扫描器,扫描的内容是什么取决于前面的定语,例如图像扫描器扫描的是图像,PDF扫描器扫描的是PDF,当然harbor scanner扫描的是harbor,harbor是啥想必大家都知道了,作为一款开源的镜像管理工具,扫描harbor其实就是扫描harbor中的image。
harbor scanner的原理是什么
最初的提案
proposal: Pluggable Image Vulnerability Scanning有需要的可以直接点链接进去, 这里我做一些总结性的概括
在harbor已经支持clair(一款漏洞扫描工具)的基础上,为了让harbor能够支持更多的漏扫工具(大家也都知道,一些做安全的公司自己内部是会维护和积累漏洞信息的,但是这样的信息并不会开源出来),因此harbor有必要提供一个通用的框架能够使用户能够自定义sanner,以此检查harbor中image的漏洞信息。如是,harborsanner应运而生。
harbor scanner 定义了一些列的标准数据结构,包括scanner的元信息(application/vnd.scanner.adapter.scanner.metadata+json)、扫描结果(application/vnd.scanner.adapter.vuln.report.harbor+json)、扫描请求(application/vnd.scanner.adapter.scan.request+json)等等。倘若我们要将自己的漏扫工具适配harbor,我们就需要严格按照这些规范做适配器(adapter)。
拿两张提案中的图稍微看一下
如上图1,harbor 已经帮我们实现了上三层、公司(漏扫软件)已经实现了最下面的一层,我们需要做的就是将这两大层用adapter连接起来,从时序图2可以看到整个流程,我总结一下
- 首先harbor发送扫描请求到adapter,adapter负责解析请求,
- 再将发送请求到漏扫软件所在的服务器, 服务器下载相关的需要扫描的镜像
- 使用漏扫进行扫描,然后将扫描结果发送给adapter
- adapter将漏扫扫出来的结果转换为harbor 规定的标准形式,发送给harbor
DEBUG harbor scanner
scanner 心跳检测
harbor 发送GET请求
/api/v1/metadata
到adater, 观察 adapter 的回复,判定该 scanner 是否处于健康状态。harbor 期望得到的回复
{ "scanner":{ "name":"test", "vendor":"chaintin", "version":"1.0.0" }, "capabilities":[ { "consumes_mime_types":[ "application/vnd.oci.image.manifest.v2+json", "application/vnd.docker.distribution.manifest.v2+json" ], "produces_mime_types":[ "application/vnd.scanner.adapter.vuln.report.harbor+json; version=1.0", "application/vnd.scanner.adapter.vuln.report.raw+json" ] } ], "properties":{ "harbor.scanner-adapter/scanner-type":"os-package-vulnerability", "harbor.scanner-adapter/vulnerability-database-updated-at":"" } }
- scanner 是一些描述信息
- capabilities 描述 adapter 能够处理和返回的各种信息格式
- properties 表示harbor需要的其他一些属性信息
- 注意!!! 这里的
consumes_mime_types
,produces_mime_types
是有相关格式的
- 注意!!! 这里的
倘若能够的到正确的回复,harbor 会显示该 scanner 处于健康状态
harbor 发送扫描请求
当用户使用harbor sacnner 扫描镜像时,harbor 会发送如下的请求到 adapter
{ "registry":{ "url":"http://core:8080", "authorization":"Basic cm9ib3QkbGlicmFyeSt0ZXN0LTcyYjcxNWJkLTAzNDEtMTFlZC04N2RjLTAyNDJhYzEyMDAwNDphRjI3YTYwTUNaeVVkN1lFcjNXenRkYlFmd1RlTHlnaQ==" }, "artifact":{ "repository":"library/centos", "tag":"test", "digest":"sha256:3feb5e7dbf719ba44ebe8f9ae85d3a28c3d3e0acafbb768581b24d506a2bf907", "mime_type":"application/vnd.docker.distribution.manifest.v2+json" } }
// 转成go结构,就是这样 type metadata struct { Registry struct { URL string `json:"url"` Authorization string `json:"authorization"` } `json:"registry"` Artifact struct { Repository string `json:"repository"` Tag string `json:"tag"` Digest string `json:"digest"` MimeType string `json:"mime_type"` } `json:"artifact"` }
此时 adapter 收到 harbor 发送的扫描请求后,需要返回是与此扫描事件对应的 scanid
这里我看了几个开源项目的原码
- clair 是直接通过
jobID := uuid.New().String()
生成的scanner id - trivy 整个框架几乎和 Clair 一模一样,它的scanner id 也是通过 uuid 直接生成
- anchore 独具一格,通过
scanId := base64.URLEncoding.EncodeToString([]byte(fmt.Sprintf(
%[email protected]%s, repository, digest)))
将 reqest 中的rep 和 digest 信息通过编码生成id
- clair 是直接通过
这个id 非常关键, 后面 harbor 需要根据这个 id 从 adapter 请求扫描结果
这里我用的 anchore 生成 id 的方法
type ScanResponse struct { ID string `json:"id"` }
harbor 请求扫描结果
在给 harbor 返回 scanid 之前,adapter 还需要做一系例的工作
- 将需要扫描的镜像信息传送给漏扫
- 控制漏扫下载相关镜像
- 将漏扫生成的报告转换成harbor 需要的格式
- 存储结果与scanid对应
在上述事件全部安排下去之后,并不需要完成(因为harbor也不知道这些工作是否已经完成),harbor会向 adapter 请求扫描结果,请求结构用 gin 路由表示为
/api/v1/scan/:id/report
当然这个过程中漏扫可能并没有完成,因此需要分情况讨论
- 未完成时,返回时http 302 状态码,此时 harbir 会重新发送请求
完成,则返回 200 状态码以及扫描结果
总结
- 至此,需要完成一个 harbor scanner 的工作流程就非常清晰了,实际上只需做一层 adapter 连接 harbor 和 漏扫就可以了。
- adapter 最少需要三个 api 分别对应
- 心跳检测
- 请求扫描
- 获取扫描结果
- 其中每一步的请求和返回结果是有固定格式的,开发之前务必了解清楚。
边栏推荐
猜你喜欢
QT (37) -mosquitto mqtt client
动态内存管理2之柔性数组
C language file operation
Distributed High availability and high scalability index
Strcspn, strchr special character verification
List容器的系列操作( 详解 )
MySQL optimization summary I
Advanced C language (XIV) - Document Management
Introduction to ESP privilege isolation mechanism
四种bean拷贝工具对比
随机推荐
"PHP implementation of Domain Driven Design" - integration context [reprint]
Test de pénétration - vulnérabilité d'inclusion de fichiers et application de pseudo - protocoles PHP
Metahuman Face材质球总结
h5在微信内自定义分享遇到的坑
Software test interview question: briefly describe what you have done in your previous work and what you are familiar with. The reference answers are as follows.
Don't want to wake up because it's delicious
MYSQL07_ Get date function, aggregate function, string function
QT (37) -mosquitto mqtt client
Kingbasees v8r6 is there any way to not read sys by default_ System view under catalog?
Liunx kills processes with the same name in batches
线程池.线程数量设置
Tencent im practice: low code and ultra fast instant address book
LBA converted to CHS formula
SQL optimization (IX): paging statement optimization
Implementation method of SuperMap iclient for openlayers layer group control
Pytoch environment construction
Strcspn, strchr special character verification
渗透测试-文件包含漏洞以及php伪协议的应用
C language file operation
Day009 circular structure (exercise)