1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00
sap-jenkins-library/pkg/whitesource/sytemMock.go
Sven Merk a1988f6808
feat(whitesourceExecuteScan): GitHub issue creation + SARIF (#3535)
* Add GH issue creation + SARIF

* Code cleanup

* Fix fmt, add debug

* Code enhancements

* Fix

* Added debug info

* Rework UA log scan

* Fix code

* read UA version

* Fix nil reference

* Extraction

* Credentials

* Issue creation

* Error handling

* Fix issue creation

* query escape

* Query escape 2

* Revert

* Test avoid update

* HTTP client

* Add support for custom TLS certs

* Fix code

* Fix code 2

* Fix code 3

* Disable cert check

* Fix auth

* Remove implicit trust

* Skip verification

* Fix

* Fix client

* Fix HTTP auth

* Fix trusted certs

* Trim version

* Code

* Add token

* Added token handling to client

* Fix token

* Cleanup

* Fix token

* Token rework

* Fix code

* Kick out oauth client

* Kick out oauth client

* Transport wrapping

* Token

* Simplification

* Refactor

* Variation

* Check

* Fix

* Debug

* Switch client

* Variation

* Debug

* Switch to cert check

* Add debug

* Parse self

* Cleanup

* Update resources/metadata/whitesourceExecuteScan.yaml

* Add debug

* Expose subjects

* Patch

* Debug

* Debug2

* Debug3

* Fix logging response body

* Cleanup

* Cleanup

* Fix request body logging

* Cleanup import

* Fix import cycle

* Cleanup

* Fix fmt

* Fix NopCloser reference

* Regenerate

* Reintroduce

* Fix test

* Fix tests

* Correction

* Fix error

* Code fix

* Fix tests

* Add tests

* Fix code climate issues

* Code climate

* Code climate again

* Code climate again

* Fix fmt

* Fix fmt 2

Co-authored-by: Oliver Nocon <33484802+OliverNocon@users.noreply.github.com>
2022-02-23 09:30:19 +01:00

177 lines
5.5 KiB
Go

//go:build !release
// +build !release
package whitesource
import (
"fmt"
"strconv"
"time"
)
// SystemMock stores a number of WhiteSource objects and, based on that, mocks the behavior of System.
type SystemMock struct {
ProductName string
Products []Product
Projects []Project
Alerts []Alert
AlertType string
AlertError error
Libraries []Library
RiskReport []byte
VulnerabilityReport []byte
}
// GetProductByName mimics retrieving a Product by name. It returns an error of no Product is stored in the mock.
func (m *SystemMock) GetProductByName(productName string) (Product, error) {
for _, product := range m.Products {
if product.Name == productName {
return product, nil
}
}
return Product{}, fmt.Errorf("no product with name '%s' found in Whitesource", productName)
}
// CreateProduct appends a new Product to the system mock and returns its token ("mock-product-token-<index>").
func (m *SystemMock) CreateProduct(productName string) (string, error) {
now := time.Now().Format(DateTimeLayout)
productIndex := len(m.Products)
product := Product{
Name: productName,
Token: "mock-product-token-" + strconv.Itoa(productIndex),
CreationDate: now,
LastUpdateDate: now,
}
m.Products = append(m.Products, product)
return product.Token, nil
}
// SetProductAssignments checks if the system mock contains a product with the given token and returns
// an error depending on that, but otherwise does nothing with the provided arguments.
func (m *SystemMock) SetProductAssignments(productToken string, _, _, _ *Assignment) error {
for _, product := range m.Products {
if product.Token == productToken {
return nil
}
}
return fmt.Errorf("no product with that token")
}
// GetProjectsMetaInfo returns the list of Projects stored in the mock or an error if token is unknown.
func (m *SystemMock) GetProjectsMetaInfo(productToken string) ([]Project, error) {
for _, product := range m.Products {
if product.Token == productToken {
return m.Projects, nil
}
}
return nil, fmt.Errorf("no product with that token")
}
// GetProjectToken checks the Projects stored in the mock and returns a valid token, or an empty token and no error.
func (m *SystemMock) GetProjectToken(productToken, projectName string) (string, error) {
for _, project := range m.Projects {
if project.Name == projectName {
return project.Token, nil
}
}
return "", nil
}
// GetProjectByToken checks the Projects stored in the mock and returns the one with the given token or an error.
func (m *SystemMock) GetProjectByToken(projectToken string) (Project, error) {
for _, project := range m.Projects {
if project.Token == projectToken {
return project, nil
}
}
return Project{}, fmt.Errorf("no project with token '%s' found in Whitesource", projectToken)
}
// GetProjectRiskReport mocks retrieving a risc report.
func (m *SystemMock) GetProjectRiskReport(projectToken string) ([]byte, error) {
return m.RiskReport, nil
}
// GetProjectVulnerabilityReport mocks retrieving a vulnerability report.
// Behavior depends on what is stored in the mock.
func (m *SystemMock) GetProjectVulnerabilityReport(projectToken string, format string) ([]byte, error) {
_, err := m.GetProjectByToken(projectToken)
if err != nil {
return nil, err
}
if m.VulnerabilityReport == nil {
return nil, fmt.Errorf("no report available")
}
return m.VulnerabilityReport, nil
}
// GetProjectAlerts returns the alerts stored in the SystemMock.
func (m *SystemMock) GetProjectAlerts(projectToken string) ([]Alert, error) {
return m.Alerts, nil
}
// GetProjectAlertsByType returns the alerts stored in the SystemMock and records the type.
func (m *SystemMock) GetProjectAlertsByType(projectToken, alertType string) ([]Alert, error) {
if m.AlertError != nil {
return m.Alerts, m.AlertError
}
m.AlertType = alertType
return m.Alerts, nil
}
// GetProjectLibraryLocations returns the libraries stored in the SystemMock.
func (m *SystemMock) GetProjectLibraryLocations(projectToken string) ([]Library, error) {
return m.Libraries, nil
}
// NewSystemMockWithProjectName returns a pointer to a new instance of SystemMock using a project with a defined name.
func NewSystemMockWithProjectName(lastUpdateDate, projectName string) *SystemMock {
mockLibrary := Library{
Name: "mock-library",
Filename: "mock-library-file",
Version: "mock-library-version",
}
return &SystemMock{
ProductName: "mock-product",
Products: []Product{
{
Name: "mock-product",
Token: "mock-product-token",
CreationDate: "last-thursday",
LastUpdateDate: lastUpdateDate,
},
},
Projects: []Project{
{
ID: 42,
Name: projectName,
PluginName: "mock-plugin-name",
Token: "mock-project-token",
UploadedBy: "MrBean",
CreationDate: "last-thursday",
LastUpdateDate: lastUpdateDate,
},
},
Alerts: []Alert{
{
Vulnerability: Vulnerability{
Name: "something severe",
Score: 5,
},
Library: mockLibrary,
Project: projectName,
CreationDate: "last-thursday",
},
},
Libraries: []Library{mockLibrary},
RiskReport: []byte("mock-risk-report"),
VulnerabilityReport: []byte("mock-vulnerability-report"),
}
}
// NewSystemMock returns a pointer to a new instance of SystemMock.
func NewSystemMock(lastUpdateDate string) *SystemMock {
const projectName = "mock-project - 1"
return NewSystemMockWithProjectName(lastUpdateDate, projectName)
}