Skip to content
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

fix(provider): wrong container image name #772

Merged
merged 7 commits into from
Oct 13, 2023
Merged
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
40 changes: 40 additions & 0 deletions api/models/containerimageinfo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright © 2023 Cisco Systems, Inc. and its affiliates.
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package models

// GetFirstRepoTag returns the first repo tag if it exists. Otherwise, returns false.
func (c *ContainerImageInfo) GetFirstRepoTag() (string, bool) {
var tag string
var ok bool

if c.RepoTags != nil && len(*c.RepoTags) > 0 {
tag, ok = (*c.RepoTags)[0], true
}

return tag, ok
}

// GetFirstRepoDigest returns the first repo digest if it exists. Otherwise, returns false.
func (c *ContainerImageInfo) GetFirstRepoDigest() (string, bool) {
var digest string
var ok bool

if c.RepoDigests != nil && len(*c.RepoDigests) > 0 {
digest, ok = (*c.RepoDigests)[0], true
}

return digest, ok
}
17 changes: 9 additions & 8 deletions api/models/models.gen.go

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

18 changes: 13 additions & 5 deletions api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2223,7 +2223,7 @@ components:
type: string
image:
$ref: '#/components/schemas/ContainerImageInfo'
id:
containerID:
type: string
createdAt:
type: string
Expand All @@ -2235,16 +2235,23 @@ components:
nullable: true
required:
- objectType
- containerID

ContainerImageInfo:
type: object
properties:
objectType:
type: string
id:
type: string
name:
imageID:
type: string
repoTags:
type: array
items:
type: string
repoDigests:
type: array
items:
type: string
labels:
type: array
items:
Expand All @@ -2257,7 +2264,8 @@ components:
size:
type: integer
required:
- objectType
- objectType
- imageID

DirInfo:
type: object
Expand Down
273 changes: 137 additions & 136 deletions api/server/server.gen.go

Large diffs are not rendered by default.

