-
Notifications
You must be signed in to change notification settings - Fork 9
feat: use gaf to get sast settings #319
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,6 +44,28 @@ func defaultFuncOrganizationSlug(engine workflow.Engine, config configuration.Co | |
return callback | ||
} | ||
|
||
func defaultFuncGetSastSettings(engine workflow.Engine, config configuration.Configuration, logger *zerolog.Logger, apiClientFactory func(url string, client *http.Client) api.ApiClient) configuration.DefaultValueFunction { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Issue: Please don't add domain logic into There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So we want:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think so, what I wanted to say is, to keep product and domain specific logic close to each other. This means in this case that the default function for SAST specific configuration values should be registered here and implemented somewhere there as well. One benefit is, that these values only get available if an application registers the code workflow and are not wide spread through the code base. |
||
callback := func(existingValue interface{}) (interface{}, error) { | ||
if existingValue != nil { | ||
return existingValue, nil | ||
} | ||
client := engine.GetNetworkAccess().GetHttpClient() | ||
url := config.GetString(configuration.API_URL) | ||
apiClient := apiClientFactory(url, client) | ||
orgId := config.GetString(configuration.ORGANIZATION) | ||
if len(orgId) == 0 { | ||
return existingValue, nil | ||
} | ||
response, err := apiClient.GetSastSettings(orgId) | ||
Comment on lines
+52
to
+59
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can't we have caching here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought about this and it might not make sense to have caching for this in GAF and leave it up to the consumer to cache if necessary. |
||
if err != nil { | ||
logger.Err(err).Msg("Failed to access settings.") | ||
return false, err | ||
} | ||
return response, nil | ||
} | ||
return callback | ||
} | ||
|
||
func defaultFuncOrganization(engine workflow.Engine, config configuration.Configuration, logger *zerolog.Logger, apiClientFactory func(url string, client *http.Client) api.ApiClient) configuration.DefaultValueFunction { | ||
callback := func(existingValue interface{}) (interface{}, error) { | ||
client := engine.GetNetworkAccess().GetHttpClient() | ||
|
@@ -213,6 +235,7 @@ func initConfiguration(engine workflow.Engine, config configuration.Configuratio | |
|
||
config.AddDefaultValue(configuration.ORGANIZATION, defaultFuncOrganization(engine, config, logger, apiClientFactory)) | ||
config.AddDefaultValue(configuration.ORGANIZATION_SLUG, defaultFuncOrganizationSlug(engine, config, logger, apiClientFactory)) | ||
config.AddDefaultValue(configuration.SAST_SETTINGS, defaultFuncGetSastSettings(engine, config, logger, apiClientFactory)) | ||
|
||
config.AddDefaultValue(configuration.FF_OAUTH_AUTH_FLOW_ENABLED, func(existingValue any) (any, error) { | ||
if existingValue == nil { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package contract | ||
package common | ||
|
||
type LocalCodeEngine struct { | ||
AllowCloudUpload bool `json:"allowCloudUpload"` | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ package configuration | |
import ( | ||
"context" | ||
"encoding/json" | ||
|
||
"os" | ||
"path/filepath" | ||
"sync" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,8 +8,8 @@ import ( | |
"github.com/spf13/pflag" | ||
|
||
"github.com/snyk/go-application-framework/internal/api" | ||
"github.com/snyk/go-application-framework/internal/api/contract" | ||
"github.com/snyk/go-application-framework/internal/utils" | ||
"github.com/snyk/go-application-framework/pkg/common" | ||
"github.com/snyk/go-application-framework/pkg/configuration" | ||
"github.com/snyk/go-application-framework/pkg/local_workflows/code_workflow" | ||
"github.com/snyk/go-application-framework/pkg/local_workflows/config_utils" | ||
|
@@ -45,18 +45,9 @@ func GetCodeFlagSet() *pflag.FlagSet { | |
// WORKFLOWID_CODE defines a new workflow identifier | ||
var WORKFLOWID_CODE workflow.Identifier = workflow.NewWorkflowIdentifier(codeWorkflowName) | ||
|
||
func getSastSettings(engine workflow.Engine) (*contract.SastResponse, error) { | ||
func getSastSettings(engine workflow.Engine) (*common.SastResponse, error) { | ||
config := engine.GetConfiguration() | ||
Comment on lines
+48
to
49
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if this func is still needed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
org := config.GetString(configuration.ORGANIZATION) | ||
key := fmt.Sprintf("CACHE_SAST_RESPONSE_%s", org) | ||
|
||
cachedContent := config.Get(key) | ||
if cachedContent != nil { | ||
cachedResponse, ok := cachedContent.(*contract.SastResponse) | ||
if ok { | ||
return cachedResponse, nil | ||
} | ||
} | ||
|
||
client := engine.GetNetworkAccess().GetHttpClient() | ||
url := config.GetString(configuration.API_URL) | ||
|
@@ -67,7 +58,7 @@ func getSastSettings(engine workflow.Engine) (*contract.SastResponse, error) { | |
return &tmp, err | ||
} | ||
|
||
engine.GetConfiguration().Set(key, &tmp) | ||
engine.GetConfiguration().Set(org, &tmp) | ||
return &tmp, nil | ||
} | ||
|
||
|
@@ -151,6 +142,7 @@ func codeWorkflowEntryPoint(invocationCtx workflow.InvocationContext, _ []workfl | |
logger := invocationCtx.GetEnhancedLogger() | ||
|
||
sastEnabledI, err := config.GetWithError(code_workflow.ConfigurationSastEnabled) | ||
|
||
if err != nil { | ||
return result, err | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ package localworkflows | |
import ( | ||
"encoding/json" | ||
"fmt" | ||
"github.com/snyk/go-application-framework/pkg/common" | ||
"math/rand" | ||
"net/http" | ||
"net/http/httptest" | ||
|
@@ -37,9 +38,9 @@ func Test_Code_entrypoint(t *testing.T) { | |
fmt.Println(r.URL) | ||
if strings.HasSuffix(r.URL.String(), "/v1/cli-config/settings/sast?org="+org) { | ||
sastSettingsCalled++ | ||
sastSettings := &contract.SastResponse{ | ||
sastSettings := &common.SastResponse{ | ||
SastEnabled: true, | ||
LocalCodeEngine: contract.LocalCodeEngine{ | ||
LocalCodeEngine: common.LocalCodeEngine{ | ||
Enabled: true, /* ensures that legacycli will be called */ | ||
}, | ||
} | ||
|
@@ -98,7 +99,7 @@ func Test_Code_entrypoint(t *testing.T) { | |
assert.NoError(t, err) | ||
assert.NotNil(t, rs) | ||
assert.Equal(t, expectedData, rs[0].GetPayload().(string)) | ||
assert.Equal(t, 1, sastSettingsCalled) | ||
assert.Equal(t, 2, sastSettingsCalled) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because now its also called when then default func is setup here:
And it is also called in
Before adding the default func, we only did the second config call in the test case. |
||
} | ||
|
||
func Test_Code_legacyImplementation_happyPath(t *testing.T) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@PeterSchafer fyi we need SastResponse struct to be outside internal pkg to use it in ls.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Understand! Can we find a more descriptive name than
common
?