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

clairctl: command to add the relevant config options for disconnected #2178

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
117 changes: 117 additions & 0 deletions cmd/clairctl/disconnected.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package main

import (
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"strings"

"github.com/urfave/cli/v2"
"gopkg.in/yaml.v3"
)

// DisconnectedCmd is the "disconnected" subcommand.
var DisconnectedCmd = &cli.Command{
Name: "disconnected",
Action: disconnectedAction,
Usage: "add disconnected config drop-in",
ArgsUsage: "",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "red-hat-repo-to-cpe-file-path",
Usage: "file path for the Red Hat repo-cpe-map data.",
Value: "",
EnvVars: []string{"RED_HAT_REPO_TO_CPE_FILE_PATH"},
},
&cli.StringFlag{
Name: "red-hat-container-to-repos-file-path",
Usage: "file path for the Red Hat container-to-repos data.",
Value: "",
EnvVars: []string{"RED_HAT_CONTAINER_TO_REPOS_FILE_PATH"},
},
&cli.BoolFlag{
Name: "dry-run",
Aliases: []string{"d"},
Usage: "just print out drop-in.",
},
},
Description: `Adds drop-in config for disconnected environments`,
}

type disconnectedCfgDropin struct {
Indexer struct {
Airgap bool `json:"airgap" yaml:"airgap"`
Scanner struct {
Package struct {
RHELContainerScanner map[string]string `json:"rhel_containerscanner" yaml:"rhel_containerscanner"`
} `yaml:"package,omitempty" json:"package,omitempty"`
Repo struct {
RHELRepositoryScanner map[string]string `json:"rhel-repository-scanner" yaml:"rhel-repository-scanner"`
} `yaml:"repo,omitempty" json:"repo,omitempty"`
} `yaml:"scanner,omitempty" json:"scanner,omitempty"`
} `yaml:"indexer,omitempty" json:"indexer,omitempty"`
Matcher struct {
DisableUpdaters bool `yaml:"disable_updaters,omitempty" json:"disable_updaters,omitempty"`
}
}

func disconnectedAction(c *cli.Context) error {
repoCPEMapFile := c.String("red-hat-repo-to-cpe-file-path")
containerRepoMapFile := c.String("red-hat-container-to-repos-file-path")

newConf := &disconnectedCfgDropin{}
if repoCPEMapFile == "" {
return errors.New("could not find repo to CPE file, either specify with --red-hat-repo-to-cpe-file-path or RED_HAT_REPO_TO_CPE_FILE_PATH")
}
if containerRepoMapFile == "" {
return errors.New("could not find container to repos file, either specify with --red-hat-container-to-repos-file-path or RED_HAT_CONTAINER_TO_REPOS_FILE_PATH")

}

newConf.Indexer.Scanner.Repo.RHELRepositoryScanner = map[string]string{"repo2cpe_mapping_file": repoCPEMapFile}
newConf.Indexer.Scanner.Package.RHELContainerScanner = map[string]string{"name2repos_mapping_file": containerRepoMapFile}
newConf.Indexer.Airgap = true
newConf.Matcher.DisableUpdaters = true

cfgPath := c.Path("config")
var (
dropinPath string
dropinData []byte
err error
)
switch {
case strings.HasSuffix(cfgPath, ".json"):
dropinPath = filepath.Join(cfgPath+".d", "disconnected.json")
if dropinData, err = json.Marshal(newConf); err != nil {
return err
}
case strings.HasSuffix(cfgPath, ".yaml"):
dropinPath = filepath.Join(cfgPath+".d", "disconnected.yaml")
if dropinData, err = yaml.Marshal(newConf); err != nil {
return err
}
default:
return errors.New("unknown config format, file is neither .yaml or .json")
}
if err := os.MkdirAll(filepath.Dir(dropinPath), 0o755); err != nil {
return fmt.Errorf("unable to create needed directories: %v", err)
}
if c.Bool("dry-run") {
os.Stdout.Write(dropinData)
return nil
} else {
f, err := os.Create(dropinPath)
if err != nil {
return err
}
defer f.Close()

if _, err = f.Write(dropinData); err != nil {
return err
}
}

return nil
}
1 change: 1 addition & 0 deletions cmd/clairctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func main() {
DeleteCmd,
CheckConfigCmd,
AdminCmd,
DisconnectedCmd,
},
Flags: []cli.Flag{
&cli.BoolFlag{
Expand Down
Loading