Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add warnings to the customer facing Boundary API. #3347

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions api/action_warning.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions api/behavior_warning.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions api/field_warning.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions api/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@ func (r *Response) StatusCode() int {
return r.resp.StatusCode
}

func (r *Response) Warnings() ([]*Warning, error) {
ws := r.resp.Header.Get("X-Boundary-Warnings")
if ws == "" {
return nil, nil
}

var ret WarningResponse
if err := json.Unmarshal([]byte(ws), &ret); err != nil {
return nil, fmt.Errorf("when unmarshaling warning %q: %w", ws, err)
}

return ret.Warnings, nil
}

func (r *Response) Decode(inStruct any) (*Error, error) {
if r == nil || r.resp == nil {
return nil, fmt.Errorf("nil response, cannot decode")
Expand Down
12 changes: 12 additions & 0 deletions api/warning.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions api/warning_response.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions internal/api/genapi/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,31 @@ type structInfo struct {
}

var inputStructs = []*structInfo{
{
inProto: &api.WarningResponse{},
outFile: "warning_response.gen.go",
skipOptions: true,
},
{
inProto: &api.Warning{},
outFile: "warning.gen.go",
skipOptions: true,
},
{
inProto: &api.FieldWarning{},
outFile: "field_warning.gen.go",
skipOptions: true,
},
{
inProto: &api.BehaviorWarning{},
outFile: "behavior_warning.gen.go",
skipOptions: true,
},
{
inProto: &api.ActionWarning{},
outFile: "action_warning.gen.go",
skipOptions: true,
},
{
inProto: &api.Error{},
outFile: "error.gen.go",
Expand Down
52 changes: 44 additions & 8 deletions internal/cmd/base/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,25 @@ func (c *Command) PrintApiError(in *api.Error, contextStr string, opt ...Option)
}
}

func (c *Command) PrintWarning(w *api.Warning) {
if w == nil {
return
}
switch Format(c.UI) {
case "table":
var descStr string
switch {
case w.Action != nil:
descStr = fmt.Sprintf("%s: %s", w.Action.Name, w.Action.Warning)
case w.Behavior != nil:
descStr = w.Behavior.Warning
case w.RequestField != nil:
descStr = fmt.Sprintf("%s: %s", w.RequestField.Name, w.RequestField.Warning)
}
c.UI.Warn(fmt.Sprintf("Code %d: %s", w.Code, descStr))
}
}

// PrintCliError prints the given CLI error to the UI in the appropriate format
func (c *Command) PrintCliError(err error) {
switch Format(c.UI) {
Expand All @@ -302,6 +321,14 @@ func (c *Command) PrintJsonItem(resp *api.Response, opt ...Option) bool {
c.PrintCliError(errors.New("Error formatting as JSON: no response given to item formatter"))
return false
}
ws, err := resp.Warnings()
if err != nil {
c.PrintCliError(fmt.Errorf("Error getting warnings: %w", err))
}
if len(ws) > 0 {
opt = append(opt, WithApiWarning(ws))
}

if r := resp.HttpResponse(); r != nil {
opt = append(opt, WithStatusCode(r.StatusCode))
}
Expand All @@ -312,11 +339,13 @@ func (c *Command) PrintJsonItem(resp *api.Response, opt ...Option) bool {
func (c *Command) PrintJson(input json.RawMessage, opt ...Option) bool {
opts := getOpts(opt...)
output := struct {
StatusCode int `json:"status_code,omitempty"`
Item json.RawMessage `json:"item,omitempty"`
StatusCode int `json:"status_code,omitempty"`
Item json.RawMessage `json:"item,omitempty"`
ApiWarnings []*api.Warning `json:"api_warnings,omitempty"`
}{
StatusCode: opts.withStatusCode,
Item: input,
StatusCode: opts.withStatusCode,
ApiWarnings: opts.withApiWarnings,
Item: input,
}
b, err := JsonFormatter{}.Format(output)
if err != nil {
Expand Down Expand Up @@ -347,12 +376,19 @@ func (c *Command) PrintJsonItems(resp *api.Response) bool {
return false
}
}
ws, err := resp.Warnings()
if err != nil {
c.PrintCliError(fmt.Errorf("Error getting warnings: %w", err))
return false
}
output := struct {
StatusCode int `json:"status_code"`
Items json.RawMessage `json:"items"`
StatusCode int `json:"status_code"`
Items json.RawMessage `json:"items"`
ApiWarnings []*api.Warning `json:"api_warnings"`
}{
StatusCode: resp.HttpResponse().StatusCode,
Items: input.Items,
StatusCode: resp.HttpResponse().StatusCode,
Items: input.Items,
ApiWarnings: ws,
}
b, err := JsonFormatter{}.Format(output)
if err != nil {
Expand Down
9 changes: 9 additions & 0 deletions internal/cmd/base/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package base

import (
"github.com/hashicorp/boundary/api"
"github.com/hashicorp/boundary/internal/event"
"github.com/hashicorp/boundary/sdk/pbs/plugin"
wrapping "github.com/hashicorp/go-kms-wrapping/v2"
Expand Down Expand Up @@ -42,6 +43,7 @@ type Options struct {
withEventWrapper wrapping.Wrapper
withAttributeFieldPrefix string
withStatusCode int
withApiWarnings []*api.Warning
withHostPlugin func() (string, plugin.HostPluginServiceClient)
withEventGating bool
}
Expand Down Expand Up @@ -169,6 +171,13 @@ func WithAttributeFieldPrefix(p string) Option {
}
}

// WithApiWarning allows passing api warnings to functions
func WithApiWarning(warnings []*api.Warning) Option {
return func(o *Options) {
o.withApiWarnings = warnings
}
}

// WithStatusCode allows passing status codes to functions
func WithStatusCode(statusCode int) Option {
return func(o *Options) {
Expand Down
21 changes: 21 additions & 0 deletions internal/cmd/commands/accountscmd/accounts.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions internal/cmd/commands/accountscmd/ldap_accounts.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions internal/cmd/commands/accountscmd/oidc_accounts.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions internal/cmd/commands/accountscmd/password_accounts.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions internal/cmd/commands/authenticate/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ func saveAndOrPrintToken(c *base.Command, result *authmethods.AuthenticateResult

switch base.Format(c.UI) {
case "table":
warnings, err := result.GetResponse().Warnings()
if err != nil {
c.PrintCliError(fmt.Errorf("Error getting warnings: %w", err))
}
for _, w := range warnings {
c.PrintWarning(w)
}

c.UI.Output(base.WrapForHelpText([]string{
"",
"Authentication information:",
Expand Down
21 changes: 21 additions & 0 deletions internal/cmd/commands/authmethodscmd/authmethods.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading