Files
supabase/apps/studio/components/interfaces/Settings/Database/SSLConfiguration.tsx
Danny White 0b9e4f6ab1 fix(studio): clarify token access settings and SSL prerequisites (#54507)
## Problem

- Token access confirmations use stilted copy. 
- The SSL prerequisite notice sits outside the Token access card,
repeats “enabled”, and includes an unnecessary project reference.
- SSL configuration also shows enforcement as off when the latest update
failed, and its dependency tooltip needs clearer guidance.

## Solution

- Rewrite the token access and SSL confirmations, keeping the connection
warnings.
- Group prerequisite notices inside the Token access card with one
divider, remove the project reference, and simplify their descriptions.
- Keep the SSL switch tied to the fetched configuration independently of
update success.
- Explain the token access dependency in the disabled switch’s tooltip:
“Database token access requires SSL enforcement.” Leave the existing SSL
failure warning unchanged apart from its final full stop.

| Before | After |
| --- | --- |
| <img width="727" height="501" alt="CleanShot 2026-12-03 at 11 21 35"
src="https://github.com/user-attachments/assets/9fd18a91-528a-47dd-82e8-7349e26a8daa"
/> | <img width="736" height="506" alt="CleanShot 2026-12-03 at 12 05
57"
src="https://github.com/user-attachments/assets/8263303a-49d8-4910-9c88-e2fe5ecc1c34"
/> |
| <img width="362" height="236" alt="CleanShot 2026-12-03 at 11 21 54"
src="https://github.com/user-attachments/assets/026852d4-9c1b-4a9d-9833-02e047794199"
/> | <img width="421" height="229" alt="CleanShot 2026-12-03 at 12 06
24"
src="https://github.com/user-attachments/assets/9a53c459-c7ca-4c18-8567-499d67a9c068"
/> |
| <img width="420" height="206" alt="CleanShot 2026-12-03 at 11 22 36"
src="https://github.com/user-attachments/assets/4d2f8711-fd75-4730-832f-ac9e2e3d85ad"
/> | <img width="420" height="207" alt="CleanShot 2026-12-03 at 11 52
51"
src="https://github.com/user-attachments/assets/2fce238e-c8d7-4087-8402-05c2122f92f0"
/> |
| <img width="420" height="208" alt="CleanShot 2026-12-03 at 11 32 11"
src="https://github.com/user-attachments/assets/f081e782-0a6c-499a-947d-0da149311218"
/> | <img width="417" height="219" alt="CleanShot 2026-12-03 at 11 52
05"
src="https://github.com/user-attachments/assets/6b6cfcf5-2c54-4f38-9b16-bcc0cdd500a0"
/> |
| <img width="809" height="255" alt="CleanShot 2026-12-03 at 12 28 47"
src="https://github.com/user-attachments/assets/19cffa41-691f-4f60-96b5-7ba1ebcfb78e"
/> | <img width="804" height="246" alt="CleanShot 2026-12-03 at 12 44
39"
src="https://github.com/user-attachments/assets/2c3f2b74-21a8-41e7-b25e-ea2293350fcc"
/> |


## Review instructions

1. Open Project → Database → Settings in the preview for a project that
requires SSL enforcement before token access.
2. Check that SSL enforcement required sits inside Token access,
separated by one divider, with no project reference in its description.
3. Select Enable SSL enforcement and review the confirmation copy.
Cancel without applying the change.
4. On a project where token access is available, toggle it on and off to
review both confirmations. Cancel each dialog.
5. With token access on, check that SSL enforcement stays on and locked,
with a tooltip explaining that database token access requires SSL
enforcement. Check that the tooltip also opens on keyboard focus.
6. If the API reports an unsuccessful SSL update, check that the switch
still reflects `currentConfig.database` and the existing warning remains
visible.
2026-12-03 11:46:10 +07:00

246 lines
10 KiB
TypeScript

import { useParams } from 'common'
import { template } from 'lodash'
import { DownloadIcon, Loader2Icon } from 'lucide-react'
import { useMemo, useState } from 'react'
import { toast } from 'sonner'
import { Button, Card, CardContent, Switch, Tooltip, TooltipContent, TooltipTrigger } from 'ui'
import { Admonition } from 'ui-patterns/Admonition'
import { FormLayout } from 'ui-patterns/form/Layout/FormLayout'
import {
PageSection,
PageSectionContent,
PageSectionMeta,
PageSectionSummary,
PageSectionTitle,
} from 'ui-patterns/PageSection'
import { SSLEnforcementConfirmDialog } from './SSLEnforcementConfirmDialog'
import { SupportLink } from '@/components/interfaces/Support/SupportLink'
import { ButtonTooltip } from '@/components/ui/ButtonTooltip'
import { DocsButton } from '@/components/ui/DocsButton'
import { InlineLinkClassName } from '@/components/ui/InlineLink'
import { useProjectSettingsV2Query } from '@/data/config/project-settings-v2-query'
import { useJitDbAccessQuery } from '@/data/jit-db-access/jit-db-access-query'
import { useSSLEnforcementQuery } from '@/data/ssl-enforcement/ssl-enforcement-query'
import { useSSLEnforcementUpdateMutation } from '@/data/ssl-enforcement/ssl-enforcement-update-mutation'
import { useCustomContent } from '@/hooks/custom-content/useCustomContent'
import { FGA_PERMISSIONS, useAsyncCheckPermissionsV2 } from '@/hooks/misc/useCheckPermissionsV2'
import { useHighAvailability } from '@/hooks/misc/useHighAvailability'
import { DOCS_URL } from '@/lib/constants'
export const SSLConfiguration = () => {
const { ref } = useParams()
const [isConfirmDialogOpen, setIsConfirmDialogOpen] = useState(false)
const { data: settings } = useProjectSettingsV2Query({ projectRef: ref })
// High Availability projects always enforce SSL and the API rejects any
// attempt to read or change the setting, so skip the query for them and
// show the setting as always on.
const { isHighAvailability, isPending: isHighAvailabilityPending } = useHighAvailability()
const canLoadSSLEnforcement = !isHighAvailability && !isHighAvailabilityPending
const {
data: sslEnforcementConfiguration,
isPending: isSSLEnforcementPending,
isSuccess: isSSLEnforcementSuccess,
} = useSSLEnforcementQuery({ projectRef: ref }, { enabled: canLoadSSLEnforcement })
const isLoading = isHighAvailabilityPending || (!isHighAvailability && isSSLEnforcementPending)
const isSuccess = isHighAvailability || isSSLEnforcementSuccess
const { data: jitDbAccessConfiguration } = useJitDbAccessQuery({ projectRef: ref })
const { mutate: updateSSLEnforcement, isPending: isSubmitting } = useSSLEnforcementUpdateMutation(
{
onSuccess: () => {
toast.success('Successfully updated SSL configuration')
},
onError: (error) => {
toast.error(`Failed to update SSL enforcement: ${error.message}`)
},
}
)
const { can: canUpdateSSLEnforcement } = useAsyncCheckPermissionsV2(
FGA_PERMISSIONS.PROJECT.DATABASE_SSL_CONFIG_WRITE
)
// Derived directly from the query so a refetch triggered elsewhere (e.g.
// enabling SSL enforcement from the JIT DB access unavailable banner) is
// reflected here too, instead of relying on a mirrored local state that
// would only resync on the initial load.
const isEnforced =
isHighAvailability ||
(isSSLEnforcementSuccess && sslEnforcementConfiguration.currentConfig.database)
const hasAccessToSSLEnforcement = !(
sslEnforcementConfiguration !== undefined &&
'isNotAllowed' in sslEnforcementConfiguration &&
sslEnforcementConfiguration.isNotAllowed
)
// Database token access requires SSL enforcement to be enabled, so SSL enforcement
// can't be turned off again while database token access is still enabled.
const isTemporaryAccessEnabled =
jitDbAccessConfiguration?.state === 'enabled' && jitDbAccessConfiguration.appliedSuccessfully
const isSwitchDisabled =
isLoading ||
isSubmitting ||
isHighAvailability ||
!canUpdateSSLEnforcement ||
!hasAccessToSSLEnforcement ||
isTemporaryAccessEnabled
let switchTooltipMessage: string | undefined
if (isHighAvailability) {
switchTooltipMessage = 'SSL is always enforced on High Availability projects.'
} else if (!canUpdateSSLEnforcement) {
switchTooltipMessage =
'You need additional permissions to update SSL enforcement for your project.'
} else if (!hasAccessToSSLEnforcement) {
switchTooltipMessage = 'Your project does not have access to SSL enforcement.'
} else if (isTemporaryAccessEnabled) {
switchTooltipMessage = 'Database token access requires SSL enforcement.'
}
let loadingAnnouncement = ''
if (isSubmitting) {
loadingAnnouncement = 'Updating SSL enforcement'
} else if (isLoading) {
loadingAnnouncement = 'Loading SSL configuration'
}
const env = process.env.NEXT_PUBLIC_ENVIRONMENT === 'prod' ? 'prod' : 'staging'
const hasSSLCertificate =
settings?.inserted_at !== undefined && new Date(settings.inserted_at) >= new Date('2021-04-30')
const { sslCertificateUrl: sslCertificateUrlTemplate } = useCustomContent(['ssl:certificate_url'])
const sslCertificateUrl = useMemo(
() => template(sslCertificateUrlTemplate ?? '')({ env }),
[sslCertificateUrlTemplate, env]
)
const setSSLEnforcement = (isTargetEnforced: boolean) => {
if (!ref) return console.error('Project ref is required')
updateSSLEnforcement({ projectRef: ref, requestedConfig: { database: isTargetEnforced } })
}
// Only enabling enforcement closes existing connections, so only enabling needs a confirmation
const handleSwitchChange = (isChecked: boolean) => {
if (isChecked) {
setIsConfirmDialogOpen(true)
} else {
setSSLEnforcement(false)
}
}
return (
<PageSection id="ssl-configuration">
<PageSectionMeta>
<PageSectionSummary>
<PageSectionTitle>SSL configuration</PageSectionTitle>
</PageSectionSummary>
<DocsButton href={`${DOCS_URL}/guides/platform/ssl-enforcement`} />
</PageSectionMeta>
<PageSectionContent>
<Card>
<CardContent className="space-y-4">
<FormLayout
layout="flex-row-reverse"
label="Enforce SSL on incoming connections"
description="Reject non-SSL connections to your database"
>
<div className="flex items-center justify-end mt-2.5 space-x-2">
{(isLoading || isSubmitting) && (
<Loader2Icon
aria-hidden="true"
className="animate-spin motion-reduce:animate-none"
strokeWidth={1.5}
size={16}
/>
)}
{isSuccess && (
<Tooltip>
<TooltipTrigger asChild>
{/* [Joshen] Added div as tooltip is messing with data state property of toggle */}
{/* A disabled switch can't take focus, so the wrapper becomes the focus
target to keep the tooltip reachable by keyboard */}
<div tabIndex={isSwitchDisabled ? 0 : undefined}>
{/* The dialog is opened from the switch itself rather than a wrapping
trigger, so a disabled switch can never open it. */}
<Switch
aria-label="Enforce SSL on incoming connections"
size="large"
checked={isEnforced}
disabled={isSwitchDisabled}
onCheckedChange={handleSwitchChange}
/>
</div>
</TooltipTrigger>
{switchTooltipMessage && (
<TooltipContent side="bottom" className="w-64 text-center">
{switchTooltipMessage}
</TooltipContent>
)}
</Tooltip>
)}
{/* Kept mounted so screen readers announce loading state changes */}
<span className="sr-only" role="status">
{loadingAnnouncement}
</span>
</div>
<SSLEnforcementConfirmDialog
open={isConfirmDialogOpen}
onOpenChange={setIsConfirmDialogOpen}
isSubmitting={isSubmitting}
onConfirm={() => setSSLEnforcement(true)}
/>
</FormLayout>
{isSSLEnforcementSuccess && !sslEnforcementConfiguration.appliedSuccessfully && (
<Admonition
type="warning"
layout="horizontal"
title="SSL enforcement was not updated successfully"
description={
<>
Please try updating again, or contact{' '}
<SupportLink className={InlineLinkClassName}>support</SupportLink> if this error
persists.
</>
}
/>
)}
</CardContent>
<CardContent>
<FormLayout
layout="flex-row-reverse"
label="SSL Certificate"
description="Use this certificate when connecting to your database to prevent snooping and man-in-the-middle attacks."
>
<div className="flex items-end justify-end">
{!hasSSLCertificate ? (
<ButtonTooltip
disabled
icon={<DownloadIcon />}
tooltip={{
content: {
side: 'bottom',
text: 'Projects before 15:08 (GMT+08), 29th April 2021 do not have SSL certificates installed',
},
}}
>
Download certificate
</ButtonTooltip>
) : (
<Button icon={<DownloadIcon />}>
<a href={sslCertificateUrl}>Download certificate</a>
</Button>
)}
</div>
</FormLayout>
</CardContent>
</Card>
</PageSectionContent>
</PageSection>
)
}