From f05f37d0fa4167d6a723795f81469994159e516a Mon Sep 17 00:00:00 2001 From: Jeff Mitchell Date: Fri, 9 Apr 2021 12:09:51 -0500 Subject: [PATCH] Fix panic in non-generated API calls when printing as JSON (#1095) Fixes #992 --- CHANGELOG.md | 3 +++ api/accounts/changepassword.go | 3 +-- api/accounts/setpassword.go | 2 +- api/authmethods/authenticate.go | 1 - api/authmethods/changestate.go | 3 +-- api/targets/custom.go | 9 +++++---- internal/cmd/base/format.go | 12 ++++++++---- internal/cmd/commands/targetscmd/funcs.go | 6 ++---- 8 files changed, 21 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 193b744d6..6c59ba92b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -47,6 +47,9 @@ Canonical reference for changes, improvements, and bugfixes for Boundary. * server: Roles for auto generated scopes are now generated at database init. [PR](https://github.com/hashicorp/boundary/pull/996) +* cli: Don't panic on certain commands when outputting in `json` format + ([Issue](https://github.com/hashicorp/boundary/pull/992), + [PR](https://github.com/hashicorp/boundary/pull/1095)) ## 0.1.8 (2021/03/10) diff --git a/api/accounts/changepassword.go b/api/accounts/changepassword.go index 467925ced..8066dae78 100644 --- a/api/accounts/changepassword.go +++ b/api/accounts/changepassword.go @@ -57,10 +57,9 @@ func (c *Client) ChangePassword(ctx context.Context, accountId, currentPassword, if err != nil { return nil, fmt.Errorf("error decoding ChangePassword response: %w", err) } - if apiErr != nil { return nil, apiErr } - + target.response = resp return target, nil } diff --git a/api/accounts/setpassword.go b/api/accounts/setpassword.go index e9f6557ff..f12a61f66 100644 --- a/api/accounts/setpassword.go +++ b/api/accounts/setpassword.go @@ -59,6 +59,6 @@ func (c *Client) SetPassword(ctx context.Context, accountId, password string, ve if apiErr != nil { return nil, apiErr } - + target.response = resp return target, nil } diff --git a/api/authmethods/authenticate.go b/api/authmethods/authenticate.go index c586a6f26..e76555131 100644 --- a/api/authmethods/authenticate.go +++ b/api/authmethods/authenticate.go @@ -96,6 +96,5 @@ func (c *Client) Authenticate(ctx context.Context, authMethodId, command string, return nil, apiErr } target.response = resp - return target, nil } diff --git a/api/authmethods/changestate.go b/api/authmethods/changestate.go index dbfdac467..3cc80946f 100644 --- a/api/authmethods/changestate.go +++ b/api/authmethods/changestate.go @@ -59,10 +59,9 @@ func (c *Client) ChangeState(ctx context.Context, authMethodId string, version u if err != nil { return nil, fmt.Errorf("error decoding ChangeState response: %w", err) } - if apiErr != nil { return nil, apiErr } - + target.response = resp return target, nil } diff --git a/api/targets/custom.go b/api/targets/custom.go index 30d59ab85..e6ee01fe9 100644 --- a/api/targets/custom.go +++ b/api/targets/custom.go @@ -63,14 +63,15 @@ func (c *Client) AuthorizeSession(ctx context.Context, targetId string, opt ...O return nil, fmt.Errorf("error performing client request during AuthorizeSession call: %w", err) } - sar := new(SessionAuthorizationResult) - sar.Item = new(SessionAuthorization) - apiErr, err := resp.Decode(sar.Item) + target := new(SessionAuthorizationResult) + target.Item = new(SessionAuthorization) + apiErr, err := resp.Decode(target.Item) if err != nil { return nil, fmt.Errorf("error decoding AuthorizeSession response: %w", err) } if apiErr != nil { return nil, apiErr } - return sar, nil + target.response = resp + return target, nil } diff --git a/internal/cmd/base/format.go b/internal/cmd/base/format.go index 77b4d07c0..d520b5a1a 100644 --- a/internal/cmd/base/format.go +++ b/internal/cmd/base/format.go @@ -234,8 +234,10 @@ func (c *Command) PrintJsonItem(result api.GenericResult, item interface{}) bool StatusCode int `json:"status_code"` Item interface{} `json:"item"` }{ - StatusCode: result.GetResponse().HttpResponse().StatusCode, - Item: item, + Item: item, + } + if result.GetResponse() != nil && result.GetResponse().HttpResponse() != nil { + output.StatusCode = result.GetResponse().HttpResponse().StatusCode } b, err := JsonFormatter{}.Format(output) if err != nil { @@ -252,8 +254,10 @@ func (c *Command) PrintJsonItems(result api.GenericListResult, items []interface StatusCode int `json:"status_code"` Items []interface{} `json:"items"` }{ - StatusCode: result.GetResponse().HttpResponse().StatusCode, - Items: items, + Items: items, + } + if result.GetResponse() != nil && result.GetResponse().HttpResponse() != nil { + output.StatusCode = result.GetResponse().HttpResponse().StatusCode } b, err := JsonFormatter{}.Format(output) if err != nil { diff --git a/internal/cmd/commands/targetscmd/funcs.go b/internal/cmd/commands/targetscmd/funcs.go index 7ed2df69d..34af0aeb6 100644 --- a/internal/cmd/commands/targetscmd/funcs.go +++ b/internal/cmd/commands/targetscmd/funcs.go @@ -444,11 +444,9 @@ func printCustomActionOutputImpl(c *Command) (bool, error) { return true, nil case "json": - b, err := base.JsonFormatter{}.Format(item) - if err != nil { - return false, fmt.Errorf("Error formatting as JSON: %w", err) + if ok := c.PrintJsonItem(c.sar, item); !ok { + return false, fmt.Errorf("Error formatting as JSON") } - c.UI.Output(string(b)) return true, nil } }