|
| 1 | +// This file is part of MinIO Console Server |
| 2 | +// Copyright (c) 2022 MinIO, Inc. |
| 3 | +// |
| 4 | +// This program is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU Affero General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// This program is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU Affero General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU Affero General Public License |
| 15 | +// along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +import React, { useState } from "react"; |
| 18 | +import { connect } from "react-redux"; |
| 19 | +import withStyles from "@mui/styles/withStyles"; |
| 20 | +import { setErrorSnackMessage } from "../../../../../../actions"; |
| 21 | +import {decodeFileName, deleteCookie, getCookieValue, performDownload} from "../../../../../../common/utils"; |
| 22 | +import FormSwitchWrapper from "../../../../Common/FormComponents/FormSwitchWrapper/FormSwitchWrapper"; |
| 23 | +import ModalWrapper from "../../../../Common/ModalWrapper/ModalWrapper"; |
| 24 | +import {InspectMenuIcon} from "../../../../../../icons/SidebarMenus"; |
| 25 | +import Button from "@mui/material/Button"; |
| 26 | +import Grid from "@mui/material/Grid"; |
| 27 | +import {Theme} from "@mui/material/styles"; |
| 28 | +import createStyles from "@mui/styles/createStyles"; |
| 29 | +import {formFieldStyles, modalStyleUtils, spacingUtils} from "../../../../Common/FormComponents/common/styleLibrary"; |
| 30 | +import {PasswordKeyIcon} from "../../../../../../icons"; |
| 31 | +import {Box, DialogContentText} from "@mui/material"; |
| 32 | +import KeyRevealer from "../../../../Tools/KeyRevealer"; |
| 33 | + |
| 34 | +const styles = (theme: Theme) => |
| 35 | + createStyles({ |
| 36 | + ...formFieldStyles, |
| 37 | + ...modalStyleUtils, |
| 38 | + ...spacingUtils, |
| 39 | + }); |
| 40 | + |
| 41 | +interface IInspectObjectProps { |
| 42 | + classes: any; |
| 43 | + closeInspectModalAndRefresh: (refresh: boolean) => void; |
| 44 | + inspectOpen: boolean; |
| 45 | + inspectPath: string; |
| 46 | + volumeName: string; |
| 47 | + setErrorSnackMessage: typeof setErrorSnackMessage; |
| 48 | +} |
| 49 | + |
| 50 | +const InspectObject = ({ |
| 51 | + classes, |
| 52 | + closeInspectModalAndRefresh, |
| 53 | + inspectOpen, |
| 54 | + inspectPath, |
| 55 | + volumeName, |
| 56 | + setErrorSnackMessage, |
| 57 | +}: IInspectObjectProps) => { |
| 58 | + const onClose = () => closeInspectModalAndRefresh(false); |
| 59 | + const [isEncrypt, setIsEncrypt] = useState<boolean>(true); |
| 60 | + const [decryptionKey, setDecryptionKey] = useState<string>(""); |
| 61 | + const [insFileName, setInsFileName] = useState<string>(""); |
| 62 | + |
| 63 | + if (!inspectPath) { |
| 64 | + return null; |
| 65 | + } |
| 66 | + const makeRequest = async (url: string) => { |
| 67 | + return await fetch(url, { method: "GET" }); |
| 68 | + }; |
| 69 | + |
| 70 | + const performInspect = async () => { |
| 71 | + const file = encodeURIComponent(inspectPath+"/xl.meta"); |
| 72 | + const volume = encodeURIComponent(volumeName); |
| 73 | + |
| 74 | + const urlOfInspectApi = `/api/v1/admin/inspect?volume=${volume}&file=${file}&encrypt=${isEncrypt}`; |
| 75 | + |
| 76 | + makeRequest(urlOfInspectApi) |
| 77 | + .then(async (res) => { |
| 78 | + if (!res.ok) { |
| 79 | + const resErr: any = await res.json(); |
| 80 | + |
| 81 | + setErrorSnackMessage({ |
| 82 | + errorMessage: resErr.message, |
| 83 | + detailedError: resErr.code, |
| 84 | + }); |
| 85 | + } |
| 86 | + const blob: Blob = await res.blob(); |
| 87 | + |
| 88 | + //@ts-ignore |
| 89 | + const filename = res.headers.get("content-disposition").split('"')[1]; |
| 90 | + const decryptKey = getCookieValue(filename) || ""; |
| 91 | + |
| 92 | + performDownload(blob, filename); |
| 93 | + setInsFileName(filename); |
| 94 | + if (decryptKey === "") { |
| 95 | + onClose(); |
| 96 | + return; |
| 97 | + } |
| 98 | + setDecryptionKey(decryptKey); |
| 99 | + }) |
| 100 | + .catch((err) => { |
| 101 | + setErrorSnackMessage(err); |
| 102 | + }); |
| 103 | + }; |
| 104 | + |
| 105 | + const onCloseDecKeyModal = () => { |
| 106 | + deleteCookie(insFileName); |
| 107 | + onClose(); |
| 108 | + setDecryptionKey(""); |
| 109 | + }; |
| 110 | + |
| 111 | + const onSubmit = (e: React.FormEvent) => { |
| 112 | + e.preventDefault(); |
| 113 | + }; |
| 114 | + |
| 115 | + return ( |
| 116 | + <React.Fragment> |
| 117 | + {!decryptionKey && <ModalWrapper |
| 118 | + modalOpen={inspectOpen} |
| 119 | + titleIcon={<InspectMenuIcon />} |
| 120 | + title={`Inspect Object`} |
| 121 | + onClose={onClose} |
| 122 | + > |
| 123 | + <form |
| 124 | + noValidate |
| 125 | + autoComplete="off" |
| 126 | + onSubmit={(e: React.FormEvent<HTMLFormElement>) => { |
| 127 | + onSubmit(e); |
| 128 | + }} |
| 129 | + > |
| 130 | + Would you like to encrypt{" "} |
| 131 | + <b>{decodeFileName(inspectPath)}</b>? <br /> |
| 132 | + <FormSwitchWrapper |
| 133 | + label={"Encrypt"} |
| 134 | + indicatorLabels={["Yes", "No"]} |
| 135 | + checked={isEncrypt} |
| 136 | + value={"encrypt"} |
| 137 | + id="encrypt" |
| 138 | + name="encrypt" |
| 139 | + onChange={(e) => { |
| 140 | + setIsEncrypt(!isEncrypt); |
| 141 | + }} |
| 142 | + description="" |
| 143 | + /> |
| 144 | + <Grid item xs={12} className={classes.modalButtonBar}> |
| 145 | + <Button |
| 146 | + type="submit" |
| 147 | + variant="contained" |
| 148 | + color="primary" |
| 149 | + onClick={performInspect} |
| 150 | + > |
| 151 | + Inspect |
| 152 | + </Button> |
| 153 | + </Grid> |
| 154 | + </form> |
| 155 | + </ModalWrapper>} |
| 156 | + {decryptionKey ? ( |
| 157 | + <ModalWrapper |
| 158 | + modalOpen={inspectOpen} |
| 159 | + title="Inspect Decryption Key" |
| 160 | + onClose={onCloseDecKeyModal} |
| 161 | + titleIcon={<PasswordKeyIcon />} |
| 162 | + > |
| 163 | + <DialogContentText> |
| 164 | + <Box> |
| 165 | + This will be displayed only once. It cannot be recovered. |
| 166 | + <br /> |
| 167 | + Use secure medium to share this key. |
| 168 | + </Box> |
| 169 | + <Box> |
| 170 | + <KeyRevealer value={decryptionKey} /> |
| 171 | + </Box> |
| 172 | + </DialogContentText> |
| 173 | + </ModalWrapper> |
| 174 | + ) : null} |
| 175 | + </React.Fragment> |
| 176 | + ); |
| 177 | +}; |
| 178 | + |
| 179 | +const mapDispatchToProps = { |
| 180 | + setErrorSnackMessage, |
| 181 | +}; |
| 182 | + |
| 183 | +const connector = connect(null, mapDispatchToProps); |
| 184 | + |
| 185 | +export default withStyles(styles)(connector(InspectObject)); |
0 commit comments