• Stars
    star
    161
  • Rank 225,048 (Top 5 %)
  • Language PLpgSQL
  • License
    Apache License 2.0
  • Created over 1 year ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Extension for storing encrypted secrets in the Vault

Introduction to the Vault (Beta)

Many applications have sensitive data that must have additional storage protection relative to other data. For example, your application may access external services with an "API Key". This key is issued to you by that specific external service provider, and you must keep it safe from being stolen or leaked. If someone got their hands on your payment processor key, for example, they may be able to use it to send money or digital assets out of your account to someone else. Wherever this key is stored, it would make sense to store it in an encrypted form.

Supabase provides a table called vault.secrets that can be used to store sensitive information like API keys. These secrets will be stored in an encrypted format on disk and in any database dumps. This is often called Encryption At Rest. Decrypting this table is done through a special database view called vault.decrypted_secrets that uses an encryption key that is itself not avaiable to SQL, but can be referred to by ID. Supabase manages these internal keys for you, so you can't leak them out of the database, you can only refer to them by their ids.

Installation

The Vault extension is enabled by default. If you install the Vault yourself locally, from SQL you can do:

CREATE EXTENSION supabase_vault CASCADE;

Using the Vault

Using the vault is as simple as INSERTing data into the vault.secret table.

postgres=> INSERT INTO vault.secrets (secret) VALUES ('s3kre3t_k3y') RETURNING *;
-[ RECORD 1 ]-------------------------------------------------------------
id          | d91596b8-1047-446c-b9c0-66d98af6d001
name        | 
description | 
secret      | S02eXS9BBY+kE3r621IS8beAytEEtj+dDHjs9/0AoMy7HTbog+ylxcS22A==
key_id      | 7f5ad44b-6bd5-4c99-9f68-4b6c7486f927
nonce       | \x3aa2e92f9808e496aa4163a59304b895
created_at  | 2022-12-14 02:29:21.3625+00
updated_at  | 2022-12-14 02:29:21.3625+00

There is also a handy function for creating secrets called vault.create_secret():

postgres=> select vault.create_secret('another_s3kre3t');
-[ RECORD 1 ]-+-------------------------------------
create_secret | c9b00867-ca8b-44fc-a81d-d20b8169be17

The function returns the UUID of the new secret.

Name and Description

Secrets can also have an optional unique name, or an optional description. These are also arguments to vault.create_secret():

postgres=> select vault.create_secret('another_s3kre3t', 'unique_name', 'This is the description');
-[ RECORD 1 ]-+-------------------------------------
create_secret | 7095d222-efe5-4cd5-b5c6-5755b451e223

postgres=> select * from vault.secrets where id = '7095d222-efe5-4cd5-b5c6-5755b451e223';
-[ RECORD 1 ]-----------------------------------------------------------------
id          | 7095d222-efe5-4cd5-b5c6-5755b451e223
name        | unique_name
description | This is the description
secret      | 3mMeOcoG84a5F2uOfy2ugWYDp9sdxvCTmi6kTeT97bvA8rCEsG5DWWZtTU8VVeE=
key_id      | c62da7a0-b85d-471d-8ea7-52aae21d7354
nonce       | \x9f2d60954ba5eb566445736e0760b0e3
created_at  | 2022-12-14 02:34:23.85159+00
updated_at  | 2022-12-14 02:34:23.85159+00

Querying Data from the Vault

If you look in the vault.secrets table, you will see that your data is stored encrypted. To decrypt the data, there is an automatically created view vault.decrypted_secrets. This view will decrypt secret data on the fly:

