From 3ebc896169751dc5f284e1e68d2684936d6725be Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Fri, 1 Dec 2023 18:54:46 +0100 Subject: [PATCH] [#154] Improve UX for rejected / allowed files Signed-off-by: Knut Ahlers --- cmd/ots-cli/cmd_create.go | 2 ++ cmd/ots-cli/cmd_fetch.go | 4 +++- pkg/client/sanity.go | 6 +++++- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/cmd/ots-cli/cmd_create.go b/cmd/ots-cli/cmd_create.go index 96350c4..cefb35e 100644 --- a/cmd/ots-cli/cmd_create.go +++ b/cmd/ots-cli/cmd_create.go @@ -45,6 +45,8 @@ func init() { } func createRunE(cmd *cobra.Command, _ []string) (err error) { + cmd.SilenceUsage = true + var secret client.Secret if client.HTTPClient, err = constructHTTPClient(cmd); err != nil { diff --git a/cmd/ots-cli/cmd_fetch.go b/cmd/ots-cli/cmd_fetch.go index ce09228..d777d92 100644 --- a/cmd/ots-cli/cmd_fetch.go +++ b/cmd/ots-cli/cmd_fetch.go @@ -16,7 +16,7 @@ import ( const storeFileMode = 0o600 // We assume the attached file to be a secret var fetchCmd = &cobra.Command{ - Use: "fetch url", + Use: "fetch ", Short: "Retrieves a secret from the instance by its URL", Long: "", Args: cobra.ExactArgs(1), @@ -39,6 +39,8 @@ func checkDirWritable(dir string) error { } func fetchRunE(cmd *cobra.Command, args []string) error { + cmd.SilenceUsage = true + fileDir, err := cmd.Flags().GetString("file-dir") if err != nil { return fmt.Errorf("getting file-dir parameter: %w", err) diff --git a/pkg/client/sanity.go b/pkg/client/sanity.go index 0cfa717..047691f 100644 --- a/pkg/client/sanity.go +++ b/pkg/client/sanity.go @@ -73,12 +73,15 @@ func SanityCheck(instanceURL string, secret Secret) error { func attachmentAllowed(file SecretAttachment, allowed []string) bool { mimeType, _, _ := strings.Cut(file.Type, ";") + logger := Logger.WithField("content-type", mimeType) + for _, a := range allowed { switch { case mimeRegex.MatchString(a): // That's a mime type if glob.Glob(a, mimeType) { // The mime "glob" matches the file type + logger.WithField("allowed_by", a).Debug("attachment allowed") return true } @@ -86,12 +89,13 @@ func attachmentAllowed(file SecretAttachment, allowed []string) bool { // That's a file extension if strings.HasSuffix(file.Name, a) { // The filename has the right extension + logger.WithField("allowed_by", a).Debug("attachment allowed") return true } } } - Logger.WithField("content-type", mimeType).Debug("attachment type not allowed") + logger.Debug("attachment type not allowed") return false }