-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathspace.js
More file actions
200 lines (189 loc) · 5.79 KB
/
space.js
File metadata and controls
200 lines (189 loc) · 5.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/**
* Space Capabilities
*
* These can be imported directly with:
* ```js
* import * as Space from '@storacha/capabilities/space'
* ```
*
* @module
*/
import * as Store from './store.js'
import { capability, Schema, ok, fail } from '@ucanto/validator'
import { SpaceDID, equalWith } from './utils.js'
import * as Upload from './upload.js'
export { top } from './top.js'
// Need this to workaround TS bug
// @see https://github.com/microsoft/TypeScript/issues/51548
export { Store }
export const space = capability({
can: 'space/*',
with: SpaceDID,
derives: equalWith,
})
/**
* `space/info` can be derived from any of the `store/*` or `upload/*`
* capability that has matching `with`. This allows store service
* to identify account based on any user request.
*/
export const info = Store.list
.or(Store.remove)
.or(Upload.add)
.or(Upload.list)
.or(Upload.remove)
.derive({
to: capability({
can: 'space/info',
with: SpaceDID,
}),
derives: equalWith,
})
export const allocate = capability({
can: 'space/allocate',
with: SpaceDID,
nb: Schema.struct({
size: Schema.integer(),
}),
derives: (child, parent) => {
const result = equalWith(child, parent)
if (result.ok) {
return child.nb.size <= parent.nb.size
? ok({})
: fail(
`Claimed size ${child.nb.size} escalates delegated size ${parent.nb.size}`
)
} else {
return result
}
},
})
/**
* The capability grants permission for all content serve operations that fall under the "space/content/serve" namespace.
* It can be derived from any of the `space/*` capability that has matching `with`.
*/
export const contentServe = capability({
can: 'space/content/serve/*',
with: SpaceDID,
derives: equalWith,
})
/**
* Capability can be invoked by an agent to record egress data for a given resource.
* It can be derived from any of the `space/content/serve/*` capability that has matching `with`.
*/
export const egressRecord = capability({
can: 'space/content/serve/egress/record',
with: SpaceDID,
nb: Schema.struct({
/** CID of the resource that was served. */
resource: Schema.link(),
/** Amount of bytes served. */
bytes: Schema.integer().greaterThan(0),
/** Timestamp of the event in milliseconds after Unix epoch. */
servedAt: Schema.integer().greaterThan(-1),
}),
derives: equalWith,
})
/**
* The capability grants permission to decrypt a given resource.
* It can be derived from `space/content/decrypt` capability that has matching `with` and `nb.resource`.
*/
export const decrypt = capability({
can: 'space/content/decrypt',
with: SpaceDID,
nb: Schema.struct({
resource: Schema.link(),
}),
derives: (child, parent) => {
if (child.with !== parent.with) {
return fail(
`Can not derive ${child.can} with ${child.with} from ${parent.with}`
)
}
if (child.nb.resource.toString() !== parent.nb.resource.toString()) {
return fail(
`Can not derive ${child.can} resource ${child.nb.resource} from ${parent.nb.resource}`
)
}
return ok({})
},
})
/**
* "Setup encryption for a Space using asymmetric keys in KMS."
*
* A Principal who may `space/encryption/setup` is permitted to initialize
* encryption for a Space. This generates an RSA key pair in Google KMS
* for the Space and returns the public key that clients can use to encrypt
* per-file symmetric keys.
*
* This operation is idempotent - invoking it the first time generates the
* asymmetric key for the space, but future invocations just return the
* existing public key.
*
* The Space must be provisioned for a paid plan to use encryption.
*/
export const EncryptionSetup = capability({
can: 'space/encryption/setup',
with: SpaceDID,
nb: Schema.struct({
/**
* The location of the KMS key to use for encryption. If not provided, the Storacha Key Manager will use the default location.
*/
location: Schema.string().optional(),
/**
* The keyring of the KMS key to use for encryption. If not provided, the Storacha Key Manager will use the default keyring.
*/
keyring: Schema.string().optional(),
}),
derives: (child, parent) => {
if (child.with !== parent.with) {
return fail(
`Can not derive ${child.can} with ${child.with} from ${parent.with}`
)
}
if (child.nb.location !== parent.nb.location) {
return fail(
`Can not derive ${child.can} location ${child.nb.location} from ${parent.nb.location}`
)
}
if (child.nb.keyring !== parent.nb.keyring) {
return fail(
`Can not derive ${child.can} keyring ${child.nb.keyring} from ${parent.nb.keyring}`
)
}
return ok({})
},
})
/**
* "Decrypt symmetric keys for encrypted content owned by the subject Space."
*
* A Principal who may `space/encryption/key/decrypt` is permitted to decrypt
* the symmetric keys for any encrypted content owned by the Space. This capability
* is used by the gateway to validate that a client has permission to access encrypted
* content and receive the decrypted Data Encryption Keys (DEKs).
*
* The gateway will validate this capability against UCAN delegations before
* providing decrypted Data Encryption Keys (DEKs) to authorized clients.
*/
export const EncryptionKeyDecrypt = capability({
can: 'space/encryption/key/decrypt',
with: SpaceDID,
nb: Schema.struct({
/**
* The encrypted symmetric key to be decrypted
*/
key: Schema.bytes(),
}),
derives: (child, parent) => {
if (child.with !== parent.with) {
return fail(
`Can not derive ${child.can} with ${child.with} from ${parent.with}`
)
}
if (child.nb.key !== parent.nb.key) {
return fail(
`Can not derive ${child.can} key ${child.nb.key} from ${parent.nb.key}`
)
}
return ok({})
},
})