utils

package
v3.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 17, 2026 License: Apache-2.0 Imports: 63 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// MaxConcurrentScanners represents the maximum number of threads for running JFrog CLI scanners concurrently
	MaxConcurrentScanners = 5

	// VCS providers params
	GitHub          vcsProvider = "github"
	GitLab          vcsProvider = "gitlab"
	BitbucketServer vcsProvider = "bitbucketServer"
	BitbucketCloud  vcsProvider = "bitbucketCloud"
	AzureRepos      vcsProvider = "azureRepos"

	// JFrog platform environment variables
	JFrogUrlEnv = "JF_URL"

	JFrogUserEnv     = "JF_USER"     // TODO should add support once Catalog Enrich API is fixed (remember adding to extraction tests)
	JFrogPasswordEnv = "JF_PASSWORD" // TODO should add support once Catalog Enrich API is fixed (remember adding to extraction tests)
	JFrogTokenEnv    = "JF_ACCESS_TOKEN"

	// Git environment variables
	GitProvider                     = "JF_GIT_PROVIDER"
	GitRepoOwnerEnv                 = "JF_GIT_OWNER"
	GitRepoEnv                      = "JF_GIT_REPO"
	GitAzureProjectEnv              = "JF_GIT_AZURE_PROJECT"
	GitBitBucketUsernameEnv         = "JF_GIT_BB_USERNAME"
	GitDependencyGraphSubmissionEnv = "JF_UPLOAD_SBOM_TO_VCS"
	UploadPrSecurityResultsToVcsEnv = "JF_UPLOAD_PR_SECURITY_RESULTS_TO_VCS"

	//#nosec G101 -- False positive - no hardcoded credentials.
	GitTokenEnv                   = "JF_GIT_TOKEN"
	GitBaseBranchEnv              = "JF_GIT_BASE_BRANCH"
	GitPullRequestIDEnv           = "JF_GIT_PULL_REQUEST_ID"
	GitApiEndpointEnv             = "JF_GIT_API_ENDPOINT"
	GitlabScanResultsOutputDirEnv = "JF_SCAN_RESULTS_OUTPUT_DIR"
	GitWorkspaceEnv               = "JF_GIT_WORKSPACE"

	// The 'GITHUB_ACTIONS' environment variable exists when the CI is GitHub Actions
	GitHubActionsEnv = "GITHUB_ACTIONS"
	// The 'GITHUB_WORKFLOW_REF' environment variable contains the ref path to the workflow file, e.g. owner/repo/.github/workflows/frogbot.yml@refs/heads/main
	GitHubWorkflowRefEnv = "GITHUB_WORKFLOW_REF"

	// Placeholders for templates
	PackagePlaceHolder    = "{IMPACTED_PACKAGE}"
	FixVersionPlaceHolder = "{FIX_VERSION}"
	BranchHashPlaceHolder = "{BRANCH_NAME_HASH}"

	// Default naming templates
	BranchNameTemplate                       = "frogbot-" + PackagePlaceHolder + "-" + BranchHashPlaceHolder
	AggregatedBranchNameTemplate             = "frogbot-update-" + BranchHashPlaceHolder + "-dependencies"
	CommitMessageTemplate                    = "Upgrade " + PackagePlaceHolder + " to " + FixVersionPlaceHolder
	PullRequestTitleTemplate                 = outputwriter.FrogbotTitlePrefix + " Update version of " + PackagePlaceHolder + " to " + FixVersionPlaceHolder
	AggregatePullRequestTitleDefaultTemplate = outputwriter.FrogbotTitlePrefix + " Update %s dependencies"
)
View Source
const (
	InvalidPathConfigProfile    = "invalid-path-from-root-profile"
	InvalidModulesConfigProfile = "invalid-modules-profile"
	NonExistingProfile          = "non-existing-profile"
)
View Source
const (
	ScanPullRequest = "scan-pull-request"
	ScanRepository  = "scan-repository"
	RootDir         = "."

	JfrogHomeDirEnv = "JFROG_CLI_HOME_DIR"
)

