Skip to content

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

Hook up update secret, but yet to test properly

Joshen Lim92accd9b48d7
diff --git a/studio/components/interfaces/Settings/Vault/Keys/EncryptionKeySelector.tsx b/studio/components/interfaces/Settings/Vault/Keys/EncryptionKeySelector.tsx
index d685f67631d..e46894cce50 100644
--- a/studio/components/interfaces/Settings/Vault/Keys/EncryptionKeySelector.tsx
+++ b/studio/components/interfaces/Settings/Vault/Keys/EncryptionKeySelector.tsx
@@ -48,7 +48,7 @@ const EncryptionKeySelector: FC<Props> = ({
{keys.map((key) => (
<Listbox.Option
key={key.id}
- label={key.status === 'default' ? `${key.id} (Default)` : key.id}
+ label={key.name === 'default_vault_key' ? `${key.id} (Default)` : key.id}
value={key.id}
>
<div className="space-y-1">
diff --git a/studio/components/interfaces/Settings/Vault/Secrets/EditSecretModal.tsx b/studio/components/interfaces/Settings/Vault/Secrets/EditSecretModal.tsx
index c6e93afef3b..58198b19f3f 100644
--- a/studio/components/interfaces/Settings/Vault/Secrets/EditSecretModal.tsx
+++ b/studio/components/interfaces/Settings/Vault/Secrets/EditSecretModal.tsx
@@ -1,7 +1,9 @@
+import { isEmpty } from 'lodash'
import { FC, useState, useEffect } from 'react'
+import { Modal, Form, Input, Button, IconEyeOff, IconEye } from 'ui'
+
import { useStore } from 'hooks'
import { VaultSecret } from 'types'
-import { Modal, Form, Input, Button, IconEyeOff, IconEye } from 'ui'
import EncryptionKeySelector from '../Keys/EncryptionKeySelector'
import ShimmeringLoader from 'components/ui/ShimmeringLoader'
@@ -11,10 +13,11 @@ interface Props {
}
const EditSecretModal: FC<Props> = ({ selectedSecret, onClose }) => {
- const { vault } = useStore()
- const [isLoadingSecretValue, setIsLoadingSecretValue] = useState(false)
- const [showSecretValue, setShowSecretValue] = useState(false)
+ const { ui, vault } = useStore()
const [selectedKeyId, setSelectedKeyId] = useState<string>()
+ const [secretValue, setSecretValue] = useState<string>()
+ const [showSecretValue, setShowSecretValue] = useState(false)
+ const [isLoadingSecretValue, setIsLoadingSecretValue] = useState(false)
let INITIAL_VALUES = {
name: selectedSecret?.name ?? '',
@@ -25,7 +28,10 @@ const EditSecretModal: FC<Props> = ({ selectedSecret, onClose }) => {
useEffect(() => {
if (selectedSecret !== undefined) {
setShowSecretValue(false)
- setSelectedKeyId(selectedSecret?.key_id)
+ setSelectedKeyId(selectedSecret.key_id)
+ if (selectedSecret.decryptedSecret !== undefined) {
+ setSecretValue(selectedSecret.decryptedSecret)
+ }
}
}, [selectedSecret])
@@ -37,9 +43,28 @@ const EditSecretModal: FC<Props> = ({ selectedSecret, onClose }) => {
}
const onUpdateSecret = async (values: any, { setSubmitting }: any) => {
- setSubmitting(true)
+ const payload: Partial<VaultSecret> = {}
+ if (values.name !== selectedSecret.name) payload.name = values.name
+ if (values.description !== selectedSecret.description) payload.description = values.description
+ if (values.secret !== secretValue) payload.secret = values.secret
+ if (selectedKeyId !== selectedSecret.key_id) payload.key_id = selectedKeyId
- setSubmitting(false)
+ if (!isEmpty(payload)) {
+ setSubmitting(true)
+ const res = await vault.updateSecret(selectedSecret.id, payload)
+ if (!res.error) {
+ ui.setNotification({ category: 'success', message: 'Successfully updated secret' })
+ setSubmitting(false)
+ onClose()
+ } else {
+ ui.setNotification({
+ error: res.error,
+ category: 'error',
+ message: `Failed to update secret: ${res.error.message}`,
+ })
+ setSubmitting(false)
+ }
+ }
}
return (
@@ -69,6 +94,7 @@ const EditSecretModal: FC<Props> = ({ selectedSecret, onClose }) => {
if (selectedSecret === undefined) return
setIsLoadingSecretValue(true)
const res = await vault.fetchSecretValue(selectedSecret.id)
+ setSecretValue(res)
resetForm({
values: { ...INITIAL_VALUES, secret: res },
initialValues: { ...INITIAL_VALUES, secret: res },
diff --git a/studio/stores/project/VaultStore.ts b/studio/stores/project/VaultStore.ts
index d425e2a6c1a..cc281403107 100644
--- a/studio/stores/project/VaultStore.ts
+++ b/studio/stores/project/VaultStore.ts
@@ -16,7 +16,7 @@ export interface IVaultStore {
listSecrets: (filter?: any) => VaultSecret[]
addSecret: (secret: Partial<VaultSecret>) => any
- updateSecret: (payload: any) => any
+ updateSecret: (id: string, payload: Partial<VaultSecret>) => any
deleteSecret: (id: string) => any
fetchSecretValue: (id: string) => any
}
@@ -156,8 +156,20 @@ export default class VaultStore implements IVaultStore {
return res
}
- async updateSecret(payload: any) {
- const query = new Query().from('decrypted_secrets', 'vault')
+ async updateSecret(id: string, payload: Partial<VaultSecret>) {
+ const query = new Query()
+ .from('decrypted_secrets', 'vault')
+ .update(payload, { returning: true })
+ .match({ id })
+ .toSql()
+ const res = await this.rootStore.meta.query(query)
+ if (!res.error) {
+ this.data.secrets = this.data.secrets.map((secret) => {
+ if (secret.id === res[0].id) return res[0]
+ else return secret
+ })
+ }
+ return res
}
async deleteSecret(id: string) {