postgres=> select * from vault.decrypted_secrets order by created_at desc limit 3;
-[ RECORD 1 ]----+-----------------------------------------------------------------
id               | 7095d222-efe5-4cd5-b5c6-5755b451e223
name             | unique_name
description      | This is the description
secret           | 3mMeOcoG84a5F2uOfy2ugWYDp9sdxvCTmi6kTeT97bvA8rCEsG5DWWZtTU8VVeE=
decrypted_secret | another_s3kre3t
key_id           | c62da7a0-b85d-471d-8ea7-52aae21d7354
nonce            | \x9f2d60954ba5eb566445736e0760b0e3
created_at       | 2022-12-14 02:34:23.85159+00
updated_at       | 2022-12-14 02:34:23.85159+00
-[ RECORD 2 ]----+-----------------------------------------------------------------
id               | c9b00867-ca8b-44fc-a81d-d20b8169be17
name             | 
description      | 
secret           | a1CE4vXwQ53+N9bllJj1D7fasm59ykohjb7K90PPsRFUd9IbBdxIGZNoSQLIXl4=
decrypted_secret | another_s3kre3t
key_id           | 8c72b05e-b931-4372-abf9-a09cfad18489
nonce            | \x1d3b2761548c4efb2d29ca11d44aa22f
created_at       | 2022-12-14 02:32:50.58921+00
updated_at       | 2022-12-14 02:32:50.58921+00
-[ RECORD 3 ]----+-----------------------------------------------------------------
id               | d91596b8-1047-446c-b9c0-66d98af6d001
name             | 
description      | 
secret           | S02eXS9BBY+kE3r621IS8beAytEEtj+dDHjs9/0AoMy7HTbog+ylxcS22A==
decrypted_secret | s3kre3t_k3y
key_id           | 7f5ad44b-6bd5-4c99-9f68-4b6c7486f927
nonce            | \x3aa2e92f9808e496aa4163a59304b895
created_at       | 2022-12-14 02:29:21.3625+00
updated_at       | 2022-12-14 02:29:21.3625+00

Notice how this view has a decrypted_secret column that contains the decrypted secrets. Views are not stored on disk, they are only run at query time, so the secret remains encrypted on disk, and in any backup dumps or replication streams.

You should ensure that you protect access to this view with the appropriate SQL privilege settings at all times, as anyone that has access to the view has access to decrypted secrets.

Updating Secrets

A secret can be updated with the vault.update_secret() function, this function makes updating secrets easy, just provide the secret UUID as the first argument, and then an updated secret, updated optional unique name, or updated description:

postgres=> select vault.update_secret('7095d222-efe5-4cd5-b5c6-5755b451e223', 'n3w_upd@ted_s3kret', 
    'updated_unique_name', 'This is the updated description');
-[ RECORD 1 ]-+-
update_secret | 

postgres=> select * from vault.decrypted_secrets where id = '7095d222-efe5-4cd5-b5c6-5755b451e223';
-[ RECORD 1 ]----+---------------------------------------------------------------------
id               | 7095d222-efe5-4cd5-b5c6-5755b451e223
name             | updated_unique_name
description      | This is the updated description
secret           | lhb3HBFxF+qJzp/HHCwhjl4QFb5dYDsIQEm35DaZQOovdkgp2iy6UMufTKJGH4ThMrU=
decrypted_secret | n3w_upd@ted_s3kret
key_id           | c62da7a0-b85d-471d-8ea7-52aae21d7354
nonce            | \x9f2d60954ba5eb566445736e0760b0e3
created_at       | 2022-12-14 02:34:23.85159+00
updated_at       | 2022-12-14 02:51:13.938396+00

Internal Details

To encrypt data, you need a key id. You can use the default key id created automatically for every project, or create your own key ids Using the pgsodium.create_key() function. Key ids are used to internally derive the encryption key used to encrypt secrets in the vault. Vault users typically do not have access to the key itself, only the key id.

Both vault.create_secret() and vault.update_secret() take an optional fourth new_key_id argument. This argument can be used to store a different key id for the secret instead of the default value.

postgres=> select vault.create_secret('another_s3kre3t_key', 'another_unique_name', 
   'This is another description', (pgsodium.create_key()).id);
-[ RECORD 1 ]-+-------------------------------------
create_secret | cec9e005-a44d-4b19-86e1-febf3cd40619

