-
Notifications
You must be signed in to change notification settings - Fork 712
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conditional report censoring #3571
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
97fdcdc
Option to censor raw reports by command line args and env vars.
fbarl 3c5320e
Polished the code and applied censoring to other API endpoints.
fbarl 0f1b7e5
Prepare to filter node summaries post-render.
fbarl 2c56ec2
Made censoring work properly.
fbarl c5022bd
Code cleanup.
fbarl 353ab75
Added some tests for censoring.
fbarl b9e692c
Lock the time in tests to make them pass.
fbarl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package detailed | ||
|
||
import ( | ||
"github.com/weaveworks/scope/report" | ||
) | ||
|
||
func censorNodeSummary(s NodeSummary, cfg report.CensorConfig) NodeSummary { | ||
if cfg.HideCommandLineArguments && s.Metadata != nil { | ||
// Iterate through all the metadata rows and strip the | ||
// arguments from all the values containing a command | ||
// (while making sure everything is done in a non-mutable way). | ||
metadata := []report.MetadataRow{} | ||
for _, row := range s.Metadata { | ||
if report.IsCommandEntry(row.ID) { | ||
row.Value = report.StripCommandArgs(row.Value) | ||
} | ||
metadata = append(metadata, row) | ||
} | ||
s.Metadata = metadata | ||
} | ||
if cfg.HideEnvironmentVariables && s.Tables != nil { | ||
// Copy across all the tables except the environment | ||
// variable ones (ensuring the operation is non-mutable). | ||
tables := []report.Table{} | ||
for _, table := range s.Tables { | ||
if !report.IsEnvironmentVarsEntry(table.ID) { | ||
tables = append(tables, table) | ||
} | ||
} | ||
s.Tables = tables | ||
} | ||
return s | ||
} | ||
|
||
// CensorNode removes any sensitive data from a node. | ||
func CensorNode(node Node, cfg report.CensorConfig) Node { | ||
node.NodeSummary = censorNodeSummary(node.NodeSummary, cfg) | ||
return node | ||
} | ||
|
||
// CensorNodeSummaries removes any sensitive data from a list of node summaries. | ||
func CensorNodeSummaries(summaries NodeSummaries, cfg report.CensorConfig) NodeSummaries { | ||
censored := NodeSummaries{} | ||
for key := range summaries { | ||
censored[key] = censorNodeSummary(summaries[key], cfg) | ||
} | ||
return censored | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,228 @@ | ||
package detailed_test | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/weaveworks/common/test" | ||
"github.com/weaveworks/scope/render/detailed" | ||
"github.com/weaveworks/scope/report" | ||
) | ||
|
||
func TestCensorNode(t *testing.T) { | ||
node := detailed.Node{ | ||
NodeSummary: detailed.NodeSummary{ | ||
Metadata: []report.MetadataRow{ | ||
{ID: "cmdline", Label: "Command", Value: "prog -a --b=c"}, | ||
}, | ||
Tables: []report.Table{ | ||
{ID: "blibli", Rows: []report.Row{{ID: "bli"}}}, | ||
{ID: "docker_env_", Rows: []report.Row{{ID: "env_var"}}}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, c := range []struct { | ||
label string | ||
have, want detailed.Node | ||
}{ | ||
{ | ||
label: "no censoring", | ||
have: detailed.CensorNode(node, report.CensorConfig{ | ||
HideCommandLineArguments: false, | ||
HideEnvironmentVariables: false, | ||
}), | ||
want: detailed.Node{ | ||
NodeSummary: detailed.NodeSummary{ | ||
Metadata: []report.MetadataRow{ | ||
{ID: "cmdline", Label: "Command", Value: "prog -a --b=c"}, | ||
}, | ||
Tables: []report.Table{ | ||
{ID: "blibli", Rows: []report.Row{{ID: "bli"}}}, | ||
{ID: "docker_env_", Rows: []report.Row{{ID: "env_var"}}}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
label: "censor only command line args", | ||
have: detailed.CensorNode(node, report.CensorConfig{ | ||
HideCommandLineArguments: true, | ||
HideEnvironmentVariables: false, | ||
}), | ||
want: detailed.Node{ | ||
NodeSummary: detailed.NodeSummary{ | ||
Metadata: []report.MetadataRow{ | ||
{ID: "cmdline", Label: "Command", Value: "prog"}, | ||
}, | ||
Tables: []report.Table{ | ||
{ID: "blibli", Rows: []report.Row{{ID: "bli"}}}, | ||
{ID: "docker_env_", Rows: []report.Row{{ID: "env_var"}}}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
label: "censor only env variables", | ||
have: detailed.CensorNode(node, report.CensorConfig{ | ||
HideCommandLineArguments: false, | ||
HideEnvironmentVariables: true, | ||
}), | ||
want: detailed.Node{ | ||
NodeSummary: detailed.NodeSummary{ | ||
Metadata: []report.MetadataRow{ | ||
{ID: "cmdline", Label: "Command", Value: "prog -a --b=c"}, | ||
}, | ||
Tables: []report.Table{ | ||
{ID: "blibli", Rows: []report.Row{{ID: "bli"}}}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
label: "censor both command line args and env vars", | ||
have: detailed.CensorNode(node, report.CensorConfig{ | ||
HideCommandLineArguments: true, | ||
HideEnvironmentVariables: true, | ||
}), | ||
want: detailed.Node{ | ||
NodeSummary: detailed.NodeSummary{ | ||
Metadata: []report.MetadataRow{ | ||
{ID: "cmdline", Label: "Command", Value: "prog"}, | ||
}, | ||
Tables: []report.Table{ | ||
{ID: "blibli", Rows: []report.Row{{ID: "bli"}}}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} { | ||
if !reflect.DeepEqual(c.want, c.have) { | ||
t.Errorf("%s - %s", c.label, test.Diff(c.want, c.have)) | ||
} | ||
} | ||
} | ||
|
||
func TestCensorNodeSummaries(t *testing.T) { | ||
summaries := detailed.NodeSummaries{ | ||
"a": detailed.NodeSummary{ | ||
Metadata: []report.MetadataRow{ | ||
{ID: "blublu", Label: "blabla", Value: "blu blu"}, | ||
{ID: "docker_container_command", Label: "Command", Value: "scope --token=blibli"}, | ||
}, | ||
}, | ||
"b": detailed.NodeSummary{ | ||
Metadata: []report.MetadataRow{ | ||
{ID: "cmdline", Label: "Command", Value: "prog -a --b=c"}, | ||
}, | ||
Tables: []report.Table{ | ||
{ID: "blibli", Rows: []report.Row{{ID: "bli"}}}, | ||
{ID: "docker_env_", Rows: []report.Row{{ID: "env_var"}}}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, c := range []struct { | ||
label string | ||
have, want detailed.NodeSummaries | ||
}{ | ||
{ | ||
label: "no censoring", | ||
have: detailed.CensorNodeSummaries(summaries, report.CensorConfig{ | ||
HideCommandLineArguments: false, | ||
HideEnvironmentVariables: false, | ||
}), | ||
want: detailed.NodeSummaries{ | ||
"a": detailed.NodeSummary{ | ||
Metadata: []report.MetadataRow{ | ||
{ID: "blublu", Label: "blabla", Value: "blu blu"}, | ||
{ID: "docker_container_command", Label: "Command", Value: "scope --token=blibli"}, | ||
}, | ||
}, | ||
"b": detailed.NodeSummary{ | ||
Metadata: []report.MetadataRow{ | ||
{ID: "cmdline", Label: "Command", Value: "prog -a --b=c"}, | ||
}, | ||
Tables: []report.Table{ | ||
{ID: "blibli", Rows: []report.Row{{ID: "bli"}}}, | ||
{ID: "docker_env_", Rows: []report.Row{{ID: "env_var"}}}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
label: "censor only command line args", | ||
have: detailed.CensorNodeSummaries(summaries, report.CensorConfig{ | ||
HideCommandLineArguments: true, | ||
HideEnvironmentVariables: false, | ||
}), | ||
want: detailed.NodeSummaries{ | ||
"a": detailed.NodeSummary{ | ||
Metadata: []report.MetadataRow{ | ||
{ID: "blublu", Label: "blabla", Value: "blu blu"}, | ||
{ID: "docker_container_command", Label: "Command", Value: "scope"}, | ||
}, | ||
}, | ||
"b": detailed.NodeSummary{ | ||
Metadata: []report.MetadataRow{ | ||
{ID: "cmdline", Label: "Command", Value: "prog"}, | ||
}, | ||
Tables: []report.Table{ | ||
{ID: "blibli", Rows: []report.Row{{ID: "bli"}}}, | ||
{ID: "docker_env_", Rows: []report.Row{{ID: "env_var"}}}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
label: "censor only env variables", | ||
have: detailed.CensorNodeSummaries(summaries, report.CensorConfig{ | ||
HideCommandLineArguments: false, | ||
HideEnvironmentVariables: true, | ||
}), | ||
want: detailed.NodeSummaries{ | ||
"a": detailed.NodeSummary{ | ||
Metadata: []report.MetadataRow{ | ||
{ID: "blublu", Label: "blabla", Value: "blu blu"}, | ||
{ID: "docker_container_command", Label: "Command", Value: "scope --token=blibli"}, | ||
}, | ||
}, | ||
"b": detailed.NodeSummary{ | ||
Metadata: []report.MetadataRow{ | ||
{ID: "cmdline", Label: "Command", Value: "prog -a --b=c"}, | ||
}, | ||
Tables: []report.Table{ | ||
{ID: "blibli", Rows: []report.Row{{ID: "bli"}}}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
label: "censor both command line args and env vars", | ||
have: detailed.CensorNodeSummaries(summaries, report.CensorConfig{ | ||
HideCommandLineArguments: true, | ||
HideEnvironmentVariables: true, | ||
}), | ||
want: detailed.NodeSummaries{ | ||
"a": detailed.NodeSummary{ | ||
Metadata: []report.MetadataRow{ | ||
{ID: "blublu", Label: "blabla", Value: "blu blu"}, | ||
{ID: "docker_container_command", Label: "Command", Value: "scope"}, | ||
}, | ||
}, | ||
"b": detailed.NodeSummary{ | ||
Metadata: []report.MetadataRow{ | ||
{ID: "cmdline", Label: "Command", Value: "prog"}, | ||
}, | ||
Tables: []report.Table{ | ||
{ID: "blibli", Rows: []report.Row{{ID: "bli"}}}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} { | ||
if !reflect.DeepEqual(c.want, c.have) { | ||
t.Errorf("%s - %s", c.label, test.Diff(c.want, c.have)) | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this mutating the existing row?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It shouldn't since:
range
in the loop above returns a copy ofrow
and we're changing that copy.