forked from goharbor/harbor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
job.go
166 lines (153 loc) · 4.48 KB
/
job.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
// Copyright 2018 Project Harbor 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 utils contains methods to support security, cache, and webhook functions.
package utils
import (
"github.com/goharbor/harbor/src/common/dao"
"github.com/goharbor/harbor/src/common/job"
jobmodels "github.com/goharbor/harbor/src/common/job/models"
"github.com/goharbor/harbor/src/common/models"
"github.com/goharbor/harbor/src/common/utils/log"
"github.com/goharbor/harbor/src/core/config"
"encoding/json"
"fmt"
"sync"
)
var (
cl sync.Mutex
jobServiceClient job.Client
)
// ScanAllImages scans all images of Harbor by submiting a scan all job to jobservice, and the job handler will call API
// on the "core" service
func ScanAllImages() error {
_, err := scanAll("")
return err
}
// ScheduleScanAllImages will schedule a scan all job based on the cron string, add append a record in admin job table.
func ScheduleScanAllImages(cron string) error {
_, err := scanAll(cron)
return err
}
func scanAll(cron string, c ...job.Client) (string, error) {
var client job.Client
if c == nil || len(c) == 0 {
client = GetJobServiceClient()
} else {
client = c[0]
}
kind := job.JobKindGeneric
if len(cron) > 0 {
kind = job.JobKindPeriodic
}
meta := &jobmodels.JobMetadata{
JobKind: kind,
IsUnique: true,
Cron: cron,
}
id, err := dao.AddAdminJob(&models.AdminJob{
Name: job.ImageScanAllJob,
Kind: kind,
})
if err != nil {
return "", err
}
data := &jobmodels.JobData{
Name: job.ImageScanAllJob,
Metadata: meta,
StatusHook: fmt.Sprintf("%s/service/notifications/jobs/adminjob/%d", config.InternalCoreURL(), id),
}
log.Infof("scan_all job scheduled/triggered, cron string: '%s'", cron)
return client.SubmitJob(data)
}
// GetJobServiceClient returns the job service client instance.
func GetJobServiceClient() job.Client {
cl.Lock()
defer cl.Unlock()
if jobServiceClient == nil {
jobServiceClient = job.NewDefaultClient(config.InternalJobServiceURL(), config.CoreSecret())
}
return jobServiceClient
}
// TriggerImageScan triggers an image scan job on jobservice.
func TriggerImageScan(repository string, tag string) error {
repoClient, err := NewRepositoryClientForUI("harbor-core", repository)
if err != nil {
return err
}
digest, exist, err := repoClient.ManifestExist(tag)
if !exist {
return fmt.Errorf("unable to perform scan: the manifest of image %s:%s does not exist", repository, tag)
}
if err != nil {
log.Errorf("Failed to get Manifest for %s:%s", repository, tag)
return err
}
return triggerImageScan(repository, tag, digest, GetJobServiceClient())
}
func triggerImageScan(repository, tag, digest string, client job.Client) error {
id, err := dao.AddScanJob(models.ScanJob{
Repository: repository,
Digest: digest,
Tag: tag,
Status: models.JobPending,
})
if err != nil {
return err
}
err = dao.SetScanJobForImg(digest, id)
if err != nil {
return err
}
data, err := buildScanJobData(id, repository, tag, digest)
if err != nil {
return err
}
uuid, err := client.SubmitJob(data)
if err != nil {
return err
}
err = dao.SetScanJobUUID(id, uuid)
if err != nil {
log.Warningf("Failed to set UUID for scan job, ID: %d, repository: %s, tag: %s", id, uuid, repository, tag)
}
return nil
}
func buildScanJobData(jobID int64, repository, tag, digest string) (*jobmodels.JobData, error) {
parms := job.ScanJobParms{
JobID: jobID,
Repository: repository,
Digest: digest,
Tag: tag,
}
parmsMap := make(map[string]interface{})
b, err := json.Marshal(parms)
if err != nil {
return nil, err
}
err = json.Unmarshal(b, &parmsMap)
if err != nil {
return nil, err
}
meta := jobmodels.JobMetadata{
JobKind: job.JobKindGeneric,
IsUnique: false,
}
data := &jobmodels.JobData{
Name: job.ImageScanJob,
Parameters: jobmodels.Parameters(parmsMap),
Metadata: &meta,
StatusHook: fmt.Sprintf("%s/service/notifications/jobs/scan/%d", config.InternalCoreURL(), jobID),
}
return data, nil
}