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(ui): reset the widget when access expires #51

Merged
merged 2 commits into from
Oct 30, 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
133 changes: 77 additions & 56 deletions ui/src/component/ephemeral-access-details.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,37 @@ const EphemeralAccessDetails: React.FC<AccessDetailsComponentProps> = ({
localStorage.setItem(application?.metadata?.name, 'null');
}

if (accessRequest && accessRequest.status === AccessRequestResponseBodyStatus.GRANTED) {
setEnabled(false);
switch (accessRequestData?.status) {
case AccessRequestResponseBodyStatus.GRANTED:
setEnabled(false);
break;
case AccessRequestResponseBodyStatus.DENIED:
notify(
'last request was denied: ' + accessRequestData?.message &&
accessRequestData?.message + '. Please try again!'
);
setEnabled(true);
setAccessRequest(null);
break;
case AccessRequestResponseBodyStatus.REQUESTED:
setEnabled(false);
break;
default:
setEnabled(true);
break;
}
return accessRequestData;
} catch (error) {
setEnabled(true);
notify('Failed to connect to backend: ' + error.message);
}

return null;
}, []);

const requestAccessHandler = useCallback(async (): Promise<CreateAccessRequestBody> => {
const requestAccessHandler = useCallback(async (): Promise<CreateAccessRequestBody | null> => {
try {
const { data } = await createAccessrequest(
await createAccessrequest(
{
roleName: window?.EPHEMERAL_ACCESS_VARS?.EPHEMERAL_ACCESS_DEFAULT_ROLE
},
Expand All @@ -79,69 +96,73 @@ const EphemeralAccessDetails: React.FC<AccessDetailsComponentProps> = ({
}
);

if (
data.status === AccessRequestResponseBodyStatus.GRANTED ||
data.status === AccessRequestResponseBodyStatus.DENIED
) {
setEnabled(false);
} else {
const intervalId = setInterval(async () => {
const accessData = await fetchAccess();
if (
accessData?.status === AccessRequestResponseBodyStatus.GRANTED ||
accessData?.status === AccessRequestResponseBodyStatus.DENIED
) {
setEnabled(false);
clearInterval(intervalId);
const intervalId = setInterval(async () => {
const updatedAccessData = await fetchAccess();
if (
updatedAccessData?.status === AccessRequestResponseBodyStatus.GRANTED ||
updatedAccessData?.status === AccessRequestResponseBodyStatus.DENIED
) {
if (updatedAccessData?.expiresAt) {
const timeoutDuration =
moment.parseZone(updatedAccessData.expiresAt).valueOf() - moment().valueOf();
if (timeoutDuration > 0) {
setTimeout(() => {
setAccessRequest(null);
setEnabled(true);
}, timeoutDuration);
}
}
}, 500);
}
clearInterval(intervalId);
}
}, 500);
} catch (error) {
setEnabled(true);
if (error.response.status === 409) {
notify('Permission request already exists');
const accessData = await fetchAccess();
if (
accessData?.status === AccessRequestResponseBodyStatus.GRANTED ||
accessData?.status === AccessRequestResponseBodyStatus.DENIED
) {
setAccessRequest(accessData);
setEnabled(false);
if (error.response) {
switch (error.response.status) {
case 409:
notify('permission request already exists');
const accessData = await fetchAccess();
if (
accessData?.status === AccessRequestResponseBodyStatus.GRANTED ||
accessData?.status === AccessRequestResponseBodyStatus.DENIED
) {
setAccessRequest(accessData);
setEnabled(false);
}
break;
case 401:
case 403:
notify('extension is not authorized: ' + error.message);
break;
case 502:
notify('error occurred while requesting permission: ' + error.message);
break;
default:
notify('failed to connect to backend: ' + error.message);
break;
}
} else {
notify('An unexpected error occurred: ' + error.message);
}
if (error.response.status === 401) {
notify('Extension is not enabled');
setEnabled(false);
}

if (error.response.status === 502) {
notify('Error occurred while requesting permission');
setEnabled(false);
}
setEnabled(false);
return null;
}
}, [applicationName, applicationNamespace, project, username, fetchAccess]);

}, [fetchAccess]);
useEffect(() => {
const fetchData = async () => {
const currentAccess = await fetchAccess();
switch (currentAccess?.status) {
case AccessRequestResponseBodyStatus.GRANTED:
setEnabled(false);
break;
case AccessRequestResponseBodyStatus.DENIED:
notify('last request was denied: ' + accessRequest?.message + '. Please try again!');
setEnabled(true);
setAccessRequest(null);
break;
case AccessRequestResponseBodyStatus.REQUESTED:
setEnabled(false);
break;
default:
setEnabled(true);
break;
const updatedAccessData = await fetchAccess();
if (updatedAccessData?.expiresAt) {
const timeoutDuration =
moment.parseZone(updatedAccessData.expiresAt).valueOf() - moment().valueOf();
if (timeoutDuration > 0) {
setTimeout(() => {
setAccessRequest(null);
setEnabled(true);
}, timeoutDuration);
}
}
};

fetchData();
}, []);

Expand Down
14 changes: 0 additions & 14 deletions ui/src/component/ephemeral-access-panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,15 @@ const DisplayAccessPermission: React.FC<{ application: Application }> = ({ appli
}
}, [application.metadata?.name]);

const validatePermissions = () => {
const accessPermission = JSON.parse(localStorage.getItem(application.metadata?.name));
if (accessPermission === null) {
localStorage.removeItem(application.metadata?.name);
setAccessRequest(null);
}
};

useEffect(() => {
const intervalId = setInterval(() => {
getPermissions();
validatePermissions();
}, 500);

return () => clearInterval(intervalId);
}, [getPermissions]);


const handleLinkClick = useCallback(() => {
window.location.href = `/applications/argocd/${application.metadata.name}?view=tree&resource=&extension=ephemeral_access`;
}, []);

return EnableEphemeralAccess(application) ? (
<div
key='ephemeral-access-status-icon'
Expand All @@ -70,7 +57,6 @@ const DisplayAccessPermission: React.FC<{ application: Application }> = ({ appli
</label>
<div style={{ display: 'flex', flexDirection: 'column', alignItems: 'flex-start' }}>
<div
onClick={handleLinkClick}
style={{
marginRight: '5px',
position: 'relative',
Expand Down
Loading