Skip to content

Commit 9115397

Browse files
Ankita Thomastimflannagan
authored andcommitted
extend skipTLS option to podman
Upstream-repository: operator-registry Upstream-commit: 935854c8692a90e05535d0062ab63f5b9360d453
1 parent 7f7dbdf commit 9115397

File tree

15 files changed

+164
-45
lines changed

15 files changed

+164
-45
lines changed

staging/operator-registry/cmd/opm/index/add.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,13 @@ func runIndexAddCmdFunc(cmd *cobra.Command, args []string) error {
108108
return err
109109
}
110110

111-
skipTLS, err := cmd.Flags().GetBool("skip-tls")
112-
if err != nil {
113-
return err
111+
var skipTLS *bool
112+
if cmd.Flags().Changed("skip-tls") {
113+
skipTLSVal, err := cmd.Flags().GetBool("skip-tls")
114+
if err != nil {
115+
return err
116+
}
117+
skipTLS = &skipTLSVal
114118
}
115119

116120
mode, err := cmd.Flags().GetString("mode")

staging/operator-registry/cmd/opm/index/delete.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,13 @@ func runIndexDeleteCmdFunc(cmd *cobra.Command, args []string) error {
9292
return err
9393
}
9494

95-
skipTLS, err := cmd.Flags().GetBool("skip-tls")
96-
if err != nil {
97-
return err
95+
var skipTLS *bool
96+
if cmd.Flags().Changed("skip-tls") {
97+
skipTLSVal, err := cmd.Flags().GetBool("skip-tls")
98+
if err != nil {
99+
return err
100+
}
101+
skipTLS = &skipTLSVal
98102
}
99103

100104
logger := logrus.WithFields(logrus.Fields{"operators": operators})

staging/operator-registry/cmd/opm/index/deprecate.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,13 @@ func runIndexDeprecateTruncateCmdFunc(cmd *cobra.Command, args []string) error {
106106
return err
107107
}
108108

109-
skipTLS, err := cmd.Flags().GetBool("skip-tls")
110-
if err != nil {
111-
return err
109+
var skipTLS *bool
110+
if cmd.Flags().Changed("skip-tls") {
111+
skipTLSVal, err := cmd.Flags().GetBool("skip-tls")
112+
if err != nil {
113+
return err
114+
}
115+
skipTLS = &skipTLSVal
112116
}
113117

114118
logger := logrus.WithFields(logrus.Fields{"bundles": bundles})

staging/operator-registry/cmd/opm/index/export.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,13 @@ func runIndexExportCmdFunc(cmd *cobra.Command, args []string) error {
7676
return err
7777
}
7878

79-
skipTLS, err := cmd.Flags().GetBool("skip-tls")
80-
if err != nil {
81-
return err
79+
var skipTLS *bool
80+
if cmd.Flags().Changed("skip-tls") {
81+
skipTLSVal, err := cmd.Flags().GetBool("skip-tls")
82+
if err != nil {
83+
return err
84+
}
85+
skipTLS = &skipTLSVal
8286
}
8387

8488
logger := logrus.WithFields(logrus.Fields{"index": index, "package": packageName})

staging/operator-registry/cmd/opm/index/prune.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,13 @@ func runIndexPruneCmdFunc(cmd *cobra.Command, args []string) error {
9696
return err
9797
}
9898

99-
skipTLS, err := cmd.Flags().GetBool("skip-tls")
100-
if err != nil {
101-
return err
99+
var skipTLS *bool
100+
if cmd.Flags().Changed("skip-tls") {
101+
skipTLSVal, err := cmd.Flags().GetBool("skip-tls")
102+
if err != nil {
103+
return err
104+
}
105+
skipTLS = &skipTLSVal
102106
}
103107

104108
logger := logrus.WithFields(logrus.Fields{"packages": packages})

staging/operator-registry/cmd/opm/index/prunestranded.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ func newIndexPruneStrandedCmd() *cobra.Command {
3636
indexCmd.Flags().StringP("binary-image", "i", "", "container image for on-image `opm` command")
3737
indexCmd.Flags().StringP("container-tool", "c", "podman", "tool to interact with container images (save, build, etc.). One of: [docker, podman]")
3838
indexCmd.Flags().StringP("tag", "t", "", "custom tag for container image being built")
39+
indexCmd.Flags().Bool("skip-tls", false, "skip TLS certificate verification for container image registries while pulling index")
3940

4041
if err := indexCmd.Flags().MarkHidden("debug"); err != nil {
4142
logrus.Panic(err.Error())
@@ -80,6 +81,15 @@ func runIndexPruneStrandedCmdFunc(cmd *cobra.Command, args []string) error {
8081
return err
8182
}
8283

84+
var skipTLS *bool
85+
if cmd.Flags().Changed("skip-tls") {
86+
skipTLSVal, err := cmd.Flags().GetBool("skip-tls")
87+
if err != nil {
88+
return err
89+
}
90+
skipTLS = &skipTLSVal
91+
}
92+
8393
logger := logrus.WithFields(logrus.Fields{})
8494

8595
logger.Info("pruning stranded bundles from the index")
@@ -92,6 +102,7 @@ func runIndexPruneStrandedCmdFunc(cmd *cobra.Command, args []string) error {
92102
BinarySourceImage: binaryImage,
93103
OutDockerfile: outDockerfile,
94104
Tag: tag,
105+
SkipTLS: skipTLS,
95106
}
96107

97108
err = indexPruner.PruneStrandedFromIndex(request)

staging/operator-registry/cmd/opm/registry/add.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,13 @@ func addFunc(cmd *cobra.Command, args []string) error {
4141
if err != nil {
4242
return err
4343
}
44-
skipTLS, err := cmd.Flags().GetBool("skip-tls")
45-
if err != nil {
46-
return err
44+
var skipTLS *bool
45+
if cmd.Flags().Changed("skip-tls") {
46+
skipTLSVal, err := cmd.Flags().GetBool("skip-tls")
47+
if err != nil {
48+
return err
49+
}
50+
skipTLS = &skipTLSVal
4751
}
4852
fromFilename, err := cmd.Flags().GetString("database")
4953
if err != nil {

staging/operator-registry/pkg/containertools/factory_docker.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ func (d *DockerCommandFactory) BuildCommand(o BuildOptions) (*exec.Cmd, error) {
2222
args = append(args, "-t", tag)
2323
}
2424

25+
if o.secure {
26+
args = append(args, "--tls")
27+
}
28+
2529
if o.context == "" {
2630
return nil, fmt.Errorf("context not provided")
2731
}

staging/operator-registry/pkg/containertools/factory_podman.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ func (p *PodmanCommandFactory) BuildCommand(o BuildOptions) (*exec.Cmd, error) {
2424
args = append(args, "-t", tag)
2525
}
2626

27+
if !o.secure {
28+
args = append(args, "--tls-verify=false")
29+
}
30+
2731
if o.context == "" {
2832
return nil, fmt.Errorf("context not provided")
2933
}

staging/operator-registry/pkg/containertools/factory_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func TestBuildCommand(t *testing.T) {
7777
Factory: &PodmanCommandFactory{},
7878
Options: DefaultBuildOptions(),
7979
Args: []string{
80-
"podman", "build", "--format", "docker", ".",
80+
"podman", "build", "--format", "docker", "--tls-verify=false", ".",
8181
},
8282
},
8383
{
@@ -88,7 +88,7 @@ func TestBuildCommand(t *testing.T) {
8888
format: "oci",
8989
},
9090
Args: []string{
91-
"podman", "build", "--format", "oci", ".",
91+
"podman", "build", "--format", "oci", "--tls-verify=false", ".",
9292
},
9393
},
9494
{
@@ -98,7 +98,7 @@ func TestBuildCommand(t *testing.T) {
9898
context: "foo",
9999
},
100100
Args: []string{
101-
"podman", "build", "--format", "docker", "foo",
101+
"podman", "build", "--format", "docker", "--tls-verify=false", "foo",
102102
},
103103
},
104104
{
@@ -109,7 +109,7 @@ func TestBuildCommand(t *testing.T) {
109109
dockerfile: "foo",
110110
},
111111
Args: []string{
112-
"podman", "build", "--format", "docker", "-f", "foo", ".",
112+
"podman", "build", "--format", "docker", "-f", "foo", "--tls-verify=false", ".",
113113
},
114114
},
115115
{
@@ -120,7 +120,7 @@ func TestBuildCommand(t *testing.T) {
120120
tags: []string{"foo"},
121121
},
122122
Args: []string{
123-
"podman", "build", "--format", "docker", "-t", "foo", ".",
123+
"podman", "build", "--format", "docker", "-t", "foo", "--tls-verify=false", ".",
124124
},
125125
},
126126
{
@@ -131,7 +131,7 @@ func TestBuildCommand(t *testing.T) {
131131
tags: []string{"foo", "bar"},
132132
},
133133
Args: []string{
134-
"podman", "build", "--format", "docker", "-t", "foo", "-t", "bar", ".",
134+
"podman", "build", "--format", "docker", "-t", "foo", "-t", "bar", "--tls-verify=false", ".",
135135
},
136136
},
137137
{

staging/operator-registry/pkg/containertools/option_build.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ type BuildOptions struct {
55
tags []string
66
dockerfile string
77
context string
8+
secure bool
89
}
910

1011
func (o *BuildOptions) SetFormatDocker() {
@@ -27,6 +28,10 @@ func (o *BuildOptions) SetContext(context string) {
2728
o.context = context
2829
}
2930

31+
func (o *BuildOptions) SetSkipTLS(skipTLS bool) {
32+
o.secure = !skipTLS
33+
}
34+
3035
func DefaultBuildOptions() BuildOptions {
3136
var o BuildOptions
3237
o.SetFormatDocker()

staging/operator-registry/pkg/containertools/runner.go

Lines changed: 68 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,75 @@ 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+
if skip != nil {
37+
config.SkipTLS = *skip
38+
}
39+
}
40+
}
41+
42+
func (r *RunnerConfig) apply(options []RunnerOption) {
43+
for _, option := range options {
44+
option(r)
45+
}
46+
}
47+
48+
func (r *ContainerCommandRunner) argsForCmd(cmd string, args... string) []string {
49+
cmdArgs := []string{cmd}
50+
switch r.containerTool {
51+
case PodmanTool:
52+
switch cmd {
53+
case "build", "pull", "push", "login", "search":
54+
// --tls-verify is a valid flag for these podman subcommands
55+
if r.config.SkipTLS {
56+
cmdArgs = append(cmdArgs, "--tls-verify=false")
57+
}
58+
}
59+
case DockerTool:
60+
if !r.config.SkipTLS {
61+
cmdArgs = append(cmdArgs, "--tls")
62+
}
63+
default:
64+
}
65+
cmdArgs = append(cmdArgs, args...)
66+
return cmdArgs
67+
}
68+
69+
func defaultConfig(toolName string) *RunnerConfig {
70+
switch toolName {
71+
case "docker":
72+
// docker disables tls verify by default, mimic that behavior
73+
return &RunnerConfig{
74+
SkipTLS: true,
75+
}
76+
case "podman":
77+
return &RunnerConfig{
78+
SkipTLS: false,
79+
}
80+
default:
81+
return &RunnerConfig{}
82+
}
2583
}
2684

2785
// NewCommandRunner takes the containerTool as an input string and returns a
2886
// CommandRunner to run commands with that cli tool
29-
func NewCommandRunner(containerTool ContainerTool, logger *logrus.Entry) *ContainerCommandRunner {
87+
func NewCommandRunner(containerTool ContainerTool, logger *logrus.Entry, opts... RunnerOption) *ContainerCommandRunner {
88+
config := defaultConfig(containerTool.String())
89+
config.apply(opts)
3090
r := &ContainerCommandRunner{
3191
logger: logger,
3292
containerTool: containerTool,
93+
config: config,
3394
}
3495
return r
3596
}
@@ -42,7 +103,7 @@ func (r *ContainerCommandRunner) GetToolName() string {
42103
// Pull takes a container image path hosted on a container registry and runs the
43104
// pull command to download it onto the local environment
44105
func (r *ContainerCommandRunner) Pull(image string) error {
45-
args := []string{"pull", image}
106+
args := r.argsForCmd("pull", image)
46107

47108
command := exec.Command(r.containerTool.String(), args...)
48109

@@ -65,6 +126,7 @@ func (r *ContainerCommandRunner) Build(dockerfile, tag string) error {
65126
}
66127
o.SetDockerfile(dockerfile)
67128
o.SetContext(".")
129+
o.SetSkipTLS(r.config.SkipTLS)
68130
command, err := r.containerTool.CommandFactory().BuildCommand(o)
69131
if err != nil {
70132
return fmt.Errorf("unable to perform build: %v", err)
@@ -84,7 +146,7 @@ func (r *ContainerCommandRunner) Build(dockerfile, tag string) error {
84146

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

89151
command := exec.Command(r.containerTool.String(), args...)
90152

@@ -98,7 +160,7 @@ func (r *ContainerCommandRunner) Unpack(image, src, dst string) error {
98160
}
99161

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

104166
r.logger.Infof("running %s cp", r.containerTool)
@@ -110,7 +172,7 @@ func (r *ContainerCommandRunner) Unpack(image, src, dst string) error {
110172
return fmt.Errorf("error copying container directory %s: %v", string(out), err)
111173
}
112174

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

116178
r.logger.Infof("running %s rm", r.containerTool)
@@ -128,7 +190,7 @@ func (r *ContainerCommandRunner) Unpack(image, src, dst string) error {
128190
// Inspect runs the 'inspect' command to get image metadata of a local container
129191
// image and returns a byte array of the command's output
130192
func (r *ContainerCommandRunner) Inspect(image string) ([]byte, error) {
131-
args := []string{"inspect", image}
193+
args := r.argsForCmd("inspect", image)
132194

133195
command := exec.Command(r.containerTool.String(), args...)
134196

staging/operator-registry/pkg/image/execregistry/registry.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ type Registry struct {
2626
var _ image.Registry = &Registry{}
2727

2828
// NewRegistry instantiates and returns a new registry which manipulates images via exec podman/docker commands.
29-
func NewRegistry(tool containertools.ContainerTool, logger *logrus.Entry) (registry *Registry, err error) {
29+
func NewRegistry(tool containertools.ContainerTool, logger *logrus.Entry, opts... containertools.RunnerOption) (registry *Registry, err error) {
3030
return &Registry{
3131
log: logger,
32-
cmd: containertools.NewCommandRunner(tool, logger),
32+
cmd: containertools.NewCommandRunner(tool, logger, opts...),
3333
}, nil
3434
}
3535

0 commit comments

Comments
 (0)