-
Notifications
You must be signed in to change notification settings - Fork 79
/
images.go
360 lines (326 loc) · 11.7 KB
/
images.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
// Copyright 2022 The kubegems.io Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package application
import (
"context"
"errors"
"fmt"
"net/url"
"sort"
"strconv"
"github.com/containerd/containerd/reference"
"github.com/gin-gonic/gin"
"github.com/goharbor/harbor/src/pkg/scan/vuln"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"kubegems.io/kubegems/pkg/log"
"kubegems.io/kubegems/pkg/service/handlers"
"kubegems.io/kubegems/pkg/service/models"
"kubegems.io/kubegems/pkg/utils/harbor"
)
const (
unpublishableLabelKey = "不可发布"
unpublishableLabelValue = "该镜像被标记为不可被使用"
)
var ErrNotManagedRegistry = errors.New("unsupported image")
type ImageTag struct {
TagName string `json:"name"`
Image string `json:"image"`
Unpublishable bool `json:"unpublishable"` // 不可发布
}
type ImageHandler struct {
BaseHandler
}
// @Tags ProjectImage
// @Summary 镜像安全报告
// @Description 镜像安全报告
// @Accept json
// @Produce json
// @Param tenant_id path int true "tenaut id"
// @Param project_id path int true "project id"
// @Param image query string true "eg. kubegems/nginx:v1.14"
// @Success 200 {object} handlers.ResponseStruct{Data=vuln.Report} "ok"
// @Router /v1/tenant/{tenant_id}/project/{project_id}/images/vulnerabilities [get]
// @Security JWT
func (h *ImageHandler) Vulnerabilities(c *gin.Context) {
onharborfunc := func(ctx context.Context, cli *harbor.Client, image string) (interface{}, error) {
vulnerabilities, err := cli.GetArtifactVulnerabilities(ctx, image)
if err != nil {
return nil, err
}
report := vuln.Report{}
for _, v := range *vulnerabilities {
report = v
}
// sort 按照漏洞等级排序
sort.Slice(report.Vulnerabilities, func(i, j int) bool {
return report.Vulnerabilities[i].Severity.Code() > report.Vulnerabilities[j].Severity.Code()
})
return report, nil
}
ocifunc := func(ctx context.Context, cli *harbor.OCIDistributionClient, image string) (interface{}, error) {
return vuln.Report{}, nil
}
h.OnHarborFunc(c, onharborfunc, ocifunc)
}
type ImageSummaryItem struct {
Image string `json:"image,omitempty"`
IsHarborRegistry bool `json:"isHarborRegistry,omitempty"` // 是否为受集群管理的镜像仓库且为 harbor 仓库
ScanStatus string `json:"scanStatus,omitempty"` // 扫描状态
Severity vuln.Severity `json:"severity,omitempty"` // 从低到高依次为 "None""Unknown" "Negligible" "Low""Medium""High""Critical"
Report interface{} `json:"report,omitempty"` // harbor 报告原文: {"fixable": 122,"summary": {"Critical": 3,"High": 10,"Low": 61,"Medium": 125},"total": 199}
Unpublishable bool `json:"unpublishable,omitempty"` // 不可发布状态,若为true则不可发布
Labels []harbor.Label `json:"labels,omitempty"` // harbor 标签
Status string `json:"status,omitempty"`
UpdatedAt *metav1.Time `json:"updatedAt,omitempty"` // time.Time.Format(RFC3339) 格式,若非可管理仓库则为空
}
// @Tags ProjectImage
// @Summary 镜像summary
// @Description 镜像summary
// @Accept json
// @Produce json
// @Param tenant_id path int true "tenaut id"
// @Param project_id path int true "project id"
// @Param image query string true "eg. kubegems/nginx:v1.14"
// @Success 200 {object} handlers.ResponseStruct{Data=vuln.Report} "ok"
// @Router /v1/tenant/{tenant_id}/project/{project_id}/images/summary [get]
// @Security JWT
func (h *ImageHandler) Summary(c *gin.Context) {
onharborfunc := func(ctx context.Context, cli *harbor.Client, image string) (interface{}, error) {
artifacts, err := cli.ListArtifact(ctx, image, harbor.GetArtifactOptions{
WithScanOverview: true,
WithLabel: true,
WithTag: true,
})
if err != nil {
return nil, err
}
summaries := []ImageSummaryItem{}
image, _ = harbor.SplitImageNameTag(image)
for _, artifact := range artifacts {
if artifact.Type != "IMAGE" || len(artifact.Tags) == 0 {
continue
}
item := ImageSummaryItem{
Image: image + ":" + artifact.Tags[0].Name,
UpdatedAt: &metav1.Time{Time: artifact.Artifact.PushTime},
Labels: artifact.Labels,
IsHarborRegistry: true,
Unpublishable: func() bool {
for _, label := range artifact.Labels {
if label.Name == unpublishableLabelKey {
return true
}
}
return false
}(),
}
for _, overview := range artifact.ScanOverview {
item.Report = overview.Summary
item.ScanStatus = overview.ScanStatus
item.Severity = overview.Severity
}
summaries = append(summaries, item)
}
paged := handlers.NewPageDataFromContext(c, summaries, nil, nil)
return paged, nil
}
ocifunc := func(_ context.Context, _ *harbor.OCIDistributionClient, _ string) (interface{}, error) {
summaries := []ImageSummaryItem{}
return handlers.NewPageDataFromContext(c, summaries, nil, nil), nil
}
h.OnHarborFunc(c, onharborfunc, ocifunc)
}
// @Tags ProjectImage
// @Summary 镜像不可发布标记
// @Description 镜像不可发布标记
// @Accept json
// @Produce json
// @Param tenant_id path int true "tenaut id"
// @Param project_id path int true "project id"
// @Param image query string true "eg. kubegems/nginx:v1.14"
// @Success 200 {object} handlers.ResponseStruct{Data=vuln.Report} "ok"
// @Router /v1/tenant/{tenant_id}/project/{project_id}/images/unpublishable [get]
// @Security JWT
func (h *ImageHandler) Unpublishable(c *gin.Context) {
onharborfunc := func(ctx context.Context, cli *harbor.Client, image string) (interface{}, error) {
isunpublishable, _ := strconv.ParseBool(c.Query("unpublishable"))
if isunpublishable {
if err := cli.AddArtifactLabelFromKey(ctx, image, unpublishableLabelKey, unpublishableLabelValue); err != nil {
return nil, err
}
} else {
if err := cli.DeleteArtifactLabelFromKey(ctx, image, unpublishableLabelKey); err != nil {
return nil, err
}
}
return "ok", nil
}
ocifunc := func(ctx context.Context, cli *harbor.OCIDistributionClient, image string) (interface{}, error) {
return "ok", nil
}
h.OnHarborFunc(c, onharborfunc, ocifunc)
}
// @Tags ProjectImage
// @Summary 镜像扫描
// @Description 触发镜像扫描
// @Accept json
// @Produce json
// @Param tenant_id path int true "tenaut id"
// @Param project_id path int true "project id"
// @Param image query string true "eg. kubegems/nginx:v1.14"
// @Success 200 {object} handlers.ResponseStruct{Data=vuln.Report} "ok"
// @Router /v1/tenant/{tenant_id}/project/{project_id}/images/scan [post]
// @Security JWT
func (h *ImageHandler) Scan(c *gin.Context) {
onharborfunc := func(ctx context.Context, cli *harbor.Client, image string) (interface{}, error) {
if err := cli.ScanArtifact(ctx, image); err != nil {
return nil, err
}
return "ok", nil
}
ocifunc := func(ctx context.Context, cli *harbor.OCIDistributionClient, image string) (interface{}, error) {
return "ok", nil
}
h.OnHarborFunc(c, onharborfunc, ocifunc)
}
// @Tags ProjectImage
// @Summary 镜像tags
// @Description 查询镜像tags
// @Accept json
// @Produce json
// @Param tenant_id path int true "tenaut id"
// @Param project_id path int true "project id"
// @Param application_id path int true "application id"
// @Param image query string true "eg. kubegems/nginx:v1.14"
// @Success 200 {object} handlers.ResponseStruct{Data=vuln.Report} "ok"
// @Router /v1/tenant/{tenant_id}/project/{project_id}/images/tags [post]
// @Security JWT
func (h *ImageHandler) ImageTags(c *gin.Context) {
harborfunc := func(ctx context.Context, cli *harbor.Client, image string) (interface{}, error) {
arts, err := cli.ListArtifact(ctx, image, harbor.GetArtifactOptions{
WithTag: true,
WithLabel: true,
})
if err != nil {
return nil, err
}
unpublishableLabeld := func(labels []harbor.Label) bool {
for _, label := range labels {
if label.Name == unpublishableLabelKey {
return true
}
}
return false
}
name, _ := harbor.SplitImageNameTag(image)
ret := []ImageTag{}
for _, item := range arts {
for _, tag := range item.Tags {
ret = append(ret, ImageTag{
TagName: tag.Name,
Image: name + ":" + tag.Name,
Unpublishable: unpublishableLabeld(item.Labels),
})
}
}
return ret, nil
}
ocifunc := func(ctx context.Context, cli *harbor.OCIDistributionClient, image string) (interface{}, error) {
fullname, _ := harbor.SplitImageNameTag(image)
tags, _ := cli.ListTags(ctx, image)
ret := []ImageTag{}
for _, tag := range tags.Tags {
ret = append(ret, ImageTag{
TagName: tag,
Image: fullname + ":" + tag,
})
}
return ret, nil
}
h.OnHarborFunc(c, harborfunc, ocifunc)
}
type (
HarborFunc func(ctx context.Context, cli *harbor.Client, image string) (interface{}, error)
OCIFunc func(ctx context.Context, cli *harbor.OCIDistributionClient, image string) (interface{}, error)
)
type RegistryOptions struct {
URL string
Username string
Password string
}
func (h *ImageHandler) OnHarborFunc(c *gin.Context, harborfun HarborFunc, ocifunc OCIFunc) {
process := func(ctx context.Context) (interface{}, error) {
params := struct {
ProjectID uint `uri:"project_id" binding:"required"`
TenautID uint `uri:"tenant_id" binding:"required"`
}{}
if err := c.ShouldBindUri(¶ms); err != nil {
return nil, err
}
image := c.Query("image")
if image == "" {
return nil, fmt.Errorf("empty image name")
}
u, _, _, _, _ := harbor.ParseImag(image)
if u == "docker.io" {
u = "index.docker.io"
}
u = "https://" + u
options := &RegistryOptions{
URL: u,
}
_ = h.completeRegistryOption(options, image, params.ProjectID) // ignore
// try harbor
cli := harbor.NewClient(options.URL, options.Username, options.Password)
if _, err := cli.SystemInfo(ctx); err != nil {
// try oci
ocicli := harbor.NewOCIDistributionClient(options.URL, options.Username, options.Password)
return ocifunc(ctx, ocicli, image)
}
// isharbor
return harborfun(ctx, cli, image)
}
if data, err := process(c.Request.Context()); err != nil {
handlers.NotOK(c, err)
} else {
handlers.OK(c, data)
}
}
func (h *ImageHandler) completeRegistryOption(options *RegistryOptions, imgname string, projectid uint) error {
spec, err := reference.Parse(imgname)
if err != nil {
return err
}
hostname := spec.Hostname()
registries := []models.Registry{}
if err := h.GetDataBase().DB().Where(&models.Registry{ProjectID: projectid}).Find(®istries).Error; err != nil {
return err
}
for _, registry := range registries {
u, err := url.Parse(registry.RegistryAddress)
if err != nil {
log.WithField("registry", registry.RegistryAddress).Warn("invalid addr skiped")
continue
}
if u.Hostname() == hostname {
// found
options.URL = registry.RegistryAddress
options.Password = registry.Password
options.Username = registry.Username
return nil
}
}
// 不是内置的,或者该项目的镜像仓库
return ErrNotManagedRegistry
}