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

test(bundle): Verify bundle usage #173

Merged
merged 2 commits into from
Jul 9, 2024
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
6 changes: 6 additions & 0 deletions scripts/testdata/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
resource "aws_vpc" "example" {
cidr_block = "10.1.0.0/16"
tags = {
Name = "my-vpc-resource"
}
}
51 changes: 37 additions & 14 deletions scripts/verify-bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"path/filepath"
"strings"

"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/mount"
Expand All @@ -14,6 +15,7 @@ import (

var bundlePath = "bundle.tar.gz"
var OrasPush = []string{"--config", "/dev/null:application/vnd.cncf.openpolicyagent.config.v1+json", fmt.Sprintf("%s:application/vnd.cncf.openpolicyagent.layer.v1.tar+gzip", bundlePath)}
var supportedTrivyVersions = []string{"latest", "canary"} // TODO: add more versions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More versions will be added later?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't able to find a way without using Docker's API (requires a token) to be able to fetch tags. We could scrape them but I thought we can start with these first and then improve upon.

Do you know of any way to get the tags without auth or scrape?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can retrieve the tags from GitHub.


func createRegistryContainer(ctx context.Context) (testcontainers.Container, string) {
reqReg := testcontainers.ContainerRequest{
Expand Down Expand Up @@ -62,14 +64,26 @@ func createOrasContainer(ctx context.Context, regIP string, bundlePath string) t
return orasC
}

func createTrivyContainer(ctx context.Context, regIP string) testcontainers.Container {
func createTrivyContainer(ctx context.Context, trivyVersion string, regIP string) testcontainers.Container {
testDataPath, err := filepath.Abs("scripts/testdata")
if err != nil {
panic(err)
}

reqTrivy := testcontainers.ContainerRequest{
Image: "aquasec/trivy:latest",
Cmd: []string{"--debug", "config", fmt.Sprintf("--policy-bundle-repository=%s:5111/defsec-test:latest", regIP), "."},
Image: fmt.Sprintf("aquasec/trivy:%s", trivyVersion),
Cmd: []string{"--debug", "config", fmt.Sprintf("--policy-bundle-repository=%s:5111/defsec-test:latest", regIP), "/testdata"},
HostConfigModifier: func(config *container.HostConfig) {
config.NetworkMode = "host"
config.Mounts = []mount.Mount{
{
Type: mount.TypeBind,
Source: testDataPath,
Target: "/testdata",
},
}
},
WaitingFor: wait.ForLog("Policies successfully loaded from disk"),
WaitingFor: wait.ForLog("OS is not detected."),
}
trivyC, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: reqTrivy,
Expand All @@ -92,7 +106,7 @@ func debugLogsForContainer(ctx context.Context, c testcontainers.Container) stri
return string(b)
}

func LoadBundle() {
func LoadAndVerifyBundle() {
ctx := context.Background()

bundlePath, err := filepath.Abs("bundle.tar.gz")
Expand All @@ -114,20 +128,29 @@ func LoadBundle() {
}
}()

trivyC := createTrivyContainer(ctx, regIP)
defer func() {
fmt.Println(debugLogsForContainer(ctx, regC))
fmt.Println(debugLogsForContainer(ctx, orasC))

for _, trivyVersion := range supportedTrivyVersions {
fmt.Println("=======Testing version: ", trivyVersion, "==========")
trivyC := createTrivyContainer(ctx, trivyVersion, regIP)
fmt.Println(debugLogsForContainer(ctx, trivyC))

if !assertInLogs(debugLogsForContainer(ctx, trivyC), `Tests: 1 (SUCCESSES: 0, FAILURES: 1, EXCEPTIONS: 0)`) {
panic("asserting Trivy logs for misconfigurations failed, check Trivy log output")
}

if err = trivyC.Terminate(ctx); err != nil {
panic(err)
}
}()
}

// for debugging
fmt.Println(debugLogsForContainer(ctx, regC))
fmt.Println(debugLogsForContainer(ctx, orasC))
fmt.Println(debugLogsForContainer(ctx, trivyC))
}

// TODO: Verify by using bundle to scan
func assertInLogs(containerLogs, assertion string) bool {
return strings.Contains(containerLogs, assertion)
}

func main() {
LoadBundle()
LoadAndVerifyBundle()
}
Loading