-
Notifications
You must be signed in to change notification settings - Fork 58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for multiple policy in on block event #857
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
09f0489
Add support for multiple policy in on block event.
asafambar b404350
Fix sca issues.
asafambar 4dcff18
Fix comments, add new tag examples with descriptions.
asafambar 7e24ac0
Merge remote-tracking branch 'upstream/dev' into curation-multi-policies
asafambar 17aa426
Fix CR.
asafambar a9d2982
Fix CR.
asafambar a608c5c
Fix CR.
asafambar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,35 +64,33 @@ type PackageStatus struct { | |
Action string `json:"action"` | ||
ParentName string `json:"direct_dependency_package_name"` | ||
ParentVersion string `json:"direct_dependency_package_version"` | ||
BlockedPackageUrl string `json:"blocked_package_url"` | ||
BlockedPackageUrl string `json:"blocked_package_url,omitempty"` | ||
PackageName string `json:"blocked_package_name"` | ||
PackageVersion string `json:"blocked_package_version"` | ||
BlockingReason string `json:"blocking_reason"` | ||
DepRelation string `json:"dependency_relation"` | ||
PkgType string `json:"type"` | ||
Policy []Policy `json:"policies"` | ||
Policy []Policy `json:"policies,omitempty"` | ||
} | ||
|
||
type Policy struct { | ||
asafambar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Policy string `json:"policy"` | ||
Condition string `json:"condition"` | ||
Policy string `json:"policy"` | ||
Condition string `json:"condition"` | ||
Explanation string `json:"explanation"` | ||
Recommendation string `json:"recommendation"` | ||
} | ||
|
||
type PackageStatusTable struct { | ||
Status string `col-name:"Action"` | ||
ParentName string `col-name:"Direct Dependency\nPackage Name"` | ||
ParentVersion string `col-name:"Direct Dependency\nPackage Version"` | ||
BlockedPackageUrl string `col-name:"Blocked Package URL"` | ||
PackageName string `col-name:"Blocked Package\nName"` | ||
PackageVersion string `col-name:"Blocked Package\nVersion"` | ||
BlockingReason string `col-name:"Blocking Reason"` | ||
PkgType string `col-name:"Package Type"` | ||
Policy []policyTable `embed-table:"true"` | ||
} | ||
|
||
type policyTable struct { | ||
Policy string `col-name:"Violated Policy\nName"` | ||
Condition string `col-name:"Violated Condition\nName"` | ||
ParentName string `col-name:"Direct\nDependency\nPackage\nName" auto-merge:"true"` | ||
ParentVersion string `col-name:"Direct\nDependency\nPackage\nVersion" auto-merge:"true"` | ||
PackageName string `col-name:"Blocked\nPackage\nName" auto-merge:"true"` | ||
PackageVersion string `col-name:"Blocked\nPackage\nVersion" auto-merge:"true"` | ||
BlockingReason string `col-name:"Blocking Reason" auto-merge:"true"` | ||
PkgType string `col-name:"Package\nType" auto-merge:"true"` | ||
Policy string `col-name:"Violated\nPolicy\nName"` | ||
Condition string `col-name:"Violated Condition\nName"` | ||
Explanation string `col-name:"Explanation Name"` | ||
Recommendation string `col-name:"Recommendation Name"` | ||
} | ||
|
||
type treeAnalyzer struct { | ||
|
@@ -279,24 +277,34 @@ func printResult(format utils.OutputFormat, projectPath string, packagesStatus [ | |
|
||
func convertToPackageStatusTable(packagesStatus []*PackageStatus) []PackageStatusTable { | ||
var pkgStatusTable []PackageStatusTable | ||
for _, pkgStatus := range packagesStatus { | ||
for index, pkgStatus := range packagesStatus { | ||
// We use auto-merge supported by the 'go-pretty' library. It doesn't have an option to merge lines by a group of unique fields. | ||
// In order to so, we make each group merge only with itself by adding or not adding space. This way, it won't be merged with the next group. | ||
uniqLineSep := "" | ||
if index%2 == 0 { | ||
uniqLineSep = " " | ||
} | ||
pkgTable := PackageStatusTable{ | ||
Status: pkgStatus.Action, | ||
ParentName: pkgStatus.ParentName, | ||
ParentVersion: pkgStatus.ParentVersion, | ||
BlockedPackageUrl: pkgStatus.BlockedPackageUrl, | ||
PackageName: pkgStatus.PackageName, | ||
PackageVersion: pkgStatus.PackageVersion, | ||
BlockingReason: pkgStatus.BlockingReason, | ||
PkgType: pkgStatus.PkgType, | ||
ParentName: pkgStatus.ParentName + uniqLineSep, | ||
ParentVersion: pkgStatus.ParentVersion + uniqLineSep, | ||
PackageName: pkgStatus.PackageName + uniqLineSep, | ||
PackageVersion: pkgStatus.PackageVersion + uniqLineSep, | ||
BlockingReason: pkgStatus.BlockingReason + uniqLineSep, | ||
PkgType: pkgStatus.PkgType + uniqLineSep, | ||
} | ||
if len(pkgStatus.Policy) == 0 { | ||
pkgStatusTable = append(pkgStatusTable, pkgTable) | ||
continue | ||
} | ||
var policiesCondTable []policyTable | ||
for _, policyCond := range pkgStatus.Policy { | ||
policiesCondTable = append(policiesCondTable, policyTable(policyCond)) | ||
pkgTable.Policy = policyCond.Policy | ||
pkgTable.Explanation = policyCond.Explanation | ||
pkgTable.Recommendation = policyCond.Recommendation | ||
pkgTable.Condition = policyCond.Condition | ||
pkgStatusTable = append(pkgStatusTable, pkgTable) | ||
} | ||
pkgTable.Policy = policiesCondTable | ||
pkgStatusTable = append(pkgStatusTable, pkgTable) | ||
} | ||
|
||
return pkgStatusTable | ||
} | ||
|
||
|
@@ -450,23 +458,37 @@ func (nc *treeAnalyzer) getBlockedPackageDetails(packageUrl string, name string, | |
} | ||
|
||
// Return policies and conditions names from the FORBIDDEN HTTP error message. | ||
// Message structure: Package %s:%s download was blocked by JFrog Packages Curation service due to the following policies violated {%s, %s},{%s, %s}. | ||
// Message structure: Package %s:%s download was blocked by JFrog Packages Curation service due to the following policies violated {%s, %s, %s, %s},{%s, %s, %s, %s}. | ||
func (nc *treeAnalyzer) extractPoliciesFromMsg(respError *ErrorsResp) []Policy { | ||
var policies []Policy | ||
msg := respError.Errors[0].Message | ||
allMatches := nc.extractPoliciesRegex.FindAllString(msg, -1) | ||
for _, match := range allMatches { | ||
match = strings.TrimSuffix(strings.TrimPrefix(match, "{"), "}") | ||
polCond := strings.Split(match, ",") | ||
if len(polCond) == 2 { | ||
if len(polCond) >= 2 { | ||
pol := polCond[0] | ||
cond := polCond[1] | ||
|
||
if len(polCond) == 4 { | ||
exp, rec := makeLegiblePolicyDetails(polCond[2], polCond[3]) | ||
policies = append(policies, Policy{Policy: strings.TrimSpace(pol), | ||
Condition: strings.TrimSpace(cond), Explanation: strings.TrimSpace(exp), Recommendation: strings.TrimSpace(rec)}) | ||
continue | ||
} | ||
policies = append(policies, Policy{Policy: strings.TrimSpace(pol), Condition: strings.TrimSpace(cond)}) | ||
} | ||
} | ||
return policies | ||
} | ||
|
||
// Adding a new line after the headline and replace every "|" with a new line. | ||
func makeLegiblePolicyDetails(explanation, recommendation string) (string, string) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's a unit test for this frunction. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have unit test for the function which using it, anyway, I can add dedicated one |
||
explanation = strings.ReplaceAll(strings.Replace(explanation, ": ", ":\n", 1), " | ", "\n") | ||
recommendation = strings.ReplaceAll(strings.Replace(recommendation, ": ", ":\n", 1), " | ", "\n") | ||
return explanation, recommendation | ||
} | ||
|
||
func getUrlNameAndVersionByTech(tech coreutils.Technology, nodeId, artiUrl, repo string) (downloadUrl string, name string, version string) { | ||
if tech == coreutils.Npm { | ||
return getNameScopeAndVersion(nodeId, artiUrl, repo, coreutils.Npm.ToString()) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// Example(auto-merge): --> // Example (auto-merge):