github.com/SAP/jenkins-library@v1.362.0/cmd/protecodeExecuteScan.go (about)

     1  package cmd
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"io"
     7  	"os"
     8  	"path"
     9  	"path/filepath"
    10  	"regexp"
    11  	"strconv"
    12  	"strings"
    13  	"time"
    14  
    15  	"github.com/pkg/errors"
    16  
    17  	"github.com/SAP/jenkins-library/pkg/command"
    18  	"github.com/SAP/jenkins-library/pkg/docker"
    19  	piperDocker "github.com/SAP/jenkins-library/pkg/docker"
    20  	"github.com/SAP/jenkins-library/pkg/log"
    21  	"github.com/SAP/jenkins-library/pkg/piperutils"
    22  	"github.com/SAP/jenkins-library/pkg/protecode"
    23  	"github.com/SAP/jenkins-library/pkg/telemetry"
    24  	"github.com/SAP/jenkins-library/pkg/toolrecord"
    25  	"github.com/SAP/jenkins-library/pkg/versioning"
    26  )
    27  
    28  const (
    29  	webReportPath    = "%s/#/product/%v/"
    30  	scanResultFile   = "protecodescan_vulns.json"
    31  	stepResultFile   = "protecodeExecuteScan.json"
    32  	dockerConfigFile = ".pipeline/docker/config.json"
    33  )
    34  
    35  type protecodeUtils interface {
    36  	piperutils.FileUtils
    37  	piperDocker.Download
    38  }
    39  
    40  type protecodeUtilsBundle struct {
    41  	*piperutils.Files
    42  	*piperDocker.Client
    43  }
    44  
    45  func protecodeExecuteScan(config protecodeExecuteScanOptions, telemetryData *telemetry.CustomData, influx *protecodeExecuteScanInflux) {
    46  	c := command.Command{}
    47  	// reroute command output to loging framework
    48  	c.Stdout(log.Writer())
    49  	c.Stderr(log.Writer())
    50  
    51  	//create client for sending api request
    52  	log.Entry().Debug("Create protecode client")
    53  	client := createProtecodeClient(&config)
    54  
    55  	dClientOptions := piperDocker.ClientOptions{ImageName: config.ScanImage, RegistryURL: config.DockerRegistryURL, LocalPath: config.FilePath, ImageFormat: "legacy"}
    56  	dClient := &piperDocker.Client{}
    57  	dClient.SetOptions(dClientOptions)
    58  
    59  	utils := protecodeUtilsBundle{
    60  		Client: dClient,
    61  		Files:  &piperutils.Files{},
    62  	}
    63  
    64  	influx.step_data.fields.protecode = false
    65  	if err := runProtecodeScan(&config, influx, client, utils, "./cache"); err != nil {
    66  		log.Entry().WithError(err).Fatal("Failed to execute protecode scan.")
    67  	}
    68  	influx.step_data.fields.protecode = true
    69  }
    70  
    71  func runProtecodeScan(config *protecodeExecuteScanOptions, influx *protecodeExecuteScanInflux, client protecode.Protecode, utils protecodeUtils, cachePath string) error {
    72  	// make sure cache exists
    73  	if err := utils.MkdirAll(cachePath, 0755); err != nil {
    74  		return err
    75  	}
    76  
    77  	if err := correctDockerConfigEnvVar(config, utils); err != nil {
    78  		return err
    79  	}
    80  
    81  	var fileName, filePath string
    82  	var err error
    83  
    84  	if len(config.FetchURL) == 0 && len(config.FilePath) == 0 {
    85  		log.Entry().Debugf("Get docker image: %v, %v, %v", config.ScanImage, config.DockerRegistryURL, config.FilePath)
    86  		fileName, filePath, err = getDockerImage(utils, config, cachePath)
    87  		if err != nil {
    88  			return errors.Wrap(err, "failed to get Docker image")
    89  		}
    90  		if len(config.FilePath) <= 0 {
    91  			(*config).FilePath = filePath
    92  			log.Entry().Debugf("Filepath for upload image: %v", config.FilePath)
    93  		}
    94  	} else if len(config.FilePath) > 0 {
    95  		parts := strings.Split(config.FilePath, "/")
    96  		pathFragment := strings.Join(parts[:len(parts)-1], "/")
    97  		if len(pathFragment) > 0 {
    98  			(*config).FilePath = pathFragment
    99  		} else {
   100  			(*config).FilePath = "./"
   101  		}
   102  		fileName = parts[len(parts)-1]
   103  
   104  	} else if len(config.FetchURL) > 0 {
   105  		// Get filename from a fetch URL
   106  		fileName = filepath.Base(config.FetchURL)
   107  		log.Entry().Debugf("[DEBUG] ===> Filename from fetch URL: %v", fileName)
   108  	}
   109  
   110  	log.Entry().Debug("Execute protecode scan")
   111  	if err := executeProtecodeScan(influx, client, config, fileName, utils); err != nil {
   112  		return err
   113  	}
   114  
   115  	defer func() { _ = utils.FileRemove(config.FilePath) }()
   116  
   117  	if err := utils.RemoveAll(cachePath); err != nil {
   118  		log.Entry().Warnf("Error during cleanup folder %v", err)
   119  	}
   120  
   121  	return nil
   122  }
   123  
   124  // TODO: extract to version utils
   125  func handleArtifactVersion(artifactVersion string) string {
   126  	matches, _ := regexp.MatchString("([\\d\\.]){1,}-[\\d]{14}([\\Wa-z\\d]{41})?", artifactVersion)
   127  	if matches {
   128  		split := strings.SplitN(artifactVersion, ".", 2)
   129  		log.Entry().WithField("old", artifactVersion).WithField("new", split[0]).Debug("Trimming version to major version digit.")
   130  		return split[0]
   131  	}
   132  	return artifactVersion
   133  }
   134  
   135  func getDockerImage(utils protecodeUtils, config *protecodeExecuteScanOptions, cachePath string) (string, string, error) {
   136  	m := regexp.MustCompile(`[\s@:/]`)
   137  
   138  	tarFileName := fmt.Sprintf("%s.tar", m.ReplaceAllString(config.ScanImage, "-"))
   139  	tarFilePath, err := filepath.Abs(filepath.Join(cachePath, tarFileName))
   140  
   141  	if err != nil {
   142  		return "", "", err
   143  	}
   144  
   145  	if _, err = utils.DownloadImage(config.ScanImage, tarFilePath); err != nil {
   146  		return "", "", errors.Wrap(err, "failed to download docker image")
   147  	}
   148  
   149  	return filepath.Base(tarFilePath), filepath.Dir(tarFilePath), nil
   150  }
   151  
   152  func executeProtecodeScan(influx *protecodeExecuteScanInflux, client protecode.Protecode, config *protecodeExecuteScanOptions, fileName string, utils protecodeUtils) error {
   153  	reportPath := "./"
   154  
   155  	log.Entry().Debugf("[DEBUG] ===> Load existing product Group:%v, VerifyOnly:%v, Filename:%v, replaceProductId:%v", config.Group, config.VerifyOnly, fileName, config.ReplaceProductID)
   156  
   157  	var productID int
   158  
   159  	// If replaceProductId is not provided then switch to automatic existing product detection
   160  	if config.ReplaceProductID > 0 {
   161  
   162  		log.Entry().Infof("replaceProductID has been provided (%v) and checking ...", config.ReplaceProductID)
   163  
   164  		// Validate provided product id, if not valid id then throw an error
   165  		if client.VerifyProductID(config.ReplaceProductID) {
   166  			log.Entry().Infof("replaceProductID has been checked and it's valid")
   167  			productID = config.ReplaceProductID
   168  		} else {
   169  			log.Entry().Debugf("[DEBUG] ===> ReplaceProductID doesn't exist")
   170  			return fmt.Errorf("ERROR -> the product id is not valid '%d'", config.ReplaceProductID)
   171  		}
   172  
   173  	} else {
   174  		// Get existing product id by filename
   175  		log.Entry().Infof("replaceProductID is not provided and automatic search starts from group: %v ... ", config.Group)
   176  		productID = client.LoadExistingProduct(config.Group, fileName)
   177  
   178  		if productID > 0 {
   179  			log.Entry().Infof("Automatic search completed and found following product id: %v", productID)
   180  		} else {
   181  			log.Entry().Infof("Automatic search completed but not found any similar product scan, now starts new scan creation")
   182  		}
   183  	}
   184  
   185  	// check if no existing is found
   186  	productID = uploadScanOrDeclareFetch(utils, *config, productID, client, fileName)
   187  
   188  	if productID <= 0 {
   189  		return fmt.Errorf("the product id is not valid '%d'", productID)
   190  	}
   191  
   192  	//pollForResult
   193  	log.Entry().Debugf("Poll for scan result %v", productID)
   194  	result := client.PollForResult(productID, config.TimeoutMinutes)
   195  	// write results to file
   196  	jsonData, _ := json.Marshal(result)
   197  	if err := utils.FileWrite(filepath.Join(reportPath, scanResultFile), jsonData, 0644); err != nil {
   198  		log.Entry().Warningf("failed to write result file: %v", err)
   199  	}
   200  
   201  	//check if result is ok else notify
   202  	if protecode.HasFailed(result) {
   203  		log.SetErrorCategory(log.ErrorService)
   204  		return fmt.Errorf("protecode scan failed: %v/products/%v", config.ServerURL, productID)
   205  	}
   206  
   207  	//loadReport
   208  	log.Entry().Debugf("Load report %v for %v", config.ReportFileName, productID)
   209  	resp := client.LoadReport(config.ReportFileName, productID)
   210  
   211  	buf, err := io.ReadAll(*resp)
   212  
   213  	if err != nil {
   214  		return fmt.Errorf("unable to process protecode report %v", err)
   215  	}
   216  
   217  	if err = utils.FileWrite(config.ReportFileName, buf, 0644); err != nil {
   218  		log.Entry().Warningf("failed to write report: %s", err)
   219  	}
   220  
   221  	//clean scan from server
   222  	log.Entry().Debugf("Delete scan %v for %v", config.CleanupMode, productID)
   223  	client.DeleteScan(config.CleanupMode, productID)
   224  
   225  	//count vulnerabilities
   226  	log.Entry().Debug("Parse scan result")
   227  	parsedResult, vulns := client.ParseResultForInflux(result.Result, config.ExcludeCVEs)
   228  
   229  	log.Entry().Debug("Write report to filesystem")
   230  	if err := protecode.WriteReport(
   231  		protecode.ReportData{
   232  			ServerURL:                   config.ServerURL,
   233  			FailOnSevereVulnerabilities: config.FailOnSevereVulnerabilities,
   234  			ExcludeCVEs:                 config.ExcludeCVEs,
   235  			Target:                      config.ReportFileName,
   236  			Vulnerabilities:             vulns,
   237  			ProductID:                   fmt.Sprintf("%v", productID),
   238  		}, reportPath, stepResultFile, parsedResult, utils); err != nil {
   239  		log.Entry().Warningf("failed to write report: %v", err)
   240  	}
   241  
   242  	log.Entry().Debug("Write influx data")
   243  	setInfluxData(influx, parsedResult)
   244  
   245  	// write reports JSON
   246  	reports := []piperutils.Path{
   247  		{Target: config.ReportFileName, Mandatory: true},
   248  		{Target: stepResultFile, Mandatory: true},
   249  		{Target: scanResultFile, Mandatory: true},
   250  	}
   251  	// write links JSON
   252  	webuiURL := fmt.Sprintf(webReportPath, config.ServerURL, productID)
   253  	links := []piperutils.Path{
   254  		{Name: "Protecode WebUI", Target: webuiURL},
   255  		{Name: "Protecode Report", Target: path.Join("artifact", config.ReportFileName), Scope: "job"},
   256  	}
   257  
   258  	// write custom report
   259  	scanReport := protecode.CreateCustomReport(fileName, productID, parsedResult, vulns)
   260  	paths, err := protecode.WriteCustomReports(scanReport, fileName, fmt.Sprint(productID), utils)
   261  	if err != nil {
   262  		// do not fail - consider failing later on
   263  		log.Entry().Warning("failed to create custom HTML/MarkDown file ...", err)
   264  	} else {
   265  		reports = append(reports, paths...)
   266  	}
   267  
   268  	// create toolrecord file
   269  	toolRecordFileName, err := createToolRecordProtecode(utils, "./", config, productID, webuiURL)
   270  	if err != nil {
   271  		// do not fail until the framework is well established
   272  		log.Entry().Warning("TR_PROTECODE: Failed to create toolrecord file ...", err)
   273  	} else {
   274  		reports = append(reports, piperutils.Path{Target: toolRecordFileName})
   275  	}
   276  
   277  	piperutils.PersistReportsAndLinks("protecodeExecuteScan", "", utils, reports, links)
   278  
   279  	if config.FailOnSevereVulnerabilities && protecode.HasSevereVulnerabilities(result.Result, config.ExcludeCVEs) {
   280  		log.SetErrorCategory(log.ErrorCompliance)
   281  		return fmt.Errorf("the product is not compliant")
   282  	} else if protecode.HasSevereVulnerabilities(result.Result, config.ExcludeCVEs) {
   283  		log.Entry().Infof("policy violation(s) found - step will only create data but not fail due to setting failOnSevereVulnerabilities: false")
   284  	}
   285  	return nil
   286  }
   287  
   288  func setInfluxData(influx *protecodeExecuteScanInflux, result map[string]int) {
   289  	influx.protecode_data.fields.historical_vulnerabilities = result["historical_vulnerabilities"]
   290  	influx.protecode_data.fields.triaged_vulnerabilities = result["triaged_vulnerabilities"]
   291  	influx.protecode_data.fields.excluded_vulnerabilities = result["excluded_vulnerabilities"]
   292  	influx.protecode_data.fields.minor_vulnerabilities = result["minor_vulnerabilities"]
   293  	influx.protecode_data.fields.major_vulnerabilities = result["major_vulnerabilities"]
   294  	influx.protecode_data.fields.vulnerabilities = result["vulnerabilities"]
   295  }
   296  
   297  func createProtecodeClient(config *protecodeExecuteScanOptions) protecode.Protecode {
   298  	var duration time.Duration = time.Duration(time.Minute * 1)
   299  
   300  	if len(config.TimeoutMinutes) > 0 {
   301  		dur, err := time.ParseDuration(fmt.Sprintf("%vm", config.TimeoutMinutes))
   302  		if err != nil {
   303  			log.Entry().Warnf("Failed to parse timeout %v, switched back to default timeout %v minutes", config.TimeoutMinutes, duration)
   304  		} else {
   305  			duration = dur
   306  		}
   307  	}
   308  
   309  	pc := protecode.Protecode{}
   310  
   311  	protecodeOptions := protecode.Options{
   312  		ServerURL:  config.ServerURL,
   313  		Logger:     log.Entry().WithField("package", "SAP/jenkins-library/pkg/protecode"),
   314  		Duration:   duration,
   315  		Username:   config.Username,
   316  		Password:   config.Password,
   317  		UserAPIKey: config.UserAPIKey,
   318  	}
   319  
   320  	pc.SetOptions(protecodeOptions)
   321  
   322  	return pc
   323  }
   324  
   325  func uploadScanOrDeclareFetch(utils protecodeUtils, config protecodeExecuteScanOptions, productID int, client protecode.Protecode, fileName string) int {
   326  
   327  	// check if product doesn't exist then create a new one.
   328  	if productID <= 0 {
   329  		log.Entry().Infof("New product creation started ... ")
   330  		productID = uploadFile(utils, config, productID, client, fileName, false)
   331  
   332  		log.Entry().Infof("New product has been successfully created: %v", productID)
   333  		return productID
   334  
   335  		// In case product already exists and "VerifyOnly (reuseExisting)" is false then we replace binary without creating a new product.
   336  	} else if (productID > 0) && !config.VerifyOnly {
   337  		log.Entry().Infof("Product already exists and 'VerifyOnly (reuseExisting)' is false then product (%v) binary and scan result will be replaced without creating a new product.", productID)
   338  		productID = uploadFile(utils, config, productID, client, fileName, true)
   339  
   340  		return productID
   341  
   342  		// If product already exists and "reuseExisting" option is enabled then return the latest similar scan result.
   343  	} else {
   344  		log.Entry().Infof("VerifyOnly (reuseExisting) option is enabled and returned productID: %v", productID)
   345  		return productID
   346  	}
   347  }
   348  
   349  func uploadFile(utils protecodeUtils, config protecodeExecuteScanOptions, productID int, client protecode.Protecode, fileName string, replaceBinary bool) int {
   350  
   351  	// get calculated version for Version field
   352  	version := getProcessedVersion(&config)
   353  
   354  	if len(config.FetchURL) > 0 {
   355  		log.Entry().Debugf("Declare fetch url %v", config.FetchURL)
   356  		resultData := client.DeclareFetchURL(config.CleanupMode, config.Group, config.CustomDataJSONMap, config.FetchURL, version, productID, replaceBinary)
   357  		productID = resultData.Result.ProductID
   358  	} else {
   359  		log.Entry().Debugf("Upload file path: %v", config.FilePath)
   360  		if len(config.FilePath) <= 0 {
   361  			log.Entry().Fatalf("There is no file path configured for upload : %v", config.FilePath)
   362  		}
   363  		pathToFile := filepath.Join(config.FilePath, fileName)
   364  		if exists, err := utils.FileExists(pathToFile); err != nil && !exists {
   365  			log.Entry().Fatalf("There is no file for upload: %v", pathToFile)
   366  		}
   367  
   368  		combinedFileName := fileName
   369  		if len(config.PullRequestName) > 0 {
   370  			combinedFileName = fmt.Sprintf("%v_%v", config.PullRequestName, fileName)
   371  		}
   372  
   373  		resultData := client.UploadScanFile(config.CleanupMode, config.Group, config.CustomDataJSONMap, pathToFile, combinedFileName, version, productID, replaceBinary)
   374  		productID = resultData.Result.ProductID
   375  	}
   376  	return productID
   377  }
   378  
   379  func correctDockerConfigEnvVar(config *protecodeExecuteScanOptions, utils protecodeUtils) error {
   380  	var err error
   381  	path := config.DockerConfigJSON
   382  
   383  	if len(config.DockerConfigJSON) > 0 && len(config.DockerRegistryURL) > 0 && len(config.ContainerRegistryPassword) > 0 && len(config.ContainerRegistryUser) > 0 {
   384  		path, err = docker.CreateDockerConfigJSON(config.DockerRegistryURL, config.ContainerRegistryUser, config.ContainerRegistryPassword, dockerConfigFile, config.DockerConfigJSON, utils)
   385  	}
   386  
   387  	if err != nil {
   388  		return errors.Wrapf(err, "failed to create / update docker config json file")
   389  	}
   390  
   391  	if len(path) > 0 {
   392  		log.Entry().Infof("Docker credentials configuration: %v", path)
   393  		path, _ := filepath.Abs(path)
   394  		// use parent directory
   395  		path = filepath.Dir(path)
   396  		os.Setenv("DOCKER_CONFIG", path)
   397  	} else {
   398  		log.Entry().Info("Docker credentials configuration: NONE")
   399  	}
   400  	return nil
   401  }
   402  
   403  // Calculate version based on versioning model and artifact version or return custom scan version provided by user
   404  func getProcessedVersion(config *protecodeExecuteScanOptions) string {
   405  	processedVersion := config.CustomScanVersion
   406  	if len(processedVersion) > 0 {
   407  		log.Entry().Infof("Using custom version: %v", processedVersion)
   408  	} else {
   409  		if len(config.VersioningModel) > 0 {
   410  			processedVersion = versioning.ApplyVersioningModel(config.VersioningModel, config.Version)
   411  		} else {
   412  			// By default 'major' if <config.VersioningModel> not provided
   413  			processedVersion = versioning.ApplyVersioningModel("major", config.Version)
   414  		}
   415  	}
   416  	return processedVersion
   417  }
   418  
   419  // create toolrecord file for protecode
   420  // todo: check if group and product names can be retrieved
   421  func createToolRecordProtecode(utils protecodeUtils, workspace string, config *protecodeExecuteScanOptions, productID int, webuiURL string) (string, error) {
   422  	record := toolrecord.New(utils, workspace, "protecode", config.ServerURL)
   423  	groupURL := config.ServerURL + "/#/groups/" + config.Group
   424  	err := record.AddKeyData("group",
   425  		config.Group,
   426  		config.Group, // todo figure out display name
   427  		groupURL)
   428  	if err != nil {
   429  		return "", err
   430  	}
   431  	err = record.AddKeyData("product",
   432  		strconv.Itoa(productID),
   433  		strconv.Itoa(productID), // todo figure out display name
   434  		webuiURL)
   435  	if err != nil {
   436  		return "", err
   437  	}
   438  	err = record.Persist()
   439  	if err != nil {
   440  		return "", err
   441  	}
   442  	return record.GetFileName(), nil
   443  }