Skip to content

Commit

Permalink
security: disable pprof by default (#1672)
Browse files Browse the repository at this point in the history
(cherry picked from commit 24786f4)
  • Loading branch information
oilbeater committed Jul 8, 2022
1 parent 761ddcb commit 8190df3
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
14 changes: 11 additions & 3 deletions cmd/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"github.com/kubeovn/kube-ovn/pkg/util"
"net/http"
_ "net/http/pprof" // #nosec
"net/http/pprof"
"os"
"time"

Expand Down Expand Up @@ -40,8 +40,16 @@ func CmdMain() {

go loopOvnNbctlDaemon(config)
go func() {
http.Handle("/metrics", promhttp.Handler())
klog.Fatal(http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", config.PprofPort), nil))
mux := http.NewServeMux()
mux.Handle("/metrics", promhttp.Handler())
if config.EnablePprof {
mux.HandleFunc("/debug/pprof/", pprof.Index)
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
}
klog.Fatal(http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", config.PprofPort), mux))
}()

ctl := controller.NewController(config)
Expand Down
15 changes: 12 additions & 3 deletions cmd/daemon/cniserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"encoding/json"
"fmt"
"net/http"
_ "net/http/pprof" // #nosec
"net/http/pprof"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -84,8 +84,17 @@ func CmdMain() {
if err := mvCNIConf(config.CniConfDir, config.CniConfFile, config.CniConfName); err != nil {
klog.Fatalf("failed to mv cni conf, %v", err)
}
http.Handle("/metrics", promhttp.Handler())
klog.Fatal(http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", config.PprofPort), nil))

mux := http.NewServeMux()
mux.Handle("/metrics", promhttp.Handler())
if config.EnablePprof {
mux.HandleFunc("/debug/pprof/", pprof.Index)
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
}
klog.Fatal(http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", config.PprofPort), mux))
}

func mvCNIConf(configDir, configFile, confName string) error {
Expand Down
3 changes: 3 additions & 0 deletions pkg/controller/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ type Configuration struct {

WorkerNum int
PprofPort int
EnablePprof bool
NodePgProbeTime int

NetworkType string
Expand Down Expand Up @@ -113,6 +114,7 @@ func ParseFlags() (*Configuration, error) {
argClusterUdpSessionLoadBalancer = pflag.String("cluster-udp-session-loadbalancer", "cluster-udp-session-loadbalancer", "The name for cluster udp session loadbalancer")

argWorkerNum = pflag.Int("worker-num", 3, "The parallelism of each worker")
argEnablePprof = pflag.Bool("enable-pprof", false, "Enable pprof")
argPprofPort = pflag.Int("pprof-port", 10660, "The port to get profiling data")
argNodePgProbeTime = pflag.Int("nodepg-probe-time", 1, "The probe interval for node port-group, the unit is minute")

Expand Down Expand Up @@ -175,6 +177,7 @@ func ParseFlags() (*Configuration, error) {
ClusterTcpSessionLoadBalancer: *argClusterTcpSessionLoadBalancer,
ClusterUdpSessionLoadBalancer: *argClusterUdpSessionLoadBalancer,
WorkerNum: *argWorkerNum,
EnablePprof: *argEnablePprof,
PprofPort: *argPprofPort,
NetworkType: *argNetworkType,
DefaultVlanID: *argDefaultVlanID,
Expand Down
3 changes: 3 additions & 0 deletions pkg/daemon/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type Configuration struct {
ServiceClusterIPRange string
NodeLocalDnsIP string
EncapChecksum bool
EnablePprof bool
PprofPort int
NetworkType string
CniConfDir string
Expand All @@ -69,6 +70,7 @@ func ParseFlags() *Configuration {
argServiceClusterIPRange = pflag.String("service-cluster-ip-range", "10.96.0.0/12", "The kubernetes service cluster ip range")
argNodeLocalDnsIP = pflag.String("node-local-dns-ip", "", "If use nodelocaldns the local dns server ip should be set here.")
argEncapChecksum = pflag.Bool("encap-checksum", true, "Enable checksum")
argEnablePprof = pflag.Bool("enable-pprof", false, "Enable pprof")
argPprofPort = pflag.Int("pprof-port", 10665, "The port to get profiling data")

argsNetworkType = pflag.String("network-type", "geneve", "The ovn network type")
Expand Down Expand Up @@ -110,6 +112,7 @@ func ParseFlags() *Configuration {
BindSocket: *argBindSocket,
OvsSocket: *argOvsSocket,
KubeConfigFile: *argKubeConfigFile,
EnablePprof: *argEnablePprof,
PprofPort: *argPprofPort,
NodeName: strings.ToLower(*argNodeName),
ServiceClusterIPRange: *argServiceClusterIPRange,
Expand Down

0 comments on commit 8190df3

Please sign in to comment.