Skip to content

Commit f23723b

Browse files
authored
fix: using getSastSettingsConfig for sastsettings (#321)
1 parent 80a6150 commit f23723b

File tree

8 files changed

+57
-23
lines changed

8 files changed

+57
-23
lines changed

internal/api/api.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111

1212
"github.com/snyk/go-application-framework/internal/api/contract"
1313
"github.com/snyk/go-application-framework/internal/constants"
14+
15+
"github.com/snyk/go-application-framework/pkg/local_workflows/code_workflow/sast_contract"
1416
)
1517

1618
type ApiClient interface {
@@ -21,7 +23,7 @@ type ApiClient interface {
2123
GetFeatureFlag(flagname string, origId string) (bool, error)
2224
GetUserMe() (string, error)
2325
GetSelf() (contract.SelfResponse, error)
24-
GetSastSettings(orgId string) (contract.SastResponse, error)
26+
GetSastSettings(orgId string) (sast_contract.SastResponse, error)
2527
}
2628

2729
var _ ApiClient = (*snykApiClient)(nil)
@@ -196,9 +198,9 @@ func (a *snykApiClient) GetSelf() (contract.SelfResponse, error) {
196198
return selfData, nil
197199
}
198200

199-
func (a *snykApiClient) GetSastSettings(orgId string) (contract.SastResponse, error) {
200-
var response contract.SastResponse
201-
var defaultResult contract.SastResponse
201+
func (a *snykApiClient) GetSastSettings(orgId string) (sast_contract.SastResponse, error) {
202+
var response sast_contract.SastResponse
203+
var defaultResult sast_contract.SastResponse
202204

203205
endpoint := a.url + "/v1/cli-config/settings/sast?org=" + url.QueryEscape(orgId)
204206
res, err := a.client.Get(endpoint)

internal/mocks/api.go

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/local_workflows/code_workflow.go

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ import (
88
"github.com/spf13/pflag"
99

1010
"github.com/snyk/go-application-framework/internal/api"
11-
"github.com/snyk/go-application-framework/internal/api/contract"
1211
"github.com/snyk/go-application-framework/internal/utils"
1312
"github.com/snyk/go-application-framework/pkg/configuration"
1413
"github.com/snyk/go-application-framework/pkg/local_workflows/code_workflow"
1514
"github.com/snyk/go-application-framework/pkg/local_workflows/config_utils"
1615
"github.com/snyk/go-application-framework/pkg/workflow"
16+
17+
"github.com/snyk/go-application-framework/pkg/local_workflows/code_workflow/sast_contract"
1718
)
1819

1920
const (
@@ -45,19 +46,9 @@ func GetCodeFlagSet() *pflag.FlagSet {
4546
// WORKFLOWID_CODE defines a new workflow identifier
4647
var WORKFLOWID_CODE workflow.Identifier = workflow.NewWorkflowIdentifier(codeWorkflowName)
4748

48-
func getSastSettings(engine workflow.Engine) (*contract.SastResponse, error) {
49+
func getSastSettings(engine workflow.Engine) (*sast_contract.SastResponse, error) {
4950
config := engine.GetConfiguration()
5051
org := config.GetString(configuration.ORGANIZATION)
51-
key := fmt.Sprintf("CACHE_SAST_RESPONSE_%s", org)
52-
53-
cachedContent := config.Get(key)
54-
if cachedContent != nil {
55-
cachedResponse, ok := cachedContent.(*contract.SastResponse)
56-
if ok {
57-
return cachedResponse, nil
58-
}
59-
}
60-
6152
client := engine.GetNetworkAccess().GetHttpClient()
6253
url := config.GetString(configuration.API_URL)
6354
apiClient := api.NewApi(url, client)
@@ -67,10 +58,27 @@ func getSastSettings(engine workflow.Engine) (*contract.SastResponse, error) {
6758
return &tmp, err
6859
}
6960

70-
engine.GetConfiguration().Set(key, &tmp)
61+
engine.GetConfiguration().Set(code_workflow.ConfigurationSastSettings, &tmp)
7162
return &tmp, nil
7263
}
7364

65+
func getSastSettingsConfig(engine workflow.Engine) configuration.DefaultValueFunction {
66+
callback := func(existingValue interface{}) (interface{}, error) {
67+
if existingValue != nil {
68+
return existingValue, nil
69+
}
70+
71+
response, err := getSastSettings(engine)
72+
if err != nil {
73+
engine.GetLogger().Err(err).Msg("Failed to access settings.")
74+
return false, err
75+
}
76+
77+
return response, nil
78+
}
79+
return callback
80+
}
81+
7482
func getSastEnabled(engine workflow.Engine) configuration.DefaultValueFunction {
7583
callback := func(existingValue interface{}) (interface{}, error) {
7684
if existingValue != nil {
@@ -132,6 +140,7 @@ func InitCodeWorkflow(engine workflow.Engine) error {
132140
return err
133141
}
134142

143+
engine.GetConfiguration().AddDefaultValue(code_workflow.ConfigurationSastSettings, getSastSettingsConfig(engine))
135144
engine.GetConfiguration().AddDefaultValue(code_workflow.ConfigurationSastEnabled, getSastEnabled(engine))
136145
engine.GetConfiguration().AddDefaultValue(code_workflow.ConfigurarionSlceEnabled, getSlceEnabled(engine))
137146
engine.GetConfiguration().AddDefaultValue(code_workflow.ConfigurationTestFLowName, configuration.StandardDefaultValueFunction("cli_test"))

pkg/local_workflows/code_workflow/native_workflow.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ const (
3939
ConfigurationProjectId = "project-id"
4040
ConfigurationCommitId = "commit-id"
4141
ConfigurationSastEnabled = "internal_sast_enabled"
42+
ConfigurationSastSettings = "internal_sast_settings"
4243
ConfigurarionSlceEnabled = "internal_snyk_scle_enabled"
4344
FfNameNativeImplementation = "snykCodeClientNativeImplementation"
4445
)

internal/api/contract/SastSettings.go renamed to pkg/local_workflows/code_workflow/sast_contract/sast_settings.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package contract
1+
package sast_contract
22

33
type LocalCodeEngine struct {
44
AllowCloudUpload bool `json:"allowCloudUpload"`

pkg/local_workflows/code_workflow_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/snyk/go-application-framework/internal/api/contract"
2222
"github.com/snyk/go-application-framework/pkg/configuration"
2323
"github.com/snyk/go-application-framework/pkg/local_workflows/code_workflow"
24+
"github.com/snyk/go-application-framework/pkg/local_workflows/code_workflow/sast_contract"
2425
"github.com/snyk/go-application-framework/pkg/local_workflows/content_type"
2526
"github.com/snyk/go-application-framework/pkg/local_workflows/json_schemas"
2627
"github.com/snyk/go-application-framework/pkg/mocks"
@@ -37,9 +38,9 @@ func Test_Code_entrypoint(t *testing.T) {
3738
fmt.Println(r.URL)
3839
if strings.HasSuffix(r.URL.String(), "/v1/cli-config/settings/sast?org="+org) {
3940
sastSettingsCalled++
40-
sastSettings := &contract.SastResponse{
41+
sastSettings := &sast_contract.SastResponse{
4142
SastEnabled: true,
42-
LocalCodeEngine: contract.LocalCodeEngine{
43+
LocalCodeEngine: sast_contract.LocalCodeEngine{
4344
Enabled: true, /* ensures that legacycli will be called */
4445
},
4546
}
@@ -98,7 +99,7 @@ func Test_Code_entrypoint(t *testing.T) {
9899
assert.NoError(t, err)
99100
assert.NotNil(t, rs)
100101
assert.Equal(t, expectedData, rs[0].GetPayload().(string))
101-
assert.Equal(t, 1, sastSettingsCalled)
102+
assert.Equal(t, 2, sastSettingsCalled)
102103
}
103104

104105
func Test_Code_legacyImplementation_happyPath(t *testing.T) {

pkg/mocks/networking.go

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/networking/networking.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ type NetworkAccess interface {
4141
AddHeaderField(key, value string)
4242
// AddDynamicHeaderField adds a dynamic header field to the request.
4343
AddDynamicHeaderField(key string, f DynamicHeaderFunc)
44+
// RemoveHeaderField removes a static header field value from requests.
45+
RemoveHeaderField(key string)
4446
// AddRootCAs adds the root CAs from the given PEM file.
4547
AddRootCAs(pemFileLocation string) error
4648
// AddErrorHandler registers an error handler for the underlying http.RoundTripper.
@@ -172,6 +174,12 @@ func (n *networkImpl) AddHeaderField(key, value string) {
172174
n.staticHeader.Add(key, value)
173175
}
174176

177+
// RemoveHeaderField removes a static header field value from requests.
178+
func (n *networkImpl) RemoveHeaderField(key string) {
179+
// Remove the specified header from the request
180+
n.staticHeader.Del(key)
181+
}
182+
175183
// AddErrorHandler registers an error handler for the underlying http.RoundTripper and registers the response middleware
176184
// that maps non 2xx status codes to Error Catalog errors.
177185
func (n *networkImpl) AddErrorHandler(handler networktypes.ErrorHandlerFunc) {

0 commit comments

Comments
 (0)