Skip to content

Commit 0deaced

Browse files
Merge pull request #402 from ankitathomas/skiptls
Bug 1866437: skip TLS option for pulling indexes
2 parents 10bc084 + 36ce1c6 commit 0deaced

File tree

15 files changed

+146
-40
lines changed

15 files changed

+146
-40
lines changed

cmd/opm/index/add.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ func addIndexAddCmd(parent *cobra.Command) {
5353
if err := indexCmd.MarkFlagRequired("bundles"); err != nil {
5454
logrus.Panic("Failed to set required `bundles` flag for `index add`")
5555
}
56-
indexCmd.Flags().Bool("skip-tls", false, "skip TLS certificate verification for container image registries while pulling bundles")
5756
indexCmd.Flags().StringP("binary-image", "i", "", "container image for on-image `opm` command")
5857
indexCmd.Flags().StringP("container-tool", "c", "", "tool to interact with container images (save, build, etc.). One of: [docker, podman]")
5958
indexCmd.Flags().StringP("build-tool", "u", "", "tool to build container images. One of: [docker, podman]. Defaults to podman. Overrides part of container-tool.")

cmd/opm/index/cmd.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,15 @@ func AddCommand(parent *cobra.Command) {
1818
}
1919
return nil
2020
},
21+
PersistentPreRun: func(cmd *cobra.Command, args []string) {
22+
if skipTLS, err := cmd.Flags().GetBool("skip-tls"); err == nil && skipTLS {
23+
logrus.Warn("--skip-tls flag is set: this mode is insecure and meant for development purposes only.")
24+
}
25+
},
2126
}
2227

2328
parent.AddCommand(cmd)
29+
parent.PersistentFlags().Bool("skip-tls", false, "skip TLS certificate verification for container image registries while pulling bundles or index")
2430
cmd.AddCommand(newIndexDeleteCmd())
2531
addIndexAddCmd(cmd)
2632
cmd.AddCommand(newIndexExportCmd())

cmd/opm/index/delete.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ func runIndexDeleteCmdFunc(cmd *cobra.Command, args []string) error {
9191
return err
9292
}
9393

94+
skipTLS, err := cmd.Flags().GetBool("skip-tls")
95+
if err != nil {
96+
return err
97+
}
98+
9499
logger := logrus.WithFields(logrus.Fields{"operators": operators})
95100

96101
logger.Info("building the index")
@@ -108,6 +113,7 @@ func runIndexDeleteCmdFunc(cmd *cobra.Command, args []string) error {
108113
Operators: operators,
109114
Tag: tag,
110115
Permissive: permissive,
116+
SkipTLS: skipTLS,
111117
}
112118

113119
err = indexDeleter.DeleteFromIndex(request)

cmd/opm/index/deprecate.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ func runIndexDeprecateTruncateCmdFunc(cmd *cobra.Command, args []string) error {
105105
return err
106106
}
107107

108+
skipTLS, err := cmd.Flags().GetBool("skip-tls")
109+
if err != nil {
110+
return err
111+
}
112+
108113
logger := logrus.WithFields(logrus.Fields{"bundles": bundles})
109114

110115
logger.Info("deprecating bundles from the index")
@@ -122,6 +127,7 @@ func runIndexDeprecateTruncateCmdFunc(cmd *cobra.Command, args []string) error {
122127
Tag: tag,
123128
Bundles: bundles,
124129
Permissive: permissive,
130+
SkipTLS: skipTLS,
125131
}
126132

127133
err = indexDeprecator.DeprecateFromIndex(request)

cmd/opm/index/export.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ func runIndexExportCmdFunc(cmd *cobra.Command, args []string) error {
7575
return err
7676
}
7777

78+
skipTLS, err := cmd.Flags().GetBool("skip-tls")
79+
if err != nil {
80+
return err
81+
}
82+
7883
logger := logrus.WithFields(logrus.Fields{"index": index, "package": packageName})
7984

8085
logger.Info("export from the index")
@@ -86,6 +91,7 @@ func runIndexExportCmdFunc(cmd *cobra.Command, args []string) error {
8691
Package: packageName,
8792
DownloadPath: downloadPath,
8893
ContainerTool: containertools.NewContainerTool(containerTool, containertools.NoneTool),
94+
SkipTLS: skipTLS,
8995
}
9096

