-
-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: s3 protocol
- Loading branch information
Showing
99 changed files
with
6,644 additions
and
670 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,16 +56,13 @@ jobs: | |
SERVICE_KEY: ${{ secrets.SERVICE_KEY }} | ||
TENANT_ID: ${{ secrets.TENANT_ID }} | ||
REGION: ${{ secrets.REGION }} | ||
POSTGREST_URL: ${{ secrets.POSTGREST_URL }} | ||
GLOBAL_S3_BUCKET: ${{ secrets.GLOBAL_S3_BUCKET }} | ||
PGRST_JWT_SECRET: ${{ secrets.PGRST_JWT_SECRET }} | ||
AUTHENTICATED_KEY: ${{ secrets.AUTHENTICATED_KEY }} | ||
DATABASE_URL: postgresql://postgres:[email protected]/postgres | ||
PGOPTIONS: -c search_path=storage,public | ||
FILE_SIZE_LIMIT: '52428800' | ||
STORAGE_BACKEND: s3 | ||
MULTITENANT_DATABASE_URL: postgresql://postgres:[email protected]:5433/postgres | ||
POSTGREST_URL_SUFFIX: /rest/v1 | ||
ADMIN_API_KEYS: apikey | ||
ENABLE_IMAGE_TRANSFORMATION: true | ||
IMGPROXY_URL: http://127.0.0.1:50020 | ||
|
@@ -79,6 +76,9 @@ jobs: | |
ENABLE_DEFAULT_METRICS: false | ||
PG_QUEUE_ENABLE: false | ||
MULTI_TENANT: false | ||
S3_PROTOCOL_ACCESS_KEY_ID: ${{ secrets.TENANT_ID }} | ||
S3_PROTOCOL_ACCESS_KEY_SECRET: ${{ secrets.SERVICE_KEY }} | ||
|
||
|
||
- name: Upload coverage results to Coveralls | ||
uses: coverallsapp/github-action@master | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
import { getConfig, setEnvPaths } from './src/config' | ||
|
||
setEnvPaths(['.env.test', '.env']) | ||
|
||
beforeEach(() => { | ||
getConfig({ reload: true }) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
|
||
|
||
CREATE TABLE IF NOT EXISTS tenants_s3_credentials ( | ||
id UUID PRIMARY KEY default gen_random_uuid(), | ||
description text NOT NULL, | ||
tenant_id text REFERENCES tenants(id) ON DELETE CASCADE, | ||
access_key text NOT NULL, | ||
secret_key text NOT NULL, | ||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW() | ||
); | ||
|
||
CREATE INDEX IF NOT EXISTS tenants_s3_credentials_tenant_id_idx ON tenants_s3_credentials(tenant_id); | ||
CREATE UNIQUE INDEX IF NOT EXISTS tenants_s3_credentials_access_key_idx ON tenants_s3_credentials(tenant_id, access_key); | ||
|
||
|
||
CREATE OR REPLACE FUNCTION tenants_s3_credentials_update_notify_trigger () | ||
RETURNS TRIGGER | ||
AS $$ | ||
BEGIN | ||
PERFORM | ||
pg_notify('tenants_s3_credentials_update', '"' || NEW.id || ':' || NEW.access_key || '"'); | ||
RETURN NULL; | ||
END; | ||
$$ | ||
LANGUAGE plpgsql; | ||
|
||
CREATE OR REPLACE FUNCTION tenants_s3_credentials_delete_notify_trigger () | ||
RETURNS TRIGGER | ||
AS $$ | ||
BEGIN | ||
PERFORM | ||
pg_notify('tenants_s3_credentials_update', '"' || OLD.id || ':' || OLD.access_key || '"'); | ||
RETURN NULL; | ||
END; | ||
$$ | ||
LANGUAGE plpgsql; | ||
|
||
CREATE TRIGGER tenants_s3_credentials_update_notify_trigger | ||
AFTER UPDATE ON tenants_s3_credentials | ||
FOR EACH ROW | ||
EXECUTE PROCEDURE tenants_s3_credentials_update_notify_trigger (); | ||
|
||
CREATE TRIGGER tenants_s3_credentials_delete_notify_trigger | ||
AFTER DELETE ON tenants_s3_credentials | ||
FOR EACH ROW | ||
EXECUTE PROCEDURE tenants_s3_credentials_delete_notify_trigger (); |
3 changes: 3 additions & 0 deletions
3
migrations/multitenant/0009-add-scope-token-column-to-tenants-s3.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
|
||
|
||
ALTER TABLE tenants_s3_credentials ADD COLUMN claims json NOT NULL DEFAULT '{}'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
|
||
|
||
CREATE OR REPLACE FUNCTION storage.list_objects_with_delimiter(bucket_id text, prefix_param text, delimiter_param text, max_keys integer default 100, start_after text DEFAULT '', next_token text DEFAULT '') | ||
RETURNS TABLE (name text, id uuid, metadata jsonb, updated_at timestamptz) AS | ||
$$ | ||
BEGIN | ||
RETURN QUERY EXECUTE | ||
'SELECT DISTINCT ON(name COLLATE "C") * from ( | ||
SELECT | ||
CASE | ||
WHEN position($2 IN substring(name from length($1) + 1)) > 0 THEN | ||
substring(name from 1 for length($1) + position($2 IN substring(name from length($1) + 1))) | ||
ELSE | ||
name | ||
END AS name, id, metadata, updated_at | ||
FROM | ||
storage.objects | ||
WHERE | ||
bucket_id = $5 AND | ||
name ILIKE $1 || ''%'' AND | ||
CASE | ||
WHEN $6 != '''' THEN | ||
name COLLATE "C" > $6 | ||
ELSE true END | ||
AND CASE | ||
WHEN $4 != '''' THEN | ||
CASE | ||
WHEN position($2 IN substring(name from length($1) + 1)) > 0 THEN | ||
substring(name from 1 for length($1) + position($2 IN substring(name from length($1) + 1))) COLLATE "C" > $4 | ||
ELSE | ||
name COLLATE "C" > $4 | ||
END | ||
ELSE | ||
true | ||
END | ||
ORDER BY | ||
name COLLATE "C" ASC) as e order by name COLLATE "C" LIMIT $3' | ||
USING prefix_param, delimiter_param, max_keys, next_token, bucket_id, start_after; | ||
END; | ||
$$ LANGUAGE plpgsql; | ||
|
||
CREATE INDEX IF NOT EXISTS idx_objects_bucket_id_name | ||
ON storage.objects (bucket_id, (name COLLATE "C")); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
CREATE TABLE IF NOT EXISTS storage.s3_multipart_uploads ( | ||
id text PRIMARY KEY, | ||
in_progress_size int NOT NULL default 0, | ||
upload_signature text NOT NULL, | ||
bucket_id text NOT NULL references storage.buckets(id), | ||
key text COLLATE "C" NOT NULL , | ||
version text NOT NULL, | ||
owner_id text NULL, | ||
created_at timestamptz NOT NULL default now() | ||
); | ||
|
||
CREATE TABLE IF NOT EXISTS storage.s3_multipart_uploads_parts ( | ||
id uuid PRIMARY KEY default gen_random_uuid(), | ||
upload_id text NOT NULL references storage.s3_multipart_uploads(id) ON DELETE CASCADE, | ||
size int NOT NULL default 0, | ||
part_number int NOT NULL, | ||
bucket_id text NOT NULL references storage.buckets(id), | ||
key text COLLATE "C" NOT NULL, | ||
etag text NOT NULL, | ||
owner_id text NULL, | ||
version text NOT NULL, | ||
created_at timestamptz NOT NULL default now() | ||
); | ||
|
||
CREATE INDEX IF NOT EXISTS idx_multipart_uploads_list | ||
ON storage.s3_multipart_uploads (bucket_id, (key COLLATE "C"), created_at ASC); | ||
|
||
CREATE OR REPLACE FUNCTION storage.list_multipart_uploads_with_delimiter(bucket_id text, prefix_param text, delimiter_param text, max_keys integer default 100, next_key_token text DEFAULT '', next_upload_token text default '') | ||
RETURNS TABLE (key text, id text, created_at timestamptz) AS | ||
$$ | ||
BEGIN | ||
RETURN QUERY EXECUTE | ||
'SELECT DISTINCT ON(key COLLATE "C") * from ( | ||
SELECT | ||
CASE | ||
WHEN position($2 IN substring(key from length($1) + 1)) > 0 THEN | ||
substring(key from 1 for length($1) + position($2 IN substring(key from length($1) + 1))) | ||
ELSE | ||
key | ||
END AS key, id, created_at | ||
FROM | ||
storage.s3_multipart_uploads | ||
WHERE | ||
bucket_id = $5 AND | ||
key ILIKE $1 || ''%'' AND | ||
CASE | ||
WHEN $4 != '''' AND $6 = '''' THEN | ||
CASE | ||
WHEN position($2 IN substring(key from length($1) + 1)) > 0 THEN | ||
substring(key from 1 for length($1) + position($2 IN substring(key from length($1) + 1))) COLLATE "C" > $4 | ||
ELSE | ||
key COLLATE "C" > $4 | ||
END | ||
ELSE | ||
true | ||
END AND | ||
CASE | ||
WHEN $6 != '''' THEN | ||
id COLLATE "C" > $6 | ||
ELSE | ||
true | ||
END | ||
ORDER BY | ||
key COLLATE "C" ASC, created_at ASC) as e order by key COLLATE "C" LIMIT $3' | ||
USING prefix_param, delimiter_param, max_keys, next_key_token, bucket_id, next_upload_token; | ||
END; | ||
$$ LANGUAGE plpgsql; | ||
|
||
ALTER TABLE storage.s3_multipart_uploads ENABLE ROW LEVEL SECURITY; | ||
ALTER TABLE storage.s3_multipart_uploads_parts ENABLE ROW LEVEL SECURITY; | ||
|
||
DO $$ | ||
DECLARE | ||
anon_role text = COALESCE(current_setting('storage.anon_role', true), 'anon'); | ||
authenticated_role text = COALESCE(current_setting('storage.authenticated_role', true), 'authenticated'); | ||
service_role text = COALESCE(current_setting('storage.service_role', true), 'service_role'); | ||
BEGIN | ||
EXECUTE 'revoke all on storage.s3_multipart_uploads from ' || anon_role || ', ' || authenticated_role; | ||
EXECUTE 'revoke all on storage.s3_multipart_uploads_parts from ' || anon_role || ', ' || authenticated_role; | ||
EXECUTE 'GRANT ALL ON TABLE storage.s3_multipart_uploads TO ' || service_role; | ||
EXECUTE 'GRANT ALL ON TABLE storage.s3_multipart_uploads_parts TO ' || service_role; | ||
EXECUTE 'GRANT SELECT ON TABLE storage.s3_multipart_uploads TO ' || authenticated_role || ', ' || anon_role; | ||
EXECUTE 'GRANT SELECT ON TABLE storage.s3_multipart_uploads_parts TO ' || authenticated_role || ', ' || anon_role; | ||
END$$; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
ALTER TABLE storage.s3_multipart_uploads ALTER COLUMN in_progress_size TYPE bigint; | ||
ALTER TABLE storage.s3_multipart_uploads_parts ALTER COLUMN size TYPE bigint; |
Oops, something went wrong.