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

Feat/add suport for scoped oauth #70

Merged
merged 2 commits into from
Feb 12, 2024
Merged
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,4 @@ tsconfig.tmp.json
!config.d.ts
!custom.d.ts
.gitignore
app-config.dev.yaml
15 changes: 10 additions & 5 deletions app-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,17 @@ integrations:
# token: ${GHE_TOKEN}
pagerDuty:
eventsBaseUrl: 'https://events.pagerduty.com/v2'
apiToken: ${PAGERDUTY_TOKEN}
oauth:
clientId: ${PAGERDUTY_CLIENT_ID}
clientSecret: ${PAGERDUTY_CLIENT_SECRET}
subDomain: ${PAGERDUTY_ACCOUNT}

proxy:
'/pagerduty':
target: https://api.pagerduty.com
headers:
Authorization: Token token=${PAGERDUTY_TOKEN}
# proxy:
# '/pagerduty':
# target: https://api.pagerduty.com
# headers:
# Authorization: Token token=${PAGERDUTY_TOKEN}
### Example for how to add a proxy endpoint for the frontend.
### A typical reason to do this is to handle HTTPS and CORS for internal services.
# endpoints:
Expand Down
8 changes: 8 additions & 0 deletions config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { PagerDutyOAuthConfig } from "@pagerduty/backstage-plugin-common";

