From 942c55cf517996dea0c3a5c46644941c71ee1148 Mon Sep 17 00:00:00 2001 From: Monis Khan Date: Fri, 13 Aug 2021 20:19:32 -0400 Subject: [PATCH] cli: prevent browser output from breaking ExecCredential output This change updates the pinniped CLI entrypoint to prevent browser processes that we spawn from polluting our std out stream. For example, chrome will print the following message to std out: Opening in existing browser session. Which leads to the following incomprehensible error message from kubectl: Unable to connect to the server: getting credentials: decoding stdout: couldn't get version/kind; json parse error: json: cannot unmarshal string into Go value of type struct { APIVersion string "json:\"apiVersion,omitempty\""; Kind string "json:\"kind,omitempty\"" } This would only occur on the initial login when we opened the browser. Since credentials would be cached afterwards, kubectl would work as expected for future invocations as no browser was opened. I could not think of a good way to actually test this change. There is a clear gap in our integration tests - we never actually launch a browser in the exact same way a user does - we instead open a chrome driver at the login URL as a subprocess of the integration test binary and not the pinniped CLI. Thus even if the chrome driver was writing to std out, we would not notice any issues. It is also unclear if there is a good way to prevent future related bugs since std out is global to the process. Signed-off-by: Monis Khan --- cmd/pinniped/main.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/cmd/pinniped/main.go b/cmd/pinniped/main.go index 96213ec8..6b5a4611 100644 --- a/cmd/pinniped/main.go +++ b/cmd/pinniped/main.go @@ -3,7 +3,20 @@ package main -import "go.pinniped.dev/cmd/pinniped/cmd" +import ( + "os" + + "github.com/pkg/browser" + + "go.pinniped.dev/cmd/pinniped/cmd" +) + +//nolint: gochecknoinits +func init() { + // browsers like chrome like to write to our std out which breaks our JSON ExecCredential output + // thus we redirect the browser's std out to our std err + browser.Stdout = os.Stderr +} func main() { cmd.Execute()