Variables

View Source
var BuildToolsDependenciesMap = map[techutils.Technology][]string{
	techutils.Go:  {"github.com/golang/go"},
	techutils.Pip: {"pip", "setuptools", "wheel"},
}
View Source
var (
	FrogbotVersion = "0.0.0"
)

Functions

func AssertSanitizedEnv

func AssertSanitizedEnv(t *testing.T)

Make sure the environment variables does not contain any Frogbot variables

func BuildServerConfigFile

func BuildServerConfigFile(server *config.ServerDetails) (previousJFrogHomeDir, currentJFrogHomeDir string, err error)

func ChangeToTempDirWithCallback

func ChangeToTempDirWithCallback(t *testing.T) (string, func() error)

func Chdir

func Chdir(dir string) (cbk func() error, err error)

func ConvertSarifPathsToRelative

func ConvertSarifPathsToRelative(issues *issues.ScansIssuesCollection, workingDirs ...string)

func CopyTestdataProjectsToTemp

func CopyTestdataProjectsToTemp(t *testing.T, testDir string) (tmpDir string, restoreFunc func())

Create a temporary directory and copy the content of "testdata/testDir" into it

func CreateDotGitWithCommit

func CreateDotGitWithCommit(t *testing.T, wd, port string, repositoriesPath ...string)

func CreateErrorIfFailUponScannerErrorEnabled

func CreateErrorIfFailUponScannerErrorEnabled(fail bool, messageForLog string, err error) error

CreateErrorIfFailUponScannerErrorEnabled This function checks if fail upn scanner error configuration is enabled by the user. If not - instead of returning an error we log the error and continue as if we didn't have an error

func CreateMockServerForDependencySubmission

func CreateMockServerForDependencySubmission(t *testing.T, owner, repo string) *httptest.Server

CreateMockServerForDependencySubmission creates a mock GitHub server for testing dependency submission

func CreateMockServerForDependencySubmissionError

func CreateMockServerForDependencySubmissionError(t *testing.T, owner, repo string) *httptest.Server

CreateMockServerForDependencySubmissionError creates a mock GitHub server that returns an error

func CreateScanEvent

func CreateScanEvent(serviceDetails *config.ServerDetails, gitInfo *xscservices.XscGitInfoContext, scanType string) *xscservices.XscAnalyticsGeneralEvent

func CreateSyntheticHeadCommit added in v3.4.1

func CreateSyntheticHeadCommit(repoPath string) error

CreateSyntheticHeadCommit creates an empty commit in an existing .git directory so that jgit-based tools (e.g. Gradle versioning plugins) can resolve HEAD without a real clone.

func CreateXscMockServerForConfigProfile

func CreateXscMockServerForConfigProfile(t *testing.T, xrayVersion string) (mockServer *httptest.Server, serverDetails *config.ServerDetails, observedProfileByUrlReq *services.GetRepoConfigurationProfileRequest)

CreateXscMockServerForConfigProfile returns a mock XSC server plus a pointer to the last GetRepoConfigurationProfileRequest observed on the /xsc/profile_repos endpoint. Callers can inspect that pointer after the request to assert what was sent (e.g. workspace_name). It stays nil if the endpoint was never hit.

func DeleteExistingPullRequestComments

func DeleteExistingPullRequestComments(repository *Repository, client vcsclient.VcsClient) error

Delete existing pull request regular comments (Summary, Fallback review comments)

func DeleteExistingPullRequestReviewComments

func DeleteExistingPullRequestReviewComments(repo *Repository, pullRequestID int, client vcsclient.VcsClient) (err error)

Delete existing pull request review comments (Applicable, Sast, Iac)

func DeletePullRequestComments

func DeletePullRequestComments(repo *Repository, client vcsclient.VcsClient, pullRequestID int) (err error)

func DownloadRepoToTempDir

func DownloadRepoToTempDir(client vcsclient.VcsClient, repoOwner, repoName, branch string) (wd string, cleanup func() error, err error)

