gabotorresruiz commented on code in PR #45997: URL: https://github.com/apache/superset/pull/45997#discussion_r4469599726
########## superset-frontend/src/features/databases/DatabaseModal/SecureExtraHints.tsx: ########## @@ -0,0 +1,238 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +import { useEffect, useRef, useState } from 'react'; +import { t } from '@apache-superset/core/translation'; +import { Alert } from '@apache-superset/core/components'; +import { css, SupersetTheme } from '@apache-superset/core/theme'; +import { SupersetClient } from '@superset-ui/core'; +import { Button, Icons, Input, Tooltip } from '@superset-ui/core/components'; +import { EncryptedExtraHint, EncryptedExtraSecretField } from '../types'; +import { StyledInputContainer } from './styles'; + +const BULLETS = '•'.repeat(20); +const MASK = 'XXXXXXXXXX'; + +// StyledInputContainer borders every text input; inside the affix wrapper +// that would draw a second box, so restore antd's affixed input defaults. +// Inter's contextual alternates would restyle the bullets before the suffix. +const INPUT_STYLES = { + input: { borderWidth: 0, padding: 0, fontVariantLigatures: 'no-contextual' }, +}; + +// Same colours as antd's password field eye. +const eyeIconStyles = (theme: SupersetTheme) => css` + color: ${theme.colorIcon}; + &:hover { + color: ${theme.colorIconHover}; + } +`; + +const isRecord = (value: unknown): value is Record<string, unknown> => + typeof value === 'object' && value !== null && !Array.isArray(value); + +/** + * Lists the masked values of a Secure extra JSON that sit at the engine's + * declared sensitive paths (`$.a.b`, with `*` matching every key of a level). + */ +export const resolveMaskedSecretFields = ( + maskedEncryptedExtra: string | undefined, + sensitiveFields: Record<string, string>, +): EncryptedExtraSecretField[] => { + let payload: unknown; + try { + payload = JSON.parse(maskedEncryptedExtra ?? ''); + } catch { + return []; + } + const fields = new Map<string, string>(); + Object.entries(sensitiveFields).forEach(([declared, label]) => { + let matches: [string, unknown][] = [['$', payload]]; + declared + .split('.') + .slice(1) + .forEach(segment => { + matches = matches.flatMap(([path, value]): [string, unknown][] => { + if (!isRecord(value)) { + return []; + } + return segment === '*' + ? Object.entries(value).map(([key, child]) => [ + `${path}.${key}`, + child, + ]) + : [[`${path}.${segment}`, value[segment]]]; + }); + }); + matches.forEach(([path, value]) => { + if (value === MASK && !fields.has(path)) { + fields.set(path, label); + } + }); + }); + return Array.from(fields, ([path, label]) => ({ path, label })); +}; + +type HintsState = + | { status: 'idle' } + | { status: 'loading' } + | { status: 'loaded'; hints: EncryptedExtraHint[] } + | { status: 'error' }; + +const SecureExtraHints = ({ + databaseId, + fields, + minLength, +}: { + databaseId: number; + fields: EncryptedExtraSecretField[]; + // Shortest secret whose ending the server reveals. + minLength: number; +}) => { + const [state, setState] = useState<HintsState>({ status: 'idle' }); + const inFlight = useRef(false); + const mounted = useRef(true); + + // Set on every run: strict mode and fast refresh rerun effects after cleanup. + useEffect(() => { + mounted.current = true; + return () => { + mounted.current = false; + }; + }, []); + + const fetchHints = () => { + if (inFlight.current) { + return; + } + inFlight.current = true; + setState({ status: 'loading' }); + SupersetClient.get({ + endpoint: `/api/v1/database/${databaseId}/encrypted_extra_hints/`, + }) + .then(({ json }) => { + if (mounted.current) { + setState({ status: 'loaded', hints: json?.result ?? [] }); + } + }) Review Comment: Addressed structurally: `SecureExtraHints` is now rendered with `key={db.id}`, so a database change on a mounted modal remounts the component and the unmount guard drops any in-flight response. It was not reachable before either, because both hosts of `DatabaseModal` close through `onClose`, which dispatches `Reset` and nulls `db` before another database can load. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
