From 2c56ec2bf154cd7241e92b6e8fd851b0f9c908f0 Mon Sep 17 00:00:00 2001 From: Filip Barl Date: Thu, 21 Feb 2019 16:46:17 +0100 Subject: [PATCH] Made censoring work properly. --- render/detailed/censor.go | 33 +++++++++++++++++++++++++++++++-- report/censor.go | 12 ++++++------ 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/render/detailed/censor.go b/render/detailed/censor.go index a36bca95e..6a90c7a78 100644 --- a/render/detailed/censor.go +++ b/render/detailed/censor.go @@ -4,12 +4,41 @@ import ( "github.com/weaveworks/scope/report" ) +func isCommand(key string) bool { + return key == report.Cmdline || key == report.DockerContainerCommand +} + +func censorNodeSummary(s *NodeSummary, cfg report.CensorConfig) { + if cfg.HideEnvironmentVariables { + tables := []report.Table{} + for _, t := range s.Tables { + if t.ID != report.DockerEnvPrefix { + tables = append(tables, t) + } + } + s.Tables = tables + } + if cfg.HideCommandLineArguments { + for r := range s.Metadata { + if isCommand(s.Metadata[r].ID) { + s.Metadata[r].Value = report.StripCommandArgs(s.Metadata[r].Value) + } + } + } +} + // CensorNode ... func CensorNode(n Node, cfg report.CensorConfig) Node { + censorNodeSummary(&n.NodeSummary, cfg) return n } // CensorNodeSummaries ... -func CensorNodeSummaries(n NodeSummaries, cfg report.CensorConfig) NodeSummaries { - return n +func CensorNodeSummaries(ns NodeSummaries, cfg report.CensorConfig) NodeSummaries { + for key := range ns { + n := ns[key] + censorNodeSummary(&n, cfg) + ns[key] = n + } + return ns } diff --git a/report/censor.go b/report/censor.go index 913ddf1c9..aced7710a 100644 --- a/report/censor.go +++ b/report/censor.go @@ -36,15 +36,15 @@ func censorTopology(t *Topology, match keyMatcher, censor censorValueFunc) { // CensorConfig describes how probe reports should // be censored when rendered through the API. type CensorConfig struct { - hideCommandLineArguments bool - hideEnvironmentVariables bool + HideCommandLineArguments bool + HideEnvironmentVariables bool } // GetCensorConfigFromQueryParams extracts censor config from request query params. func GetCensorConfigFromQueryParams(req *http.Request) CensorConfig { return CensorConfig{ - hideCommandLineArguments: true || req.URL.Query().Get("hideCommandLineArguments") == "true", - hideEnvironmentVariables: true || req.URL.Query().Get("hideEnvironmentVariables") == "true", + HideCommandLineArguments: true || req.URL.Query().Get("hideCommandLineArguments") == "true", + HideEnvironmentVariables: true || req.URL.Query().Get("hideEnvironmentVariables") == "true", } } @@ -54,11 +54,11 @@ func CensorRawReport(r Report, cfg CensorConfig) Report { var ( makeEmpty = func(string) string { return "" } ) - if cfg.hideCommandLineArguments { + if cfg.HideCommandLineArguments { censorTopology(&r.Process, keyEquals(Cmdline), StripCommandArgs) censorTopology(&r.Container, keyEquals(DockerContainerCommand), StripCommandArgs) } - if cfg.hideEnvironmentVariables { + if cfg.HideEnvironmentVariables { censorTopology(&r.Container, keyStartsWith(DockerEnvPrefix), makeEmpty) } return r