Skip to content

Commit

Permalink
Add lib to preflight
Browse files Browse the repository at this point in the history
Testing the lib

Switch back to old repo name

Dep updates

Operator Framework update

lock otel

more dep updates

Dep update

update deps

Add NewManualConfig

Add NewManualOperatorConfig

Rename back
  • Loading branch information
sebrandon1 committed Oct 21, 2022
1 parent a41492b commit c7b8663
Show file tree
Hide file tree
Showing 14 changed files with 839 additions and 743 deletions.
22 changes: 22 additions & 0 deletions certification/runtime/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,28 @@ func NewConfigFrom(vcfg viper.Viper) (*Config, error) {
return &cfg, nil
}

func NewManualContainerConfig(image, responseFormat, artifactsDir string, submit, writeJUnit bool) *Config {
return &Config{
Image: image,
Submit: submit,
WriteJUnit: writeJUnit,
ResponseFormat: responseFormat,
Artifacts: artifactsDir,
}
}

func NewManualOperatorConfig(image, responseFormat, artifactsDir string, writeJUnit bool) *Config {
return &Config{
Image: image,
Submit: false, // operator results are not submitted
WriteJUnit: writeJUnit,
ResponseFormat: responseFormat,
Artifacts: artifactsDir,
Bundle: false,
Scratch: false,
}
}

// storeContainerPolicyConfiguration reads container-policy-specific config
// items in viper, normalizes them, and stores them in Config.
func (c *Config) storeContainerPolicyConfiguration(vcfg viper.Viper) {
Expand Down
20 changes: 0 additions & 20 deletions cmd/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmd
import (
"bytes"
"context"
"fmt"
"strings"

"github.com/redhat-openshift-ecosystem/openshift-preflight/certification/artifacts"
Expand Down Expand Up @@ -63,25 +62,6 @@ func resultsFilenameWithExtension(ext string) string {
return strings.Join([]string{"results", ext}, ".")
}

func buildConnectURL(projectID string) string {
connectURL := fmt.Sprintf("https://connect.redhat.com/projects/%s", projectID)

pyxisEnv := viper.GetString("pyxis_env")
if len(pyxisEnv) > 0 && pyxisEnv != "prod" {
connectURL = fmt.Sprintf("https://connect.%s.redhat.com/projects/%s", viper.GetString("pyxis_env"), projectID)
}

return connectURL
}

func buildOverviewURL(projectID string) string {
return fmt.Sprintf("%s/overview", buildConnectURL(projectID))
}

func buildScanResultsURL(projectID string, imageID string) string {
return fmt.Sprintf("%s/images/%s/scan-results", buildConnectURL(projectID), imageID)
}

func convertPassedOverall(passedOverall bool) string {
if passedOverall {
return "PASSED"
Expand Down
106 changes: 9 additions & 97 deletions cmd/check_container.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package cmd

import (
"context"
"fmt"
"strings"

"github.com/redhat-openshift-ecosystem/openshift-preflight/certification"
"github.com/redhat-openshift-ecosystem/openshift-preflight/certification/engine"
"github.com/redhat-openshift-ecosystem/openshift-preflight/certification/formatters"
"github.com/redhat-openshift-ecosystem/openshift-preflight/certification/policy"
"github.com/redhat-openshift-ecosystem/openshift-preflight/certification/runtime"
"github.com/redhat-openshift-ecosystem/openshift-preflight/lib"
"github.com/redhat-openshift-ecosystem/openshift-preflight/version"

log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -52,53 +50,6 @@ func checkContainerCmd() *cobra.Command {
return checkContainerCmd
}

// checkContainerRunner contains all of the components necessary to run checkContainer.
type checkContainerRunner struct {
cfg *runtime.Config
pc pyxisClient
eng engine.CheckEngine
formatter formatters.ResponseFormatter
rw resultWriter
rs resultSubmitter
}

func newCheckContainerRunner(ctx context.Context, cfg *runtime.Config) (*checkContainerRunner, error) {
cfg.Policy = policy.PolicyContainer
cfg.Submit = submit

pyxisClient := newPyxisClient(ctx, cfg.ReadOnly())
// If we have a pyxisClient, we can query for container policy exceptions.
if pyxisClient != nil {
policy, err := getContainerPolicyExceptions(ctx, pyxisClient)
if err != nil {
return nil, err
}

cfg.Policy = policy
}

engine, err := engine.NewForConfig(ctx, cfg.ReadOnly())
if err != nil {
return nil, err
}

fmttr, err := formatters.NewForConfig(cfg.ReadOnly())
if err != nil {
return nil, err
}

rs := resolveSubmitter(pyxisClient, cfg.ReadOnly())

return &checkContainerRunner{
cfg: cfg,
pc: pyxisClient,
eng: engine,
formatter: fmttr,
rw: &runtime.ResultWriterFile{},
rs: rs,
}, nil
}

// checkContainerRunE executes checkContainer using the user args to inform the execution.
func checkContainerRunE(cmd *cobra.Command, args []string) error {
log.Info("certification library version ", version.Version.String())
Expand All @@ -114,62 +65,23 @@ func checkContainerRunE(cmd *cobra.Command, args []string) error {
cfg.Image = containerImage
cfg.ResponseFormat = formatters.DefaultFormat

checkContainer, err := newCheckContainerRunner(ctx, cfg)
checkContainer, err := lib.NewCheckContainerRunner(ctx, cfg, submit)
if err != nil {
return err
}

// Run the container check.
cmd.SilenceUsage = true
return preflightCheck(ctx,
checkContainer.cfg,
checkContainer.pc,
checkContainer.eng,
checkContainer.formatter,
checkContainer.rw,
checkContainer.rs,
return lib.PreflightCheck(ctx,
checkContainer.Cfg,
checkContainer.Pc,
checkContainer.Eng,
checkContainer.Formatter,
checkContainer.Rw,
checkContainer.Rs,
)
}

// resolveSubmitter will build out a resultSubmitter if the provided pyxisClient, pc, is not nil.
// The pyxisClient is a required component of the submitter. If pc is nil, then a noop submitter
// is returned instead, which does nothing.
func resolveSubmitter(pc pyxisClient, cfg certification.Config) resultSubmitter {
if pc != nil {
return &containerCertificationSubmitter{
certificationProjectID: cfg.CertificationProjectID(),
pyxis: pc,
dockerConfig: cfg.DockerConfig(),
preflightLogFile: cfg.LogFile(),
}
}

return &noopSubmitter{emitLog: true}
}

// getContainerPolicyExceptions will query Pyxis to determine if
// a given project has a certification excemptions, such as root or scratch.
// This will then return the corresponding policy.
//
// If no policy exception flags are found on the project, the standard
// container policy is returned.
func getContainerPolicyExceptions(ctx context.Context, pc pyxisClient) (policy.Policy, error) {
certProject, err := pc.GetProject(ctx)
if err != nil {
return "", fmt.Errorf("could not retrieve project: %w", err)
}
log.Debugf("Certification project name is: %s", certProject.Name)
if certProject.Container.Type == "scratch" {
return policy.PolicyScratch, nil
}

// if a partner sets `Host Level Access` in connect to `Privileged`, enable RootExceptionContainerPolicy checks
if certProject.Container.Privileged {
return policy.PolicyRoot, nil
}
return policy.PolicyContainer, nil
}

func checkContainerPositionalArgs(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return fmt.Errorf("a container image positional argument is required")
Expand Down
Loading

0 comments on commit c7b8663

Please sign in to comment.