Skip to content

Commit

Permalink
✨ rapid reset scaffold remediation (#288)
Browse files Browse the repository at this point in the history
* add flags to disable HTTP/2 and securely serve metrics

in both the hybrid scaffolds and the binaries produced by this
project for running helm and hybrid-helm operators to finish remediation
of:
GHSA-qppj-fm5r-hxr3
GHSA-4374-p667-p6c8

Signed-off-by: everettraven <everettraven@gmail.com>

* add missing import to hybrid scaffolding and regenerate testdata

Signed-off-by: everettraven <everettraven@gmail.com>

* remove Port field from scaffolded manager.Options

Signed-off-by: everettraven <everettraven@gmail.com>

* update hybrid scaffolding to point to the appropriate main.go in Makefile and Dockerfile

Signed-off-by: everettraven <everettraven@gmail.com>

---------

Signed-off-by: everettraven <everettraven@gmail.com>
  • Loading branch information
everettraven committed Jan 18, 2024
1 parent 9359e2f commit 4e326df
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 47 deletions.
25 changes: 25 additions & 0 deletions internal/flags/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
package flags

import (
"crypto/tls"
"runtime"
"time"

"github.com/spf13/pflag"
"k8s.io/client-go/tools/leaderelection/resourcelock"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/webhook"
)

// Flags - Options to be used by a helm operator
Expand All @@ -33,6 +35,8 @@ type Flags struct {
LeaderElectionNamespace string
MaxConcurrentReconciles int
ProbeAddr string
EnableHTTP2 bool
SecureMetrics bool

// Path to a controller-runtime componentconfig file.
// If this is empty, use default values.
Expand Down Expand Up @@ -117,6 +121,16 @@ func (f *Flags) AddTo(flagSet *pflag.FlagSet) {
" holding the leader lock (required if running locally with leader"+
" election enabled).",
)
flagSet.BoolVar(&f.EnableHTTP2,
"enable-http2",
false,
"enables HTTP/2 on the webhook and metrics servers",
)
flagSet.BoolVar(&f.SecureMetrics,
"metrics-secure",
false,
"enables secure serving of the metrics endpoint",
)
}

// ToManagerOptions uses the flag set in f to configure options.
Expand Down Expand Up @@ -151,5 +165,16 @@ func (f *Flags) ToManagerOptions(options manager.Options) manager.Options {
if options.LeaderElectionResourceLock == "" {
options.LeaderElectionResourceLock = resourcelock.LeasesResourceLock
}

disableHTTP2 := func(c *tls.Config) {
c.NextProtos = []string{"http/1.1"}
}
if !f.EnableHTTP2 {
options.WebhookServer = webhook.NewServer(webhook.Options{
TLSOpts: []func(*tls.Config){disableHTTP2},
})
options.Metrics.TLSOpts = append(options.Metrics.TLSOpts, disableHTTP2)
}
options.Metrics.SecureServing = f.SecureMetrics
return options
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ COPY go.sum go.sum
RUN go mod download
# Copy the go source
COPY main.go main.go
COPY cmd/ cmd/
COPY api/ api/
COPY controllers/ controllers/
# Build
RUN GOOS=linux GOARCH=amd64 go build -a -o manager main.go
RUN GOOS=linux GOARCH=amd64 go build -a -o manager cmd/main.go
FROM registry.access.redhat.com/ubi8/ubi-micro:8.7
Expand Down
32 changes: 30 additions & 2 deletions pkg/plugins/hybrid/v1alpha/scaffolds/internal/templates/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ var mainTemplate = `{{ .Boilerplate }}
package main
import (
"crypto/tls"
"flag"
"os"
"runtime"
Expand All @@ -206,6 +207,9 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/controller-runtime/pkg/webhook"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
%s
)
Expand All @@ -230,6 +234,8 @@ func main() {
watchesPath string
probeAddr string
enableLeaderElection bool
enableHTTP2 bool
secureMetrics bool
)
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
Expand All @@ -239,6 +245,10 @@ func main() {
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
"Enable leader election for controller manager. " +
"Enabling this will ensure there is only one active controller manager.")
flag.BoolVar(&secureMetrics, "metrics-secure", false,
"Whether or not the metrics endpoint should be served securely")
flag.BoolVar(&enableHTTP2, "enable-http2", false,
"Whether or not HTTP/2 should be enabled for the metrics and webhook servers")
{{- else }}
var configFile string
flag.StringVar(&configFile, "config", "",
Expand All @@ -255,10 +265,28 @@ func main() {
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
{{ if not .ComponentConfig }}
disableHTTP2 := func(c *tls.Config) {
setupLog.Info("disabling http/2")
c.NextProtos = []string{"http/1.1"}
}
tlsOpts := []func(*tls.Config){}
if !enableHTTP2 {
tlsOpts = append(tlsOpts, disableHTTP2)
}
webhookServer := webhook.NewServer(webhook.Options{
TLSOpts: tlsOpts,
})
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
MetricsBindAddress: metricsAddr,
Port: 9443,
Metrics: metricsserver.Options{
BindAddress: metricsAddr,
SecureServing: secureMetrics,
TLSOpts: tlsOpts,
},
WebhookServer: webhookServer,
HealthProbeBindAddress: probeAddr,
LeaderElection: enableLeaderElection,
LeaderElectionID: leaderElectionID,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,11 @@ help: ## Display this help.
##@ Build
.PHONY: build
build: manifests generate fmt vet ## Build manager binary.
go build -o bin/manager main.go
go build -o bin/manager cmd/main.go
.PHONY: run
run: manifests generate fmt vet ## Run against the configured Kubernetes cluster in ~/.kube/config
go run ./main.go
go run cmd/main.go
.PHONY: docker-build
docker-build: ## Build docker image with the manager.
Expand Down
4 changes: 2 additions & 2 deletions testdata/hybrid/memcached-operator/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ COPY go.sum go.sum
RUN go mod download

# Copy the go source
COPY main.go main.go
COPY cmd/ cmd/
COPY api/ api/
COPY controllers/ controllers/

# Build
RUN GOOS=linux GOARCH=amd64 go build -a -o manager main.go
RUN GOOS=linux GOARCH=amd64 go build -a -o manager cmd/main.go

FROM registry.access.redhat.com/ubi8/ubi-micro:8.7

Expand Down
4 changes: 2 additions & 2 deletions testdata/hybrid/memcached-operator/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ help: ## Display this help.
##@ Build
.PHONY: build
build: manifests generate fmt vet ## Build manager binary.
go build -o bin/manager main.go
go build -o bin/manager cmd/main.go

.PHONY: run
run: manifests generate fmt vet ## Run against the configured Kubernetes cluster in ~/.kube/config
go run ./main.go
go run cmd/main.go

.PHONY: docker-build
docker-build: ## Build docker image with the manager.
Expand Down
33 changes: 30 additions & 3 deletions testdata/hybrid/memcached-operator/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package main

import (
"crypto/tls"
"flag"
"os"
"runtime"
Expand All @@ -34,6 +35,8 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
"sigs.k8s.io/controller-runtime/pkg/webhook"

cachev1alpha1 "github.com/example/memcached-operator/api/v1alpha1"
"github.com/example/memcached-operator/internal/controller"
Expand Down Expand Up @@ -61,6 +64,8 @@ func main() {
watchesPath string
probeAddr string
enableLeaderElection bool
enableHTTP2 bool
secureMetrics bool
)

flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
Expand All @@ -70,6 +75,10 @@ func main() {
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
"Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.")
flag.BoolVar(&secureMetrics, "metrics-secure", false,
"Whether or not the metrics endpoint should be served securely")
flag.BoolVar(&enableHTTP2, "enable-http2", false,
"Whether or not HTTP/2 should be enabled for the metrics and webhook servers")
opts := zap.Options{
Development: true,
}
Expand All @@ -78,10 +87,28 @@ func main() {

ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))

disableHTTP2 := func(c *tls.Config) {
setupLog.Info("disabling http/2")
c.NextProtos = []string{"http/1.1"}
}

tlsOpts := []func(*tls.Config){}
if !enableHTTP2 {
tlsOpts = append(tlsOpts, disableHTTP2)
}

webhookServer := webhook.NewServer(webhook.Options{
TLSOpts: tlsOpts,
})

mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
MetricsBindAddress: metricsAddr,
Port: 9443,
Scheme: scheme,
Metrics: metricsserver.Options{
BindAddress: metricsAddr,
SecureServing: secureMetrics,
TLSOpts: tlsOpts,
},
WebhookServer: webhookServer,
HealthProbeBindAddress: probeAddr,
LeaderElection: enableLeaderElection,
LeaderElectionID: leaderElectionID,
Expand Down
22 changes: 11 additions & 11 deletions testdata/hybrid/memcached-operator/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.20
require (
github.com/onsi/ginkgo/v2 v2.13.0
github.com/onsi/gomega v1.30.0
github.com/operator-framework/helm-operator-plugins v0.1.1
github.com/operator-framework/helm-operator-plugins v0.1.2
k8s.io/apimachinery v0.28.5
k8s.io/client-go v0.28.5
sigs.k8s.io/controller-runtime v0.16.3
Expand Down Expand Up @@ -60,7 +60,7 @@ require (
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/pprof v0.0.0-20230510103437-eeec1cb781c3 // indirect
github.com/google/pprof v0.0.0-20230907193218-d3ddc7976beb // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/google/uuid v1.3.1 // indirect
github.com/gorilla/mux v1.8.0 // indirect
Expand All @@ -83,7 +83,7 @@ require (
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
Expand All @@ -100,10 +100,10 @@ require (
github.com/operator-framework/operator-lib v0.12.0 // indirect
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_golang v1.16.0 // indirect
github.com/prometheus/client_model v0.4.0 // indirect
github.com/prometheus/common v0.44.0 // indirect
github.com/prometheus/procfs v0.10.1 // indirect
github.com/prometheus/client_golang v1.18.0 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.45.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/rivo/uniseg v0.4.2 // indirect
github.com/rubenv/sql-migrate v1.5.2 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
Expand All @@ -127,13 +127,13 @@ require (
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/oauth2 v0.11.0 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/oauth2 v0.12.0 // indirect
golang.org/x/sync v0.4.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/term v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/tools v0.12.0 // indirect
golang.org/x/tools v0.14.0 // indirect
gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230711160842-782d3b101e98 // indirect
Expand All @@ -157,5 +157,5 @@ require (
sigs.k8s.io/kustomize/api v0.13.5-0.20230601165947-6ce0bf390ce3 // indirect
sigs.k8s.io/kustomize/kyaml v0.14.3-0.20230601165947-6ce0bf390ce3 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
)
Loading

0 comments on commit 4e326df

Please sign in to comment.