Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Hook up TCE

Joshen Lim6d23225aeb70
diff --git a/studio/components/interfaces/Settings/Vault/Keys/EncryptionKeySelector.tsx b/studio/components/interfaces/Settings/Vault/Keys/EncryptionKeySelector.tsx
index e46894cce50..64aeac9c118 100644
--- a/studio/components/interfaces/Settings/Vault/Keys/EncryptionKeySelector.tsx
+++ b/studio/components/interfaces/Settings/Vault/Keys/EncryptionKeySelector.tsx
@@ -8,6 +8,7 @@ interface Props {
label?: string
labelOptional?: string
selectedKeyId?: any
+ error?: string
onSelectKey: (keyId: string) => void
onUpdateDescription?: (desc: string) => void
}
@@ -18,6 +19,7 @@ const EncryptionKeySelector: FC<Props> = ({
label = 'Encryption key',
labelOptional,
selectedKeyId,
+ error,
onSelectKey = () => {},
onUpdateDescription = () => {},
}) => {
@@ -64,6 +66,7 @@ const EncryptionKeySelector: FC<Props> = ({
<Input
id={nameId}
label="Name"
+ error={error}
onChange={(event) => onUpdateDescription(event.target.value)}
/>
)}
diff --git a/studio/components/interfaces/Settings/Vault/Secrets/AddNewSecretModal.tsx b/studio/components/interfaces/Settings/Vault/Secrets/AddNewSecretModal.tsx
index ff74b6513bf..f43299a8672 100644
--- a/studio/components/interfaces/Settings/Vault/Secrets/AddNewSecretModal.tsx
+++ b/studio/components/interfaces/Settings/Vault/Secrets/AddNewSecretModal.tsx
@@ -26,6 +26,8 @@ const AddNewSecretModal: FC<Props> = ({ visible, onClose }) => {
const errors: any = {}
if (values.name.length === 0) errors.name = 'Please provide a name for your secret'
if (values.secret.length === 0) errors.secret = 'Please enter your secret value'
+ if (selectedKeyId === 'create-new' && values.keyName.length === 0)
+ errors.keyName = 'Please provide a name for your new key'
return errors
}
@@ -34,7 +36,7 @@ const AddNewSecretModal: FC<Props> = ({ visible, onClose }) => {
let encryptionKeyId = selectedKeyId
if (values.keyId === 'create-new') {
- const addKeyRes = await vault.addKey(values.keyDescription || undefined)
+ const addKeyRes = await vault.addKey(values.keyName || undefined)
if (addKeyRes.error) {
return ui.setNotification({
error: addKeyRes.error,
@@ -81,7 +83,7 @@ const AddNewSecretModal: FC<Props> = ({ visible, onClose }) => {
>
<Form
id="add-new-secret-form"
- initialValues={{ name: '', description: '', secret: '', keyId: '', keyDescription: '' }}
+ initialValues={{ name: '', description: '', secret: '', keyId: '', keyName: '' }}
validate={validate}
validateOnBlur={false}
onSubmit={onAddNewSecret}
@@ -114,7 +116,7 @@ const AddNewSecretModal: FC<Props> = ({ visible, onClose }) => {
<div className="py-4 space-y-4">
<EncryptionKeySelector
id="keyId"
- nameId="keyDescription"
+ nameId="keyName"
label="Select a key to encrypt your secret with"
labelOptional="Optional"
selectedKeyId={selectedKeyId}
diff --git a/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.tsx b/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.tsx
index 03a98f82c48..7326d4816d2 100644
--- a/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.tsx
+++ b/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.tsx
@@ -40,7 +40,12 @@ interface Props {
payload: CreateColumnPayload | UpdateColumnPayload,
foreignKey: Partial<PostgresRelationship> | undefined,
isNewRecord: boolean,
- configuration: { columnId: string | undefined },
+ configuration: {
+ columnId: string | undefined
+ isEncrypted: boolean
+ keyId?: string
+ keyName?: string
+ },
resolve: any
) => void
updateEditorDirty: () => void
@@ -64,7 +69,7 @@ const ColumnEditor: FC<Props> = ({
(type: PostgresType) => !meta.excludedSchemas.includes(type.schema)
)
const keys = vault.listKeys()
- const defaultKey = keys.find((key) => key.status === 'default')
+ const defaultKey = keys.find((key) => key.name === 'default_vault_key') || keys[0]
const [errors, setErrors] = useState<Dictionary<any>>({})
const [columnFields, setColumnFields] = useState<ColumnField>()
@@ -139,7 +144,7 @@ const ColumnEditor: FC<Props> = ({
columnId: column?.id,
isEncrypted: columnFields.isEncrypted,
keyId: columnFields.keyId,
- keyDescription: columnFields.keyDescription,
+ keyName: columnFields.keyName,
}
// [Joshen TODO], we'll need to update the callback here to
@@ -324,9 +329,10 @@ const ColumnEditor: FC<Props> = ({
{columnFields.isEncrypted && (
<EncryptionKeySelector
label="Select a key to encrypt your column with"
+ error={errors?.keyName}
selectedKeyId={columnFields.keyId}
onSelectKey={(id) => onUpdateField({ keyId: id })}
- onUpdateDescription={(desc) => onUpdateField({ keyDescription: desc })}
+ onUpdateDescription={(name) => onUpdateField({ keyName: name })}
/>
)}
</FormSectionContent>
diff --git a/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.utils.ts b/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.utils.ts
index be7e62bafd3..c3e33805929 100644
--- a/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.utils.ts
+++ b/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.utils.ts
@@ -165,6 +165,9 @@ export const validateFields = (field: ColumnField) => {
if (field.format.length === 0) {
errors['format'] = `Please select a type for your column`
}
+ if (field.keyId === 'create-new' && (field?.keyName ?? '').length === 0) {
+ errors['keyName'] = 'Please provide a name for your new key'
+ }
return errors
}
diff --git a/studio/components/interfaces/TableGridEditor/SidePanelEditor/SidePanelEditor.tsx b/studio/components/interfaces/TableGridEditor/SidePanelEditor/SidePanelEditor.tsx
index 5a4fc21a283..b5b3bdd782e 100644
--- a/studio/components/interfaces/TableGridEditor/SidePanelEditor/SidePanelEditor.tsx
+++ b/studio/components/interfaces/TableGridEditor/SidePanelEditor/SidePanelEditor.tsx
@@ -122,17 +122,19 @@ const SidePanelEditor: FC<Props> = ({
payload: CreateColumnPayload | UpdateColumnPayload,
foreignKey: Partial<PostgresRelationship> | undefined,
isNewRecord: boolean,
- configuration: { columnId?: string },
+ configuration: { columnId?: string; isEncrypted: boolean; keyId?: string; keyName?: string },
resolve: any
) => {
+ const { columnId, ...securityConfig } = configuration
const response = isNewRecord
? await meta.createColumn(
payload as CreateColumnPayload,
selectedTable as PostgresTable,
- foreignKey
+ foreignKey,
+ securityConfig
)
: await meta.updateColumn(
- configuration.columnId as string,
+ columnId as string,
payload as UpdateColumnPayload,
selectedTable as PostgresTable,
foreignKey
diff --git a/studio/components/interfaces/TableGridEditor/SidePanelEditor/SidePanelEditor.types.ts b/studio/components/interfaces/TableGridEditor/SidePanelEditor/SidePanelEditor.types.ts
index 64fa197810a..3fc105799d3 100644
--- a/studio/components/interfaces/TableGridEditor/SidePanelEditor/SidePanelEditor.types.ts
+++ b/studio/components/interfaces/TableGridEditor/SidePanelEditor/SidePanelEditor.types.ts
@@ -70,9 +70,9 @@ export interface ColumnField {
isPrimaryKey: boolean
isNewColumn: boolean
- isEncrypted?: boolean
+ isEncrypted: boolean
keyId?: string
- keyDescription?: string
+ keyName?: string
}
export interface PostgresDataTypeOption {
diff --git a/studio/stores/pgmeta/MetaStore.ts b/studio/stores/pgmeta/MetaStore.ts
index 7829d7a7d77..05f9e61dec8 100644
--- a/studio/stores/pgmeta/MetaStore.ts
+++ b/studio/stores/pgmeta/MetaStore.ts
@@ -1,6 +1,6 @@
import Papa from 'papaparse'
import { makeObservable, observable } from 'mobx'
-import { find, isUndefined, isEqual, isEmpty, chunk } from 'lodash'
+import { find, isUndefined, isEqual, isEmpty, chunk, values } from 'lodash'
import { Query } from 'components/grid/query/Query'
import {
@@ -87,7 +87,8 @@ export interface IMetaStore {
createColumn: (
payload: CreateColumnPayload,
selectedTable: PostgresTable,
- foreignKey?: Partial<PostgresRelationship>
+ foreignKey?: Partial<PostgresRelationship>,
+ securityConfiguration?: { isEncrypted: boolean; keyId?: string; keyName?: string }
) => any
updateColumn: (
id: string,
@@ -347,8 +348,13 @@ export default class MetaStore implements IMetaStore {
async createColumn(
payload: CreateColumnPayload,
selectedTable: PostgresTable,
- foreignKey?: Partial<PostgresRelationship>
+ foreignKey?: Partial<PostgresRelationship>,
+ securityConfiguration?: { isEncrypted: boolean; keyId?: string; keyName?: string }
) {
+ const toastId = this.rootStore.ui.setNotification({
+ category: 'loading',
+ message: `Creating column "${payload.name}"...`,
+ })
try {
// Once pg-meta supports composite keys, we can remove this logic
const { isPrimaryKey, ...formattedPayload } = payload
@@ -358,6 +364,11 @@ export default class MetaStore implements IMetaStore {
// Firing createColumn in createTable will bypass this block
if (isPrimaryKey) {
+ this.rootStore.ui.setNotification({
+ id: toastId,
+ category: 'loading',
+ message: 'Assigning primary key to column...',
+ })
// Same logic in createTable: Remove any primary key constraints first (we'll add it back later)
// @ts-ignore
const existingPrimaryKeys = selectedTable.primary_keys.map((x) => x.name)
@@ -373,10 +384,45 @@ export default class MetaStore implements IMetaStore {
}
if (!isUndefined(foreignKey)) {
+ this.rootStore.ui.setNotification({
+ id: toastId,
+ category: 'loading',
+ message: 'Adding foreign key to column...',
+ })
const relation: any = await this.addForeignKey(foreignKey)
if (relation.error) throw relation.error
}
+
+ const { isEncrypted, keyId, keyName } = securityConfiguration || {}
+ if (isEncrypted) {
+ this.rootStore.ui.setNotification({
+ id: toastId,
+ category: 'loading',
+ message: 'Encrypting column...',
+ })
+ let encryptionKey = keyId
+ if (keyId === 'create-new') {
+ const addKeyRes = await this.rootStore.vault.addKey(keyName)
+ if (addKeyRes.error) throw addKeyRes.error
+ else encryptionKey = addKeyRes[0].id
+ }
+ const query = `security label for pgsodium on column "${column.table}"."${column.name}" is 'ENCRYPT WITH KEY ID ${encryptionKey}';`
+ const encryptColumnRes = await this.rootStore.meta.query(query)
+ if (encryptColumnRes.error) throw encryptColumnRes.error
+ }
+ this.rootStore.ui.setNotification({
+ id: toastId,
+ category: 'success',
+ message: `Successfully created column ${column.name}`,
+ })
} catch (error: any) {
+ this.rootStore.ui.setNotification({
+ id: toastId,
+ category: 'error',
+ message: `An error occured while creating the column "${payload.name}"`,
+ })
+ const query = `alter table "${selectedTable.name}" drop column if exists "${payload.name}";`
+ await this.rootStore.meta.query(query)
return { error }
}
}