export interface Config {
/**
* Configuration for the PagerDuty plugin
Expand All @@ -29,5 +32,10 @@ export interface Config {
* @visibility frontend
*/
apiToken?: string;
/**
* Optional PagerDuty Scoped OAuth Token used in API calls from the backend component.
* @visibility frontend
*/
oauth?: PagerDutyOAuthConfig;
};
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"react-use": "^17.2.4"
},
"peerDependencies": {
"@pagerduty/backstage-plugin-common": "^0.0.2",
"@pagerduty/backstage-plugin-common": "^0.1.0",
"react": "^16.13.1 || ^17.0.0 || ^18.0.0",
"react-dom": "^16.13.1 || ^17.0.0 || ^18.0.0",
"react-router-dom": "6.0.0-beta.0 || ^6.3.0"
Expand All @@ -60,7 +60,7 @@
"@backstage/test-utils": "^1.4.5",
"@commitlint/cli": "^17.7.1",
"@commitlint/config-conventional": "^17.7.0",
"@pagerduty/backstage-plugin-common": "^0.0.2",
"@pagerduty/backstage-plugin-common": "^0.1.0",
"@testing-library/dom": "^8.0.0",
"@testing-library/jest-dom": "^5.10.1",
"@testing-library/react": "^12.1.3",
Expand Down
2 changes: 1 addition & 1 deletion src/components/ChangeEvents/ChangeEvents.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe('Incidents', () => {
it("Renders a forbidden state when change events is undefined", async () => {
mockPagerDutyApi.getChangeEventsByServiceId = jest
.fn()
.mockImplementationOnce(async () => {throw new Error("Forbidden: You allowed to perform this action");});
.mockImplementationOnce(async () => {throw new Error("Forbidden: You are not allowed to perform this action");});

const { getByText, queryByTestId } = render(
wrapInTestApp(
Expand Down
8 changes: 4 additions & 4 deletions src/components/EntityPagerDutyCard/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,8 @@ describe('EntityPagerDutyCard', () => {

expect(
getByText(
'Error encountered while fetching information. An error occurred',
),
"You don't have the required permissions to perform this action. See README for more details."
)
).toBeInTheDocument();
});

Expand Down Expand Up @@ -305,8 +305,8 @@ describe('EntityPagerDutyCard', () => {

expect(
getByText(
'Error encountered while fetching information. An error occurred',
),
"You don't have the required permissions to perform this action. See README for more details."
)
).toBeInTheDocument();
});

Expand Down
36 changes: 36 additions & 0 deletions src/components/Errors/ForbiddenError.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2020 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// eslint-disable-next-line @backstage/no-undeclared-imports
import React from 'react';
import { Button } from '@material-ui/core';
import { EmptyState } from '@backstage/core-components';

export const ForbiddenError = () => (
<EmptyState
missing="info"
title="Unauthorized"
description="You don't have the required permissions to perform this action. See README for more details."
action={
<Button
color="primary"
variant="contained"
href="https://pagerduty.github.io/backstage-plugin-docs/getting-started/pagerduty/"
>
Read More
</Button>
}
/>
);
24 changes: 24 additions & 0 deletions src/components/Escalation/Escalation.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,30 @@ describe("Escalation", () => {
expect(mockPagerDutyApi.getOnCallByPolicyId).toHaveBeenCalledWith("456");
});

it("Renders a forbidden state when change events is undefined", async () => {
mockPagerDutyApi.getOnCallByPolicyId = jest
.fn()
.mockImplementationOnce(async () => {
throw new Error(
"Forbidden: You are not allowed to perform this action"
);
});

const { getByText, queryByTestId } = render(
wrapInTestApp(
<ApiProvider apis={apis}>
<EscalationPolicy policyId="abc" />
</ApiProvider>
)
);
await waitFor(() => !queryByTestId("progress"));
expect(
getByText(
"You don't permissions to list on-calls. Check your OAuth token permissions."
)
).toBeInTheDocument();
});

it("Render a list of users", async () => {
mockPagerDutyApi.getOnCallByPolicyId = jest
.fn()
Expand Down
9 changes: 9 additions & 0 deletions src/components/Escalation/EscalationPolicy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import React from 'react';
import { List, ListSubheader } from '@material-ui/core';
import { EscalationUsersEmptyState } from './EscalationUsersEmptyState';
import { EscalationUsersForbiddenState } from './EscalationUsersForbiddenState';
import { EscalationUser } from './EscalationUser';
import useAsync from 'react-use/lib/useAsync';
import { pagerDutyApiRef } from '../../api';
Expand All @@ -41,6 +42,14 @@ export const EscalationPolicy = ({ policyId }: Props) => {
});

if (error) {
if (error.message.includes("Forbidden")) {
return (
<List dense subheader={<ListSubheader>ON CALL</ListSubheader>}>
<EscalationUsersForbiddenState />
</List>
);
}

return (
<Alert severity="error">
Error encountered while fetching information. {error.message}
Expand Down
48 changes: 48 additions & 0 deletions src/components/Escalation/EscalationUsersForbiddenState.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2020 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// eslint-disable-next-line @backstage/no-undeclared-imports
import React from 'react';
import {
ListItem,
ListItemIcon,
ListItemText,
makeStyles,
} from '@material-ui/core';
import { StatusError } from '@backstage/core-components';

const useStyles = makeStyles({
denseListIcon: {
marginRight: 0,
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
justifyContent: 'center',
},
});

export const EscalationUsersForbiddenState = () => {
const classes = useStyles();
return (
<ListItem>
<ListItemIcon>
<div className={classes.denseListIcon}>
<StatusError />
</div>
</ListItemIcon>
<ListItemText primary="You don't permissions to list on-calls. Check your OAuth token permissions." />
</ListItem>
);
};
37 changes: 37 additions & 0 deletions src/components/Incident/IncidentForbiddenState.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2020 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// eslint-disable-next-line @backstage/no-undeclared-imports
import React from 'react';
import { Grid, Typography } from '@material-ui/core';
import ForbiddenStateImage from '../../assets/forbiddenstate.svg';

export const IncidentForbiddenState = () => {
return (
<Grid container justify="center" direction="column" alignItems="center">
<Grid item xs={12}>
<Typography variant="h5">Feature not available with your account or token.</Typography>
</Grid>
<Grid item xs={12}>
<img
src={ForbiddenStateImage}
alt="ForbiddenState"
data-testid="forbiddenStateImg"
/>
</Grid>
</Grid>
);
};
20 changes: 20 additions & 0 deletions src/components/Incident/Incidents.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,26 @@ describe('Incidents', () => {
expect(screen.getByText('Nice! No incidents found!')).toBeInTheDocument();
});

it("Renders a forbidden state when incidents is undefined", async () => {
mockPagerDutyApi.getIncidentsByServiceId = jest
.fn()
.mockImplementationOnce(async () => {
throw new Error("Forbidden: You are not allowed to perform this action");
});

const { getByText, queryByTestId } = render(
wrapInTestApp(
<ApiProvider apis={apis}>
<Incidents serviceId="abc" refreshIncidents={false} />
</ApiProvider>
)
);
await waitFor(() => !queryByTestId("progress"));
expect(
getByText("Feature not available with your account or token.")
).toBeInTheDocument();
});

it('Renders all incidents', async () => {
mockPagerDutyApi.getIncidentsByServiceId = jest
.fn()
Expand Down
5 changes: 5 additions & 0 deletions src/components/Incident/Incidents.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { Alert } from '@material-ui/lab';

import { useApi } from '@backstage/core-plugin-api';
import { Progress } from '@backstage/core-components';
import { IncidentForbiddenState } from './IncidentForbiddenState';

type Props = {
serviceId: string;
Expand All @@ -47,6 +48,10 @@ export const Incidents = ({ serviceId, refreshIncidents }: Props) => {
}, [refreshIncidents, getIncidents]);

if (error) {
if (error.message.includes('Forbidden')) {
return <IncidentForbiddenState />;
}

return (
<Alert severity="error">
Error encountered while fetching information. {error.message}
Expand Down
6 changes: 3 additions & 3 deletions src/components/PagerDutyCard/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ describe('PagerDutyCard', () => {

expect(
getByText(
'Error encountered while fetching information. An error occurred',
),
"You don't have the required permissions to perform this action. See README for more details."
)
).toBeInTheDocument();
});

Expand Down Expand Up @@ -222,7 +222,7 @@ describe('PagerDutyCard', () => {

expect(
getByText(
'Error encountered while fetching information. An error occurred',
"You don't have the required permissions to perform this action. See README for more details.",
),
).toBeInTheDocument();
});
Expand Down
8 changes: 2 additions & 6 deletions src/components/PagerDutyCard/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import { Card, CardHeader, Divider, CardContent } from '@material-ui/core';
import { Incidents } from '../Incident';
import { EscalationPolicy } from '../Escalation';
import useAsync from 'react-use/lib/useAsync';
import { Alert } from '@material-ui/lab';
import { pagerDutyApiRef, UnauthorizedError } from '../../api';
import AlarmAddIcon from '@material-ui/icons/AlarmAdd';
import { MissingTokenError, ServiceNotFoundError } from '../Errors';
Expand All @@ -39,6 +38,7 @@ import {
InfoCard,
} from '@backstage/core-components';
import { PagerDutyEntity } from '../../types';
import { ForbiddenError } from '../Errors/ForbiddenError';

const BasicCard = ({ children }: { children: ReactNode }) => (
<InfoCard title="PagerDuty">{children}</InfoCard>
Expand Down Expand Up @@ -100,11 +100,7 @@ export const PagerDutyCard = (props: PagerDutyCardProps) => {
errorNode = <ServiceNotFoundError />;
break;
default:
errorNode = (
<Alert severity="error">
Error encountered while fetching information. {error.message}
</Alert>
);
errorNode = <ForbiddenError />;
}

return <BasicCard>{errorNode}</BasicCard>;
Expand Down
Loading
Loading