The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
cleans up vault text
Copplebd1696870f58
diff --git a/apps/www/_blog/2022-08-19-supabase-vault.mdx b/apps/www/_blog/2022-08-19-supabase-vault.mdxindex 31d55cf8743..9a3216f53f1 100644--- a/apps/www/_blog/2022-08-19-supabase-vault.mdx+++ b/apps/www/_blog/2022-08-19-supabase-vault.mdx@@ -1,6 +1,6 @@---title: 'Supabase Vault'-description: "Today we're pre-releasing Vault,a Postgres extension for managing secrets and encryption inside your database."+description: "Today we're announcing Vault, a Postgres extension for managing secrets and encryption inside your database."author: michelimage: lw5-community-day/community_day_og.jpgthumb: lw5-community-day/community_day_og.jpg@@ -13,20 +13,20 @@ toc_depth: 2Today we're pre-releasing [Supabase Vault](https://github.com/supabase/vault), a Postgres extension for managing secrets and encryption inside your database.Vault is a thin usability-layer on top of [pgsodium](https://github.com/michelp/pgsodium).-Vault is not available on the Supabase platform yet as we finalize usability and security testing.+Vault is not available on the Supabase platform yet. We're finalizing usability and security testing.This release is intended to gather user-feedback from our community and customers.-The SQL code for the vault itself is relatively simply, so if you want to live on the edge a bit all the Vault requires is the pgosdium extension.-Soon we will be releasing a blog post that describes more of the technology in [libsodium](https://doc.libsodium.org/) and-[pgsodium](https://doc.libsodium.org/) that's behind the Vault. For now, we will go through a quick example of storing a secret, like a service access token, into the Vault.+Soon, we'll release a blog post that describes more about the technology behind Vault - [libsodium](https://doc.libsodium.org/) and+[pgsodium](https://doc.libsodium.org/). For now, we will go through a quick example of storing a secret, like a service access token, into the Vault.## BackgroundUntil now, the industry-standard for PostgreSQL encryption is a built-in extension called [`pgcrypto`](https://www.postgresql.org/docs/current/pgcrypto.html).Like most cloud providers, Supabase offers `pgcrypto` for developers to use in their applications. `pgcrypto` has been around for a long time,-and while it supports some basic encryption and decryption abilities, it lacks features like public key signing, key derivation APIs, streaming encryption, and other modern features required by advanced security-first applications.+and while it supports some basic encryption and decryption abilities, it lacks features like public key signing, key derivation APIs, streaming encryption,+and other modern features required by security-first applications.-### Problems with pgcrypto+### Problems with raw keysDatabases often store sensitive information, and they need tools that guarantee this data is stored and backed-up in an encrypted form.A fundamental issue with `pgcrypto` is that it lacks the ability to derive keys from outside of SQL. Instead you must have the raw encryption key for the algorithm you wish to use:@@ -35,50 +35,58 @@ A fundamental issue with `pgcrypto` is that it lacks the ability to derive keyscreate extension pgcrypto;create table users (- id serial primary key,- email varchar not null unique+ id serial primary key,+ email varchar not null unique);-insert into users (email) values- (pgp_sym_encrypt('[email protected]', 's3kr3t_k3y')),- (pgp_sym_encrypt('[email protected]', 's3kr3t_key'));+insert into users+ (email)+values+ (pgp_sym_encrypt('[email protected]', 's3kr3t_k3y')),+ (pgp_sym_encrypt('[email protected]', 's3kr3t_key'));```-`pgcrypto` works with _raw_ keys. In order to encrypt the data with pgcrypto you must pass the key directly to the encryption function.+pgcrypto works with _raw_ keys. In order to encrypt the data with pgcrypto you must pass the key directly to the encryption function.Leaking those raw keys is all too easy - logs, files, clients, tables, replication streams - you name it.-Wouldn't it be great if you could encrypt data, but instead of specifying the raw key, you specify some indirect way, like with a key ID?+Wouldn't it be great if you could encrypt data, but instead of specifying the raw key you reference it indirectly, like with a key ID?## Supabase Vault-We've created a [new PostgreSQL extension](https://github.com/supabase/vault) called `vault`.-The Vault is a good place to put things like API keys, access tokens, and other secrets from external services that you use in your application.+Supabase Vault allows you to store secrets without leaking any sensitive information.++The Vault is a good place to put things like API keys, access tokens, and other secrets from external services that you need to access within your database.The core of the Supabase Vault is a table with some metadata and an encrypted text column where you can put your secrets and any metadata related to them.-We take the pain out of key management by pre-generating a unique, per-database Key ID that is used by default - a “root” key - which is stored outside of the SQL language,+We take the pain out of key management by pre-generating a unique, per-database key that is used by default - a “root” key - which is stored outside of the SQL language,accessibly only internally in the Postgres server by the libsodium library. This root key is managed by the [pgsodium](https://github.com/michelp/pgsodium) Postgres extensionwhen the server boots using [Server Key Management.](https://github.com/michelp/pgsodium#server-key-management)pgsodium provides an Encryption and Key Derivation API based on the [libsodium library](https://libsodium.gitbook.io/doc/) and can get it's root key from a-variety of source depending on how you configure it. Supabase generates and preserves your project's root key behind the scenes, so you don't have to worry about it.+variety of sources depending on how you configure it. Supabase generates and preserves your project's root key behind the scenes, so you don't have to worry about it.If you install pgsodium locally the default is to generate a random root key from the `/dev/urandom` device which is then saved in a file in your Postgres data directory.-To get started, install the `vault` extension:+Installing the `vault` extension is the same as any other Postgres extension:```sqlcreate extension vault with schema vault;```-Once enabled, you can start inserting secrets into the `vault.secrets` table immediately:+Once enabled, you can insert secrets into the `vault.secrets` table:```sql-insert into vault.secrets (secret, associated)- values ('s3kr3t_k3y', 'This is the secret API service key.');+insert into vault.secrets+ (secret, associated)+values+ ('s3kr3t_k3y', 'This is the secret API service key.');```Now when you look in the `vault.secrets` table, the secret is encrypted:```sql-> select * from vault.secrets;+select * from vault.secrets;+```++```text hideCopy-[ RECORD 1 ]--------------------------------------------------------id | f6a2fe0a-3471-4eea-a581-75c4d2be396bsecret | /eT9bb96POTJ7L2gYrluTZ3r3pG5IMwPSQo6pQP0xdZTarpRrpWPXTWQ@@ -90,8 +98,9 @@ created_at | 2022-08-18 19:33:15.312651+00Notice how the row has a `key_id` column. This is the _ID_ of the internally derived key that is used to encrypt the secret, not the key itself.The actual raw key is not available to you in SQL, it is managed entirely outside of the SQL language in the Postgres server.-pgsodium [supports a variety of ways](https://github.com/michelp/pgsodium#server-key-management) to place the root key into Postgres.+At Supabase, we manage this key for your project automatically and generate a unique default Key ID for you in the `secrets` table.+For self-hosting, pgsodium [supports a variety of ways](https://github.com/michelp/pgsodium#server-key-management) to place the root key into Postgres.To see the decrypted data, there is a special view created called `vault.decrypted_secrets`:@@ -99,9 +108,7 @@ To see the decrypted data, there is a special view created called `vault.decryptselect * from vault.decrypted_secrets;```-<small>Result</small>--```+```text hideCopy-[ RECORD 1 ]----+---------------------------------------------------------id | f6a2fe0a-3471-4eea-a581-75c4d2be396bsecret | /eT9bb96POTJ7L2gYrluTZ3r3pG5IMwPSQo6pQP0xdZTarpRrpWPXTWQ@@ -112,19 +119,17 @@ nonce | \x77c7381c523630ba72f1f137626a9f9acreated_at | 2022-08-18 19:33:15.312651+00```-Now you can see that there is a new `decrypted_secret` column that contains the decrypted secret that was originally inserted into the table.+Now you can see a new `decrypted_secret` column that contains the decrypted secret we originally inserted into the table.This `vault.decrypted_secrets` view automatically decrypts rows in the `vault.secrets` table “on-the-fly” as you query them, but the secret is stored on disk in _encrypted_ form.If you take a backup, or pause your project, that data remains encrypted. We will keep your hidden root key safe in our backend systems for when you need to restore or un-pause your projects.-If you wish to use different key ids for different secrets, you can create new keys with the `pgsodium.create_key()` function:+If you wish to use your own Key ID for different secrets, insted of the default Key ID we've generated, you can create one using the `pgsodium.create_key()` function:```sqlselect * from pgsodium.create_key('API_KEY_DEV');```-<small>Result</small>--```+```text hideCopy-[ RECORD 1 ]-------------------------------------id | f9f176eb-7069-4743-9403-582c04354ffcstatus | valid@@ -149,9 +154,7 @@ values (returning *;```-<small>Result</small>--```+```text hideCopy-[ RECORD 1 ]------------------------------------------------------------id | 9c58a0f3-aa40-4789-b683-6db48b241f9esecret | YWxuTnWdF55MuRrZ7xneBvaz2uH59U1dJV/7CCZjSn5B5jELOoy/csq8x/s=@@ -163,15 +166,15 @@ created_at | 2022-08-18 22:34:07.219941+00The type of encryption used by the Vault is called [Authenticated Encryption with Associated Data](https://en.wikipedia.org/wiki/Authenticated_encryption).The data you insert into the `associated` column is combined with the encrypted text when libsodium creates the authentication signature for the secret.-This means that when you read the secret, you know that the associated data is also authentic. This would be a good place to put account IDs or other-data that ties your system to the secret. And as always, you can refer to rows in the secrets table by their primary key UUID.+This means that when you read the secret, you know that the associated data is also authentic. The associated data could be an account ID or some+information that ties your system to the secret. And as always, you can refer to rows in the secrets table by their primary key UUID.If you only want to store secrets that you know are encrypted on disk and in backups, then all you need to know is shown above. Just insert secrets into the table,-optionally creating new keys depending on your purposes, and select them from the view when you want to use them.+optionally creating new keys, and select them from the view when you want to use them.## Going Beyond the Vault-The Vault is good for a reasonable amount of secure data, like API keys for services, access tokens or environment variables,-but if you have a lot more sensitive information, like personally Identifiable Information (PII) that you want to relate to your overall model,-you may want to break them out into side tables that uses pgsodium's+The Vault is good for a reasonable amount of secure data, like API keys, access tokens, or environment variables.+But if you have a lot more sensitive information, like personally Identifiable Information (PII),+you may want to break them out into side-tables using pgsodium's[Transparent Column Encryption](https://github.com/michelp/pgsodium#transparent-column-encryption) which we will describe soon in a follow-up blog post. Stay tuned!