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: [WD-19336] Storage volumes permission checks #1134

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 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 src/api/instances.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const instanceEntitlements = [
"can_delete",
"can_edit",
"can_exec",
"can_view",
"can_manage_backups",
"can_manage_snapshots",
"can_update_state",
Expand Down
6 changes: 5 additions & 1 deletion src/api/storage-volumes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ import axios from "axios";
import type { LxdApiResponse } from "types/apiResponse";
import { withEntitlementsQuery } from "util/entitlements/api";

export const storageVolumeEntitlements = ["can_delete", "can_edit"];
export const storageVolumeEntitlements = [
"can_delete",
"can_edit",
"can_manage_snapshots",
];

export const fetchStorageVolumes = async (
pool: string,
Expand Down
4 changes: 3 additions & 1 deletion src/pages/storage/MigrateVolumeBtn.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { useNavigate } from "react-router-dom";
import ResourceLabel from "components/ResourceLabel";
import ResourceLink from "components/ResourceLink";
import { migrateStorageVolume } from "api/storage-volumes";
import { useStorageVolumeEntitlements } from "util/entitlements/storage-volumes";

interface Props {
storageVolume: LxdStorageVolume;
Expand All @@ -32,6 +33,7 @@ const MigrateVolumeBtn: FC<Props> = ({
const queryClient = useQueryClient();
const navigate = useNavigate();
const [isVolumeLoading, setVolumeLoading] = useState(false);
const { canEditVolume } = useStorageVolumeEntitlements();

const handleSuccess = (
newTarget: string,
Expand Down Expand Up @@ -157,7 +159,7 @@ const MigrateVolumeBtn: FC<Props> = ({
type="button"
className={classname}
loading={isVolumeLoading}
disabled={isVolumeLoading}
disabled={!canEditVolume(storageVolume) || isVolumeLoading}
title="Migrate volume"
>
<Icon name="machines" />
Expand Down
6 changes: 5 additions & 1 deletion src/pages/storage/StorageVolumeHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { useSupportedFeatures } from "context/useSupportedFeatures";
import ResourceLink from "components/ResourceLink";
import ResourceLabel from "components/ResourceLabel";
import { renameStorageVolume } from "api/storage-volumes";
import { useStorageVolumeEntitlements } from "util/entitlements/storage-volumes";

interface Props {
volume: LxdStorageVolume;
Expand All @@ -28,6 +29,7 @@ const StorageVolumeHeader: FC<Props> = ({ volume, project }) => {
const toastNotify = useToastNotification();
const controllerState = useState<AbortController | null>(null);
const { hasClusterInternalCustomVolumeCopy } = useSupportedFeatures();
const { canEditVolume } = useStorageVolumeEntitlements();

const RenameSchema = Yup.object().shape({
name: Yup.string()
Expand Down Expand Up @@ -123,7 +125,9 @@ const StorageVolumeHeader: FC<Props> = ({ volume, project }) => {
renameDisabledReason={
(volume.used_by?.length ?? 0) > 0
? "Can not rename, volume is currently in use."
: undefined
: !canEditVolume(volume)
? "You do not have permission to rename this volume"
: undefined
}
/>
);
Expand Down
43 changes: 34 additions & 9 deletions src/pages/storage/StorageVolumeNameLink.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
hasVolumeDetailPage,
} from "util/storageVolume";
import { useCurrentProject } from "context/useCurrentProject";
import { useInstances } from "context/useInstances";
import { useImagesInProject } from "context/useImages";

interface Props {
volume: LxdStorageVolume;
Expand All @@ -23,21 +25,44 @@ const StorageVolumeNameLink: FC<Props> = ({
}) => {
const { project } = useCurrentProject();
const isExternalLink = !hasVolumeDetailPage(volume);
const caption = overrideName ? overrideName : volume.name;

const isInstance =
volume.type === "container" || volume.type === "virtual-machine";
const { data: instances = [] } = useInstances(volume.project);
const instance =
isInstance && instances.find((instance) => instance.name === volume.name);

const isImage = volume.type === "image";
const { data: images = [] } = useImagesInProject(volume.project);
const image =
isImage && images.find((image) => image.fingerprint === volume.name);

const isCustom = volume.type === "custom";
const displayLink = instance || image || isCustom;

const caption = overrideName
? displayLink
? overrideName
: ""
: volume.name;

return (
<div className={classnames("u-flex", className)}>
<div
className={classnames("u-truncate", "volume-name-link")}
title={caption}
title={volume.name}
>
<Link
to={generateLinkForVolumeDetail(volume, project?.name ?? "")}
className={isExternalLink ? "has-icon" : undefined}
>
{caption}
{isExternalLink && <Icon name={ICONS.externalLink} />}
</Link>
{displayLink ? (
<Link
to={generateLinkForVolumeDetail(volume, project?.name ?? "")}
className={isExternalLink ? "has-icon" : undefined}
>
{caption}
{isExternalLink && <Icon name={ICONS.externalLink} />}
</Link>
) : (
caption
)}
</div>
</div>
);
Expand Down
4 changes: 2 additions & 2 deletions src/pages/storage/StorageVolumes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ const StorageVolumes: FC = () => {
<Icon className="external-link-icon" name="external-link" />
</a>
</p>
<CreateVolumeBtn project={project} className="empty-state-button" />
<CreateVolumeBtn projectName={project} className="empty-state-button" />
</EmptyState>
) : (
<div className="storage-volumes">
Expand Down Expand Up @@ -425,7 +425,7 @@ const StorageVolumes: FC = () => {
{hasVolumes && (
<PageHeader.BaseActions>
<CreateVolumeBtn
project={project}
projectName={project}
defaultPool={defaultPoolForVolumeCreate}
className="u-float-right u-no-margin--bottom"
/>
Expand Down
15 changes: 12 additions & 3 deletions src/pages/storage/actions/CreateVolumeBtn.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,28 @@ import type { FC } from "react";
import { Button, Icon } from "@canonical/react-components";
import { useNavigate } from "react-router-dom";
import { useSmallScreen } from "context/useSmallScreen";
import { useProjectEntitlements } from "util/entitlements/projects";
import { useProject } from "context/useProjects";

interface Props {
project: string;
projectName: string;
defaultPool?: string;
className?: string;
}

const CreateVolumeBtn: FC<Props> = ({ project, className, defaultPool }) => {
const CreateVolumeBtn: FC<Props> = ({
projectName,
className,
defaultPool,
}) => {
const navigate = useNavigate();
const isSmallScreen = useSmallScreen();
const { canCreateStorageVolumes } = useProjectEntitlements();
const { data: project } = useProject(projectName);

const handleAdd = () => {
navigate(
`/ui/project/${project}/storage/volumes/create${
`/ui/project/${projectName}/storage/volumes/create${
defaultPool ? `?pool=${defaultPool}` : ""
}`,
);
Expand All @@ -27,6 +35,7 @@ const CreateVolumeBtn: FC<Props> = ({ project, className, defaultPool }) => {
hasIcon={!isSmallScreen}
onClick={handleAdd}
className={className}
disabled={!canCreateStorageVolumes(project)}
>
{!isSmallScreen && <Icon name="plus" light />}
<span>Create volume</span>
Expand Down
3 changes: 3 additions & 0 deletions src/pages/storage/actions/DuplicateVolumeBtn.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Button, Icon } from "@canonical/react-components";
import DuplicateVolumeForm from "../forms/DuplicateVolumeForm";
import type { LxdStorageVolume } from "types/storage";
import { usePortal } from "@canonical/react-components";
import { useProjectEntitlements } from "util/entitlements/projects";

interface Props {
volume: LxdStorageVolume;
Expand All @@ -12,6 +13,7 @@ interface Props {

const DuplicateVolumeBtn: FC<Props> = ({ volume, className, onClose }) => {
const { openPortal, closePortal, isOpen, Portal } = usePortal();
const { canCreateStorageVolumes } = useProjectEntitlements();

const handleClose = () => {
closePortal();
Expand All @@ -31,6 +33,7 @@ const DuplicateVolumeBtn: FC<Props> = ({ volume, className, onClose }) => {
className={className}
onClick={openPortal}
title="Duplicate volume"
disabled={!canCreateStorageVolumes(volume)}
>
<Icon name="canvas" />
<span>Duplicate</span>
Expand Down
8 changes: 6 additions & 2 deletions src/pages/storage/actions/snapshots/VolumeAddSnapshotBtn.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { LxdStorageVolume } from "types/storage";
import { Button, Icon, Tooltip } from "@canonical/react-components";
import { usePortal } from "@canonical/react-components";
import CreateVolumeSnapshotForm from "pages/storage/forms/CreateVolumeSnapshotForm";
import { useStorageVolumeEntitlements } from "util/entitlements/storage-volumes";

interface Props {
volume: LxdStorageVolume;
Expand All @@ -18,6 +19,7 @@ const VolumeAddSnapshotBtn: FC<Props> = ({
className,
}) => {
const { openPortal, closePortal, isOpen, Portal } = usePortal();
const { canManageStorageVolumeSnapshots } = useStorageVolumeEntitlements();

return (
<>
Expand All @@ -37,9 +39,11 @@ const VolumeAddSnapshotBtn: FC<Props> = ({
title={
isDisabled
? `Snapshot creation is blocked for project ${volume.project}`
: "Add Snapshot"
: !canManageStorageVolumeSnapshots(volume)
? "You do not have permission to create or delete snapshots of this volume."
: "Add Snapshot"
}
disabled={isDisabled}
disabled={isDisabled || !canManageStorageVolumeSnapshots(volume)}
className={className}
>
<Icon name="add-canvas" />
Expand Down
8 changes: 8 additions & 0 deletions src/util/entitlements/storage-volumes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,16 @@ export const useStorageVolumeEntitlements = () => {
const canEditVolume = (volume?: LxdStorageVolume) =>
hasEntitlement(isFineGrained, "can_edit", volume?.access_entitlements);

const canManageStorageVolumeSnapshots = (volume?: LxdStorageVolume) =>
hasEntitlement(
isFineGrained,
"can_manage_snapshots",
volume?.access_entitlements,
);

return {
canDeleteVolume,
canEditVolume,
canManageStorageVolumeSnapshots,
};
};
Loading