From f1cccbe2f958886eaf53dba7a6ec8c75b622e30c Mon Sep 17 00:00:00 2001 From: Laura Brehm Date: Tue, 22 Aug 2023 10:19:10 +0100 Subject: [PATCH] scout hint: tell user logging in is required The `docker scout quickview` hint, displayed after a user pulls or builds an image, only works if the user is logged in to Hub. Check if user isn't logged in, and make hint more explicit in this case. Signed-off-by: Laura Brehm --- cli/mobycli/scout_suggest.go | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/cli/mobycli/scout_suggest.go b/cli/mobycli/scout_suggest.go index b6f32eb07..2eba13abd 100644 --- a/cli/mobycli/scout_suggest.go +++ b/cli/mobycli/scout_suggest.go @@ -21,6 +21,7 @@ import ( "os" "strings" + "github.com/docker/cli/cli/config" "github.com/docker/compose/v2/pkg/utils" "github.com/fatih/color" @@ -53,10 +54,14 @@ func displayScoutQuickViewSuggestMsg(image string) { } out := os.Stderr b := color.New(color.Bold) - _, _ = fmt.Fprintln(out) - _, _ = b.Fprintln(out, "What's Next?") - _, _ = fmt.Fprintf(out, " View summary of image vulnerabilities and recommendations → %s", color.CyanString("docker scout quickview%s", image)) - _, _ = fmt.Fprintln(out) + + _, _ = b.Fprintln(out, "\nWhat's Next?") + if !hubLoggedIn() { + _, _ = fmt.Fprintf(out, " View summary of image vulnerabilities and recommendations (login required) → %s\n", color.CyanString("docker scout quickview%s", image)) + _, _ = fmt.Fprint(out, " To sign in, use the `docker login` command or sign in via Docker Desktop\n") + } else { + _, _ = fmt.Fprintf(out, " View summary of image vulnerabilities and recommendations → %s\n", color.CyanString("docker scout quickview%s", image)) + } } func pulledImageFromArgs(args []string) string { @@ -74,3 +79,17 @@ func pulledImageFromArgs(args []string) string { } return image } + +// hubLoggedIn checks whether the user has credentials configured +// for Docker Hub. This can be an expensive operation, so use it +// mindfully. +func hubLoggedIn() bool { + // todo: run this with a timeout, this call can be slow (esp. on Windows) + // and we don't want to hang execution here while we check + hubAuth, err := config.LoadDefaultConfigFile(nil).GetAuthConfig("index.docker.io") + if err != nil { + // preserve original behaviour if we fail to fetch creds + return true + } + return hubAuth.Username != "" +}