Skip to content

Commit 095c502

Browse files
Merge branch 'main' into other/elchanan/oss-realtime-location-structure
2 parents 3b5677a + c4be2e5 commit 095c502

File tree

8 files changed

+50
-20
lines changed

8 files changed

+50
-20
lines changed

internal/commands/result.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ func resultShowSubCommand(
306306
"Cancel the policy evaluation and fail after the timeout in minutes",
307307
)
308308
resultShowCmd.PersistentFlags().Bool(commonParams.IgnorePolicyFlag, false, "Do not evaluate policies")
309+
_ = resultShowCmd.PersistentFlags().MarkHidden(commonParams.IgnorePolicyFlag)
309310
resultShowCmd.PersistentFlags().Bool(commonParams.SastRedundancyFlag, false,
310311
"Populate SAST results 'data.redundancy' with values '"+fixLabel+"' (to fix) or '"+redundantLabel+"' (no need to fix)")
311312
resultShowCmd.PersistentFlags().Bool(commonParams.ScaHideDevAndTestDepFlag, false, scaHideDevAndTestDepFlagDescription)
@@ -1022,7 +1023,6 @@ func runGetResultCommand(
10221023
sastRedundancy, _ := cmd.Flags().GetBool(commonParams.SastRedundancyFlag)
10231024
agent, _ := cmd.Flags().GetString(commonParams.AgentFlag)
10241025
scaHideDevAndTestDep, _ := cmd.Flags().GetBool(commonParams.ScaHideDevAndTestDepFlag)
1025-
ignorePolicy, _ := cmd.Flags().GetBool(commonParams.IgnorePolicyFlag)
10261026
waitDelay, _ := cmd.Flags().GetInt(commonParams.WaitDelayFlag)
10271027
policyTimeout, _ := cmd.Flags().GetInt(commonParams.PolicyTimeoutFlag)
10281028

@@ -1050,7 +1050,7 @@ func runGetResultCommand(
10501050

10511051
var policyResponseModel *wrappers.PolicyResponseModel
10521052
if !isScanPending(string(scan.Status)) {
1053-
policyResponseModel, err = services.HandlePolicyEvaluation(cmd, policyWrapper, scan, ignorePolicy, agent, waitDelay, policyTimeout)
1053+
policyResponseModel, err = services.HandlePolicyEvaluation(cmd, policyWrapper, scan, agent, waitDelay, policyTimeout)
10541054
if err != nil {
10551055
return err
10561056
}

internal/commands/root.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/checkmarx/ast-cli/internal/logger"
1313
"github.com/checkmarx/ast-cli/internal/params"
1414
"github.com/checkmarx/ast-cli/internal/wrappers/bitbucketserver"
15+
"github.com/checkmarx/ast-cli/internal/wrappers/configuration"
1516
"github.com/pkg/errors"
1617

1718
"github.com/checkmarx/ast-cli/internal/wrappers"
@@ -95,20 +96,23 @@ func NewAstCLI(
9596
rootCmd.PersistentFlags().String(params.TenantFlag, params.Tenant, params.TenantFlagUsage)
9697
rootCmd.PersistentFlags().Uint(params.RetryFlag, params.RetryDefault, params.RetryUsage)
9798
rootCmd.PersistentFlags().Uint(params.RetryDelayFlag, params.RetryDelayDefault, params.RetryDelayUsage)
99+
rootCmd.PersistentFlags().String(params.ConfigFilePathFlag, "", "Path to the configuration file")
98100

99101
rootCmd.PersistentFlags().Bool(params.ApikeyOverrideFlag, false, "")
100102

101103
_ = rootCmd.PersistentFlags().MarkHidden(params.ApikeyOverrideFlag)
102104

103105
// This monitors and traps situations where "extra/garbage" commands
104106
// are passed to Cobra.
105-
rootCmd.PersistentPreRun = func(cmd *cobra.Command, args []string) {
107+
rootCmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
106108
PrintConfiguration()
109+
err := configuration.LoadConfiguration()
107110
// Need to check the __complete command to allow correct behavior of the autocomplete
108111
if len(args) > 0 && cmd.Name() != params.Help && cmd.Name() != "__complete" {
109112
_ = cmd.Help()
110113
os.Exit(0)
111114
}
115+
return err
112116
}
113117
// Link the environment variable to the CLI argument(s).
114118
_ = viper.BindPFlag(params.AccessKeyIDConfigKey, rootCmd.PersistentFlags().Lookup(params.AccessKeyIDFlag))
@@ -124,6 +128,7 @@ func NewAstCLI(
124128
_ = viper.BindPFlag(params.AgentNameKey, rootCmd.PersistentFlags().Lookup(params.AgentFlag))
125129
_ = viper.BindPFlag(params.OriginKey, rootCmd.PersistentFlags().Lookup(params.OriginFlag))
126130
_ = viper.BindPFlag(params.IgnoreProxyKey, rootCmd.PersistentFlags().Lookup(params.IgnoreProxyFlag))
131+
_ = viper.BindPFlag(params.ConfigFilePathKey, rootCmd.PersistentFlags().Lookup(params.ConfigFilePathFlag))
127132
// Key here is the actual flag since it doesn't use an environment variable
128133
_ = viper.BindPFlag(params.DebugFlag, rootCmd.PersistentFlags().Lookup(params.DebugFlag))
129134
_ = viper.BindPFlag(params.InsecureFlag, rootCmd.PersistentFlags().Lookup(params.InsecureFlag))

internal/commands/scan.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1893,9 +1893,8 @@ func runCreateScanCommand(
18931893
}
18941894

18951895
agent, _ := cmd.Flags().GetString(commonParams.AgentFlag)
1896-
ignorePolicy, _ := cmd.Flags().GetBool(commonParams.IgnorePolicyFlag)
18971896
policyTimeout, _ := cmd.Flags().GetInt(commonParams.PolicyTimeoutFlag)
1898-
policyResponseModel, err = services.HandlePolicyEvaluation(cmd, policyWrapper, scanResponseModel, ignorePolicy, agent, waitDelay, policyTimeout)
1897+
policyResponseModel, err = services.HandlePolicyEvaluation(cmd, policyWrapper, scanResponseModel, agent, waitDelay, policyTimeout)
18991898
if err != nil {
19001899
return err
19011900
}
@@ -1922,7 +1921,7 @@ func runCreateScanCommand(
19221921
// verify break build from policy
19231922
if policyResponseModel != nil && len(policyResponseModel.Policies) > 0 && policyResponseModel.BreakBuild {
19241923
logger.PrintIfVerbose("Breaking the build due to policy violation")
1925-
return errors.Errorf("Policy Violation - Break Build Enabled. To bypass the policy evaluation and continue with the build, you can use the `--ignore-policy` flag.")
1924+
return errors.Errorf("Policy Violation - Break Build Enabled.")
19261925
}
19271926
return nil
19281927
}

internal/params/flags.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ const (
156156
ScaPrivatePackageVersionFlag = "sca-private-package-version"
157157
ScaHideDevAndTestDepFlag = "sca-hide-dev-test-dependencies"
158158
LimitFlag = "limit"
159+
ConfigFilePathFlag = "config-file-path"
159160

160161
// INDIVIDUAL FILTER FLAGS
161162
SastFilterFlag = "sast-filter"

internal/services/policy-management.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@ import (
1313

1414
var noPolicyEvaluatingIDEs = []string{commonParams.EclipseAgent, commonParams.JetbrainsAgent, commonParams.VSCodeAgent, commonParams.VisualStudioAgent}
1515

16-
func HandlePolicyEvaluation(cmd *cobra.Command, policyWrapper wrappers.PolicyWrapper, scan *wrappers.ScanResponseModel,
17-
ignorePolicy bool, agent string, waitDelay, policyTimeout int) (*wrappers.PolicyResponseModel, error) {
16+
func HandlePolicyEvaluation(cmd *cobra.Command, policyWrapper wrappers.PolicyWrapper, scan *wrappers.ScanResponseModel, agent string, waitDelay, policyTimeout int) (*wrappers.PolicyResponseModel, error) {
1817
policyResponseModel := &wrappers.PolicyResponseModel{}
1918

20-
if ignorePolicy || slices.Contains(noPolicyEvaluatingIDEs, agent) {
19+
if slices.Contains(noPolicyEvaluatingIDEs, agent) {
2120
logger.PrintIfVerbose("Skipping policy evaluation")
2221
return policyResponseModel, nil
2322
}

internal/services/policy-management_test.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ func TestHandlePolicyEvaluation(t *testing.T) {
2222
cmd: &cobra.Command{},
2323
policyWrapper: policyWrapper,
2424
scan: scanResults,
25-
ignorePolicy: false,
2625
waitDelay: 1,
2726
policyTimeout: 1,
2827
}
@@ -39,7 +38,6 @@ func TestHandlePolicyEvaluation(t *testing.T) {
3938
cmd: commonArgs.cmd,
4039
policyWrapper: commonArgs.policyWrapper,
4140
scan: commonArgs.scan,
42-
ignorePolicy: commonArgs.ignorePolicy,
4341
agent: params.DefaultAgent,
4442
waitDelay: commonArgs.waitDelay,
4543
policyTimeout: commonArgs.policyTimeout,
@@ -53,7 +51,6 @@ func TestHandlePolicyEvaluation(t *testing.T) {
5351
cmd: commonArgs.cmd,
5452
policyWrapper: commonArgs.policyWrapper,
5553
scan: commonArgs.scan,
56-
ignorePolicy: commonArgs.ignorePolicy,
5754
agent: params.EclipseAgent,
5855
waitDelay: commonArgs.waitDelay,
5956
policyTimeout: commonArgs.policyTimeout,
@@ -67,7 +64,6 @@ func TestHandlePolicyEvaluation(t *testing.T) {
6764
cmd: commonArgs.cmd,
6865
policyWrapper: commonArgs.policyWrapper,
6966
scan: commonArgs.scan,
70-
ignorePolicy: commonArgs.ignorePolicy,
7167
agent: params.VSCodeAgent,
7268
waitDelay: commonArgs.waitDelay,
7369
policyTimeout: commonArgs.policyTimeout,
@@ -81,7 +77,6 @@ func TestHandlePolicyEvaluation(t *testing.T) {
8177
cmd: commonArgs.cmd,
8278
policyWrapper: commonArgs.policyWrapper,
8379
scan: commonArgs.scan,
84-
ignorePolicy: commonArgs.ignorePolicy,
8580
agent: params.VisualStudioAgent,
8681
waitDelay: commonArgs.waitDelay,
8782
policyTimeout: commonArgs.policyTimeout,
@@ -95,7 +90,6 @@ func TestHandlePolicyEvaluation(t *testing.T) {
9590
cmd: commonArgs.cmd,
9691
policyWrapper: commonArgs.policyWrapper,
9792
scan: commonArgs.scan,
98-
ignorePolicy: commonArgs.ignorePolicy,
9993
agent: params.JetbrainsAgent,
10094
waitDelay: commonArgs.waitDelay,
10195
policyTimeout: commonArgs.policyTimeout,
@@ -112,7 +106,6 @@ func TestHandlePolicyEvaluation(t *testing.T) {
112106
tc.args.cmd,
113107
tc.args.policyWrapper,
114108
tc.args.scan,
115-
tc.args.ignorePolicy,
116109
tc.args.agent,
117110
tc.args.waitDelay,
118111
tc.args.policyTimeout,

internal/wrappers/configuration/configuration.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,7 @@ func LoadConfiguration() error {
139139
}
140140
fullPath := usr.HomeDir + configDirName
141141
verifyConfigDir(fullPath)
142-
viper.AddConfigPath(fullPath)
143-
configFile := "checkmarxcli"
144-
viper.SetConfigName(configFile)
145-
viper.SetConfigType("yaml")
142+
viper.SetConfigFile(fullPath + "/checkmarxcli.yaml")
146143
_ = viper.ReadInConfig()
147144
}
148145
return nil

test/integration/configuration_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,39 @@ func TestSetConfigProperty_EnvVarConfigFilePath(t *testing.T) {
7373
err, _ = executeCommand(t, "configure", "set", "--prop-name", "cx_client_id", "--prop-value", "example_client_id")
7474
assert.NilError(t, err)
7575
}
76+
77+
func TestLoadConfiguration_ConfigFilePathFlag(t *testing.T) {
78+
err, _ := executeCommand(t, "configure", "show", "--config-file-path", filePath)
79+
assert.NilError(t, err)
80+
}
81+
82+
func TestLoadConfiguration_ConfigFilePathFlagValidDirectory(t *testing.T) {
83+
err, _ := executeCommand(t, "configure", "show", "--config-file-path", "data")
84+
assert.ErrorContains(t, err, "The specified path points to a directory, not a file.")
85+
}
86+
87+
func TestLoadConfiguration_ConfigFilePathFlagFileNotFound(t *testing.T) {
88+
err, _ := executeCommand(t, "configure", "show", "--config-file-path", "data/nonexistent_config.yaml")
89+
assert.ErrorContains(t, err, "The specified file does not exist.")
90+
}
91+
92+
func TestSetConfigProperty_ConfigFilePathFlag(t *testing.T) {
93+
err, _ := executeCommand(t, "configure", "set", "--prop-name", "cx_client_id", "--prop-value", "dummy-client_id", "--config-file-path", filePath)
94+
assert.NilError(t, err)
95+
96+
content, err := os.ReadFile(filePath)
97+
assert.NilError(t, err)
98+
assert.Assert(t, strings.Contains(string(content), "dummy-client_id"))
99+
100+
err, _ = executeCommand(t, "configure", "set", "--prop-name", "cx_client_id", "--prop-value", "example_client_id", "--config-file-path", filePath)
101+
assert.NilError(t, err)
102+
}
103+
104+
func TestLoadConfiguration_ConfigFilePathFlagFileWithoutPermission(t *testing.T) {
105+
if err := os.Chmod(filePath, 0000); err != nil {
106+
t.Fatalf("failed to set file permissions: %v", err)
107+
}
108+
defer os.Chmod(filePath, 0644)
109+
err, _ := executeCommand(t, "configure", "show", "--config-file-path", filePath)
110+
assert.ErrorContains(t, err, "Access to the specified file is restricted")
111+
}

0 commit comments

Comments
 (0)