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

Refresh resources after dropping an access request #49125

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/**
* Teleport
* Copyright (C) 2024 Gravitational, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import { useEffect } from 'react';
import { renderHook, act } from '@testing-library/react';

import { MockAppContextProvider } from 'teleterm/ui/fixtures/MockAppContextProvider';
import { MockAppContext } from 'teleterm/ui/fixtures/mocks';
import { makeRootCluster } from 'teleterm/services/tshd/testHelpers';
import {
ResourcesContextProvider,
useResourcesContext,
} from 'teleterm/ui/DocumentCluster/resourcesContext';
import { RootClusterUri } from 'teleterm/ui/uri';

import { useAssumedRolesBar } from './useAssumedRolesBar';

test('dropping a request refreshes resources', async () => {
const appContext = new MockAppContext();
const cluster = makeRootCluster();
appContext.workspacesService.setState(draftState => {
draftState.rootClusterUri = cluster.uri;
draftState.workspaces[cluster.uri] = {
localClusterUri: cluster.uri,
documents: [],
location: undefined,
accessRequests: undefined,
};
});
jest.spyOn(appContext.clustersService, 'dropRoles');
const refreshListener = jest.fn();

const wrapper = ({ children }) => (
<MockAppContextProvider appContext={appContext}>
<ResourcesContextProvider>
<RequestRefreshSubscriber onResourcesRefreshRequest={refreshListener} />
{children}
</ResourcesContextProvider>
</MockAppContextProvider>
);

const { result } = renderHook(
() =>
useAssumedRolesBar({
roles: [],
id: 'mocked-request-id',
expires: new Date(),
}),
{ wrapper }
);

await act(() => result.current.dropRequest());
expect(appContext.clustersService.dropRoles).toHaveBeenCalledTimes(1);
expect(appContext.clustersService.dropRoles).toHaveBeenCalledWith(
cluster.uri,
['mocked-request-id']
);
expect(refreshListener).toHaveBeenCalledTimes(1);
expect(refreshListener).toHaveBeenCalledWith(cluster.uri);
});

function RequestRefreshSubscriber(props: {
onResourcesRefreshRequest: (rootClusterUri: RootClusterUri) => void;
}) {
const { onResourcesRefreshRequest } = useResourcesContext();
useEffect(() => {
onResourcesRefreshRequest(props.onResourcesRefreshRequest);
}, [onResourcesRefreshRequest, props.onResourcesRefreshRequest]);

return null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ import { useInterval } from 'shared/hooks';
import { useAppContext } from 'teleterm/ui/appContextProvider';
import { retryWithRelogin } from 'teleterm/ui/utils';
import { AssumedRequest } from 'teleterm/services/tshd/types';
import { useResourcesContext } from 'teleterm/ui/DocumentCluster/resourcesContext';

export function useAssumedRolesBar(assumedRequest: AssumedRequest) {
const ctx = useAppContext();
const rootClusterUri = ctx.workspacesService?.getRootClusterUri();
const { requestResourcesRefresh } = useResourcesContext();

const [duration, setDuration] = useState<Duration>(() =>
getDurationFromNow({
Expand All @@ -46,21 +48,18 @@ export function useAssumedRolesBar(assumedRequest: AssumedRequest) {
getRefreshInterval(duration)
);

const [dropRequestAttempt, dropRequest] = useAsync(() => {
return retryWithRelogin(
ctx,
rootClusterUri,
() => ctx.clustersService.dropRoles(rootClusterUri, [assumedRequest.id])
// TODO(gzdunek): We should refresh the resources,
// the same as after assuming a role in `useAssumeAccess`.
// Unfortunately, we can't do this because we don't have access to `ResourcesContext`.
// Consider moving it into `ResourcesService`.
).catch(err => {
const [dropRequestAttempt, dropRequest] = useAsync(async () => {
try {
await retryWithRelogin(ctx, rootClusterUri, () =>
ctx.clustersService.dropRoles(rootClusterUri, [assumedRequest.id])
);
requestResourcesRefresh(rootClusterUri);
} catch (err) {
ctx.notificationsService.notifyError({
title: 'Could not switch back the role',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i know this error text was already present but it seems so weird. I think it'd be better saying something like "Could not drop role" or "Could not switch back to default role" or something. Don't have to change here, just discussing.

description: err.message,
});
});
}
});

const updateDurationAndInterval = useCallback(() => {
Expand Down
17 changes: 10 additions & 7 deletions web/packages/teleterm/src/ui/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,23 @@ import AppContext from './appContext';
import { ThemeProvider } from './ThemeProvider';
import { VnetContextProvider } from './Vnet/vnetContext';
import { ConnectionsContextProvider } from './TopBar/Connections/connectionsContext';
import { ResourcesContextProvider } from './DocumentCluster/resourcesContext';

export const App: React.FC<{ ctx: AppContext }> = ({ ctx }) => {
return (
<CatchError>
<StyledApp>
<DndProvider backend={HTML5Backend}>
<AppContextProvider value={ctx}>
<ConnectionsContextProvider>
<VnetContextProvider>
<ThemeProvider>
<AppInitializer />
</ThemeProvider>
</VnetContextProvider>
</ConnectionsContextProvider>
<ResourcesContextProvider>
<ConnectionsContextProvider>
<VnetContextProvider>
<ThemeProvider>
<AppInitializer />
</ThemeProvider>
</VnetContextProvider>
</ConnectionsContextProvider>
</ResourcesContextProvider>
</AppContextProvider>
</DndProvider>
</StyledApp>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,10 +279,10 @@ function AgentSetup() {
throw error;
}

// Now that the node has joined the server, let's refresh all open DocumentCluster instances
// to show the new node.
requestResourcesRefresh();
}, [startAgent, requestResourcesRefresh])
// Now that the node has joined the server, let's refresh open DocumentCluster
// instances in the workspace to show the new node.
requestResourcesRefresh(rootClusterUri);
}, [startAgent, requestResourcesRefresh, rootClusterUri])
);

const steps: SetupStep[] = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ export const ConnectMyComputerContextProvider: FC<
}

if (hasNodeRemovalSucceeded) {
requestResourcesRefresh();
requestResourcesRefresh(rootClusterUri);
}

ctx.notificationsService.notifyInfo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ const renderActionCell = (
onClick={() => assumeRole(request)}
width="108px"
>
{flags.isAssumed ? 'assumed' : 'assume roles'}
{flags.isAssumed ? 'Assumed' : 'Assume Roles'}
</ButtonPrimary>
);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ export function useAssumeAccess() {
const [assumeRoleAttempt, runAssumeRole] = useAsync((requestId: string) =>
retryWithRelogin(ctx, clusterUri, async () => {
await ctx.clustersService.assumeRoles(rootClusterUri, [requestId]);
// refresh the current resource tabs
requestResourcesRefresh();
// Refresh the current resource tabs in the workspace.
requestResourcesRefresh(rootClusterUri);
})
);

Expand All @@ -58,8 +58,8 @@ export function useAssumeAccess() {
return;
}

// refresh the current resource tabs
requestResourcesRefresh();
// Refresh the current resource tabs in the workspace.
requestResourcesRefresh(rootClusterUri);

// open new cluster tab
const clusterDocument = documentsService.createClusterDocument({
Expand Down
Loading
Loading