func ExtractVulnerabilitiesDetailsToRows

func ExtractVulnerabilitiesDetailsToRows(vulnDetails []*VulnerabilityDetails) []formats.VulnerabilityOrViolationRow

func GenerateFixPullRequestDetails

func GenerateFixPullRequestDetails(vulnerabilities []formats.VulnerabilityOrViolationRow, resultsPlatformURL string, writer outputwriter.OutputWriter) (description string, extraComments []string)

func GetFullBranchName

func GetFullBranchName(branchName string) plumbing.ReferenceName

GetFullBranchName returns the full branch name (for example: refs/heads/master) The input branchName can be a short name (master) or a full name (refs/heads/master)

func GetRelativeWd

func GetRelativeWd(fullPathWd, baseWd string) string

GetRelativeWd receive a base working directory along with a full path containing the base working directory, and the relative part is returned without the base prefix.

func GetSortedPullRequestComments

func GetSortedPullRequestComments(client vcsclient.VcsClient, repoOwner, repoName string, prID int) ([]vcsclient.CommentInfo, error)

func GetVulnerabiltiesUniqueID

func GetVulnerabiltiesUniqueID(vulnerability formats.VulnerabilityOrViolationRow) string

func HandlePullRequestCommentsAfterScan

func HandlePullRequestCommentsAfterScan(issues *issues.ScansIssuesCollection, resultContext results.ResultContext, repo *Repository, client vcsclient.VcsClient, pullRequestID int) (err error)

In Scan PR, if there are no issues, comments will be added to the PR with a message that there are no issues.

func IsDirectDependency

func IsDirectDependency(impactPath [][]formats.ComponentRow) (bool, error)

The impact graph of direct dependencies consists of only two elements.

func IsFileTrackedByGit

func IsFileTrackedByGit(filePath, repoRootDir string) (bool, error)

func Md5Hash

func Md5Hash(values ...string) (string, error)

func PrintScanResultsTable added in v3.3.0

func PrintScanResultsTable(scanResults *results.SecurityCommandResults, showSecrets bool)

func SanitizeEnv

func SanitizeEnv() error

func SetEnvAndAssert

func SetEnvAndAssert(t *testing.T, env map[string]string)

Receive an environment variables key-values map, set and assert the environment variables. Return a callback that sets the previous values.

func SetEnvsAndAssertWithCallback

func SetEnvsAndAssertWithCallback(t *testing.T, envs map[string]string) func()

This function takes a map of environment variables and sets them, and returns a callback to UNSET them all

func UploadPrSarifToGithubSecurityTab added in v3.4.0

func UploadPrSarifToGithubSecurityTab(scanResults *results.SecurityCommandResults, repo *Repository, prId int64, client vcsclient.VcsClient) error

func UploadRepoSarifResultsToGithubSecurityTab added in v3.4.0

func UploadRepoSarifResultsToGithubSecurityTab(scanResults *results.SecurityCommandResults, repo *Repository, branch string, client vcsclient.VcsClient) error

func UploadSbomSnapshotToGithubDependencyGraph

func UploadSbomSnapshotToGithubDependencyGraph(owner, repo string, scanResults *results.SecurityCommandResults, client vcsclient.VcsClient, branch string) error

func VerifyEnv

func VerifyEnv(t *testing.T) (server config.ServerDetails, restoreFunc func())

Check connection details with JFrog instance. Return a callback method that restores the credentials after the test is done.

func VulnerabilityDetailsToMD5Hash

func VulnerabilityDetailsToMD5Hash(vulnerabilities ...formats.VulnerabilityOrViolationRow) (string, error)

Generates MD5Hash from a VulnerabilityOrViolationRow The map can be returned in different order from Xray, so we need to sort the strings before hashing.

func WriteScanResultsToGitlabDir

func WriteScanResultsToGitlabDir(outputDir string, scanResults *results.SecurityCommandResults, startTime time.Time) error

Types

type CustomTemplates

type CustomTemplates struct {
	// contains filtered or unexported fields
}

