Skip to content

Commit

Permalink
hint the user to check their privileges if they get HTTP 403
Browse files Browse the repository at this point in the history
Example output:
4 errors occurred:
        * Run 'sdpctl privileges' to see your current user privileges
        * HTTP GET https://controller.appgate.devops:8443/admin/global-settings
        * You don't have permission to access this resource.
        * Failed to determine backup option: HTTP 403 - 403 Forbidden
  • Loading branch information
dlnilsson committed Jun 14, 2023
1 parent 29dbc29 commit d7354d7
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions pkg/cmdutil/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/x509"
"errors"
"fmt"
"net/http"
"strings"

"github.com/appgate/sdpctl/pkg/api"
Expand All @@ -23,6 +24,22 @@ const (
ExitAuth ExitCode = 4
)

func privligeError(cmd *cobra.Command, err *api.Error) error {
caller := "sdpctl"
if cmd != nil {
caller = cmd.Root().Name()
}
if err.StatusCode == http.StatusForbidden {
var result *multierror.Error
result = multierror.Append(result, fmt.Errorf("Run '%s privileges' to see your current user privileges", caller))
if err.RequestURL != nil {
result = multierror.Append(result, errors.New(*err.RequestURL))
}
return result
}
return nil
}

func ExecuteCommand(cmd *cobra.Command) ExitCode {
cmd, err := cmd.ExecuteC()
if err != nil {
Expand All @@ -32,6 +49,7 @@ func ExecuteCommand(cmd *cobra.Command) ExitCode {
// if the command return a api error, (api.HTTPErrorResponse) for example HTTP 400-599, we will
// resolve each nested error and convert them to multierror to prettify it for the user in a list view.
if ae, ok := we.(*api.Error); ok {
result = multierror.Append(result, privligeError(cmd, ae))
for _, e := range ae.Errors {
result = multierror.Append(result, e)
}
Expand All @@ -48,6 +66,7 @@ func ExecuteCommand(cmd *cobra.Command) ExitCode {
}
} else {
if ae, ok := err.(*api.Error); ok {
result = multierror.Append(result, privligeError(cmd, ae))
for _, e := range ae.Errors {
result = multierror.Append(result, e)
}
Expand Down

0 comments on commit d7354d7

Please sign in to comment.