Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 35 additions & 12 deletions internal/commands/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ func NewResultsCommand(
scsScanOverviewWrapper wrappers.ScanOverviewWrapper,
policyWrapper wrappers.PolicyWrapper,
featureFlagsWrapper wrappers.FeatureFlagsWrapper,
jwtWrapper wrappers.JWTWrapper,
) *cobra.Command {
resultCmd := &cobra.Command{
Use: "results",
Expand All @@ -201,7 +202,7 @@ func NewResultsCommand(
},
}
showResultCmd := resultShowSubCommand(resultsWrapper, scanWrapper, exportWrapper, resultsPdfReportsWrapper, resultsJSONReportsWrapper,
risksOverviewWrapper, scsScanOverviewWrapper, policyWrapper, featureFlagsWrapper)
risksOverviewWrapper, scsScanOverviewWrapper, policyWrapper, featureFlagsWrapper, jwtWrapper)
codeBashingCmd := resultCodeBashing(codeBashingWrapper)
bflResultCmd := resultBflSubCommand(bflWrapper)
exitCodeSubcommand := exitCodeSubCommand(scanWrapper)
Expand Down Expand Up @@ -263,6 +264,7 @@ func resultShowSubCommand(
scsScanOverviewWrapper wrappers.ScanOverviewWrapper,
policyWrapper wrappers.PolicyWrapper,
featureFlagsWrapper wrappers.FeatureFlagsWrapper,
jwtWrapper wrappers.JWTWrapper,
) *cobra.Command {
resultShowCmd := &cobra.Command{
Use: "show",
Expand All @@ -273,7 +275,7 @@ func resultShowSubCommand(
$ cx results show --scan-id <scan Id>
`,
),
RunE: runGetResultCommand(resultsWrapper, scanWrapper, exportWrapper, resultsPdfReportsWrapper, resultsJSONReportsWrapper, risksOverviewWrapper, scsScanOverviewWrapper, policyWrapper, featureFlagsWrapper),
RunE: runGetResultCommand(resultsWrapper, scanWrapper, exportWrapper, resultsPdfReportsWrapper, resultsJSONReportsWrapper, risksOverviewWrapper, scsScanOverviewWrapper, policyWrapper, featureFlagsWrapper, jwtWrapper),
}
addScanIDFlag(resultShowCmd, "ID to report on")
addResultFormatFlag(
Expand Down Expand Up @@ -309,8 +311,7 @@ func resultShowSubCommand(
commonParams.ResultPolicyDefaultTimeout,
"Cancel the policy evaluation and fail after the timeout in minutes",
)
resultShowCmd.PersistentFlags().Bool(commonParams.IgnorePolicyFlag, false, "Do not evaluate policies")
_ = resultShowCmd.PersistentFlags().MarkHidden(commonParams.IgnorePolicyFlag)
resultShowCmd.PersistentFlags().Bool(commonParams.IgnorePolicyFlag, false, "Skip policy evaluation. Requires override-policy-management permission.")
resultShowCmd.PersistentFlags().Bool(commonParams.SastRedundancyFlag, false,
"Populate SAST results 'data.redundancy' with values '"+fixLabel+"' (to fix) or '"+redundantLabel+"' (no need to fix)")
resultShowCmd.PersistentFlags().Bool(commonParams.ScaHideDevAndTestDepFlag, false, scaHideDevAndTestDepFlagDescription)
Expand Down Expand Up @@ -853,7 +854,7 @@ func writeMarkdownSummary(targetFile string, data *wrappers.ResultSummary) error
}

// nolint: whitespace
func writeConsoleSummary(summary *wrappers.ResultSummary, featureFlagsWrapper wrappers.FeatureFlagsWrapper) error {
func writeConsoleSummary(summary *wrappers.ResultSummary, featureFlagsWrapper wrappers.FeatureFlagsWrapper, ignorePolicyFlagOmit bool) error {
if !isScanPending(summary.Status) {
fmt.Printf(" Scan Summary: \n")
fmt.Printf(" Created At: %s\n", summary.CreatedAt)
Expand All @@ -865,7 +866,7 @@ func writeConsoleSummary(summary *wrappers.ResultSummary, featureFlagsWrapper wr
summary.RiskMsg,
)
if summary.Policies != nil && !strings.EqualFold(summary.Policies.Status, policeManagementNoneStatus) {
printPoliciesSummary(summary)
printPoliciesSummary(summary, ignorePolicyFlagOmit)
}

printResultsSummaryTable(summary)
Expand All @@ -886,7 +887,7 @@ func writeConsoleSummary(summary *wrappers.ResultSummary, featureFlagsWrapper wr
return nil
}

func printPoliciesSummary(summary *wrappers.ResultSummary) {
func printPoliciesSummary(summary *wrappers.ResultSummary, ignorePolicyFlagOmit bool) {
hasViolations := false
for _, policy := range summary.Policies.Policies {
if len(policy.RulesViolated) > 0 {
Expand All @@ -896,6 +897,9 @@ func printPoliciesSummary(summary *wrappers.ResultSummary) {
}
if hasViolations {
fmt.Printf(tableLine + "\n")
if ignorePolicyFlagOmit {
printWarningIfIgnorePolicyOmiited()
}
if summary.Policies.BreakBuild {
fmt.Printf(" Policy Management Violation - Break Build Enabled: \n")
} else {
Expand Down Expand Up @@ -1017,6 +1021,7 @@ func runGetResultCommand(
scsScanOverviewWrapper wrappers.ScanOverviewWrapper,
policyWrapper wrappers.PolicyWrapper,
featureFlagsWrapper wrappers.FeatureFlagsWrapper,
jwtWrapper wrappers.JWTWrapper,
) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {
targetFile, _ := cmd.Flags().GetString(commonParams.TargetFlag)
Expand All @@ -1028,6 +1033,19 @@ func runGetResultCommand(
sastRedundancy, _ := cmd.Flags().GetBool(commonParams.SastRedundancyFlag)
agent, _ := cmd.Flags().GetString(commonParams.AgentFlag)
scaHideDevAndTestDep, _ := cmd.Flags().GetBool(commonParams.ScaHideDevAndTestDepFlag)
ignorePolicy, _ := cmd.Flags().GetBool(commonParams.IgnorePolicyFlag)
// Check if the user has permission to override policy management if --ignore-policy is set
ignorePolicyFlagOmit := false
if ignorePolicy {
overridePolicyManagementPer, err := jwtWrapper.CheckPermissionByAccessToken(OverridePolicyManagement)
if err != nil {
return err
}
if !overridePolicyManagementPer {
ignorePolicyFlagOmit = true
ignorePolicy = false
}
}
waitDelay, _ := cmd.Flags().GetInt(commonParams.WaitDelayFlag)
policyTimeout, _ := cmd.Flags().GetInt(commonParams.PolicyTimeoutFlag)

Expand Down Expand Up @@ -1055,7 +1073,7 @@ func runGetResultCommand(

var policyResponseModel *wrappers.PolicyResponseModel
if !isScanPending(string(scan.Status)) {
policyResponseModel, err = services.HandlePolicyEvaluation(cmd, policyWrapper, scan, agent, waitDelay, policyTimeout)
policyResponseModel, err = services.HandlePolicyEvaluation(cmd, policyWrapper, scan, ignorePolicy, agent, waitDelay, policyTimeout)
if err != nil {
return err
}
Expand All @@ -1069,7 +1087,7 @@ func runGetResultCommand(

_, err = CreateScanReport(resultsWrapper, risksOverviewWrapper, scsScanOverviewWrapper, exportWrapper,
policyResponseModel, resultsPdfReportsWrapper, resultsJSONReportsWrapper, scan, format, formatPdfToEmail, formatPdfOptions,
formatSbomOptions, targetFile, targetPath, agent, resultsParams, featureFlagsWrapper)
formatSbomOptions, targetFile, targetPath, agent, resultsParams, featureFlagsWrapper, ignorePolicyFlagOmit)
return err
}
}
Expand Down Expand Up @@ -1176,6 +1194,7 @@ func CreateScanReport(
agent string,
resultsParams map[string]string,
featureFlagsWrapper wrappers.FeatureFlagsWrapper,
ignorePolicyFlagOmit bool,
) (*wrappers.ScanResultsCollection, error) {
reportList := strings.Split(reportTypes, ",")
results := &wrappers.ScanResultsCollection{}
Expand Down Expand Up @@ -1206,7 +1225,7 @@ func CreateScanReport(
}
for _, reportType := range reportList {
err = createReport(reportType, formatPdfToEmail, formatPdfOptions, formatSbomOptions, targetFile,
targetPath, results, summary, exportWrapper, resultsPdfReportsWrapper, resultsJSONReportsWrapper, featureFlagsWrapper, agent)
targetPath, results, summary, exportWrapper, resultsPdfReportsWrapper, resultsJSONReportsWrapper, featureFlagsWrapper, ignorePolicyFlagOmit)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1386,7 +1405,7 @@ func createReport(format,
resultsPdfReportsWrapper wrappers.ResultsPdfWrapper,
resultsJSONReportsWrapper wrappers.ResultsJSONWrapper,
featureFlagsWrapper wrappers.FeatureFlagsWrapper,
agent string) error {
ignorePolicyFlagOmit bool) error {
if printer.IsFormat(format, printer.FormatIndentedJSON) {
return nil
}
Expand Down Expand Up @@ -1416,7 +1435,7 @@ func createReport(format,
}

if printer.IsFormat(format, printer.FormatSummaryConsole) {
return writeConsoleSummary(summary, featureFlagsWrapper)
return writeConsoleSummary(summary, featureFlagsWrapper, ignorePolicyFlagOmit)
}
if printer.IsFormat(format, printer.FormatSummary) {
summaryRpt := createTargetName(targetFile, targetPath, printer.FormatHTML)
Expand Down Expand Up @@ -2867,3 +2886,7 @@ type ScannerResponse struct {
Details string `json:"Details,omitempty"`
ErrorCode string `json:"ErrorCode,omitempty"`
}

func printWarningIfIgnorePolicyOmiited() {
fmt.Printf("\n Warning: The --ignore-policy flag was not implemented because you don’t have the required permission.\n Only users with 'override-policy-management' permission can use this flag. \n\n")
}
70 changes: 69 additions & 1 deletion internal/commands/result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1314,7 +1314,7 @@ func TestPrintPoliciesSummary_WhenNoRolViolated_ShouldNotContainPolicyViolation(
old := os.Stdout
os.Stdout = w

printPoliciesSummary(summary)
printPoliciesSummary(summary, false)

w.Close()
os.Stdout = old
Expand Down Expand Up @@ -1629,3 +1629,71 @@ func Test_addPackageInformation_DependencyTypes(t *testing.T) {
assert.Equal(t, false, testPackage.IsDevelopmentDependency, "Second package should not be marked as development dependency")
assert.Equal(t, true, testPackage.IsTestDependency, "Second package should be marked as test dependency")
}

func TestIgnorePolicyWithNoPermission(t *testing.T) {
policyResponseModel := wrappers.PolicyResponseModel{}
policyResponseModel.BreakBuild = false

policy := wrappers.Policy{}
policy.Name = "MOCK_NAME1"
policy.RulesViolated = make([]string, 1)
policy.BreakBuild = true
policy.Description = "MOCK_DESC1"
policy.Tags = make([]string, 0)

var policies []wrappers.Policy
policies = append(policies, policy)
policyResponseModel.Policies = policies
summary := &wrappers.ResultSummary{
Policies: &policyResponseModel,
}
r, w, _ := os.Pipe()
old := os.Stdout
os.Stdout = w

printPoliciesSummary(summary, true)

w.Close()
os.Stdout = old

var buf bytes.Buffer
if _, err := io.Copy(&buf, r); err != nil {
t.Fatalf("failed to copy output: %v", err) // Handle the error if io.Copy fails
}
output := buf.String()
assert.Assert(t, strings.Contains(output, "Warning: The --ignore-policy flag was not implemented because you don’t have the required permission."), "'Ignore Policy flag omitted because you dont have permission' should not be present in the output")
}

func TestIgnorePolicyWithPermission(t *testing.T) {
policyResponseModel := wrappers.PolicyResponseModel{}
policyResponseModel.BreakBuild = false

policy := wrappers.Policy{}
policy.Name = "MOCK_NAME2"
policy.RulesViolated = make([]string, 1)
policy.BreakBuild = true
policy.Description = "MOCK_DESC2"
policy.Tags = make([]string, 0)

var policies []wrappers.Policy
policies = append(policies, policy)
policyResponseModel.Policies = policies
summary := &wrappers.ResultSummary{
Policies: &policyResponseModel,
}
r, w, _ := os.Pipe()
old := os.Stdout
os.Stdout = w

printPoliciesSummary(summary, false)

w.Close()
os.Stdout = old

var buf bytes.Buffer
if _, err := io.Copy(&buf, r); err != nil {
t.Fatalf("failed to copy output: %v", err) // Handle the error if io.Copy fails
}
output := buf.String()
assert.Assert(t, !strings.Contains(output, "Warning: The --ignore-policy flag was not implemented because you don’t have the required permission."), "'Ignore Policy flag omitted because you dont have permission' should not be present in the output")
}
1 change: 1 addition & 0 deletions internal/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ func NewAstCLI(
scsScanOverviewWrapper,
policyWrapper,
featureFlagsWrapper,
jwtWrapper,
)

versionCmd := util.NewVersionCommand()
Expand Down
Loading
Loading