func LoadCustomTemplates

func LoadCustomTemplates(commitMessageTemplate, branchNameTemplate, pullRequestTitleTemplate string) (customTemplates CustomTemplates, err error)

type ErrMissingEnv

type ErrMissingEnv struct {
	VariableName string
}

func (*ErrMissingEnv) Error

func (e *ErrMissingEnv) Error() string

func (*ErrMissingEnv) IsMissingEnvErr

func (e *ErrMissingEnv) IsMissingEnvErr(err error) bool

IsMissingEnvErr returns true if err is a type of ErrMissingEnv, otherwise false

type ErrNothingToCommit

type ErrNothingToCommit struct {
	PackageName string
}

func (*ErrNothingToCommit) Error

func (err *ErrNothingToCommit) Error() string

type ErrUnsupportedFix

type ErrUnsupportedFix struct {
	PackageName  string
	FixedVersion string
	ErrorType    UnsupportedErrorType
}

func (*ErrUnsupportedFix) Error

func (err *ErrUnsupportedFix) Error() string

Custom error for unsupported fixes Currently we hold two unsupported reasons, indirect and build tools dependencies.

type FrogbotDetails

type FrogbotDetails struct {
	XrayVersion   string
	XscVersion    string
	Repository    Repository
	ServerDetails *coreconfig.ServerDetails
	GitClient     vcsclient.VcsClient
	ReleasesRepo  string
}

func GetFrogbotDetails

func GetFrogbotDetails(commandName string) (frogbotDetails *FrogbotDetails, err error)

type Git

type Git struct {
	GitProvider vcsutils.VcsProvider
	vcsclient.VcsInfo
	RepoOwner                    string
	RepoName                     string
	Branches                     []string
	PullRequestDetails           vcsclient.PullRequestInfo
	RepositoryCloneUrl           string
	UploadSbomToVcs              *bool
	UploadPrSecurityResultsToVcs bool
	GitlabScanResultsOutputDir   string
	Workspace                    string
}

func (*Git) GetRepositoryHttpsCloneUrl

func (g *Git) GetRepositoryHttpsCloneUrl(gitClient vcsclient.VcsClient) (string, error)

type GitManager

type GitManager struct {

	// When dryRun is enabled, skipClone allows skipping the cloning of a repository for testing purposes
	SkipClone bool
	// contains filtered or unexported fields
}

func NewGitManager

func NewGitManager() *GitManager

func (*GitManager) AddAllAndCommit

func (gm *GitManager) AddAllAndCommit(commitMessage string, impactedDependencyName string) error

AddAllAndCommit impactedDependencyName is being passed as a parameter to the function to provide a more meaningful error message.

func (*GitManager) BranchExistsInRemote

func (gm *GitManager) BranchExistsInRemote(branchName string) (bool, error)

func (*GitManager) Checkout

func (gm *GitManager) Checkout(branchName string) error

func (*GitManager) CheckoutToHash

func (gm *GitManager) CheckoutToHash(hash string) error

func (*GitManager) Clone

func (gm *GitManager) Clone(destinationPath, branchName string) error

func (*GitManager) CreateBranchAndCheckout

func (gm *GitManager) CreateBranchAndCheckout(branchName string, keepLocalChanges bool) error

Creates a new branch and switches to it. If keepLocalChanges is set to true, all changes made on the current branch before switching to the new one will be transferred to the new branch.

func (*GitManager) Fetch

func (gm *GitManager) Fetch() error

func (*GitManager) GenerateAggregatedCommitMessage

func (gm *GitManager) GenerateAggregatedCommitMessage(tech []techutils.Technology) string

func (*GitManager) GenerateAggregatedFixBranchName

func (gm *GitManager) GenerateAggregatedFixBranchName(baseBranch string, tech []techutils.Technology) (fixBranchName string, err error)

GenerateAggregatedFixBranchName Generating a consistent branch name to enable branch updates and to ensure that there is only one Frogbot aggregate pull request from each base branch scanned.

func (*GitManager) GenerateAggregatedPullRequestTitle

