Skip to content

Commit

Permalink
clairctl: command to add the relevant config options for disconnected
Browse files Browse the repository at this point in the history
Adding a command that can create a new drop-in config with the relevant
disconnected settings in-place. You can also specify where the red hat
indexing files are located.

Signed-off-by: crozzy <[email protected]>
  • Loading branch information
crozzy committed Nov 25, 2024
1 parent dde57fc commit 319038a
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 20 deletions.
116 changes: 116 additions & 0 deletions cmd/clairctl/disconnected.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package main

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

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

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,
IndexDataCmd,
},
Flags: []cli.Flag{
&cli.BoolFlag{
Expand Down
14 changes: 10 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ require (
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/ncruces/go-strftime v0.1.9 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0-rc3 // indirect
github.com/opencontainers/image-spec v1.1.0 // indirect
github.com/package-url/packageurl-go v0.1.3 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
Expand All @@ -87,16 +87,22 @@ require (
go.opentelemetry.io/otel/metric v1.32.0 // indirect
go.uber.org/mock v0.5.0 // indirect
golang.org/x/crypto v0.29.0 // indirect
golang.org/x/mod v0.21.0 // indirect
golang.org/x/mod v0.22.0 // indirect
golang.org/x/sys v0.27.0 // indirect
golang.org/x/text v0.20.0 // indirect
golang.org/x/tools v0.26.0 // indirect
golang.org/x/tools v0.27.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
modernc.org/gc/v3 v3.0.0-20240107210532-573471604cb6 // indirect
modernc.org/libc v1.55.3 // indirect
modernc.org/mathutil v1.6.0 // indirect
modernc.org/memory v1.8.0 // indirect
modernc.org/sqlite v1.33.1 // indirect
modernc.org/sqlite v1.34.1 // indirect
modernc.org/strutil v1.2.0 // indirect
modernc.org/token v1.1.0 // indirect
)

replace github.com/quay/claircore => ../claircore

replace github.com/quay/clair/config => ./config

replace github.com/quay/claircore/toolkit => ../claircore/toolkit
24 changes: 8 additions & 16 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
Expand Down Expand Up @@ -171,8 +170,8 @@ github.com/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdh
github.com/ncruces/go-strftime v0.1.9/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
github.com/opencontainers/image-spec v1.1.0-rc3 h1:fzg1mXZFj8YdPeNkRXMg+zb88BFV0Ys52cJydRwBkb8=
github.com/opencontainers/image-spec v1.1.0-rc3/go.mod h1:X4pATf0uXsnn3g5aiGIsVnJBR4mxhKzfwmvK/B2NTm8=
github.com/opencontainers/image-spec v1.1.0 h1:8SG7/vwALn54lVB/0yZ/MMwhFrPYtpEHQb2IpWsCzug=
github.com/opencontainers/image-spec v1.1.0/go.mod h1:W4s4sFTMaBeK1BQLXbG4AdM2szdn85PY75RI83NrTrM=
github.com/package-url/packageurl-go v0.1.3 h1:4juMED3hHiz0set3Vq3KeQ75KD1avthoXLtmE3I0PLs=
github.com/package-url/packageurl-go v0.1.3/go.mod h1:nKAWB8E6uk1MHqiS/lQb9pYBGH2+mdJ2PJc2s50dQY0=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand All @@ -188,13 +187,6 @@ github.com/prometheus/common v0.55.0 h1:KEi6DK7lXW/m7Ig5i47x0vRzuBsHuvJdi5ee6Y3G
github.com/prometheus/common v0.55.0/go.mod h1:2SECS4xJG1kd8XF9IcM1gMX6510RAEL65zxzNImwdc8=
github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0learggepc=
github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk=
github.com/quay/clair/config v1.4.1 h1:4z/7aaeWrSOdhG+sPJmXEOEUJ/4JqenKj5oXRG5Q1k0=
github.com/quay/clair/config v1.4.1/go.mod h1:GHMVGl7WIq2WB+mFrFkUFHGphDojOPEdoAhjpcTTgLg=
github.com/quay/claircore v1.5.33 h1:9DHjWuVtWpwxlxi6bnTdj6p79BLAs6vIHyNkGASa22Q=
github.com/quay/claircore v1.5.33/go.mod h1:wb3p5CeGM39eU1SDaR+ecQ/AyrrUWWcheXNHBXXjTWo=
github.com/quay/claircore/toolkit v1.0.0/go.mod h1:3ELtgf92x7o1JCTSKVOAqhcnCTXc4s5qiGaEDx62i20=
github.com/quay/claircore/toolkit v1.2.4 h1:Ld7rve32pUOrfR+7jJA9nGHeZ8nPejpEgNWkJ7OPJZM=
github.com/quay/claircore/toolkit v1.2.4/go.mod h1:m6ZRpxJClVAraNpIYyCsW/ULF/33ye7KkGTyNTMwvDY=
github.com/quay/claircore/updater/driver v1.0.0 h1:w7dAUjO3GBK6RjNyTZ2Kwz0l/Wuic3ykKJWPB80uA94=
github.com/quay/claircore/updater/driver v1.0.0/go.mod h1:My5aY1wBpgxcWaHQZ0VoPmmj/EzuH7fq4ntzJbos4OI=
github.com/quay/goval-parser v0.8.8 h1:Uf+f9iF2GIR5GPUY2pGoa9il2+4cdES44ZlM0mWm4cA=
Expand Down Expand Up @@ -310,8 +302,8 @@ golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzB
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.21.0 h1:vvrHzRwRfVKSiLrG+d4FMl/Qi4ukBCE6kZlTUkDYRT0=
golang.org/x/mod v0.21.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
golang.org/x/mod v0.22.0 h1:D4nJWe9zXqHOmWqj4VMOJhvzj7bEZg4wEYa759z1pH4=
golang.org/x/mod v0.22.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
Expand Down Expand Up @@ -390,8 +382,8 @@ golang.org/x/tools v0.0.0-20200103221440-774c71fcf114/go.mod h1:TB2adYChydJhpapK
golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/tools v0.26.0 h1:v/60pFQmzmT9ExmjDv2gGIfi3OqfKoEP6I5+umXlbnQ=
golang.org/x/tools v0.26.0/go.mod h1:TPVVj70c7JJ3WCazhD8OdXcZg/og+b9+tH/KxylGwH0=
golang.org/x/tools v0.27.0 h1:qEKojBykQkQ4EynWy4S8Weg69NumxKdn40Fce3uc/8o=
golang.org/x/tools v0.27.0/go.mod h1:sUi0ZgbwW9ZPAq26Ekut+weQPR5eIM6GQLQ1Yjm1H0Q=
golang.org/x/xerrors v0.0.0-20190410155217-1f06c39b4373/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20190513163551-3ee3066db522/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down Expand Up @@ -435,8 +427,8 @@ modernc.org/opt v0.1.3 h1:3XOZf2yznlhC+ibLltsDGzABUGVx8J6pnFMS3E4dcq4=
modernc.org/opt v0.1.3/go.mod h1:WdSiB5evDcignE70guQKxYUl14mgWtbClRi5wmkkTX0=
modernc.org/sortutil v1.2.0 h1:jQiD3PfS2REGJNzNCMMaLSp/wdMNieTbKX920Cqdgqc=
modernc.org/sortutil v1.2.0/go.mod h1:TKU2s7kJMf1AE84OoiGppNHJwvB753OYfNl2WRb++Ss=
modernc.org/sqlite v1.33.1 h1:trb6Z3YYoeM9eDL1O8do81kP+0ejv+YzgyFo+Gwy0nM=
modernc.org/sqlite v1.33.1/go.mod h1:pXV2xHxhzXZsgT/RtTFAPY6JJDEvOTcTdwADQCCWD4k=
modernc.org/sqlite v1.34.1 h1:u3Yi6M0N8t9yKRDwhXcyp1eS5/ErhPTBggxWFuR6Hfk=
modernc.org/sqlite v1.34.1/go.mod h1:pXV2xHxhzXZsgT/RtTFAPY6JJDEvOTcTdwADQCCWD4k=
modernc.org/strutil v1.2.0 h1:agBi9dp1I+eOnxXeiZawM8F4LawKv4NzGWSaLfyeNZA=
modernc.org/strutil v1.2.0/go.mod h1:/mdcBmfOibveCTBxUl5B5l6W+TTH1FXPLHZE6bTosX0=
modernc.org/token v1.1.0 h1:Xl7Ap9dKaEs5kLoOQeQmPWevfnk/DM5qcLcYlA8ys6Y=
Expand Down

0 comments on commit 319038a

Please sign in to comment.