Skip to content
Open
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
2 changes: 1 addition & 1 deletion cliv2/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require (
github.com/snyk/cli-extension-iac v0.0.0-20250829110702-b41ac109dab0
github.com/snyk/cli-extension-iac-rules v0.0.0-20260115114457-a8ac3358ec57
github.com/snyk/cli-extension-mcp-scan v0.0.0-20260120142932-0eea0566625a
github.com/snyk/cli-extension-os-flows v0.0.0-20260115160519-84f621016a34
github.com/snyk/cli-extension-os-flows v0.0.0-20260126114133-44216d2c49f1
github.com/snyk/cli-extension-sbom v0.0.0-20260109124810-cfdd074f8eeb
github.com/snyk/container-cli v0.0.0-20250321132345-1e2e01681dd7
github.com/snyk/error-catalog-golang-public v0.0.0-20260108110943-21ad0c940c14
Expand Down
4 changes: 2 additions & 2 deletions cliv2/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -532,8 +532,8 @@ github.com/snyk/cli-extension-iac-rules v0.0.0-20260115114457-a8ac3358ec57 h1:8A
github.com/snyk/cli-extension-iac-rules v0.0.0-20260115114457-a8ac3358ec57/go.mod h1:AFto63ozNmCXtKb5oTTD3Qz1jEl/HCqCVwpZCfTpSIE=
github.com/snyk/cli-extension-mcp-scan v0.0.0-20260120142932-0eea0566625a h1:ElZXU6njO7McDQ7u7nAsJ5PCcwvA2+dEsadRM4P41JQ=
github.com/snyk/cli-extension-mcp-scan v0.0.0-20260120142932-0eea0566625a/go.mod h1:i/EDskKxg68MtWaIOWFyataAMtcXOb/DQPLxT5jtLRE=
github.com/snyk/cli-extension-os-flows v0.0.0-20260115160519-84f621016a34 h1:VtbScRQpbpmOE6MqHhMHUhQszSUO83oeVUb7U8td0uE=
github.com/snyk/cli-extension-os-flows v0.0.0-20260115160519-84f621016a34/go.mod h1:s3HX7yjdyP5PYe+ZTMyrJA5wv6hCnmfGdh7Nk5la6tY=
github.com/snyk/cli-extension-os-flows v0.0.0-20260126114133-44216d2c49f1 h1:xlw3z5a6KrpoLXgoGjzjc3LyxDjZCxylHuiEoQ8U9No=
github.com/snyk/cli-extension-os-flows v0.0.0-20260126114133-44216d2c49f1/go.mod h1:eYla0N+RzCa88JT6v4t/sAQthfJgyOHO1Oj9J72/Exw=
github.com/snyk/cli-extension-sbom v0.0.0-20260109124810-cfdd074f8eeb h1:5cAi3VwdoE4d6kc6D6qSge11e/ALBMmuBatySFd8rfE=
github.com/snyk/cli-extension-sbom v0.0.0-20260109124810-cfdd074f8eeb/go.mod h1:jIACVV10j4pW7LFrlYYtjn9mZm2JnXeFBM6/aTNJgvM=
github.com/snyk/code-client-go v1.25.0 h1:1lcg6asMpMWwpaZLKVDdci3OrAv6rRoCsCcT8Ci/fUI=
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package interceptor

import (
"bytes"
"encoding/json"
"io"
"net/http"
"regexp"

"github.com/elazarl/goproxy"
"github.com/snyk/go-application-framework/pkg/workflow"
)

const FeatureFlagShowMavenBuildScope = "internal_snyk_show_maven_scope_enabled"
const FeatureFlagShowNpmBuildScope = "internal_snyk_show_npm_scope_enabled"

type legacyFeatureFlagInterceptor struct {
requestCondition goproxy.ReqCondition
invocationCtx workflow.InvocationContext
}

type featureFlagResponse struct {
OK bool `json:"ok"`
}

func (ni legacyFeatureFlagInterceptor) GetCondition() goproxy.ReqCondition {
return ni.requestCondition
}

// GetHandler for legacyFeatureFlagInterceptor will re-route all registry requests from the proxy to the configured feature flag values.
// This ensures that we can control feature flag values for the legacy CLI from the CLIv2 configuration.
// Currently, only the "show-maven-build-scope" and "show-npm-scope" feature flags are supported.
func (ni legacyFeatureFlagInterceptor) GetHandler() goproxy.FuncReqHandler {
return func(req *http.Request, proxyCtx *goproxy.ProxyCtx) (*http.Request, *http.Response) {
ni.invocationCtx.GetEnhancedLogger().Printf("legacyFeatureFlagInterceptor handling request for %s", req.URL.Path)
var configKey string
switch req.URL.Path {
case "/v1/cli-config/feature-flags/show-maven-build-scope":
configKey = FeatureFlagShowMavenBuildScope
case "/v1/cli-config/feature-flags/show-npm-scope":
configKey = FeatureFlagShowNpmBuildScope
default:
return req, nil
}

enabled := ni.invocationCtx.
GetConfiguration().
GetBool(configKey)

payload := featureFlagResponse{OK: enabled}
b, err := json.Marshal(payload)
if err != nil {
return req, nil
}

resp := &http.Response{
StatusCode: http.StatusOK,
Status: "200 OK",
Header: make(http.Header),
Body: io.NopCloser(bytes.NewReader(b)),
Request: req,
}
resp.Header.Set("Content-Type", "application/json")
ni.invocationCtx.GetEnhancedLogger().Printf("legacyFeatureFlagInterceptor response for %s is %v", configKey, enabled)

return req, resp
}
}

func NewLegacyFeatureFlagInterceptor(invocationCtx workflow.InvocationContext) Interceptor {
i := legacyFeatureFlagInterceptor{
requestCondition: goproxy.UrlMatches(
regexp.MustCompile(`/cli-config/feature-flags/(show-maven-build-scope|show-npm-scope)/?(?:\?.*)?$`),
),
invocationCtx: invocationCtx,
}
return i
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package interceptor

import (
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"testing"

"github.com/golang/mock/gomock"
"github.com/rs/zerolog"
"github.com/snyk/go-application-framework/pkg/mocks"

"github.com/elazarl/goproxy"
"github.com/stretchr/testify/assert"
)

func TestLegacyFeatureFlagInterceptor_Routing(t *testing.T) {
tests := []struct {
name string
path string
shouldHandle bool
configKey string // only used when shouldHandle == true
}{
{
name: "maven path",
path: "https://example.com/v1/cli-config/feature-flags/show-maven-build-scope?org=abc",
shouldHandle: true,
configKey: FeatureFlagShowMavenBuildScope,
},
{
name: "npm path",
path: "https://example.com/v1/cli-config/feature-flags/show-npm-scope?org=abc",
shouldHandle: true,
configKey: FeatureFlagShowNpmBuildScope,
},
{
name: "other path",
path: "https://example.com/api/v1/other-endpoint",
shouldHandle: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

configMock := mocks.NewMockConfiguration(ctrl)

// Only expect config access when the interceptor should handle the path.
if tt.shouldHandle {
configMock.EXPECT().
GetBool(tt.configKey).
Return(true).
AnyTimes()
}

invocationCtxMock := mocks.NewMockInvocationContext(ctrl)
invocationCtxMock.EXPECT().
GetConfiguration().
Return(configMock).
AnyTimes()

interceptor := NewLegacyFeatureFlagInterceptor(invocationCtxMock)
handler := interceptor.GetHandler()

req := httptest.NewRequest(http.MethodGet, tt.path, nil)
proxyCtx := &goproxy.ProxyCtx{}

matched := interceptor.GetCondition().HandleReq(req, proxyCtx)
assert.Equal(t, tt.shouldHandle, matched)

if !tt.shouldHandle {
return
}

logger := zerolog.Nop()
invocationCtxMock.EXPECT().GetEnhancedLogger().Return(&logger).AnyTimes()

_, resp := handler(req, proxyCtx)
assert.NotNil(t, resp)
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, "application/json", resp.Header.Get("Content-Type"))

bodyBytes, err := io.ReadAll(resp.Body)
assert.NoError(t, err)

var parsed featureFlagResponse
assert.NoError(t, json.Unmarshal(bodyBytes, &parsed))
assert.True(t, parsed.OK)
})
}
}
1 change: 1 addition & 0 deletions cliv2/pkg/basic_workflows/legacycli.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ func createInternalProxy(config configuration.Configuration, debugLogger *zerolo
}

wrapperProxy.RegisterInterceptor(interceptor.NewV1AnalyticsInterceptor(invocation))
wrapperProxy.RegisterInterceptor(interceptor.NewLegacyFeatureFlagInterceptor(invocation))
// The networkinjector intercepts all requests from the legacy CLI and re-routes them to the existing networking
// layer. It should therefore be kept as the last interceptor in the chain, as it circuit breaks goproxy's own
// routing. Any interceptor added later will not be called.
Expand Down
Loading