9197
err = indexExporter.ExportFromIndex(request)

cmd/opm/index/prune.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ func runIndexPruneCmdFunc(cmd *cobra.Command, args []string) error {
9595
return err
9696
}
9797

98+
skipTLS, err := cmd.Flags().GetBool("skip-tls")
99+
if err != nil {
100+
return err
101+
}
102+
98103
logger := logrus.WithFields(logrus.Fields{"packages": packages})
99104

100105
logger.Info("pruning the index")
@@ -109,6 +114,7 @@ func runIndexPruneCmdFunc(cmd *cobra.Command, args []string) error {
109114
Packages: packages,
110115
Tag: tag,
111116
Permissive: permissive,
117+
SkipTLS: skipTLS,
112118
}
113119

114120
err = indexPruner.PruneFromIndex(request)

cmd/opm/index/prunestranded.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ func runIndexPruneStrandedCmdFunc(cmd *cobra.Command, args []string) error {
8080
return err
8181
}
8282

83+
skipTLS, err := cmd.Flags().GetBool("skip-tls")
84+
if err != nil {
85+
return err
86+
}
87+
8388
logger := logrus.WithFields(logrus.Fields{})
8489

8590
logger.Info("pruning stranded bundles from the index")
@@ -92,6 +97,7 @@ func runIndexPruneStrandedCmdFunc(cmd *cobra.Command, args []string) error {
9297
BinarySourceImage: binaryImage,
9398
OutDockerfile: outDockerfile,
9499
Tag: tag,
100+
SkipTLS: skipTLS,
95101
}
96102

97103
err = indexPruner.PruneStrandedFromIndex(request)

