From 38656e71ff2faf9ea88b4ea650567814e7f1248d Mon Sep 17 00:00:00 2001 From: Daniel Holmgren Date: Fri, 8 Mar 2024 19:48:45 -0600 Subject: [PATCH] Label expiration (#2241) * sketching out label sequencer * refactor sequencer * sequencer tests * tests * add query labels endpoint & tests * add pagination * fix label formatting on temp * tidy * format labels * make use listen/notify for sequencer * ensure sig on all outgoing labels from ozone * fixing up tests * fix sequencer tests * fix hanging server test * add log on failure to update label * update description for sig * add expiration to labels * fix test * use bytes for label sigs * fix tests * add ver to labels * tidy up background queue * store signing keys as ids * fix sequencer teest --- lexicons/com/atproto/label/defs.json | 5 +++++ packages/api/src/client/lexicons.ts | 6 ++++++ packages/api/src/client/types/com/atproto/label/defs.ts | 2 ++ packages/bsky/src/lexicon/lexicons.ts | 6 ++++++ packages/bsky/src/lexicon/types/com/atproto/label/defs.ts | 2 ++ .../src/db/migrations/20240228T003647759Z-add-label-sigs.ts | 2 ++ packages/ozone/src/db/schema/label.ts | 1 + packages/ozone/src/lexicon/lexicons.ts | 6 ++++++ packages/ozone/src/lexicon/types/com/atproto/label/defs.ts | 2 ++ packages/ozone/src/mod-service/util.ts | 5 ++++- packages/ozone/tests/sequencer.test.ts | 1 + packages/pds/src/lexicon/lexicons.ts | 6 ++++++ packages/pds/src/lexicon/types/com/atproto/label/defs.ts | 2 ++ 13 files changed, 45 insertions(+), 1 deletion(-) diff --git a/lexicons/com/atproto/label/defs.json b/lexicons/com/atproto/label/defs.json index 229ad192f2a..dc6fe3f83fa 100644 --- a/lexicons/com/atproto/label/defs.json +++ b/lexicons/com/atproto/label/defs.json @@ -40,6 +40,11 @@ "format": "datetime", "description": "Timestamp when this label was created." }, + "exp": { + "type": "string", + "format": "datetime", + "description": "Timestamp at which this label expires (no longer applies)." + }, "sig": { "type": "bytes", "description": "Signature of dag-cbor encoded label." diff --git a/packages/api/src/client/lexicons.ts b/packages/api/src/client/lexicons.ts index afc7ed866b1..a7934d471d5 100644 --- a/packages/api/src/client/lexicons.ts +++ b/packages/api/src/client/lexicons.ts @@ -2239,6 +2239,12 @@ export const schemaDict = { format: 'datetime', description: 'Timestamp when this label was created.', }, + exp: { + type: 'string', + format: 'datetime', + description: + 'Timestamp at which this label expires (no longer applies).', + }, sig: { type: 'bytes', description: 'Signature of dag-cbor encoded label.', diff --git a/packages/api/src/client/types/com/atproto/label/defs.ts b/packages/api/src/client/types/com/atproto/label/defs.ts index 425978d6d0e..cfa5bb648b2 100644 --- a/packages/api/src/client/types/com/atproto/label/defs.ts +++ b/packages/api/src/client/types/com/atproto/label/defs.ts @@ -22,6 +22,8 @@ export interface Label { neg?: boolean /** Timestamp when this label was created. */ cts: string + /** Timestamp at which this label expires (no longer applies). */ + exp?: string /** Signature of dag-cbor encoded label. */ sig?: Uint8Array [k: string]: unknown diff --git a/packages/bsky/src/lexicon/lexicons.ts b/packages/bsky/src/lexicon/lexicons.ts index afc7ed866b1..a7934d471d5 100644 --- a/packages/bsky/src/lexicon/lexicons.ts +++ b/packages/bsky/src/lexicon/lexicons.ts @@ -2239,6 +2239,12 @@ export const schemaDict = { format: 'datetime', description: 'Timestamp when this label was created.', }, + exp: { + type: 'string', + format: 'datetime', + description: + 'Timestamp at which this label expires (no longer applies).', + }, sig: { type: 'bytes', description: 'Signature of dag-cbor encoded label.', diff --git a/packages/bsky/src/lexicon/types/com/atproto/label/defs.ts b/packages/bsky/src/lexicon/types/com/atproto/label/defs.ts index 935b2aa4bb3..1af8b0f3890 100644 --- a/packages/bsky/src/lexicon/types/com/atproto/label/defs.ts +++ b/packages/bsky/src/lexicon/types/com/atproto/label/defs.ts @@ -22,6 +22,8 @@ export interface Label { neg?: boolean /** Timestamp when this label was created. */ cts: string + /** Timestamp at which this label expires (no longer applies). */ + exp?: string /** Signature of dag-cbor encoded label. */ sig?: Uint8Array [k: string]: unknown diff --git a/packages/ozone/src/db/migrations/20240228T003647759Z-add-label-sigs.ts b/packages/ozone/src/db/migrations/20240228T003647759Z-add-label-sigs.ts index 098d4c4b672..59e859faab6 100644 --- a/packages/ozone/src/db/migrations/20240228T003647759Z-add-label-sigs.ts +++ b/packages/ozone/src/db/migrations/20240228T003647759Z-add-label-sigs.ts @@ -1,6 +1,7 @@ import { Kysely, sql } from 'kysely' export async function up(db: Kysely): Promise { + await db.schema.alterTable('label').addColumn('exp', 'varchar').execute() await db.schema .alterTable('label') .addColumn('sig', sql`bytea`) @@ -18,6 +19,7 @@ export async function up(db: Kysely): Promise { export async function down(db: Kysely): Promise { await db.schema.dropTable('signing_key') + await db.schema.alterTable('label').dropColumn('exp').execute() await db.schema.alterTable('label').dropColumn('sig').execute() await db.schema.alterTable('label').dropColumn('signingKey').execute() } diff --git a/packages/ozone/src/db/schema/label.ts b/packages/ozone/src/db/schema/label.ts index d19937ce59d..58042478c8d 100644 --- a/packages/ozone/src/db/schema/label.ts +++ b/packages/ozone/src/db/schema/label.ts @@ -10,6 +10,7 @@ export interface Label { val: string neg: boolean cts: string + exp: string | null sig: Buffer | null signingKeyId: number | null } diff --git a/packages/ozone/src/lexicon/lexicons.ts b/packages/ozone/src/lexicon/lexicons.ts index afc7ed866b1..a7934d471d5 100644 --- a/packages/ozone/src/lexicon/lexicons.ts +++ b/packages/ozone/src/lexicon/lexicons.ts @@ -2239,6 +2239,12 @@ export const schemaDict = { format: 'datetime', description: 'Timestamp when this label was created.', }, + exp: { + type: 'string', + format: 'datetime', + description: + 'Timestamp at which this label expires (no longer applies).', + }, sig: { type: 'bytes', description: 'Signature of dag-cbor encoded label.', diff --git a/packages/ozone/src/lexicon/types/com/atproto/label/defs.ts b/packages/ozone/src/lexicon/types/com/atproto/label/defs.ts index 935b2aa4bb3..1af8b0f3890 100644 --- a/packages/ozone/src/lexicon/types/com/atproto/label/defs.ts +++ b/packages/ozone/src/lexicon/types/com/atproto/label/defs.ts @@ -22,6 +22,8 @@ export interface Label { neg?: boolean /** Timestamp when this label was created. */ cts: string + /** Timestamp at which this label expires (no longer applies). */ + exp?: string /** Signature of dag-cbor encoded label. */ sig?: Uint8Array [k: string]: unknown diff --git a/packages/ozone/src/mod-service/util.ts b/packages/ozone/src/mod-service/util.ts index 2fc50974560..63939c27e64 100644 --- a/packages/ozone/src/mod-service/util.ts +++ b/packages/ozone/src/mod-service/util.ts @@ -14,6 +14,7 @@ export const formatLabel = (row: LabelRow): Label => { val: row.val, neg: row.neg, cts: row.cts, + exp: row.exp ?? undefined, sig: row.sig ? new Uint8Array(row.sig) : undefined, }) as Label } @@ -29,6 +30,7 @@ export const formatLabelRow = ( val: label.val, neg: !!label.neg, cts: label.cts, + exp: label.exp ?? null, sig: label.sig ? Buffer.from(label.sig) : null, signingKeyId: signingKeyId ?? null, } @@ -38,7 +40,7 @@ export const signLabel = async ( label: Label, signingKey: Keypair, ): Promise => { - const { ver, src, uri, cid, val, neg, cts } = label + const { ver, src, uri, cid, val, neg, cts, exp } = label const reformatted = noUndefinedVals({ ver: ver ?? 1, src, @@ -47,6 +49,7 @@ export const signLabel = async ( val, neg, cts, + exp, }) as Label const bytes = cborEncode(reformatted) diff --git a/packages/ozone/tests/sequencer.test.ts b/packages/ozone/tests/sequencer.test.ts index 1f5e3dee35c..cab809c34b5 100644 --- a/packages/ozone/tests/sequencer.test.ts +++ b/packages/ozone/tests/sequencer.test.ts @@ -38,6 +38,7 @@ describe('sequencer', () => { id: e.seq, ...label, cid: label.cid ? label.cid : '', + exp: null, sig: label.sig ? Buffer.from(label.sig) : null, signingKeyId: network.ozone.ctx.signingKeyId, } diff --git a/packages/pds/src/lexicon/lexicons.ts b/packages/pds/src/lexicon/lexicons.ts index afc7ed866b1..a7934d471d5 100644 --- a/packages/pds/src/lexicon/lexicons.ts +++ b/packages/pds/src/lexicon/lexicons.ts @@ -2239,6 +2239,12 @@ export const schemaDict = { format: 'datetime', description: 'Timestamp when this label was created.', }, + exp: { + type: 'string', + format: 'datetime', + description: + 'Timestamp at which this label expires (no longer applies).', + }, sig: { type: 'bytes', description: 'Signature of dag-cbor encoded label.', diff --git a/packages/pds/src/lexicon/types/com/atproto/label/defs.ts b/packages/pds/src/lexicon/types/com/atproto/label/defs.ts index 935b2aa4bb3..1af8b0f3890 100644 --- a/packages/pds/src/lexicon/types/com/atproto/label/defs.ts +++ b/packages/pds/src/lexicon/types/com/atproto/label/defs.ts @@ -22,6 +22,8 @@ export interface Label { neg?: boolean /** Timestamp when this label was created. */ cts: string + /** Timestamp at which this label expires (no longer applies). */ + exp?: string /** Signature of dag-cbor encoded label. */ sig?: Uint8Array [k: string]: unknown