From 755d0e47fa96ec40de8e506b93a72ff3adc55207 Mon Sep 17 00:00:00 2001 From: Felix Sargent Date: Thu, 25 Jun 2026 11:22:18 +0100 Subject: [PATCH] refactor: always use native Snyk Code workflow --- README.md | 5 +- .../commands/code_workflow/legacy_workflow.go | 21 -- pkg/code/code.go | 59 +---- pkg/code/code_test.go | 243 +----------------- 4 files changed, 20 insertions(+), 308 deletions(-) delete mode 100644 internal/commands/code_workflow/legacy_workflow.go diff --git a/README.md b/README.md index 944e4e9f..d7901c2 100644 --- a/README.md +++ b/README.md @@ -31,8 +31,7 @@ engine.Invoke(code.WORKFLOWID_CODE) The `codeWorkflowEntryPoint` function: - Checks if SAST is enabled for the organization -- Determines whether to use the native or legacy implementation based on feature flags -- Routes to either `EntryPointNative` (for Code Consistent Ignores support) or `EntryPointLegacy` +- Routes to the native `EntryPointNative` implementation - Returns workflow data containing scan results as local findings ### Library Functions @@ -149,4 +148,4 @@ All of our development will remain public for transparency. We thank the communi GitHub issues have been disabled on this repository as part of our move to a closed-contribution model. The Snyk support team does not actively monitor GitHub issues on any Snyk development project. -For help with the Snyk CLI or Snyk in general, please use the [Snyk support page](https://support.snyk.io/), which is the fastest way to get assistance. \ No newline at end of file +For help with the Snyk CLI or Snyk in general, please use the [Snyk support page](https://support.snyk.io/), which is the fastest way to get assistance. diff --git a/internal/commands/code_workflow/legacy_workflow.go b/internal/commands/code_workflow/legacy_workflow.go deleted file mode 100644 index 8dc68f9..0000000 --- a/internal/commands/code_workflow/legacy_workflow.go +++ /dev/null @@ -1,21 +0,0 @@ -package code_workflow - -import ( - "os" - - "github.com/snyk/go-application-framework/pkg/configuration" - "github.com/snyk/go-application-framework/pkg/workflow" -) - -func EntryPointLegacy(invocationCtx workflow.InvocationContext) (result []workflow.Data, err error) { - // get necessary objects from invocation context - config := invocationCtx.GetConfiguration() - engine := invocationCtx.GetEngine() - - config.Set(configuration.RAW_CMD_ARGS, os.Args[1:]) - config.Set(configuration.WORKFLOW_USE_STDIO, true) - - // run legacycli - result, err = engine.InvokeWithConfig(workflow.NewWorkflowIdentifier("legacycli"), config) - return result, err -} diff --git a/pkg/code/code.go b/pkg/code/code.go index 083d668..dcb4b28 100644 --- a/pkg/code/code.go +++ b/pkg/code/code.go @@ -9,16 +9,13 @@ import ( "slices" "github.com/rs/zerolog" - "github.com/snyk/error-catalog-golang-public/code" - "github.com/spf13/pflag" - "github.com/snyk/code-client-go/internal/commands/code_workflow" + "github.com/snyk/code-client-go/pkg/code/sast_contract" + "github.com/snyk/error-catalog-golang-public/code" "github.com/snyk/go-application-framework/pkg/configuration" - "github.com/snyk/go-application-framework/pkg/local_workflows/config_utils" errorutils "github.com/snyk/go-application-framework/pkg/local_workflows/error_utils" "github.com/snyk/go-application-framework/pkg/workflow" - - "github.com/snyk/code-client-go/pkg/code/sast_contract" + "github.com/spf13/pflag" ) const ( @@ -26,10 +23,9 @@ const ( ) const ( - ConfigurationSastEnabled = "internal_sast_enabled" - ConfigurationSastSettings = code_workflow.ConfigurationSastSettings - ConfigurationSlceEnabled = code_workflow.ConfigurationSlceEnabled - FfNameNativeImplementation = "snykCodeClientNativeImplementation" + ConfigurationSastEnabled = "internal_sast_enabled" + ConfigurationSastSettings = code_workflow.ConfigurationSastSettings + ConfigurationSlceEnabled = code_workflow.ConfigurationSlceEnabled ) // ConfigurarionSlceEnabled is kept for backward compatibility. @@ -166,26 +162,6 @@ func getSlceEnabled(engine workflow.Engine) configuration.DefaultValueFunction { return callback } -func useNativeImplementation(config configuration.Configuration, logger *zerolog.Logger, sastEnabled bool) bool { - useConsistentIgnoresFF := config.GetBool(configuration.FF_CODE_CONSISTENT_IGNORES) - useNativeImplementationFF := config.GetBool(configuration.FF_CODE_NATIVE_IMPLEMENTATION) - reportEnabled := config.GetBool(code_workflow.ConfigurationReportFlag) - scleEnabled := config.GetBool(ConfigurationSlceEnabled) - - // SCLE no longer forces the legacy path: code-client-go honors the local - // engine URL from SAST settings (see codeClientConfig.SnykCodeApi), so the - // native implementation supports SCLE flows directly. - nativeImplementationEnabled := useConsistentIgnoresFF || useNativeImplementationFF - - logger.Debug().Msgf("SAST Enabled: %v", sastEnabled) - logger.Debug().Msgf("Report enabled: %v", reportEnabled) - logger.Debug().Msgf("SLCE enabled: %v", scleEnabled) - logger.Debug().Msgf("FF consistent ignores: %v", useConsistentIgnoresFF) - logger.Debug().Msgf("FF native implementation: %v", useNativeImplementationFF) - - return nativeImplementationEnabled -} - // Init initializes the code workflow before registering it with the engine. func Init(engine workflow.Engine) error { // register workflow with engine @@ -200,14 +176,12 @@ func Init(engine workflow.Engine) error { engine.GetConfiguration().AddDefaultValue(ConfigurationSastEnabled, getSastEnabled(engine)) engine.GetConfiguration().AddDefaultValue(ConfigurationSlceEnabled, getSlceEnabled(engine)) engine.GetConfiguration().AddDefaultValue(code_workflow.ConfigurationTestFLowName, configuration.StandardDefaultValueFunction("cli_test")) - config_utils.AddFeatureFlagToConfig(engine, configuration.FF_CODE_CONSISTENT_IGNORES, "snykCodeConsistentIgnores") - config_utils.AddFeatureFlagToConfig(engine, configuration.FF_CODE_NATIVE_IMPLEMENTATION, FfNameNativeImplementation) return err } // codeWorkflowEntryPoint is the entry point for the code workflow. -// it provides a wrapper for the legacycli workflow +// It always runs the native code-client-go workflow. func codeWorkflowEntryPoint(invocationCtx workflow.InvocationContext, _ []workflow.Data) (result []workflow.Data, err error) { // get necessary objects from invocation context config := invocationCtx.GetConfiguration() @@ -218,26 +192,15 @@ func codeWorkflowEntryPoint(invocationCtx workflow.InvocationContext, _ []workfl return result, err } - nativeImplementation := useNativeImplementation(config, logger, sastEnabled) - if !sastEnabled { return result, code.NewFeatureIsNotEnabledError(fmt.Sprintf("Snyk Code is not supported for your current organization: `%s`.", config.GetString(configuration.ORGANIZATION_SLUG))) } - implementationName := "legacy" - if nativeImplementation { - implementationName = "native" - } - - invocationCtx.GetAnalytics().AddExtensionStringValue("implementation", implementationName) - logger.Debug().Msgf("Implementation: %s", implementationName) + invocationCtx.GetAnalytics().AddExtensionStringValue("implementation", "native") + logger.Debug().Msg("Implementation: native") - if nativeImplementation { - registerLocalEngineAuthURL(config, logger) - result, err = code_workflow.EntryPointNative(invocationCtx) - } else { - result, err = code_workflow.EntryPointLegacy(invocationCtx) - } + registerLocalEngineAuthURL(config, logger) + result, err = code_workflow.EntryPointNative(invocationCtx) err = errorutils.DecorateTestError(err, config) diff --git a/pkg/code/code_test.go b/pkg/code/code_test.go index 6482f83..587654c 100644 --- a/pkg/code/code_test.go +++ b/pkg/code/code_test.go @@ -9,7 +9,6 @@ import ( "math/rand" "net/http" "net/http/httptest" - "os" "strings" "testing" @@ -18,7 +17,6 @@ import ( "github.com/snyk/code-client-go/sarif" "github.com/snyk/code-client-go/scan" "github.com/snyk/error-catalog-golang-public/code" - "github.com/spf13/pflag" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -34,129 +32,6 @@ import ( "github.com/snyk/go-application-framework/pkg/workflow" ) -type OrgFeatureFlagResponse struct { - Ok bool `json:"ok,omitempty"` - UserMessage string `json:"userMessage,omitempty"` - Code int `json:"code,omitempty,string"` - Error string `json:"error,omitempty"` -} - -func Test_Code_entrypoint(t *testing.T) { - org := "1234" - sastSettingsCalled := 0 - - server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - fmt.Println(r.URL) - if strings.HasSuffix(r.URL.String(), "/v1/cli-config/settings/sast?org="+org) { - sastSettingsCalled++ - sastSettings := &sast_contract.SastResponse{ - SastEnabled: true, - LocalCodeEngine: sast_contract.LocalCodeEngine{ - Enabled: true, - }, - } - - err := json.NewEncoder(w).Encode(sastSettings) - assert.NoError(t, err) - } else if strings.Contains(r.URL.String(), "/v1/cli-config/feature-flags/") { - // Disable the native-implementation feature flags so the workflow - // dispatches to legacycli (the path exercised by this test). - featureFlag := OrgFeatureFlagResponse{ - Ok: false, - } - - err := json.NewEncoder(w).Encode(featureFlag) - assert.NoError(t, err) - } - - w.WriteHeader(http.StatusOK) - })) - defer server.Close() - - expectedData := "Hello World" - flagString := "--user-=bla" - callback1 := func(invocation workflow.InvocationContext, input []workflow.Data) ([]workflow.Data, error) { - typeId := workflow.NewTypeIdentifier(invocation.GetWorkflowIdentifier(), "wfl1data") - d := workflow.NewData(typeId, "text/plain", expectedData) - assert.Equal(t, []string{flagString}, invocation.GetConfiguration().Get(configuration.RAW_CMD_ARGS)) - return []workflow.Data{d}, nil - } - - // set - config := configuration.NewWithOpts() - config.Set(configuration.API_URL, server.URL) - config.Set(configuration.ORGANIZATION, org) - - engine := workflow.NewWorkFlowEngine(config) - - err := Init(engine) - assert.NoError(t, err) - - // Create legacycli workflow - mockLegacyCliWorkflowId := workflow.NewWorkflowIdentifier("legacycli") - entry1, err := engine.Register(mockLegacyCliWorkflowId, workflow.ConfigurationOptionsFromFlagset(pflag.NewFlagSet("1", pflag.ExitOnError)), callback1) - assert.Nil(t, err) - assert.NotNil(t, entry1) - - err = engine.Init() - assert.NoError(t, err) - - // Method under test - wrkflw, ok := engine.GetWorkflow(WORKFLOWID_CODE) - assert.True(t, ok) - assert.NotNil(t, wrkflw) - - os.Args = []string{"cmd", flagString} - - rs, err := engine.InvokeWithConfig(WORKFLOWID_CODE, config) - assert.NoError(t, err) - assert.NotNil(t, rs) - assert.Equal(t, expectedData, rs[0].GetPayload().(string)) - assert.Equal(t, 2, sastSettingsCalled) -} - -func Test_Code_legacyImplementation_happyPath(t *testing.T) { - expectedData := "Hello World" - flagString := "--user-=bla" - callback1 := func(invocation workflow.InvocationContext, input []workflow.Data) ([]workflow.Data, error) { - typeId := workflow.NewTypeIdentifier(invocation.GetWorkflowIdentifier(), "wfl1data") - d := workflow.NewData(typeId, "text/plain", expectedData) - assert.Equal(t, []string{flagString}, invocation.GetConfiguration().Get(configuration.RAW_CMD_ARGS)) - return []workflow.Data{d}, nil - } - - // set - config := configuration.New() - engine := workflow.NewWorkFlowEngine(config) - - config.Set(configuration.FF_CODE_CONSISTENT_IGNORES, false) - config.Set(ConfigurationSastEnabled, true) - - err := Init(engine) - assert.NoError(t, err) - - // Create legacycli workflow - mockLegacyCliWorkflowId := workflow.NewWorkflowIdentifier("legacycli") - entry1, err := engine.Register(mockLegacyCliWorkflowId, workflow.ConfigurationOptionsFromFlagset(pflag.NewFlagSet("1", pflag.ExitOnError)), callback1) - assert.Nil(t, err) - assert.NotNil(t, entry1) - - err = engine.Init() - assert.NoError(t, err) - - // Method under test - wrkflw, ok := engine.GetWorkflow(WORKFLOWID_CODE) - assert.True(t, ok) - assert.NotNil(t, wrkflw) - - os.Args = []string{"cmd", flagString} - - rs, err := engine.InvokeWithConfig(WORKFLOWID_CODE, config) - assert.NoError(t, err) - assert.NotNil(t, rs) - assert.Equal(t, expectedData, rs[0].GetPayload().(string)) -} - func Test_Code_nativeImplementation_happyPath(t *testing.T) { numberOfArtifacts := rand.Int() expectedSummary := json_schemas.TestSummary{ @@ -420,120 +295,16 @@ func Test_Code_nativeImplementation_analysisEmpty(t *testing.T) { }) } -func Test_Code_FF_CODE_CONSISTENT_IGNORES(t *testing.T) { - response := OrgFeatureFlagResponse{} - responseNativeImpl := OrgFeatureFlagResponse{} - ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - data, err := json.Marshal(response) - - if strings.Contains(r.URL.Path, FfNameNativeImplementation) { - data, err = json.Marshal(responseNativeImpl) - } - - assert.NoError(t, err) - fmt.Fprintln(w, string(data)) - })) - defer ts.Close() - - orgId := "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee" - config := configuration.NewWithOpts(configuration.WithAutomaticEnv()) - config.Set(configuration.API_URL, ts.URL) - +func Test_Code_InitDoesNotRegisterNativeFeatureFlagGate(t *testing.T) { + config := configuration.NewWithOpts() engine := workflow.NewWorkFlowEngine(config) - err := Init(engine) - assert.NoError(t, err) - - t.Run("Feature Flag set", func(t *testing.T) { - config.Set(configuration.ORGANIZATION, orgId) - response = OrgFeatureFlagResponse{Code: http.StatusOK, Ok: true} - consistentIgnores := config.GetBool(configuration.FF_CODE_CONSISTENT_IGNORES) - assert.True(t, consistentIgnores) - }) - - t.Run("Feature Flag NOT set", func(t *testing.T) { - config.Set(configuration.ORGANIZATION, orgId) - response = OrgFeatureFlagResponse{Code: http.StatusForbidden} - consistentIgnores := config.GetBool(configuration.FF_CODE_CONSISTENT_IGNORES) - assert.False(t, consistentIgnores) - }) - - t.Run("Feature Flag not available due to error", func(t *testing.T) { - config.Unset(configuration.ORGANIZATION) - consistentIgnores := config.GetBool(configuration.FF_CODE_CONSISTENT_IGNORES) - assert.False(t, consistentIgnores) - }) - t.Run("Local Native Implementation Feature Flag set", func(t *testing.T) { - config.Set(configuration.ORGANIZATION, orgId) - responseNativeImpl = OrgFeatureFlagResponse{Code: http.StatusOK, Ok: true} - consistentIgnores := config.GetBool(configuration.FF_CODE_NATIVE_IMPLEMENTATION) - assert.True(t, consistentIgnores) - }) -} - -func Test_Code_UseNativeImplementation(t *testing.T) { - logger := zerolog.Nop() - - // cciFeatureFlagEnabled bool, nativeImplementationFeatureFlag bool, ignoresFeatureFlag bool - t.Run("cci feature flag disabled, native implementation disabled", func(t *testing.T) { - expected := false - config := configuration.NewWithOpts() - config.Set(configuration.FF_CODE_CONSISTENT_IGNORES, false) - config.Set(configuration.FF_CODE_NATIVE_IMPLEMENTATION, false) - config.Set(ConfigurationSlceEnabled, false) - actual := useNativeImplementation(config, &logger, true) - assert.Equal(t, expected, actual) - }) - - t.Run("cci feature flag disabled, native implementation enabled", func(t *testing.T) { - expected := true - config := configuration.NewWithOpts() - config.Set(configuration.FF_CODE_CONSISTENT_IGNORES, false) - config.Set(configuration.FF_CODE_NATIVE_IMPLEMENTATION, true) - config.Set(ConfigurationSlceEnabled, false) - actual := useNativeImplementation(config, &logger, true) - assert.Equal(t, expected, actual) - }) - - t.Run("cci feature flag enabled, native implementation disabled", func(t *testing.T) { - expected := true - config := configuration.NewWithOpts() - config.Set(configuration.FF_CODE_CONSISTENT_IGNORES, true) - config.Set(configuration.FF_CODE_NATIVE_IMPLEMENTATION, false) - config.Set(ConfigurationSlceEnabled, false) - actual := useNativeImplementation(config, &logger, true) - assert.Equal(t, expected, actual) - }) - - t.Run("cci feature flag enabled, native implementation enabled", func(t *testing.T) { - expected := true - config := configuration.NewWithOpts() - config.Set(configuration.FF_CODE_CONSISTENT_IGNORES, true) - config.Set(configuration.FF_CODE_NATIVE_IMPLEMENTATION, true) - config.Set(ConfigurationSlceEnabled, false) - actual := useNativeImplementation(config, &logger, true) - assert.Equal(t, expected, actual) - }) - - t.Run("scle enabled no longer forces the legacy implementation", func(t *testing.T) { - expected := true - config := configuration.NewWithOpts() - config.Set(configuration.FF_CODE_CONSISTENT_IGNORES, true) - config.Set(configuration.FF_CODE_NATIVE_IMPLEMENTATION, true) - config.Set(ConfigurationSlceEnabled, true) - actual := useNativeImplementation(config, &logger, true) - assert.Equal(t, expected, actual) - }) + err := Init(engine) + require.NoError(t, err) - t.Run("scle enabled with no native feature flags stays on legacy", func(t *testing.T) { - expected := false - config := configuration.NewWithOpts() - config.Set(configuration.FF_CODE_CONSISTENT_IGNORES, false) - config.Set(configuration.FF_CODE_NATIVE_IMPLEMENTATION, false) - config.Set(ConfigurationSlceEnabled, true) - actual := useNativeImplementation(config, &logger, true) - assert.Equal(t, expected, actual) - }) + keys := config.AllKeys() + assert.NotContains(t, keys, configuration.FF_CODE_CONSISTENT_IGNORES) + assert.NotContains(t, keys, configuration.FF_CODE_NATIVE_IMPLEMENTATION) } func Test_registerLocalEngineAuthURL(t *testing.T) {