func (gm *GitManager) GenerateAggregatedPullRequestTitle(tech []techutils.Technology) string

func (*GitManager) GenerateCommitMessage

func (gm *GitManager) GenerateCommitMessage(impactedPackage string, fixVersion string) string

func (*GitManager) GenerateFixBranchName

func (gm *GitManager) GenerateFixBranchName(branch string, impactedPackage string, fixVersion string) (string, error)

func (*GitManager) GeneratePullRequestTitle

func (gm *GitManager) GeneratePullRequestTitle(impactedPackage string, version string) string

func (*GitManager) GetAuth

func (gm *GitManager) GetAuth() *githttp.BasicAuth

func (*GitManager) GetRemoteGitUrl

func (gm *GitManager) GetRemoteGitUrl() string

func (*GitManager) GetRemoteName

func (gm *GitManager) GetRemoteName() string

func (*GitManager) IsClean

func (gm *GitManager) IsClean() (bool, error)

IsClean returns true if all the files are in Unmodified status.

func (*GitManager) Push

func (gm *GitManager) Push(force bool, branchName string) error

func (*GitManager) RemoveRemoteBranch

func (gm *GitManager) RemoveRemoteBranch(branchName string) error

func (*GitManager) SetAuth

func (gm *GitManager) SetAuth(username, token string) *GitManager

func (*GitManager) SetCurrentWdAsLocalGitRepository

func (gm *GitManager) SetCurrentWdAsLocalGitRepository() error

func (*GitManager) SetCustomTemplates

func (gm *GitManager) SetCustomTemplates(customTemplates CustomTemplates) *GitManager

func (*GitManager) SetDryRun

func (gm *GitManager) SetDryRun(dryRun bool, dryRunRepoPath string) *GitManager

func (*GitManager) SetGitParams

func (gm *GitManager) SetGitParams(gitParams *Git) *GitManager

func (*GitManager) SetLocalRepositoryAndRemoteName

func (gm *GitManager) SetLocalRepositoryAndRemoteName() (*GitManager, error)

func (*GitManager) SetRemoteGitUrl

func (gm *GitManager) SetRemoteGitUrl(remoteHttpsGitUrl string) (*GitManager, error)

type JFrogPlatform

type JFrogPlatform struct {
	XrayVersion     string
	XscVersion      string
	JFrogProjectKey string
}

type Params

type Params struct {
	*services.ConfigProfile
	Git
	JFrogPlatform
}

type Repository

type Repository struct {
	Params
	OutputWriter outputwriter.OutputWriter
	Server       coreconfig.ServerDetails
}

func BuildRepositoryFromEnv

func BuildRepositoryFromEnv(xrayVersion, xscVersion string, gitClient vcsclient.VcsClient, gitParamsFromEnv *Git, server *coreconfig.ServerDetails, commandName string) (repository Repository, err error)

Builds a Repository from environment variables only Returns a Repository instance with all the defaults and necessary fields.

type ReviewComment

type ReviewComment struct {
	Location    formats.Location
	Type        ReviewCommentType
	CommentInfo vcsclient.PullRequestComment
}

type ReviewCommentType

type ReviewCommentType string
const (
	ApplicableComment ReviewCommentType = "Applicable"
	IacComment        ReviewCommentType = "Iac"
	SastComment       ReviewCommentType = "Sast"
	SecretComment     ReviewCommentType = "Secrets"
	SnippetComment    ReviewCommentType = "Snippet"
)

type ScanDetails

type ScanDetails struct {
	*Git

	*xscservices.XscGitInfoContext
	*config.ServerDetails

	ResultsToCompare *results.SecurityCommandResults
	ConfigProfile    *xscservices.ConfigProfile

	results.ResultContext
	MultiScanId string
	XrayVersion string
	XscVersion  string
	StartTime   time.Time
	// contains filtered or unexported fields
}

func NewScanDetails

func NewScanDetails(client vcsclient.VcsClient, server *config.ServerDetails, git *Git) *ScanDetails

