Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
11 changes: 10 additions & 1 deletion .github/workflows/static-unit-integration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ jobs:
- name: Validate grype output against the CycloneDX schema
run: make validate-cyclonedx-schema

- name: Build key for tar cache
- name: Build key for integration tar cache
run: make integration-fingerprint

- name: Restore integration test cache
Expand All @@ -83,6 +83,15 @@ jobs:
path: ${{ github.workspace }}/test/integration/test-fixtures/cache
key: ${{ runner.os }}-integration-test-cache-${{ hashFiles('test/integration/test-fixtures/cache.fingerprint') }}

- name: Build key for CLI tar cache
run: make cli-fingerprint

- name: Restore cli test cache
uses: actions/cache@v2
with:
path: ${{ github.workspace }}/test/cli/test-fixtures/cache
key: ${{ runner.os }}-cli-test-cache-${{ hashFiles('test/cli/test-fixtures/cache.fingerprint') }}

- name: Run integration tests
run: make integration

Expand Down
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@ integration: ## Run integration tests
integration-fingerprint:
find test/integration/*.go test/integration/test-fixtures/image-* -type f -exec md5sum {} + | awk '{print $1}' | sort | md5sum | tee test/integration/test-fixtures/cache.fingerprint

# note: this is used by CI to determine if the cli test fixture cache (docker image tars) should be busted
.PHONY: cli-fingerprint
cli-fingerprint:
find test/cli/*.go test/cli/test-fixtures/image-* -type f -exec md5sum {} + | awk '{print $1}' | sort | md5sum | tee test/cli/test-fixtures/cache.fingerprint

.PHONY: cli
cli: $(SNAPSHOTDIR) ## Run CLI tests
chmod 755 "$(SNAPSHOT_CMD)"
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
github.com/docker/docker v17.12.0-ce-rc1.0.20200309214505-aa6a9891b09c+incompatible
github.com/dustin/go-humanize v1.0.0
github.com/facebookincubator/nvdtools v0.1.4
github.com/gabriel-vasile/mimetype v1.3.0
github.com/go-test/deep v1.0.7
github.com/google/go-cmp v0.4.1
github.com/google/uuid v1.2.0
Expand Down
69 changes: 4 additions & 65 deletions grype/pkg/provider.go
Original file line number Diff line number Diff line change
@@ -1,82 +1,21 @@
package pkg

import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"os"

"github.com/anchore/grype/internal/log"

"github.com/anchore/grype/internal"
"github.com/anchore/stereoscope/pkg/image"
"github.com/anchore/syft/syft/source"
)

var errDoesNotProvide = fmt.Errorf("cannot provide packages from the given source")

type providerConfig struct {
userInput string
scopeOpt source.Scope
reader io.Reader
registryOptions *image.RegistryOptions
}

type provider func(cfg providerConfig) ([]Package, Context, error)

// Provide a set of packages and context metadata describing where they were sourced from.
func Provide(userInput string, scopeOpt source.Scope, registryOptions *image.RegistryOptions) ([]Package, Context, error) {
providers := []provider{
syftJSONProvider,
syftProvider, // important: we should try syft last
}

// capture stdin bytes, so they can be used across multiple providers
capturedStdin := bytesFromStdin()

for _, provide := range providers {
config := determineProviderConfig(userInput, scopeOpt, registryOptions, capturedStdin)

packages, ctx, err := provide(config)
if !errors.Is(err, errDoesNotProvide) {
return packages, ctx, err
}
}

return nil, Context{}, errDoesNotProvide
}

func bytesFromStdin() []byte {
isPipedInput, err := internal.IsPipedInput()
if err != nil {
log.Warnf("unable to determine if there is piped input: %+v", err)
isPipedInput = false
}

if isPipedInput {
capturedStdin, err := ioutil.ReadAll(os.Stdin)
if err != nil {
return nil
}

return capturedStdin
}

return nil
}

func determineProviderConfig(userInput string, scopeOpt source.Scope, registryOptions *image.RegistryOptions, stdin []byte) providerConfig {
config := providerConfig{
userInput: userInput,
scopeOpt: scopeOpt,
registryOptions: registryOptions,
}

if len(stdin) > 0 {
config.reader = bytes.NewReader(stdin)
packages, ctx, err := syftSBOMProvider(userInput)
if !errors.Is(err, errDoesNotProvide) {
return packages, ctx, err
}

return config
return syftProvider(userInput, scopeOpt, registryOptions)
}
84 changes: 0 additions & 84 deletions grype/pkg/provider_test.go

This file was deleted.

Loading