@pithy-sh/storage 0.2.10 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/capability.d.ts +12 -3
- package/dist/capability.d.ts.map +1 -1
- package/dist/capability.js +35 -9
- package/dist/config/config.d.ts +20 -4
- package/dist/config/config.d.ts.map +1 -1
- package/dist/config/config.js +23 -6
- package/dist/data/storageObject.d.ts +12 -0
- package/dist/data/storageObject.d.ts.map +1 -1
- package/dist/data/storageObject.js +13 -2
- package/dist/data/tables.js +1 -1
- package/dist/error/errors.js +2 -2
- package/dist/http/handlers.d.ts +48 -16
- package/dist/http/handlers.d.ts.map +1 -1
- package/dist/http/handlers.js +169 -66
- package/dist/http/responses.d.ts +173 -0
- package/dist/http/responses.d.ts.map +1 -0
- package/dist/http/responses.js +92 -0
- package/dist/http/routes.d.ts +13 -2
- package/dist/http/routes.d.ts.map +1 -1
- package/dist/http/routes.js +134 -22
- package/dist/http/schemas.d.ts +6 -0
- package/dist/http/schemas.d.ts.map +1 -1
- package/dist/http/schemas.js +7 -6
- package/dist/http/scopes.d.ts +25 -0
- package/dist/http/scopes.d.ts.map +1 -0
- package/dist/http/scopes.js +32 -0
- package/dist/http/serve.d.ts +5 -2
- package/dist/http/serve.d.ts.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -3
- package/dist/migrations/0002_ownership.d.ts +5 -0
- package/dist/migrations/0002_ownership.d.ts.map +1 -0
- package/dist/migrations/0002_ownership.js +114 -0
- package/dist/object/cloudflare.d.ts +3 -2
- package/dist/object/cloudflare.d.ts.map +1 -1
- package/dist/object/cloudflare.js +3 -3
- package/dist/object/store.d.ts +2 -7
- package/dist/object/store.d.ts.map +1 -1
- package/dist/object/store.js +1 -6
- package/dist/object/uploadedPart.d.ts +11 -0
- package/dist/object/uploadedPart.d.ts.map +1 -0
- package/dist/object/uploadedPart.js +12 -0
- package/dist/provision/provisionStorage.d.ts +14 -2
- package/dist/provision/provisionStorage.d.ts.map +1 -1
- package/dist/provision/provisionStorage.js +19 -4
- package/dist/provision/resolveStorageConfig.js +1 -1
- package/dist/quota/ceiling.d.ts +36 -0
- package/dist/quota/ceiling.d.ts.map +1 -0
- package/dist/quota/ceiling.js +31 -0
- package/dist/quota/quota.d.ts +24 -7
- package/dist/quota/quota.d.ts.map +1 -1
- package/dist/quota/quota.js +40 -12
- package/dist/secret/bundleToken.d.ts +24 -0
- package/dist/secret/bundleToken.d.ts.map +1 -0
- package/dist/secret/bundleToken.js +74 -0
- package/dist/secret/registry.d.ts +5 -1
- package/dist/secret/registry.d.ts.map +1 -1
- package/dist/secret/registry.js +7 -3
- package/dist/seeds/example.d.ts.map +1 -1
- package/dist/seeds/example.js +3 -0
- package/dist/test-utils/liveStorage.d.ts.map +1 -1
- package/dist/test-utils/liveStorage.js +5 -2
- package/dist/version.generated.d.ts +1 -1
- package/dist/version.generated.d.ts.map +1 -1
- package/dist/version.generated.js +1 -1
- package/package.json +7 -7
- package/pithy.manifest.json +20 -10
- package/src/capability.ts +52 -8
- package/src/config/config.ts +24 -6
- package/src/data/storageObject.ts +22 -6
- package/src/error/errors.ts +2 -2
- package/src/http/handlers.ts +215 -81
- package/src/http/responses.ts +168 -0
- package/src/http/routes.ts +245 -46
- package/src/http/schemas.ts +11 -5
- package/src/http/scopes.ts +39 -0
- package/src/http/serve.ts +5 -2
- package/src/index.ts +2 -0
- package/src/migrations/0002_ownership.ts +176 -0
- package/src/object/cloudflare.ts +4 -3
- package/src/object/store.ts +6 -10
- package/src/object/uploadedPart.ts +17 -0
- package/src/provision/provisionStorage.ts +22 -3
- package/src/quota/ceiling.ts +44 -0
- package/src/quota/quota.ts +63 -20
- package/src/secret/bundleToken.ts +81 -0
- package/src/secret/registry.ts +6 -8
- package/src/seeds/example.ts +3 -0
- package/src/test-utils/liveStorage.ts +3 -6
- package/src/version.generated.ts +1 -1
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 Pithy
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
type CapabilitySettings,
|
|
6
|
+
SettingsAccountIncomplete,
|
|
7
|
+
type SettingsFinding,
|
|
8
|
+
type UnansweredEnvironment,
|
|
9
|
+
} from "@pithy-sh/core/src/capability/settings";
|
|
10
|
+
import { remedy } from "@pithy-sh/core/src/cli/command";
|
|
11
|
+
import { LOCAL_ENVIRONMENT } from "@pithy-sh/core/src/naming/environment";
|
|
12
|
+
import { runtimeTokenSecretName } from "@pithy-sh/secrets/src/provision/provisionSecrets";
|
|
13
|
+
import type { ManagedEnvironment } from "@pithy-sh/secrets/src/scope";
|
|
14
|
+
|
|
15
|
+
/** The field an R2 bundle carried a Cloudflare API token in, before #728. */
|
|
16
|
+
export const R2_BUNDLE_TOKEN_FIELD = "apiToken";
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* **The app Workers move first.** A Worker on the kit before #728 requires the token in this bundle, and the
|
|
20
|
+
* new kit reads the old bundle as well as the new one. So deploying the app on this kit, then rewriting, has
|
|
21
|
+
* no gap; the other order has one.
|
|
22
|
+
*/
|
|
23
|
+
const UPGRADE_FIRST = "Deploy your app Workers on this kit first. An older one cannot read the rewritten bundle.";
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* **Nothing to revoke.** The bundle's token was the bootstrap one unless the operator passed another, and the
|
|
27
|
+
* one they passed was the R2 token the S3 key pair derives from: an R2 access key is a Cloudflare API token,
|
|
28
|
+
* its id the key id. Revoking that parent invalidates the pair the rewritten bundle still signs with.
|
|
29
|
+
*/
|
|
30
|
+
const KEEP_THE_TOKEN =
|
|
31
|
+
"Keep that token. It is your bootstrap one, or the R2 token the key pair derives from, and revoking that breaks the pair.";
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* **`pithy doctor`'s check for a Cloudflare API token still stored in an R2 bundle (#728).**
|
|
35
|
+
*
|
|
36
|
+
* Until #728, `pithy storage provision` and `pithy media provision` wrote a Cloudflare API token into the
|
|
37
|
+
* R2 bundle beside the key pair — the bootstrap one, unless the operator passed another. Nothing reads it:
|
|
38
|
+
* the object store signs every call with the pair. It is still a credential the application holds, and a
|
|
39
|
+
* deployed application holds one, `runtime`. The provision rewrites the bundle whole, so re-running it — once
|
|
40
|
+
* the app Workers run this kit — is the fix. The token itself stays: see {@link KEEP_THE_TOKEN}.
|
|
41
|
+
*
|
|
42
|
+
* Asked once `runtime` is minted in an environment, which is the mark that it has taken the upgrade, the
|
|
43
|
+
* way media's check for its own supplied token is. The bundle is sealed under a key that never leaves the
|
|
44
|
+
* manager, so the manager is asked whether it carries the field — a bit, never the value. `dev` has no
|
|
45
|
+
* manager and is never asked.
|
|
46
|
+
*/
|
|
47
|
+
export function bundleTokenCheck(options: { secret: string; provision: string }): CapabilitySettings {
|
|
48
|
+
return {
|
|
49
|
+
local: () => [],
|
|
50
|
+
async account(context) {
|
|
51
|
+
const findings: SettingsFinding[] = [];
|
|
52
|
+
const unanswered: UnansweredEnvironment[] = [];
|
|
53
|
+
const provision = remedy(options.provision, { flags: { "--worker": context.workerDirectory } });
|
|
54
|
+
for (const { name: environment } of context.environments) {
|
|
55
|
+
if (environment === LOCAL_ENVIRONMENT) continue;
|
|
56
|
+
try {
|
|
57
|
+
const runtime = runtimeTokenSecretName(context.project, environment as ManagedEnvironment);
|
|
58
|
+
if (!(await context.account.storeEntry(runtime))) continue;
|
|
59
|
+
const carries = await context.account.vaultSecretField({
|
|
60
|
+
name: options.secret,
|
|
61
|
+
environment,
|
|
62
|
+
field: R2_BUNDLE_TOKEN_FIELD,
|
|
63
|
+
});
|
|
64
|
+
if (!carries) continue;
|
|
65
|
+
} catch (cause) {
|
|
66
|
+
unanswered.push({ environment, cause });
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
findings.push({
|
|
70
|
+
setting: `${options.secret} ${R2_BUNDLE_TOKEN_FIELD}`,
|
|
71
|
+
environment,
|
|
72
|
+
problem: `${options.secret} still holds a Cloudflare API token beside the R2 key pair. Nothing reads it, and the application holds one token: runtime.`,
|
|
73
|
+
action: `${UPGRADE_FIRST} ${provision.say((command) => `Then run ${command} to rewrite the bundle without it.`)} ${KEEP_THE_TOKEN}`,
|
|
74
|
+
...(provision.fix === undefined ? {} : { fix: String(provision.fix) }),
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
if (unanswered.length > 0) throw new SettingsAccountIncomplete(findings, unanswered);
|
|
78
|
+
return findings;
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
}
|
package/src/secret/registry.ts
CHANGED
|
@@ -27,6 +27,11 @@ import { z } from "zod";
|
|
|
27
27
|
* The credentials one R2 bucket is addressed and presigned with. Composed from
|
|
28
28
|
* {@link R2Credentials} rather than redeclaring the key pair — that shape is `@pithy-sh/cloudflare`'s
|
|
29
29
|
* to define, and `CloudflareR2Manager` validates its config against it.
|
|
30
|
+
*
|
|
31
|
+
* **No Cloudflare API token (#728).** A deployed application holds one, `runtime`, and the object store
|
|
32
|
+
* needs none: every operation is S3, signed with the key pair. The bundle carried the token the pair
|
|
33
|
+
* derives from until then; a bundle written before parses, the parse drops it, and the next provision
|
|
34
|
+
* rewrites the bundle without it.
|
|
30
35
|
*/
|
|
31
36
|
export const R2StorageCredentials = R2Credentials.extend({
|
|
32
37
|
accountId: z
|
|
@@ -39,14 +44,7 @@ export const R2StorageCredentials = R2Credentials.extend({
|
|
|
39
44
|
.min(1)
|
|
40
45
|
.meta({ multiline: false })
|
|
41
46
|
.describe("The R2 bucket name every presigned URL and server-side object call targets."),
|
|
42
|
-
|
|
43
|
-
.string()
|
|
44
|
-
.min(1)
|
|
45
|
-
.meta({ multiline: false })
|
|
46
|
-
.describe(
|
|
47
|
-
"The R2-scoped Cloudflare API token the key pair was derived from. Carried alongside because an R2 S3 access key IS a CF API token — the id is the key id, the SHA-256 of the value is the secret — so whatever provisions the pair already holds it, and `CloudflareR2Manager` needs it to prove bucket access.",
|
|
48
|
-
),
|
|
49
|
-
}).describe("The account, key pair, scoped token, and bucket name the object store presigns and addresses R2 with.");
|
|
47
|
+
}).describe("The account, key pair, and bucket name the object store presigns and addresses R2 with.");
|
|
50
48
|
export type R2StorageCredentials = z.output<typeof R2StorageCredentials>;
|
|
51
49
|
|
|
52
50
|
/**
|
package/src/seeds/example.ts
CHANGED
|
@@ -77,6 +77,7 @@ export const storageExampleSeed: SeedSet = defineSeed({
|
|
|
77
77
|
key: demoKey(NOTES_ID),
|
|
78
78
|
path: "notes/analytical-engine.txt",
|
|
79
79
|
ownerId: EXAMPLE_ADA.id,
|
|
80
|
+
organizationId: null,
|
|
80
81
|
contentType: "text/plain",
|
|
81
82
|
size: byteLength(NOTES_ID),
|
|
82
83
|
visibility: "private",
|
|
@@ -91,6 +92,7 @@ export const storageExampleSeed: SeedSet = defineSeed({
|
|
|
91
92
|
key: demoKey(PLAN_ID),
|
|
92
93
|
path: "plans/compiler.txt",
|
|
93
94
|
ownerId: EXAMPLE_GRACE.id,
|
|
95
|
+
organizationId: null,
|
|
94
96
|
contentType: "text/plain",
|
|
95
97
|
size: byteLength(PLAN_ID),
|
|
96
98
|
visibility: "private",
|
|
@@ -105,6 +107,7 @@ export const storageExampleSeed: SeedSet = defineSeed({
|
|
|
105
107
|
key: demoKey(README_ID),
|
|
106
108
|
path: "public/readme.txt",
|
|
107
109
|
ownerId: EXAMPLE_ADA.id,
|
|
110
|
+
organizationId: null,
|
|
108
111
|
contentType: "text/plain",
|
|
109
112
|
size: byteLength(README_ID),
|
|
110
113
|
visibility: "public",
|
|
@@ -12,6 +12,7 @@ import { configureSharedSecrets } from "@pithy-sh/secrets/src/sharedSecretsStore
|
|
|
12
12
|
import { STORAGE_MIGRATION_ORDER } from "../capability";
|
|
13
13
|
import { type StorageDatabase, storageDatabase } from "../data/tables";
|
|
14
14
|
import { storage_0001_objects } from "../migrations/0001_objects";
|
|
15
|
+
import { storage_0002_ownership } from "../migrations/0002_ownership";
|
|
15
16
|
import { ObjectListing, ObjectMetadata, type ObjectStore, objectStore } from "../object/store";
|
|
16
17
|
import { type R2StorageCredentials, STORAGE_R2_SECRET, storageSecretsRegistry } from "../secret/registry";
|
|
17
18
|
|
|
@@ -201,11 +202,7 @@ export async function withLiveBucket<T>(
|
|
|
201
202
|
const bucketName = uniqueName("storage");
|
|
202
203
|
const manager = clients.r2({ ...r2, bucketName });
|
|
203
204
|
const open = new Map<string, OpenUpload>();
|
|
204
|
-
const store = liveObjectStore(
|
|
205
|
-
{ ...r2, accountId: creds.accountId, apiToken: creds.apiToken, bucket: bucketName },
|
|
206
|
-
manager,
|
|
207
|
-
open,
|
|
208
|
-
);
|
|
205
|
+
const store = liveObjectStore({ ...r2, accountId: creds.accountId, bucket: bucketName }, manager, open);
|
|
209
206
|
|
|
210
207
|
return withThrowawayResource(
|
|
211
208
|
() => provisioner.createBucket(bucketName),
|
|
@@ -226,7 +223,7 @@ function storageMigrations() {
|
|
|
226
223
|
database: "app",
|
|
227
224
|
namespace: "storage",
|
|
228
225
|
order: STORAGE_MIGRATION_ORDER,
|
|
229
|
-
migrations: { "0001_objects": storage_0001_objects },
|
|
226
|
+
migrations: { "0001_objects": storage_0001_objects, "0002_ownership": storage_0002_ownership },
|
|
230
227
|
},
|
|
231
228
|
]);
|
|
232
229
|
const provider = registry.app;
|
package/src/version.generated.ts
CHANGED