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
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
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"

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" feature flag is supported.
func (ni legacyFeatureFlagInterceptor) GetHandler() goproxy.FuncReqHandler {
return func(req *http.Request, proxyCtx *goproxy.ProxyCtx) (*http.Request, *http.Response) {
showMavenBuildScope := ni.invocationCtx.
GetConfiguration().
GetBool(FeatureFlagShowMavenBuildScope)

payload := featureFlagResponse{
OK: showMavenBuildScope,
}
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")

return req, resp
}
}

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

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

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

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

func TestLegacyFeatureFlagInterceptor_HandlesFeatureFlagPath(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

configMock := mocks.NewMockConfiguration(ctrl)
configMock.EXPECT().
GetBool(FeatureFlagShowMavenBuildScope).
Return(true).
AnyTimes()

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

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

req := httptest.NewRequest(http.MethodGet, "https://example.com/cli-config/feature-flags/show-maven-build-scope?org=abc", nil)
proxyCtx := &goproxy.ProxyCtx{}

assert.True(t, legacyFeatureFlagInterceptor.GetCondition().HandleReq(req, proxyCtx), "condition should match feature flag path")

_, 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.Equal(t, true, parsed.OK)
}

func TestLegacyFeatureFlagInterceptor_DoesNotHandleOtherPaths(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

configMock := mocks.NewMockConfiguration(ctrl)
configMock.EXPECT().
GetBool(FeatureFlagShowMavenBuildScope).
Return(true).
AnyTimes()

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

legacyFeatureFlagInterceptor := NewLegacyFeatureFlagInterceptor(invocationCtxMock)

req := httptest.NewRequest(http.MethodGet, "https://example.com/api/v1/other-endpoint", nil)
proxyCtx := &goproxy.ProxyCtx{}

assert.False(t, legacyFeatureFlagInterceptor.GetCondition().HandleReq(req, proxyCtx), "condition should not match non-feature-flag path")
}
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
36 changes: 31 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@
"snyk-go-plugin": "^2.0.0",
"snyk-gradle-plugin": "5.1.1",
"snyk-module": "3.1.0",
"snyk-mvn-plugin": "4.3.3",
"snyk-mvn-plugin": "4.5.0",
"snyk-nodejs-lockfile-parser": "2.4.5",
"snyk-nodejs-plugin": "1.4.5",
"snyk-nuget-plugin": "2.12.0",
Expand Down
9 changes: 7 additions & 2 deletions src/lib/plugins/get-deps-from-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
AUTO_DETECTABLE_FILES,
detectPackageManagerFromFile,
} from '../detect';
import analytics = require('../analytics');
import * as analytics from '../analytics';
import { convertSingleResultToMultiCustom } from './convert-single-splugin-res-to-multi-custom';
import { convertMultiResultToMultiCustom } from './convert-multi-plugin-res-to-multi-custom';
import { processYarnWorkspaces } from './nodejs-plugin/yarn-workspaces-parser';
Expand Down Expand Up @@ -102,7 +102,12 @@ export async function getDepsFromPlugin(
if (!options.docker && !(options.file || options.packageManager)) {
throw NoSupportedManifestsFoundError([...root]);
}
const inspectRes = await getSinglePluginResult(root, options);
const inspectRes = await getSinglePluginResult(
root,
options,
'',
featureFlags,
);

if (!pluginApi.isMultiResult(inspectRes)) {
if (!inspectRes.package && !inspectRes.dependencyGraph) {
Expand Down
Loading