cmd/opm/registry/add.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ func addFunc(cmd *cobra.Command, args []string) error {
7777

7878
logger := logrus.WithFields(logrus.Fields{"bundles": bundleImages})
7979

80+
if skipTLS {
81+
logger.Warn("--skip-tls flag is set: this mode is insecure and meant for development purposes only.")
82+
}
83+
8084
logger.Info("adding to the registry")
8185

8286
registryAdder := registry.NewRegistryAdder(logger)

go.sum

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -358,11 +358,8 @@ github.com/grpc-ecosystem/go-grpc-middleware v1.0.1-0.20190118093823-f849b5445de
358358
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
359359
github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
360360
github.com/grpc-ecosystem/grpc-gateway v1.9.5/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
361-
github.com/grpc-ecosystem/grpc-health-probe v0.2.1-0.20181220223928-2bf0a5b182db h1:UxmGBzaBcWDQuQh9E1iT1dWKQFbizZ+SpTd1EL4MSqs=
362-
github.com/grpc-ecosystem/grpc-health-probe v0.2.1-0.20181220223928-2bf0a5b182db/go.mod h1:uBKkC2RbarFsvS5jMJHpVhTLvGlGQj9JJwkaePE3FWI=
363361
github.com/grpc-ecosystem/grpc-health-probe v0.3.2 h1:daShAySXI1DnGc8U9B1E4Qm6o7qzmFR4aRIJ4vY/TUo=
364362
github.com/grpc-ecosystem/grpc-health-probe v0.3.2/go.mod h1:izVOQ4RWbjUR6lm4nn+VLJyQ+FyaiGmprEYgI04Gs7U=
365-
github.com/grpc/grpc-go v1.30.0 h1:3ttCZRhSqhlKmQ6UrrTukz9LjJF/Bi8RuRo8rlyxKhA=
366363
github.com/hailocab/go-hostpool v0.0.0-20160125115350-e80d13ce29ed/go.mod h1:tMWxXQ9wFIaZeTI9F+hmhFiGpFmhOHzyShyFUhRm0H4=
367364
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
368365
github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk=

pkg/containertools/runner.go

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,53 @@ type CommandRunner interface {
2222
type ContainerCommandRunner struct {
2323
logger *logrus.Entry
2424
containerTool ContainerTool
25+
config *RunnerConfig
26+
}
27+
28+
type RunnerConfig struct {
29+
SkipTLS bool
30+
}
31+
32+
type RunnerOption func(config *RunnerConfig)
33+
34+
func SkipTLS(skip bool) RunnerOption {
35+
return func(config *RunnerConfig) {
36+
config.SkipTLS = skip
37+
}
38+
}
39+
40+
func (r *RunnerConfig) apply(options []RunnerOption) {
41+
for _, option := range options {
42+
option(r)
43+
}
44+
}
45+
46+
func (r *ContainerCommandRunner) argsForCmd(cmd string, args ...string) []string {
47+
cmdArgs := []string{cmd}
48+
switch r.containerTool {
49+
case PodmanTool:
50+
switch cmd {
51+
case "pull", "push", "login", "search":
52+
// --tls-verify is a valid flag for these podman subcommands
53+
if r.config.SkipTLS {
54+
cmdArgs = append(cmdArgs, "--tls-verify=false")
55+
}
56+
}
57+
default:
58+
}
59+
cmdArgs = append(cmdArgs, args...)
60+
return cmdArgs
2561
}
2662

2763
// NewCommandRunner takes the containerTool as an input string and returns a
2864
// CommandRunner to run commands with that cli tool
29-
func NewCommandRunner(containerTool ContainerTool, logger *logrus.Entry) *ContainerCommandRunner {
65+
func NewCommandRunner(containerTool ContainerTool, logger *logrus.Entry, opts ...RunnerOption) *ContainerCommandRunner {
66+
var config RunnerConfig
67+
config.apply(opts)
3068
r := &ContainerCommandRunner{
3169
logger: logger,
3270
containerTool: containerTool,
71+
config: &config,
3372
}
3473
return r
3574
}
@@ -42,7 +81,7 @@ func (r *ContainerCommandRunner) GetToolName() string {
4281
// Pull takes a container image path hosted on a container registry and runs the
4382
// pull command to download it onto the local environment
4483
func (r *ContainerCommandRunner) Pull(image string) error {
45-
args := []string{"pull", image}
84+
args := r.argsForCmd("pull", image)
4685

4786
command := exec.Command(r.containerTool.String(), args...)
4887

@@ -84,7 +123,7 @@ func (r *ContainerCommandRunner) Build(dockerfile, tag string) error {
84123

85124
// Unpack copies a directory from a local container image to a directory in the local filesystem.
86125
func (r *ContainerCommandRunner) Unpack(image, src, dst string) error {
87-
args := []string{"create", image, ""}
126+
args := r.argsForCmd("create", image, "")
88127

89128
command := exec.Command(r.containerTool.String(), args...)
90129

@@ -98,7 +137,7 @@ func (r *ContainerCommandRunner) Unpack(image, src, dst string) error {
98137
}
99138

100139
id := strings.TrimSuffix(string(out), "\n")
101-
args = []string{"cp", id + ":" + src, dst}
140+
args = r.argsForCmd("cp", id+":"+src, dst)
102141
command = exec.Command(r.containerTool.String(), args...)
103142

104143
r.logger.Infof("running %s cp", r.containerTool)
@@ -110,7 +149,7 @@ func (r *ContainerCommandRunner) Unpack(image, src, dst string) error {
110149
return fmt.Errorf("error copying container directory %s: %v", string(out), err)
111150
}
112151

113-
args = []string{"rm", id}
152+
args = r.argsForCmd("rm", id)
114153
command = exec.Command(r.containerTool.String(), args...)
115154

116155
r.logger.Infof("running %s rm", r.containerTool)
@@ -128,7 +167,7 @@ func (r *ContainerCommandRunner) Unpack(image, src, dst string) error {
128167
// Inspect runs the 'inspect' command to get image metadata of a local container
129168
// image and returns a byte array of the command's output
130169
func (r *ContainerCommandRunner) Inspect(image string) ([]byte, error) {
131-
args := []string{"inspect", image}
170+
args := r.argsForCmd("inspect", image)
132171

133172
command := exec.Command(r.containerTool.String(), args...)
134173

0 commit comments

Comments
 (0)