From d24de97cdef666cc6752d5ed64a26f529baadd7e Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Wed, 23 Sep 2020 08:32:57 -0700 Subject: [PATCH] pretty print KMS key status (#3412) --- cmd/admin-console.go | 4 ++- cmd/admin-kms-key-status.go | 66 +++++++++++++++++++++++++------------ cmd/admin-trace.go | 6 +++- 3 files changed, 53 insertions(+), 23 deletions(-) diff --git a/cmd/admin-console.go b/cmd/admin-console.go index dfaa3e6e..255abfbf 100644 --- a/cmd/admin-console.go +++ b/cmd/admin-console.go @@ -81,11 +81,13 @@ func checkAdminLogSyntax(ctx *cli.Context) { // Extend madmin.LogInfo to add String() and JSON() methods type logMessage struct { + Status string `json:"status"` madmin.LogInfo } // JSON - jsonify loginfo func (l logMessage) JSON() string { + l.Status = "success" logJSON, err := json.MarshalIndent(&l, "", " ") fatalIf(probe.NewError(err), "Unable to marshal into JSON.") @@ -209,7 +211,7 @@ func mainAdminConsole(ctx *cli.Context) error { if node != "" { logInfo.NodeName = "" } - printMsg(logMessage{logInfo}) + printMsg(logMessage{LogInfo: logInfo}) } return nil } diff --git a/cmd/admin-kms-key-status.go b/cmd/admin-kms-key-status.go index c02ec29e..bb2415fc 100644 --- a/cmd/admin-kms-key-status.go +++ b/cmd/admin-kms-key-status.go @@ -19,9 +19,11 @@ package cmd import ( "fmt" + "github.com/fatih/color" "github.com/minio/cli" + json "github.com/minio/mc/pkg/colorjson" "github.com/minio/mc/pkg/probe" - "github.com/minio/minio/pkg/madmin" + "github.com/minio/minio/pkg/console" ) var adminKMSKeyStatusCmd = cli.Command{ @@ -43,7 +45,7 @@ EXAMPLES: 1. Get default master key and its status from a MinIO server/cluster. $ {{.HelpName}} play 2. Get the status of one particular master key from a MinIO server/cluster. - $ {{.HelpName}} play my-master-key + $ {{.HelpName}} play my-master-key `, } @@ -53,6 +55,9 @@ func mainAdminKMSKeyStatus(ctx *cli.Context) error { cli.ShowCommandHelpAndExit(ctx, "status", 1) // last argument is exit code } + console.SetColor("StatusSuccess", color.New(color.FgGreen, color.Bold)) + console.SetColor("StatusError", color.New(color.FgRed, color.Bold)) + client, err := newAdminClient(ctx.Args().Get(0)) fatalIf(err, "Unable to get a configured admin connection.") @@ -63,29 +68,48 @@ func mainAdminKMSKeyStatus(ctx *cli.Context) error { status, e := client.GetKeyStatus(globalContext, keyID) fatalIf(probe.NewError(e), "Failed to get status information") - printMsg(kmsKeyStatusMsg(*status)) + printMsg(kmsKeyStatusMsg{ + KeyID: status.KeyID, + Encryption: status.EncryptionErr == "", + Decryption: status.DecryptionErr == "", + EncryptionErr: status.EncryptionErr, + DecryptionErr: status.DecryptionErr, + }) return nil } -type kmsKeyStatusMsg madmin.KMSKeyStatus - -func (s kmsKeyStatusMsg) String() string { - msg := fmt.Sprintf("Key: %s\n", s.KeyID) - if s.EncryptionErr == "" { - msg = fmt.Sprintf("%s %s", msg, "\t • Encryption ✔\n") - } else { - return fmt.Sprintf("%s \t • Encryption failed: %s\n", msg, s.EncryptionErr) - } - - if s.DecryptionErr == "" { - msg = fmt.Sprintf("%s %s", msg, "\t • Decryption ✔\n") - } else { - return fmt.Sprintf("%s \t • Decryption failed: %s\n", msg, s.DecryptionErr) - } - return msg +type kmsKeyStatusMsg struct { + KeyID string `json:"keyId"` + Encryption bool `json:"encryption"` + Decryption bool `json:"decryption"` + EncryptionErr string `json:"encryptionError,omitempty"` + DecryptionErr string `json:"decryptionError,omitempty"` + Status string `json:"status"` } func (s kmsKeyStatusMsg) JSON() string { - const fmtStr = `{"key-id":"%s","encryption-error":"%s","decryption-error":"%s"}` - return fmt.Sprintf(fmtStr, s.KeyID, s.EncryptionErr, s.DecryptionErr) + s.Status = "success" + if !s.Encryption && !s.Decryption { + s.Status = "error" + } + kmsBytes, e := json.MarshalIndent(s, "", " ") + fatalIf(probe.NewError(e), "Unable to marshal into JSON.") + + return string(kmsBytes) +} + +func (s kmsKeyStatusMsg) String() string { + msg := fmt.Sprintf("Key: %s\n", s.KeyID) + if s.Encryption { + msg += " - Encryption " + console.Colorize("StatusSuccess", "✔") + "\n" + } else { + msg += fmt.Sprintf(" - Encryption %s (%s)\n", console.Colorize("StatusError", "✗"), s.EncryptionErr) + } + + if s.Decryption { + msg += " - Decryption " + console.Colorize("StatusSuccess", "✔") + "\n" + } else { + msg += fmt.Sprintf(" - Decryption %s (%s)\n", console.Colorize("StatusError", "✗"), s.DecryptionErr) + } + return msg } diff --git a/cmd/admin-trace.go b/cmd/admin-trace.go index 7830f3e7..1ac186b5 100644 --- a/cmd/admin-trace.go +++ b/cmd/admin-trace.go @@ -129,7 +129,7 @@ func mainAdminTrace(ctx *cli.Context) error { fatalIf(probe.NewError(traceInfo.Err), "Unable to listen to http trace") } if verbose { - printMsg(traceMessage{traceInfo}) + printMsg(traceMessage{ServiceTraceInfo: traceInfo}) continue } printMsg(shortTrace(traceInfo)) @@ -139,6 +139,7 @@ func mainAdminTrace(ctx *cli.Context) error { // Short trace record type shortTraceMsg struct { + Status string `json:"status"` Host string `json:"host"` Time time.Time `json:"time"` Client string `json:"client"` @@ -151,6 +152,7 @@ type shortTraceMsg struct { } type traceMessage struct { + Status string `json:"status"` madmin.ServiceTraceInfo } @@ -207,6 +209,7 @@ func shortTrace(ti madmin.ServiceTraceInfo) shortTraceMsg { } func (s shortTraceMsg) JSON() string { + s.Status = "success" buf := &bytes.Buffer{} enc := json.NewEncoder(buf) enc.SetIndent("", " ") @@ -259,6 +262,7 @@ func colorizedNodeName(nodeName string) string { } func (t traceMessage) JSON() string { + t.Status = "success" rqHdrs := make(map[string]string) rspHdrs := make(map[string]string) rq := t.Trace.ReqInfo