Skip to content

Commit a68ffc1

Browse files
Merge pull request #41 from snyk/chore/revert-local-policy-changes
Revert "Merge pull request #40 from snyk/feat(reachability)/pass-loca…
2 parents df179f8 + d96f1f4 commit a68ffc1

File tree

5 files changed

+59
-94
lines changed

5 files changed

+59
-94
lines changed

internal/commands/ostest/depgraph_flow.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ func RunUnifiedTestFlow(
2525
ctx context.Context,
2626
ictx workflow.InvocationContext,
2727
testClient testapi.TestClient,
28+
riskScoreThreshold *uint16,
29+
severityThreshold *testapi.Severity,
2830
orgID string,
2931
orgSlugOrID string,
3032
errFactory *errors.ErrorFactory,
3133
logger *zerolog.Logger,
32-
localPolicy *testapi.LocalPolicy,
3334
) ([]workflow.Data, error) {
3435
logger.Info().Msg("Starting open source test")
3536

@@ -39,6 +40,8 @@ func RunUnifiedTestFlow(
3940
return nil, err
4041
}
4142

43+
localPolicy := createLocalPolicy(riskScoreThreshold, severityThreshold)
44+
4245
allLegacyFindings, allOutputData, err := testAllDepGraphs(
4346
ctx,
4447
ictx,
@@ -227,6 +230,18 @@ func prepareJSONOutput(
227230
return bytes.TrimRight(buffer.Bytes(), "\n"), nil
228231
}
229232

233+
// Create local policy only if risk score or severity threshold are specified.
234+
func createLocalPolicy(riskScoreThreshold *uint16, severityThreshold *testapi.Severity) *testapi.LocalPolicy {
235+
if riskScoreThreshold == nil && severityThreshold == nil {
236+
return nil
237+
}
238+
239+
return &testapi.LocalPolicy{
240+
RiskScoreThreshold: riskScoreThreshold,
241+
SeverityThreshold: severityThreshold,
242+
}
243+
}
244+
230245
// createDepGraphs creates depgraphs from the file parameter in the context.
231246
func createDepGraphs(ictx workflow.InvocationContext) ([]*testapi.IoSnykApiV1testdepgraphRequestDepGraph, []string, error) {
232247
depGraphResult, err := service.GetDepGraph(ictx)

internal/commands/ostest/sbom_reachability_flow.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ func RunSbomReachabilityFlow(
2626
bsClient bundlestore.Client,
2727
orgID string,
2828
orgSlugOrID string,
29-
localPolicy *testapi.LocalPolicy,
3029
) ([]workflow.Data, error) {
3130
if sourceCodePath == "" {
3231
sourceCodePath = "."
@@ -69,7 +68,7 @@ func RunSbomReachabilityFlow(
6968
return nil, fmt.Errorf("failed to create sbom test reachability subject: %w", err)
7069
}
7170

72-
findings, summary, err := RunTest(ctx, ictx, testClient, subject, "", "", int(0), sbomPath, orgID, orgSlugOrID, errFactory, logger, localPolicy)
71+
findings, summary, err := RunTest(ctx, ictx, testClient, subject, "", "", int(0), sbomPath, orgID, orgSlugOrID, errFactory, logger, nil)
7372
if err != nil {
7473
return nil, err
7574
}

internal/commands/ostest/sbom_reachability_flow_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func Test_RunSbomReachabilityFlow_JSON(t *testing.T) {
4242
mockIctx, mockTestClient, mockBsClient, orgID, orgSlug, sbomPath, sourceCodePath := setupTest(ctx, t, ctrl, true)
4343

4444
// This should now succeed with proper finding data
45-
result, err := ostest.RunSbomReachabilityFlow(ctx, mockIctx, mockTestClient, ef, &nopLogger, sbomPath, sourceCodePath, mockBsClient, orgID, orgSlug, nil)
45+
result, err := ostest.RunSbomReachabilityFlow(ctx, mockIctx, mockTestClient, ef, &nopLogger, sbomPath, sourceCodePath, mockBsClient, orgID, orgSlug)
4646

4747
require.NoError(t, err)
4848
require.NotNil(t, result)
@@ -67,7 +67,7 @@ func Test_RunSbomReachabilityFlow_HumanReadable(t *testing.T) {
6767
mockIctx, mockTestClient, mockBsClient, orgID, orgSlug, sbomPath, sourceCodePath := setupTest(ctx, t, ctrl, false)
6868

6969
// This should now succeed with proper finding data
70-
result, err := ostest.RunSbomReachabilityFlow(ctx, mockIctx, mockTestClient, ef, &nopLogger, sbomPath, sourceCodePath, mockBsClient, orgID, orgSlug, nil)
70+
result, err := ostest.RunSbomReachabilityFlow(ctx, mockIctx, mockTestClient, ef, &nopLogger, sbomPath, sourceCodePath, mockBsClient, orgID, orgSlug)
7171

7272
require.NoError(t, err)
7373
require.NotNil(t, result)

internal/commands/ostest/workflow.go

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ func setupSBOMReachabilityFlow(
8282
errFactory *errors.ErrorFactory,
8383
logger *zerolog.Logger,
8484
sbom, sourceDir string,
85-
localPolicy *testapi.LocalPolicy,
8685
) ([]workflow.Data, error) {
8786
config := ictx.GetConfiguration()
8887

@@ -106,38 +105,55 @@ func setupSBOMReachabilityFlow(
106105
)
107106

108107
bsClient := bundlestore.NewClient(ictx.GetNetworkAccess().GetHttpClient(), codeScannerConfig, cScanner, logger)
109-
return RunSbomReachabilityFlow(ctx, ictx, testClient, errFactory, logger, sbom, sourceDir, bsClient, orgID, orgSlugOrID, localPolicy)
108+
return RunSbomReachabilityFlow(ctx, ictx, testClient, errFactory, logger, sbom, sourceDir, bsClient, orgID, orgSlugOrID)
110109
}
111110

112-
// CreateLocalPolicy will create a local policy only if risk score or severity threshold are specified in the config.
113-
func CreateLocalPolicy(config configuration.Configuration, logger *zerolog.Logger) *testapi.LocalPolicy {
114-
var riskScoreThreshold *uint16
115-
riskScoreThresholdInt := config.GetInt(flags.FlagRiskScoreThreshold)
116-
if riskScoreThresholdInt >= math.MaxUint16 {
111+
// setupDefaultTestFlow sets up and runs the default test flow with risk score and severity thresholds.
112+
func setupDefaultTestFlow(
113+
ctx context.Context,
114+
ictx workflow.InvocationContext,
115+
testClient testapi.TestClient,
116+
orgID string,
117+
orgSlugOrID string,
118+
errFactory *errors.ErrorFactory,
119+
logger *zerolog.Logger,
120+
riskScoreThreshold int,
121+
) ([]workflow.Data, error) {
122+
config := ictx.GetConfiguration()
123+
124+
// Risk Score FFs
125+
ffRiskScore := config.GetBool(FeatureFlagRiskScore)
126+
ffRiskScoreInCLI := config.GetBool(FeatureFlagRiskScoreInCLI)
127+
riskScoreFFsEnabled := ffRiskScore && ffRiskScoreInCLI
128+
129+
if riskScoreThreshold != -1 && !riskScoreFFsEnabled {
130+
// The user tried to use a risk score threshold without the required feature flags.
131+
// Return a specific error for the first missing flag found.
132+
if !ffRiskScore {
133+
return nil, errFactory.NewFeatureNotPermittedError(FeatureFlagRiskScore)
134+
}
135+
return nil, errFactory.NewFeatureNotPermittedError(FeatureFlagRiskScoreInCLI)
136+
}
137+
138+
var riskScorePtr *uint16
139+
if riskScoreThreshold >= math.MaxUint16 {
117140
// the API will enforce a range from the test spec
118-
logger.Warn().Msgf("Risk score threshold %d exceeds maximum uint16 value. Setting to maximum.", riskScoreThresholdInt)
141+
logger.Warn().Msgf("Risk score threshold %d exceeds maximum uint16 value. Setting to maximum.", riskScoreThreshold)
119142
maxVal := uint16(math.MaxUint16)
120-
riskScoreThreshold = &maxVal
121-
} else if riskScoreThresholdInt >= 0 {
122-
rs := uint16(riskScoreThresholdInt)
123-
riskScoreThreshold = &rs
143+
riskScorePtr = &maxVal
144+
} else if riskScoreThreshold >= 0 {
145+
rs := uint16(riskScoreThreshold)
146+
riskScorePtr = &rs
124147
}
125148

126-
var severityThreshold *testapi.Severity
149+
var severityThresholdPtr *testapi.Severity
127150
severityThresholdStr := config.GetString(flags.FlagSeverityThreshold)
128151
if severityThresholdStr != "" {
129152
st := testapi.Severity(severityThresholdStr)
130-
severityThreshold = &st
153+
severityThresholdPtr = &st
131154
}
132155

133-
if riskScoreThreshold == nil && severityThreshold == nil {
134-
return nil
135-
}
136-
137-
return &testapi.LocalPolicy{
138-
RiskScoreThreshold: riskScoreThreshold,
139-
SeverityThreshold: severityThreshold,
140-
}
156+
return RunUnifiedTestFlow(ctx, ictx, testClient, riskScorePtr, severityThresholdPtr, orgID, orgSlugOrID, errFactory, logger)
141157
}
142158

143159
// OSWorkflow is the entry point for the Open Source Test workflow.
@@ -187,17 +203,6 @@ func OSWorkflow(
187203
orgSlugOrID = orgID
188204
}
189205

190-
if riskScoreThreshold != -1 && !riskScoreFFsEnabled {
191-
// The user tried to use a risk score threshold without the required feature flags.
192-
// Return a specific error for the first missing flag found.
193-
if !ffRiskScore {
194-
return nil, errFactory.NewFeatureNotPermittedError(FeatureFlagRiskScore)
195-
}
196-
return nil, errFactory.NewFeatureNotPermittedError(FeatureFlagRiskScoreInCLI)
197-
}
198-
199-
localPolicy := CreateLocalPolicy(config, logger)
200-
201206
// Create Snyk client
202207
httpClient := ictx.GetNetworkAccess().GetHttpClient()
203208
snykClient := snykclient.NewSnykClient(httpClient, ictx.GetConfiguration().GetString(configuration.API_URL), orgID)
@@ -215,8 +220,8 @@ func OSWorkflow(
215220
// Route to the appropriate flow based on flags
216221
switch {
217222
case sbomReachabilityTest:
218-
return setupSBOMReachabilityFlow(ctx, ictx, testClient, orgID, orgSlugOrID, errFactory, logger, sbom, sourceDir, localPolicy)
223+
return setupSBOMReachabilityFlow(ctx, ictx, testClient, orgID, orgSlugOrID, errFactory, logger, sbom, sourceDir)
219224
default:
220-
return RunUnifiedTestFlow(ctx, ictx, testClient, orgID, orgSlugOrID, errFactory, logger, localPolicy)
225+
return setupDefaultTestFlow(ctx, ictx, testClient, orgID, orgSlugOrID, errFactory, logger, riskScoreThreshold)
221226
}
222227
}

internal/commands/ostest/workflow_test.go

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package ostest_test
33
import (
44
"encoding/json"
55
"fmt"
6-
"math"
76
"net/http"
87
"net/http/httptest"
98
"strings"
@@ -31,59 +30,6 @@ import (
3130

3231
var legacyWorkflowID = workflow.NewWorkflowIdentifier("legacycli")
3332

34-
var logger = zerolog.Nop()
35-
36-
func TestOSWorkflow_CreateLocalPolicy(t *testing.T) {
37-
// Setup - No special flags set
38-
ctrl := gomock.NewController(t)
39-
defer ctrl.Finish()
40-
41-
mockEngine := mocks.NewMockEngine(ctrl)
42-
mockInvocationCtx := createMockInvocationCtxWithURL(t, ctrl, mockEngine, "")
43-
mockConfig := mockInvocationCtx.GetConfiguration()
44-
mockConfig.Set(flags.FlagRiskScoreThreshold, 100)
45-
mockConfig.Set(flags.FlagSeverityThreshold, "high")
46-
47-
localPolicy := ostest.CreateLocalPolicy(mockConfig, &logger)
48-
require.NotNil(t, localPolicy)
49-
50-
require.NotNil(t, localPolicy.RiskScoreThreshold)
51-
assert.Equal(t, uint16(100), *localPolicy.RiskScoreThreshold)
52-
require.NotNil(t, localPolicy.SeverityThreshold)
53-
assert.Equal(t, "high", string(*localPolicy.SeverityThreshold))
54-
}
55-
56-
func TestOSWorkflow_CreateLocalPolicy_NoValues(t *testing.T) {
57-
// Setup - No special flags set
58-
ctrl := gomock.NewController(t)
59-
defer ctrl.Finish()
60-
61-
mockEngine := mocks.NewMockEngine(ctrl)
62-
mockInvocationCtx := createMockInvocationCtxWithURL(t, ctrl, mockEngine, "")
63-
mockConfig := mockInvocationCtx.GetConfiguration()
64-
65-
localPolicy := ostest.CreateLocalPolicy(mockConfig, &logger)
66-
67-
assert.Nil(t, localPolicy)
68-
}
69-
70-
func TestOSWorkflow_CreateLocalPolicy_RiskScoreOverflow(t *testing.T) {
71-
// Setup - No special flags set
72-
ctrl := gomock.NewController(t)
73-
defer ctrl.Finish()
74-
75-
mockEngine := mocks.NewMockEngine(ctrl)
76-
mockInvocationCtx := createMockInvocationCtxWithURL(t, ctrl, mockEngine, "")
77-
mockConfig := mockInvocationCtx.GetConfiguration()
78-
mockConfig.Set(flags.FlagRiskScoreThreshold, math.MaxUint16+10)
79-
80-
localPolicy := ostest.CreateLocalPolicy(mockConfig, &logger)
81-
require.NotNil(t, localPolicy)
82-
83-
assert.NotNil(t, localPolicy.RiskScoreThreshold)
84-
assert.Equal(t, uint16(math.MaxUint16), *localPolicy.RiskScoreThreshold)
85-
}
86-
8733
func TestOSWorkflow_LegacyFlow(t *testing.T) {
8834
// Setup - No special flags set
8935
ctrl := gomock.NewController(t)

0 commit comments

Comments
 (0)