func (*ScanDetails) Audit

func (sc *ScanDetails) Audit(baseDir string) (auditResults *results.SecurityCommandResults)

func (*ScanDetails) BaseBranch

func (sc *ScanDetails) BaseBranch() string

func (*ScanDetails) Client

func (sc *ScanDetails) Client() vcsclient.VcsClient

func (*ScanDetails) SetBaseBranch

func (sc *ScanDetails) SetBaseBranch(branch string) *ScanDetails

func (*ScanDetails) SetConfigProfile

func (sc *ScanDetails) SetConfigProfile(configProfile *xscservices.ConfigProfile) *ScanDetails

func (*ScanDetails) SetDiffScan

func (sc *ScanDetails) SetDiffScan(diffScan bool) *ScanDetails

func (*ScanDetails) SetJfrogVersions

func (sc *ScanDetails) SetJfrogVersions(xrayVersion, xscVersion string) *ScanDetails

func (*ScanDetails) SetRepoName

func (sc *ScanDetails) SetRepoName(repoName string) *ScanDetails

func (*ScanDetails) SetRepoOwner

func (sc *ScanDetails) SetRepoOwner(owner string) *ScanDetails

func (*ScanDetails) SetResultsContext

func (sc *ScanDetails) SetResultsContext(httpCloneUrl string, jfrogProjectKey string, includeVulnerabilities bool) *ScanDetails

func (*ScanDetails) SetResultsToCompare

func (sc *ScanDetails) SetResultsToCompare(results *results.SecurityCommandResults) *ScanDetails

func (*ScanDetails) SetRiskyTechEnvironmentGuard added in v3.5.0

func (sc *ScanDetails) SetRiskyTechEnvironmentGuard(guard func(detectedTechnologies []techutils.Technology) error) *ScanDetails

func (*ScanDetails) SetXscGitInfoContext

func (sc *ScanDetails) SetXscGitInfoContext(scannedBranch, gitProject string, client vcsclient.VcsClient) *ScanDetails

For Repo-Scan

func (*ScanDetails) SetXscPRGitInfoContext

func (sc *ScanDetails) SetXscPRGitInfoContext(gitProject string, client vcsclient.VcsClient, prDetails vcsclient.PullRequestInfo) *ScanDetails

For PR-Scan

type UnsupportedErrorType

type UnsupportedErrorType string
const (
	IndirectDependencyFixNotSupported   UnsupportedErrorType = "IndirectDependencyFixNotSupported"
	BuildToolsDependencyFixNotSupported UnsupportedErrorType = "BuildToolsDependencyFixNotSupported"
	UnsupportedForFixVulnerableVersion  UnsupportedErrorType = "UnsupportedForFixVulnerableVersion"
)

type UrlAccessChecker

type UrlAccessChecker struct {
	// contains filtered or unexported fields
}

func CheckConnection

func CheckConnection(url string) *UrlAccessChecker

CheckConnection checks if the url is accessible in a separate goroutine not to block the main thread

func (*UrlAccessChecker) IsConnected

func (ic *UrlAccessChecker) IsConnected() bool

IsConnected checks if the URL is accessible, waits for the connection check goroutine to finish

type VulnerabilityDetails

type VulnerabilityDetails struct {
	formats.VulnerabilityOrViolationRow
	SuggestedFixedVersion string
	IsDirectDependency    bool
	Cves                  []string
}

func NewVulnerabilityDetails

func NewVulnerabilityDetails(vulnerability formats.VulnerabilityOrViolationRow, fixVersion string) *VulnerabilityDetails

func (*VulnerabilityDetails) SetCves

func (vd *VulnerabilityDetails) SetCves(cves []formats.CveRow)

func (*VulnerabilityDetails) SetIsDirectDependency

func (vd *VulnerabilityDetails) SetIsDirectDependency(isDirectDependency bool)

func (*VulnerabilityDetails) UpdateFixVersionIfMax

func (vd *VulnerabilityDetails) UpdateFixVersionIfMax(fixVersion string)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL