mirror of
https://github.com/goharbor/harbor.git
synced 2025-01-23 16:11:24 +01:00
Read the system properties from scan job context
This commit is contained in:
parent
722edc9c60
commit
15580a5e8c
@ -2,12 +2,8 @@ package job
|
||||
|
||||
// ScanJobParms holds parameters used to submit jobs to jobservice
|
||||
type ScanJobParms struct {
|
||||
JobID int64 `json:"job_int_id"`
|
||||
Repository string `json:"repository"`
|
||||
Tag string `json:"tag"`
|
||||
Digest string `json:"digest"`
|
||||
Secret string `json:"job_service_secret"`
|
||||
RegistryURL string `json:"registry_url"`
|
||||
ClairEndpoint string `json:"clair_endpoint"`
|
||||
TokenEndpoint string `json:"token_endpoint"`
|
||||
JobID int64 `json:"job_int_id"`
|
||||
Repository string `json:"repository"`
|
||||
Tag string `json:"tag"`
|
||||
Digest string `json:"digest"`
|
||||
}
|
||||
|
@ -18,9 +18,11 @@ import (
|
||||
"crypto/sha256"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/docker/distribution"
|
||||
"github.com/docker/distribution/manifest/schema2"
|
||||
"github.com/vmware/harbor/src/common"
|
||||
"github.com/vmware/harbor/src/common/dao"
|
||||
"github.com/vmware/harbor/src/common/job"
|
||||
"github.com/vmware/harbor/src/common/models"
|
||||
@ -32,6 +34,10 @@ import (
|
||||
|
||||
// ClairJob is the struct to scan Harbor's Image with Clair
|
||||
type ClairJob struct {
|
||||
registryURL string
|
||||
secret string
|
||||
tokenEndpoint string
|
||||
clairEndpoint string
|
||||
}
|
||||
|
||||
// MaxFails implements the interface in job/Interface
|
||||
@ -52,6 +58,10 @@ func (cj *ClairJob) Validate(params map[string]interface{}) error {
|
||||
// Run implements the interface in job/Interface
|
||||
func (cj *ClairJob) Run(ctx env.JobContext, params map[string]interface{}) error {
|
||||
logger := ctx.GetLogger()
|
||||
if err := cj.init(ctx); err != nil {
|
||||
logger.Errorf("Failed to initialize the job, error: %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
jobParms, err := transformParam(params)
|
||||
if err != nil {
|
||||
@ -59,8 +69,9 @@ func (cj *ClairJob) Run(ctx env.JobContext, params map[string]interface{}) error
|
||||
return err
|
||||
}
|
||||
|
||||
repoClient, err := utils.NewRepositoryClientForJobservice(jobParms.Repository, jobParms.RegistryURL, jobParms.Secret, jobParms.TokenEndpoint)
|
||||
repoClient, err := utils.NewRepositoryClientForJobservice(jobParms.Repository, cj.registryURL, cj.secret, cj.tokenEndpoint)
|
||||
if err != nil {
|
||||
logger.Errorf("Failed create repository client for repo: %s, error: %v", jobParms.Repository, err)
|
||||
return err
|
||||
}
|
||||
_, _, payload, err := repoClient.PullManifest(jobParms.Tag, []string{schema2.MediaTypeManifest})
|
||||
@ -68,12 +79,12 @@ func (cj *ClairJob) Run(ctx env.JobContext, params map[string]interface{}) error
|
||||
logger.Errorf("Error pulling manifest for image %s:%s :%v", jobParms.Repository, jobParms.Tag, err)
|
||||
return err
|
||||
}
|
||||
token, err := utils.GetTokenForRepo(jobParms.Repository, jobParms.Secret, jobParms.TokenEndpoint)
|
||||
token, err := utils.GetTokenForRepo(jobParms.Repository, cj.secret, cj.tokenEndpoint)
|
||||
if err != nil {
|
||||
logger.Errorf("Failed to get token, error: %v", err)
|
||||
return err
|
||||
}
|
||||
layers, err := prepareLayers(payload, jobParms.RegistryURL, jobParms.Repository, token)
|
||||
layers, err := prepareLayers(payload, cj.registryURL, jobParms.Repository, token)
|
||||
if err != nil {
|
||||
logger.Errorf("Failed to prepare layers, error: %v", err)
|
||||
return err
|
||||
@ -82,7 +93,7 @@ func (cj *ClairJob) Run(ctx env.JobContext, params map[string]interface{}) error
|
||||
if !ok {
|
||||
loggerImpl = log.DefaultLogger()
|
||||
}
|
||||
clairClient := clair.NewClient(jobParms.ClairEndpoint, loggerImpl)
|
||||
clairClient := clair.NewClient(cj.clairEndpoint, loggerImpl)
|
||||
|
||||
for _, l := range layers {
|
||||
logger.Infof("Scanning Layer: %s, path: %s", l.Name, l.Path)
|
||||
@ -103,6 +114,32 @@ func (cj *ClairJob) Run(ctx env.JobContext, params map[string]interface{}) error
|
||||
return err
|
||||
}
|
||||
|
||||
func (cj *ClairJob) init(ctx env.JobContext) error {
|
||||
errTpl := "Failed to get required property: %s"
|
||||
if v, ok := ctx.Get(common.RegistryURL); ok && len(v.(string)) > 0 {
|
||||
cj.registryURL = v.(string)
|
||||
} else {
|
||||
return fmt.Errorf(errTpl, common.RegistryURL)
|
||||
}
|
||||
|
||||
if v := os.Getenv("JOBSERVICE_SECRET"); len(v) > 0 {
|
||||
cj.secret = v
|
||||
} else {
|
||||
return fmt.Errorf(errTpl, "JOBSERVICE_SECRET")
|
||||
}
|
||||
if v, ok := ctx.Get(common.TokenServiceURL); ok && len(v.(string)) > 0 {
|
||||
cj.tokenEndpoint = v.(string)
|
||||
} else {
|
||||
return fmt.Errorf(errTpl, common.TokenServiceURL)
|
||||
}
|
||||
if v, ok := ctx.Get(common.ClairURL); ok && len(v.(string)) > 0 {
|
||||
cj.clairEndpoint = v.(string)
|
||||
} else {
|
||||
return fmt.Errorf(errTpl, common.ClairURL)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func transformParam(params map[string]interface{}) (*job.ScanJobParms, error) {
|
||||
res := job.ScanJobParms{}
|
||||
parmsBytes, err := json.Marshal(params)
|
||||
|
@ -140,20 +140,11 @@ func triggerImageScan(repository, tag, digest string, client job.Client) error {
|
||||
}
|
||||
|
||||
func buildScanJobData(jobID int64, repository, tag, digest string) (*jobmodels.JobData, error) {
|
||||
regURL, err := config.RegistryURL()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// TODO:job service can get some parms from context.
|
||||
parms := job.ScanJobParms{
|
||||
ClairEndpoint: config.ClairEndpoint(),
|
||||
JobID: jobID,
|
||||
RegistryURL: regURL,
|
||||
Repository: repository,
|
||||
Secret: config.JobserviceSecret(),
|
||||
Digest: digest,
|
||||
Tag: tag,
|
||||
TokenEndpoint: config.InternalTokenServiceEndpoint(),
|
||||
JobID: jobID,
|
||||
Repository: repository,
|
||||
Digest: digest,
|
||||
Tag: tag,
|
||||
}
|
||||
parmsMap := make(map[string]interface{})
|
||||
b, err := json.Marshal(parms)
|
||||
|
Loading…
Reference in New Issue
Block a user