Skip to content

Commit e61339e

Browse files
CLI | Fix config retrieval logic (AST-76622) (#957)
* Add test for resubmit flag and fix config retrieval logic Introduce a new integration test to verify handling of the resubmit flag during scan creation for non-existent projects, ensuring successful creation with default config. Additionally, fix the logic to retrieve configuration only when scans are available, preventing potential nil pointer dereferences. * Refactor scan model response variable assignment Simplified the retrieval of config and engines by using a single variable, `scanModelResponse`, to improve code readability and maintainability. This change consolidates access to elements of `allScansModel.Scans[0]` into one clear reference point, making the code easier to understand and modify in the future. * Handle non-existent projects in ScansMockWrapper Get method. Updated the ScansMockWrapper's Get method to return an empty configuration if the project ID is "non-existent-project". Added a test to ensure that the method behaves correctly in this scenario, verifying that an empty configuration is returned when attempting to resubmit a scan for a non-existent project. * Refactor variable name in ScansMockWrapper Get function * fix trivy vul * fix trivy vul * fix trivy vul * fix trivy vul * fix trivy vul * dix healthcheck vul --------- Co-authored-by: AlvoBen <[email protected]>
1 parent 4bfa88e commit e61339e

File tree

7 files changed

+59
-22
lines changed

7 files changed

+59
-22
lines changed

Dockerfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
FROM cgr.dev/chainguard/bash@sha256:4eb8145143515a9be1d04b90e911431b3e48b74ddf62948c516d300c453c845f
1+
FROM cgr.dev/chainguard/bash@sha256:1abc09ac352efdc60d855bd159b9b66df6596a174400752ae3c537b5350779a9
22
USER nonroot
33

44
COPY cx /app/bin/cx
55

66
ENTRYPOINT ["/app/bin/cx"]
7+
8+
HEALTHCHECK NONE

go.mod

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ require (
2222
github.com/stretchr/testify v1.9.0
2323
github.com/tomnomnom/linkheader v0.0.0-20180905144013-02ca5825eb80
2424
github.com/xeipuuv/gojsonschema v1.2.0
25-
golang.org/x/crypto v0.26.0
26-
golang.org/x/sync v0.8.0
27-
golang.org/x/text v0.17.0
25+
golang.org/x/crypto v0.31.0
26+
golang.org/x/sync v0.10.0
27+
golang.org/x/text v0.21.0
2828
google.golang.org/grpc v1.65.0
2929
google.golang.org/protobuf v1.34.2
3030
gopkg.in/yaml.v3 v3.0.1
@@ -253,8 +253,8 @@ require (
253253
golang.org/x/mod v0.20.0 // indirect
254254
golang.org/x/net v0.28.0 // indirect
255255
golang.org/x/oauth2 v0.20.0 // indirect
256-
golang.org/x/sys v0.24.0 // indirect
257-
golang.org/x/term v0.23.0 // indirect
256+
golang.org/x/sys v0.28.0 // indirect
257+
golang.org/x/term v0.27.0 // indirect
258258
golang.org/x/time v0.5.0 // indirect
259259
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
260260
google.golang.org/genproto v0.0.0-20240227224415-6ceb2ff114de // indirect

go.sum

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,8 +1017,8 @@ golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0
10171017
golang.org/x/crypto v0.3.0/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4=
10181018
golang.org/x/crypto v0.3.1-0.20221117191849-2c476679df9a/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4=
10191019
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
1020-
golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw=
1021-
golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54=
1020+
golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U=
1021+
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
10221022
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
10231023
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
10241024
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
@@ -1141,8 +1141,8 @@ golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJ
11411141
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
11421142
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
11431143
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
1144-
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
1145-
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
1144+
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
1145+
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
11461146
golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
11471147
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
11481148
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
@@ -1221,16 +1221,16 @@ golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
12211221
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
12221222
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
12231223
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
1224-
golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg=
1225-
golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
1224+
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
1225+
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
12261226
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
12271227
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
12281228
golang.org/x/term v0.0.0-20220526004731-065cf7ba2467/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
12291229
golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc=
12301230
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
12311231
golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U=
1232-
golang.org/x/term v0.23.0 h1:F6D4vR+EHoL9/sWAWgAR1H2DcHr4PareCbAaCo1RpuU=
1233-
golang.org/x/term v0.23.0/go.mod h1:DgV24QBUrK6jhZXl+20l6UWznPlwAHm1Q1mGHtydmSk=
1232+
golang.org/x/term v0.27.0 h1:WP60Sv1nlK1T6SupCHbXzSaN0b9wUmsPoRS9b61A23Q=
1233+
golang.org/x/term v0.27.0/go.mod h1:iMsnZpn0cago0GOrHO2+Y7u7JPn5AylBrcoWkElMTSM=
12341234
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
12351235
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
12361236
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
@@ -1243,8 +1243,8 @@ golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
12431243
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
12441244
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
12451245
golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
1246-
golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc=
1247-
golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
1246+
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
1247+
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
12481248
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
12491249
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
12501250
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=

internal/commands/scan.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -834,6 +834,7 @@ func getResubmitConfiguration(scansWrapper wrappers.ScansWrapper, projectID, use
834834
var allScansModel *wrappers.ScansCollectionResponseModel
835835
var errorModel *wrappers.ErrorModel
836836
var err error
837+
var config []wrappers.Config
837838
params := make(map[string]string)
838839
params["project-id"] = projectID
839840
allScansModel, errorModel, err = scansWrapper.Get(params)
@@ -844,12 +845,17 @@ func getResubmitConfiguration(scansWrapper wrappers.ScansWrapper, projectID, use
844845
if errorModel != nil {
845846
return nil, errors.Errorf(services.ErrorCodeFormat, failedGettingAll, errorModel.Code, errorModel.Message)
846847
}
847-
config := allScansModel.Scans[0].Metadata.Configs
848-
engines := allScansModel.Scans[0].Engines
849-
// Check if there are no scan types sent using the flags, and use the latest scan engine types
850-
if userScanTypes == "" {
851-
actualScanTypes = strings.Join(engines, ",")
848+
849+
if len(allScansModel.Scans) > 0 {
850+
scanModelResponse := allScansModel.Scans[0]
851+
config = scanModelResponse.Metadata.Configs
852+
engines := scanModelResponse.Engines
853+
// Check if there are no scan types sent using the flags, and use the latest scan engine types
854+
if userScanTypes == "" {
855+
actualScanTypes = strings.Join(engines, ",")
856+
}
852857
}
858+
853859
return config, nil
854860
}
855861

internal/commands/scan_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1320,3 +1320,14 @@ func deleteOutputFile(file *os.File) {
13201320
logger.Printf("Failed to remove log file: %v", err)
13211321
}
13221322
}
1323+
1324+
func TestResubmitConfig_ProjectDoesNotExist_ReturnedEmptyConfig(t *testing.T) {
1325+
scanWrapper := mock.ScansMockWrapper{}
1326+
projectID := "non-existent-project"
1327+
userScanTypes := ""
1328+
cmd := createASTTestCommand()
1329+
cmd.PersistentFlags().String("project-name", "non-existent-project", "project name")
1330+
config, err := getResubmitConfiguration(&scanWrapper, projectID, userScanTypes)
1331+
assert.NilError(t, err)
1332+
assert.Equal(t, len(config), 0)
1333+
}

internal/wrappers/mock/scans-mock.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,15 @@ func (m *ScansMockWrapper) Create(scanModel *wrappers.Scan) (*wrappers.ScanRespo
5252
}, nil, nil
5353
}
5454

55-
func (m *ScansMockWrapper) Get(_ map[string]string) (
55+
func (m *ScansMockWrapper) Get(scanParams map[string]string) (
5656
*wrappers.ScansCollectionResponseModel,
5757
*wrappers.ErrorModel,
5858
error,
5959
) {
60+
if scanParams["project-id"] == "non-existent-project" {
61+
return &wrappers.ScansCollectionResponseModel{}, nil, nil
62+
}
63+
6064
fmt.Println("Called Get in ScansMockWrapper")
6165
sastMapConfig := make(map[string]interface{})
6266
sastMapConfig["incremental"] = "trueSastIncremental"

test/integration/scan_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1972,3 +1972,17 @@ func TestCreateAsyncScan_CallExportServiceBeforeScanFinishWithRetry_Success(t *t
19721972
asserts.Nil(t, err)
19731973
assert.Assert(t, exportRes != nil, "Export response should not be nil")
19741974
}
1975+
1976+
func TestCreateScanWithResubmitFlag_ProjectNotExist_ScanCreatedSuccessfullyWithDefaultConfig(t *testing.T) {
1977+
projectName := GenerateRandomProjectNameForScan()
1978+
args := []string{
1979+
scanCommand, "create",
1980+
flag(params.ProjectName), projectName,
1981+
flag(params.SourcesFlag), Zip,
1982+
flag(params.BranchFlag), "main",
1983+
flag(params.ScanInfoFormatFlag), printer.FormatJSON,
1984+
flag(params.ScanResubmit),
1985+
}
1986+
err, _ := executeCommand(t, args...)
1987+
assert.NilError(t, err)
1988+
}

0 commit comments

Comments
 (0)