Skip to content

Commit

Permalink
restrict pprof-addr to localhost (#893)
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-mateen authored Oct 6, 2023
1 parent 57616e4 commit 62d92bc
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions cmd/amazon-cloudwatch-agent/amazon-cloudwatch-agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const (
var fDebug = flag.Bool("debug", false,
"turn on debug logging")
var pprofAddr = flag.String("pprof-addr", "",
"pprof address to listen on, not activate pprof if empty")
"pprof address to listen on, disabled by default, examples: 'localhost:1234', ':4567' (restricted to localhost)")
var fQuiet = flag.Bool("quiet", false,
"run in quiet mode")
var fTest = flag.Bool("test", false, "enable test mode: gather metrics, print them out, and exit")
Expand Down Expand Up @@ -80,8 +80,6 @@ var fAggregatorFilters = flag.String("aggregator-filter", "",
"filter the aggregators to enable, separator is :")
var fProcessorFilters = flag.String("processor-filter", "",
"filter the processors to enable, separator is :")
var fUsage = flag.String("usage", "",
"print usage for a plugin, ie, 'telegraf --usage mysql'")
var fService = flag.String("service", "",
"operate on the service (windows only)")
var fServiceName = flag.String("service-name", "telegraf", "service name (windows only)")
Expand Down Expand Up @@ -181,17 +179,17 @@ func reloadLoop(
// The "config-translator" program populates that file.
func loadEnvironmentVariables(path string) error {
if path == "" {
return fmt.Errorf("No env config file specified")
return fmt.Errorf("no env config file specified")
}

bytes, err := os.ReadFile(path)
if err != nil {
return fmt.Errorf("Can't read env config file %s due to: %s", path, err.Error())
return fmt.Errorf("cannot read env config file %s due to: %s", path, err.Error())
}
envVars := map[string]string{}
err = json.Unmarshal(bytes, &envVars)
if err != nil {
return fmt.Errorf("Can't create env config due to: %s", err.Error())
return fmt.Errorf("cannot create env config due to: %s", err.Error())
}

for key, val := range envVars {
Expand All @@ -203,7 +201,7 @@ func loadEnvironmentVariables(path string) error {

func getEnvConfigPath(configPath, envConfigPath string) (string, error) {
if configPath == "" {
return "", fmt.Errorf("No config file specified")
return "", fmt.Errorf("no config file specified")
}
//load the environment variables that's saved in json env config file
if envConfigPath == "" {
Expand Down Expand Up @@ -437,6 +435,9 @@ func main() {
parts := strings.Split(pprofHostPort, ":")
if len(parts) == 2 && parts[0] == "" {
pprofHostPort = fmt.Sprintf("localhost:%s", parts[1])
} else if parts[0] != "localhost" {
log.Printf("W! Not starting pprof, it is restricted to localhost:nnnn")
return
}
pprofHostPort = "http://" + pprofHostPort + "/debug/pprof"

Expand Down Expand Up @@ -516,6 +517,9 @@ func main() {
}
envVars[parts[0]] = parts[1]
bytes, err = json.MarshalIndent(envVars, "", "\t")
if err != nil {
log.Fatalf("E! Failed to marshal env config: %v", err)
}
if err = os.WriteFile(*fEnvConfig, bytes, 0644); err != nil {
log.Fatalf("E! Failed to update env config: %v", err)
}
Expand Down

0 comments on commit 62d92bc

Please sign in to comment.