Skip to content

Commit

Permalink
Show forbidden errors for Helm 3 (#1450)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andres Martinez Gotor committed Jan 15, 2020
1 parent 1ad2e03 commit 6762ad3
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 16 deletions.
Expand Up @@ -150,6 +150,7 @@ exports[`when all the brokers are loaded shows a forbiden (resync) error if it e
<PermissionsErrorPage
action="resync Service Brokers"
namespace=""
rawMessage=""
roles={
Array [
Object {
Expand Down
1 change: 1 addition & 0 deletions dashboard/src/components/ErrorAlert/ErrorSelector.tsx
Expand Up @@ -52,6 +52,7 @@ const ErrorSelector: React.SFC<IErrorSelectorProps> = props => {
<PermissionsErrorAlert
namespace={namespace || ""}
roles={roles}
rawMessage={message}
action={`${action} ${resource || ""}`}
/>
);
Expand Down
24 changes: 20 additions & 4 deletions dashboard/src/components/ErrorAlert/PermissionsErrorAlert.test.tsx
Expand Up @@ -11,14 +11,16 @@ import { genericMessage } from "./UnexpectedErrorAlert";
it("renders an error message for the action", () => {
const roles: IRBACRole[] = [];
const action = "unit-test";
const wrapper = shallow(<PermissionsErrorAlert roles={roles} action={action} namespace="test" />);
const wrapper = shallow(
<PermissionsErrorAlert roles={roles} action={action} namespace="test" rawMessage="" />,
);
const header = wrapper
.find(UnexpectedErrorAlert)
.shallow()
.find(ErrorAlertHeader);
expect(header).toExist();
expect(header.shallow().text()).toContain(`You don't have sufficient permissions to ${action}`);
expect(wrapper.html()).toContain("Ask your administrator for the following RBAC roles:");
expect(wrapper.html()).toContain("The following error was returned:");
expect(wrapper).toMatchSnapshot();
});

Expand All @@ -36,13 +38,18 @@ it("renders PermissionsListItem for each RBAC role", () => {
verbs: ["list", "watch"],
},
];
const wrapper = shallow(<PermissionsErrorAlert roles={roles} action="test" namespace="test" />);
const wrapper = shallow(
<PermissionsErrorAlert roles={roles} action="test" namespace="test" rawMessage="" />,
);
expect(wrapper.html()).toContain("Ask your administrator for the following RBAC roles:");
expect(wrapper.find(PermissionsListItem)).toHaveLength(2);
});

it("renders a link to access control documentation", () => {
const roles: IRBACRole[] = [];
const wrapper = shallow(<PermissionsErrorAlert roles={roles} action="test" namespace="test" />);
const wrapper = shallow(
<PermissionsErrorAlert roles={roles} action="test" namespace="test" rawMessage="" />,
);
expect(wrapper.html()).toMatch(
/See the documentation for more info on.*access control in Kubeapps./,
);
Expand All @@ -51,3 +58,12 @@ it("renders a link to access control documentation", () => {
);
expect(wrapper.html()).not.toContain(shallow(genericMessage).html());
});

it("renders the raw message if there are no roles defined", () => {
const roles: IRBACRole[] = [];
const wrapper = shallow(
<PermissionsErrorAlert roles={roles} action="test" namespace="test" rawMessage="boom!" />,
);
expect(wrapper.html()).toMatch(/boom!/);
expect(wrapper.html()).not.toContain(shallow(genericMessage).html());
});
28 changes: 21 additions & 7 deletions dashboard/src/components/ErrorAlert/PermissionsErrorAlert.tsx
Expand Up @@ -9,12 +9,13 @@ import PermissionsListItem from "./PermissionsListItem";
interface IPermissionsErrorPage {
action: string;
roles: IRBACRole[];
rawMessage: string;
namespace: string;
}

class PermissionsErrorPage extends React.Component<IPermissionsErrorPage> {
public render() {
const { action, namespace, roles } = this.props;
const { action, namespace, roles, rawMessage } = this.props;
return (
<UnexpectedErrorAlert
title={
Expand All @@ -26,12 +27,25 @@ class PermissionsErrorPage extends React.Component<IPermissionsErrorPage> {
showGenericMessage={false}
>
<div>
<p>Ask your administrator for the following RBAC roles:</p>
<ul className="error__permisions-list">
{roles.map((r, i) => (
<PermissionsListItem key={i} namespace={namespace} role={r} />
))}
</ul>
{roles.length > 0 ? (
<>
<p>Ask your administrator for the following RBAC roles:</p>
<ul className="error__permisions-list">
{roles.map((r, i) => (
<PermissionsListItem key={i} namespace={namespace} role={r} />
))}
</ul>
</>
) : (
<>
<p>The following error was returned:</p>
<div className="error__content">
<section className="Terminal terminal__error elevation-1 type-color-white error__text">
{rawMessage}
</section>
</div>
</>
)}
<p>
See the documentation for more info on{" "}
<a
Expand Down
Expand Up @@ -11,6 +11,7 @@ exports[`ForbiddenError should show an error message with the default RBAC roles
<PermissionsErrorPage
action="view my app"
namespace="my-ns"
rawMessage=""
roles={
Array [
Object {
Expand Down
Expand Up @@ -21,11 +21,15 @@ exports[`renders an error message for the action 1`] = `
>
<div>
<p>
Ask your administrator for the following RBAC roles:
The following error was returned:
</p>
<ul
className="error__permisions-list"
/>
<div
className="error__content"
>
<section
className="Terminal terminal__error elevation-1 type-color-white error__text"
/>
</div>
<p>
See the documentation for more info on
Expand Down
2 changes: 1 addition & 1 deletion pkg/handlerutil/handlerutil.go
Expand Up @@ -39,7 +39,7 @@ func isAlreadyExists(err error) bool {
}

func isForbidden(err error) bool {
return strings.Contains(err.Error(), "Unauthorized")
return strings.Contains(err.Error(), "Unauthorized") || strings.Contains(err.Error(), "forbidden")
}

func isUnprocessable(err error) bool {
Expand Down

0 comments on commit 6762ad3

Please sign in to comment.