139 changes: 87 additions & 52 deletions e2e/basic_scan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,13 @@ import (

var _ = ginkgo.Describe("Running a basic scan (only SBOM)", func() {
reportFailedConfig := ReportFailedConfig{}
var imageID string

ginkgo.Context("which scans a docker container", func() {
ginkgo.It("should finish successfully", func(ctx ginkgo.SpecContext) {
var assets *models.Assets
var err error

ginkgo.By("waiting until test asset is found")
reportFailedConfig.objects = append(
reportFailedConfig.objects,
Expand All @@ -40,71 +44,38 @@ var _ = ginkgo.Describe("Running a basic scan (only SBOM)", func() {
Filter: utils.PointerTo(DefaultScope),
}
gomega.Eventually(func() bool {
assets, err := client.GetAssets(ctx, assetsParams)
assets, err = client.GetAssets(ctx, assetsParams)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
return len(*assets.Items) == 1
}, DefaultTimeout, time.Second).Should(gomega.BeTrue())

ginkgo.By("applying a scan configuration")
apiScanConfig, err := client.PostScanConfig(
ctx,
GetCustomScanConfig(
&models.ScanFamiliesConfig{
Sbom: &models.SBOMConfig{
Enabled: utils.PointerTo(true),
},
},
DefaultScope,
600,
))
containerInfo, err := (*assets.Items)[0].AssetInfo.AsContainerInfo()
gomega.Expect(err).NotTo(gomega.HaveOccurred())
imageID = containerInfo.Image.ImageID

RunSuccessfulScan(ctx, &reportFailedConfig, DefaultScope)
})
})

reportFailedConfig = ReportFailedConfig{}

ginkgo.Context("which scans a docker image", func() {
ginkgo.It("should finish successfully", func(ctx ginkgo.SpecContext) {
ginkgo.By("waiting until test asset is found")
filter := fmt.Sprintf("assetInfo/objectType eq 'ContainerImageInfo' and assetInfo/imageID eq '%s'", imageID)
reportFailedConfig.objects = append(
reportFailedConfig.objects,
APIObject{"scanConfig", fmt.Sprintf("id eq '%s'", *apiScanConfig.Id)},
APIObject{"asset", filter},
)

fishkerez marked this conversation as resolved.
Show resolved Hide resolved
ginkgo.By("updating scan configuration to run now")
updateScanConfig := UpdateScanConfigToStartNow(apiScanConfig)
err = client.PatchScanConfig(ctx, *apiScanConfig.Id, updateScanConfig)
gomega.Expect(err).NotTo(gomega.HaveOccurred())

ginkgo.By("waiting until scan starts")
scanParams := models.GetScansParams{
Filter: utils.PointerTo(fmt.Sprintf(
"scanConfig/id eq '%s' and state ne '%s' and state ne '%s'",
*apiScanConfig.Id,
models.ScanStateDone,
models.ScanStateFailed,
)),
}
var scans *models.Scans
gomega.Eventually(func() bool {
scans, err = client.GetScans(ctx, scanParams)
assets, err := client.GetAssets(ctx, models.GetAssetsParams{
Filter: utils.PointerTo(filter),
})
gomega.Expect(err).NotTo(gomega.HaveOccurred())
if len(*scans.Items) == 1 {
reportFailedConfig.objects = append(
reportFailedConfig.objects,
APIObject{"scan", fmt.Sprintf("id eq '%s'", *(*scans.Items)[0].Id)},
)
return true
}
return false
return len(*assets.Items) == 1
}, DefaultTimeout, time.Second).Should(gomega.BeTrue())

ginkgo.By("waiting until scan state changes to done")
scanParams = models.GetScansParams{
Filter: utils.PointerTo(fmt.Sprintf(
"scanConfig/id eq '%s' and state eq '%s'",
*apiScanConfig.Id,
models.ScanStateDone,
)),
}
gomega.Eventually(func() bool {
scans, err = client.GetScans(ctx, scanParams)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
return len(*scans.Items) == 1
}, time.Second*120, time.Second).Should(gomega.BeTrue())
RunSuccessfulScan(ctx, &reportFailedConfig, filter)
})
})

Expand All @@ -115,3 +86,67 @@ var _ = ginkgo.Describe("Running a basic scan (only SBOM)", func() {
}
})
})

// nolint:gomnd
func RunSuccessfulScan(ctx ginkgo.SpecContext, report *ReportFailedConfig, filter string) {
ginkgo.By("applying a scan configuration")
apiScanConfig, err := client.PostScanConfig(
ctx,
GetCustomScanConfig(
&models.ScanFamiliesConfig{
Sbom: &models.SBOMConfig{
Enabled: utils.PointerTo(true),
},
},
filter,
600,
))
gomega.Expect(err).NotTo(gomega.HaveOccurred())

report.objects = append(
report.objects,
APIObject{"scanConfig", fmt.Sprintf("id eq '%s'", *apiScanConfig.Id)},
)

ginkgo.By("updating scan configuration to run now")
updateScanConfig := UpdateScanConfigToStartNow(apiScanConfig)
err = client.PatchScanConfig(ctx, *apiScanConfig.Id, updateScanConfig)
gomega.Expect(err).NotTo(gomega.HaveOccurred())

ginkgo.By("waiting until scan starts")
scanParams := models.GetScansParams{
Filter: utils.PointerTo(fmt.Sprintf(
"scanConfig/id eq '%s' and state ne '%s' and state ne '%s'",
*apiScanConfig.Id,
models.ScanStateDone,
models.ScanStateFailed,
)),
}
var scans *models.Scans
gomega.Eventually(func() bool {
scans, err = client.GetScans(ctx, scanParams)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
if len(*scans.Items) == 1 {
report.objects = append(
report.objects,
APIObject{"scan", fmt.Sprintf("id eq '%s'", *(*scans.Items)[0].Id)},
)
return true
}
return false
}, DefaultTimeout, time.Second).Should(gomega.BeTrue())

ginkgo.By("waiting until scan state changes to done")
scanParams = models.GetScansParams{
Filter: utils.PointerTo(fmt.Sprintf(
"scanConfig/id eq '%s' and state eq '%s'",
*apiScanConfig.Id,
models.ScanStateDone,
)),
}
gomega.Eventually(func() bool {
scans, err = client.GetScans(ctx, scanParams)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
return len(*scans.Items) == 1
}, time.Second*120, time.Second).Should(gomega.BeTrue())
}
4 changes: 2 additions & 2 deletions pkg/apiserver/database/gorm/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,10 +305,10 @@ func (t *AssetsTableHandler) checkUniqueness(asset models.Asset) (*models.Asset,
*asset.Id, *info.DirName, *info.Location,
)
case models.ContainerInfo:
filter = fmt.Sprintf("id ne '%s' and assetInfo/id eq '%s'", *asset.Id, *info.Id)
filter = fmt.Sprintf("id ne '%s' and assetInfo/containerID eq '%s'", *asset.Id, info.ContainerID)

case models.ContainerImageInfo:
filter = fmt.Sprintf("id ne '%s' and assetInfo/id eq '%s'", *asset.Id, *info.Id)
filter = fmt.Sprintf("id ne '%s' and assetInfo/imageID eq '%s'", *asset.Id, info.ImageID)

default:
return nil, fmt.Errorf("asset type is not supported (%T): %w", discriminator, err)
Expand Down
13 changes: 10 additions & 3 deletions pkg/apiserver/database/gorm/odata.go
Original file line number Diff line number Diff line change
Expand Up @@ -571,15 +571,22 @@ var schemaMetas = map[string]odatasql.SchemaMeta{
"ContainerImageInfo": {
Fields: odatasql.Schema{
"architecture": odatasql.FieldMeta{FieldType: odatasql.StringFieldType},
"id": odatasql.FieldMeta{FieldType: odatasql.StringFieldType},
"imageID": odatasql.FieldMeta{FieldType: odatasql.StringFieldType},
"labels": odatasql.FieldMeta{
FieldType: odatasql.CollectionFieldType,
CollectionItemMeta: &odatasql.FieldMeta{
FieldType: odatasql.ComplexFieldType,
ComplexFieldSchemas: []string{"Tag"},
},
},
"name": odatasql.FieldMeta{FieldType: odatasql.StringFieldType},
"repoTags": odatasql.FieldMeta{
FieldType: odatasql.CollectionFieldType,
CollectionItemMeta: &odatasql.FieldMeta{FieldType: odatasql.StringFieldType},
},
"repoDigests": odatasql.FieldMeta{
FieldType: odatasql.CollectionFieldType,
CollectionItemMeta: &odatasql.FieldMeta{FieldType: odatasql.StringFieldType},
},
"objectType": odatasql.FieldMeta{FieldType: odatasql.StringFieldType},
"os": odatasql.FieldMeta{FieldType: odatasql.StringFieldType},
"size": odatasql.FieldMeta{FieldType: odatasql.NumberFieldType},
Expand All @@ -589,7 +596,7 @@ var schemaMetas = map[string]odatasql.SchemaMeta{
Fields: odatasql.Schema{
"containerName": odatasql.FieldMeta{FieldType: odatasql.StringFieldType},
"createdAt": odatasql.FieldMeta{FieldType: odatasql.DateTimeFieldType},
"id": odatasql.FieldMeta{FieldType: odatasql.StringFieldType},
"containerID": odatasql.FieldMeta{FieldType: odatasql.StringFieldType},
"image": odatasql.FieldMeta{FieldType: odatasql.StringFieldType},
"labels": odatasql.FieldMeta{
FieldType: odatasql.CollectionFieldType,
Expand Down
6 changes: 2 additions & 4 deletions pkg/orchestrator/provider/docker/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,19 +434,17 @@ func (c *Client) exportAsset(ctx context.Context, config *provider.ScanJobConfig

switch value := objectType.(type) {
case models.ContainerInfo:
containerID := *value.Id
contents, err := c.dockerClient.ContainerExport(ctx, containerID)
contents, err := c.dockerClient.ContainerExport(ctx, value.ContainerID)
if err != nil {
return nil, nil, fmt.Errorf("failed to export container: %w", err)
}
return contents, nil, nil

case models.ContainerImageInfo:
// Create an ephemeral container to export asset
image := *value.Name
containerResp, err := c.dockerClient.ContainerCreate(
ctx,
&container.Config{Image: image},
&container.Config{Image: value.ImageID},
nil,
nil,
nil,
Expand Down
Loading
Loading