Which roles should have access to the vault.secrets table should be carefully considered. There are two ways to grant access, the first is that the postgres user can explicitly grant access to the vault table itself.

Turning off Statement Logging

When you insert secrets into the vault table with an INSERT statement, those statements get logged by default into the Supabase logs. Since this would mean your secrets are stored unencrypted in the logs, you should turn off statement logging while using the Vault.

While turning off statement logging does hinder you if you're used to looking at the logs to debug your application, it provides a much higher level of security by ensuring that your data does not leak out of the database and into the logs. This is especially critical with encrypted column data, because the statement logs will contain the unencrypted secrets. If you must store that data encrypted, then you must turn off statement logging.

ALTER SYSTEM SET statement_log = 'none';

And then restart your project from the dashboard to enable that change.

In the future we are researching various ways to refine the way statement logging interacts with sensitive columns.

More Repositories

1

supabase

The open source Firebase alternative.
TypeScript
65,693
star
2

realtime

Broadcast, Presence, and Postgres Changes via WebSockets
Elixir
6,458
star
3

postgres_lsp

A Language Server for Postgres
Rust
3,073
star
4

supabase-js

An isomorphic Javascript client for Supabase. Query your Supabase database, subscribe to realtime events, upload and download files, browse typescript examples, invoke postgres functions via rpc, invoke supabase edge functions, query pgvector.
TypeScript
2,795
star
5

pg_graphql

GraphQL support for PostgreSQL
Rust
2,760
star
6

supavisor

A cloud-native, multi-tenant Postgres connection pooler.
Elixir
1,574
star
7

ui

Supabase UI Library
TypeScript
1,510
star
8

postgres

Unmodified Postgres with some useful plugins
Shell
1,265
star
9

index_advisor

PostgreSQL Index Advisor
PLpgSQL
1,263
star
10

auth

A JWT based API for managing users and issuing JWT tokens
Go
1,159
star
11

pg_jsonschema

PostgreSQL extension providing JSON Schema validation
Rust
929
star
12

postgrest-js

Isomorphic JavaScript client for PostgREST.
TypeScript
917
star
13

auth-helpers

A collection of framework specific Auth utilities for working with Supabase.
TypeScript
877
star
14

cli

Supabase CLI. Manage postgres migrations, run Supabase locally, deploy edge functions. Postgres backups. Generating types from your database schema.
Go
849
star
15

postgres-meta

A RESTful API for managing your Postgres. Fetch tables, add roles, and run queries
TypeScript
820
star
16

storage

S3 compatible object storage service that stores metadata in Postgres
TypeScript
640
star
17

supabase-flutter

Flutter integration for Supabase. This package makes it simple for developers to build secure and scalable products.
Dart
613
star
18

supa_audit

Generic Table Auditing
PLpgSQL
611
star
19

supabase-swift

A Swift client for Supabase
Swift
583
star
20

edge-runtime

A server based on Deno runtime, capable of running JavaScript, TypeScript, and WASM services.
Rust
557
star
21

stripe-sync-engine

Sync your Stripe account to you Postgres database.
TypeScript
457
star
22

wrappers

Postgres Foreign Data Wrapper development framework in Rust.
Rust
433
star
23

auth-ui

Pre-built Auth UI for React
TypeScript
405
star
24

supabase-dart

A Dart client for Supabase
Dart
402
star
25

pg_crdt

POC CRDT support in Postgres
Rust
372
star
26

dbdev

Database Package Registry for Postgres
PLpgSQL
329
star
27

auth-js

An isomorphic Javascript library for Supabase Auth.
CSS
298
star
28

realtime-js

An isomorphic Javascript client for Supabase Realtime server.
JavaScript
288
star
29

examples-archive

Supabase Examples Archive
TypeScript
278
star
30

pg_netstat

PostgreSQL extension to monitor database network traffic
Rust
246
star
31

grid

A react component to display your Postgresql table data. Used in Supabase Dashboard app.
TypeScript
197
star
32

vecs

Postgres/pgvector Python Client
Python
190
star
33

libcluster_postgres

