github.com/minio/console@v1.4.1/web-app/src/screens/Console/Common/CredentialsPrompt/CredentialsPrompt.tsx (about) 1 // This file is part of MinIO Console Server 2 // Copyright (c) 2021 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, { Fragment } from "react"; 18 import get from "lodash/get"; 19 import styled from "styled-components"; 20 import { 21 Box, 22 Button, 23 DownloadIcon, 24 ServiceAccountCredentialsIcon, 25 WarnIcon, 26 Grid, 27 } from "mds"; 28 import { NewServiceAccount } from "./types"; 29 import ModalWrapper from "../ModalWrapper/ModalWrapper"; 30 import CredentialItem from "./CredentialItem"; 31 import TooltipWrapper from "../TooltipWrapper/TooltipWrapper"; 32 import { modalStyleUtils } from "../FormComponents/common/styleLibrary"; 33 34 const WarningBlock = styled.div(({ theme }) => ({ 35 color: get(theme, "signalColors.danger", "#C51B3F"), 36 fontSize: ".85rem", 37 margin: ".5rem 0 .5rem 0", 38 display: "flex", 39 alignItems: "center", 40 "& svg ": { 41 marginRight: ".3rem", 42 height: 16, 43 width: 16, 44 }, 45 })); 46 47 interface ICredentialsPromptProps { 48 newServiceAccount: NewServiceAccount | null; 49 open: boolean; 50 entity: string; 51 closeModal: () => void; 52 } 53 54 const download = (filename: string, text: string) => { 55 let element = document.createElement("a"); 56 element.setAttribute("href", "data:text/plain;charset=utf-8," + text); 57 element.setAttribute("download", filename); 58 59 element.style.display = "none"; 60 document.body.appendChild(element); 61 62 element.click(); 63 document.body.removeChild(element); 64 }; 65 66 const CredentialsPrompt = ({ 67 newServiceAccount, 68 open, 69 closeModal, 70 entity, 71 }: ICredentialsPromptProps) => { 72 if (!newServiceAccount) { 73 return null; 74 } 75 const consoleCreds = get(newServiceAccount, "console", null); 76 const idp = get(newServiceAccount, "idp", false); 77 78 const downloadImport = () => { 79 let consoleExtras = {}; 80 81 if (consoleCreds) { 82 if (!Array.isArray(consoleCreds)) { 83 consoleExtras = { 84 url: consoleCreds.url, 85 accessKey: consoleCreds.accessKey, 86 secretKey: consoleCreds.secretKey, 87 api: "s3v4", 88 path: "auto", 89 }; 90 } else { 91 const cCreds = consoleCreds.map((itemMap) => { 92 return { 93 url: itemMap.url, 94 accessKey: itemMap.accessKey, 95 secretKey: itemMap.secretKey, 96 api: "s3v4", 97 path: "auto", 98 }; 99 }); 100 consoleExtras = cCreds[0]; 101 } 102 } else { 103 consoleExtras = { 104 url: newServiceAccount.url, 105 accessKey: newServiceAccount.accessKey, 106 secretKey: newServiceAccount.secretKey, 107 api: "s3v4", 108 path: "auto", 109 }; 110 } 111 112 download( 113 "credentials.json", 114 JSON.stringify({ 115 ...consoleExtras, 116 }), 117 ); 118 }; 119 120 const downloaddAllCredentials = () => { 121 let allCredentials = {}; 122 if ( 123 consoleCreds && 124 Array.isArray(consoleCreds) && 125 consoleCreds.length > 1 126 ) { 127 const cCreds = consoleCreds.map((itemMap) => { 128 return { 129 accessKey: itemMap.accessKey, 130 secretKey: itemMap.secretKey, 131 }; 132 }); 133 allCredentials = cCreds; 134 } 135 download( 136 "all_credentials.json", 137 JSON.stringify({ 138 ...allCredentials, 139 }), 140 ); 141 }; 142 143 return ( 144 <ModalWrapper 145 modalOpen={open} 146 onClose={() => { 147 closeModal(); 148 }} 149 title={`New ${entity} Created`} 150 titleIcon={<ServiceAccountCredentialsIcon />} 151 > 152 <Grid container> 153 <Grid item xs={12}> 154 A new {entity} has been created with the following details: 155 {!idp && consoleCreds && ( 156 <Fragment> 157 <Grid 158 item 159 xs={12} 160 sx={{ 161 overflowY: "auto", 162 maxHeight: 350, 163 }} 164 > 165 <Box 166 sx={{ 167 padding: ".8rem 0 0 0", 168 fontWeight: 600, 169 fontSize: ".9rem", 170 }} 171 > 172 Console Credentials 173 </Box> 174 {Array.isArray(consoleCreds) && 175 consoleCreds.map((credentialsPair, index) => { 176 return ( 177 <Fragment> 178 <CredentialItem 179 label="Access Key" 180 value={credentialsPair.accessKey} 181 /> 182 <CredentialItem 183 label="Secret Key" 184 value={credentialsPair.secretKey} 185 /> 186 </Fragment> 187 ); 188 })} 189 {!Array.isArray(consoleCreds) && ( 190 <Fragment> 191 <CredentialItem 192 label="Access Key" 193 value={consoleCreds.accessKey} 194 /> 195 <CredentialItem 196 label="Secret Key" 197 value={consoleCreds.secretKey} 198 /> 199 </Fragment> 200 )} 201 </Grid> 202 </Fragment> 203 )} 204 {(consoleCreds === null || consoleCreds === undefined) && ( 205 <> 206 <CredentialItem 207 label="Access Key" 208 value={newServiceAccount.accessKey || ""} 209 /> 210 <CredentialItem 211 label="Secret Key" 212 value={newServiceAccount.secretKey || ""} 213 /> 214 </> 215 )} 216 {idp ? ( 217 <WarningBlock> 218 Please Login via the configured external identity provider. 219 </WarningBlock> 220 ) : ( 221 <WarningBlock> 222 <WarnIcon /> 223 <span> 224 Write these down, as this is the only time the secret will be 225 displayed. 226 </span> 227 </WarningBlock> 228 )} 229 </Grid> 230 <Grid item xs={12} sx={{ ...modalStyleUtils.modalButtonBar }}> 231 {!idp && ( 232 <Fragment> 233 <TooltipWrapper 234 tooltip={ 235 "Download credentials in a JSON file formatted for import using mc alias import. This will only include the default login credentials." 236 } 237 > 238 <Button 239 id={"download-button"} 240 label={"Download for import"} 241 onClick={downloadImport} 242 icon={<DownloadIcon />} 243 variant="callAction" 244 /> 245 </TooltipWrapper> 246 247 {Array.isArray(consoleCreds) && consoleCreds.length > 1 && ( 248 <TooltipWrapper 249 tooltip={ 250 "Download all access credentials to a JSON file. NOTE: This file is not formatted for import using mc alias import. If you plan to import this alias from the file, please use the Download for Import button. " 251 } 252 > 253 <Button 254 id={"download-all-button"} 255 label={"Download all access credentials"} 256 onClick={downloaddAllCredentials} 257 icon={<DownloadIcon />} 258 variant="callAction" 259 color="primary" 260 /> 261 </TooltipWrapper> 262 )} 263 </Fragment> 264 )} 265 </Grid> 266 </Grid> 267 </ModalWrapper> 268 ); 269 }; 270 271 export default CredentialsPrompt;