Postgres strategy for libcluster
Elixir
178
star
34

pg_net

A PostgreSQL extension that enables asynchronous (non-blocking) HTTP/HTTPS requests with SQL
PLpgSQL
165
star
35

postgrest-dart

Dart client for PostgREST
Dart
137
star
36

headless-vector-search

Supabase Toolkit to perform vector similarity search on your knowledge base embeddings.
TypeScript
135
star
37

workflows

Elixir
133
star
38

supabase-grafana

Observability for your Supabase project, using Prometheus/Grafana
Shell
132
star
39

walrus

Applying RLS to PostgreSQL WAL
PLpgSQL
117
star
40

storage-js

JS Client library to interact with Supabase Storage
TypeScript
111
star
41

postgres-deno

A PostgreSQL extension for Deno: run Typescript in PostgreSQL functions and triggers.
104
star
42

realtime-dart

A dart client for Supabase Realtime server.
Dart
85
star
43

setup-cli

A GitHub action for interacting with your Supabase projects using the CLI.
TypeScript
83
star
44

repository.surf

πŸ„
JavaScript
80
star
45

embeddings-generator

GitHub Action to generate embeddings from the markdown files in your repository.
TypeScript
79
star
46

supabase-ui-web

TypeScript
74
star
47

self-hosted-edge-functions-demo

A demo of how to self-host Supabase Edge Functions on Fly.io
TypeScript
61
star
48

functions-js

TypeScript
54
star
49

supabase-admin-api

API to administer the Supabase server (KPS)
Go
51
star
50

supautils

PostgreSQL extension that secures a cluster on a cloud environment
C
49
star
51

gotrue-dart

A dart client library for GoTrue.
Dart
47
star
52

supabase-action-example

TypeScript
45
star
53

benchmarks

SCSS
41
star
54

grafana-agent-fly-example

Deploy a Grafana Agent on Fly to scrape Prometheus metrics from Supabase and send them to Grafana Cloud
Shell
36
star
55

functions-relay

API Gateway for Supabase Edge functions
TypeScript
35
star
56

nix-postgres

Experimental port of supabase/postgres to Nix
Nix
35
star
57

benchmarks-archive

Infrastucture benchmarks
Nix
31
star
58

hibp

Go library for HaveIBeenPwned.org's pwned passwords API.
Go
29
star
59

splinter

Supabase Postgres Linter
PLpgSQL
28
star
60

supabase.ai

iykyk
HTML
27
star
61

storage-dart

Dart client library to interact with Supabase Storage
Dart
22
star
62

livebooks

A collection of Elixir Livebooks for Supabase
Dockerfile
20
star
63

base64url-js

Pure TypeScript implementation of Base64-URL encoding for JavaScript strings.
TypeScript
19
star
64

terraform-provider-supabase

Go
17
star
65

orb-sync-engine

TypeScript
12
star
66

.github

Org-wide default community health files & templates.
11
star
67

auth-elements

Components to add Supabase Auth to any application
TypeScript
11
star
68

rfcs

11
star
69

functions-dart

Dart
8
star
70

test-reports

Repository to store test reports data and host reporting in gh-pages
7
star
71

plug_caisson

An Elixir Plug library for handling compressed requests
Elixir
6
star
72

flyswatter

Deploy a global pinger on Fly
Elixir
6
star
73

scoop-bucket

4
star
74

tests

TypeScript
4
star
75

pgextkit

Rust
3
star
76

homebrew-tap

Ruby
3
star
77

fly-preview

TypeScript
3
star
78

shared-types

TypeScript
3
star
79

supa_type

The Missing PostgreSQL Data Types
Nix
3
star
80

test-inspector

Check your test results against the reference run and compare coverage for multiple client libraries
Go
2
star
81

mailme

A clone of Netlify's mailme package used in Supabase Auth / GoTrue.
Go
2
star
82

productions

Supabase SynthWave. The best soundtrack to build an app in a weekend and scale to billions.
TypeScript
1
star
83

design-tokens

1
star