-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement
admin/upload/inspect
and admin/store/inspect
capa…
…bilities (#918) 1. implementations of new capabilities proposed in storacha/specs#77 2. added a new `getCID` method to each of the `{Upload|Store}Table` interfaces, and updated the in-memory implementations of each. 3. implementations of `admin/upload/inspect` and `admin/store/inspect` invocation handlers 4. tests for all of the above
- Loading branch information
Showing
20 changed files
with
797 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { capability, struct, ok, Link } from '@ucanto/validator' | ||
import { equalWith, and, equal, ProviderDID } from './utils.js' | ||
|
||
export const admin = capability({ | ||
can: 'admin/*', | ||
with: ProviderDID, | ||
derives: equalWith, | ||
}) | ||
|
||
export const upload = { | ||
/** | ||
* Capability can be invoked by a provider to get information about a content CID. | ||
*/ | ||
inspect: capability({ | ||
can: 'admin/upload/inspect', | ||
with: ProviderDID, | ||
nb: struct({ | ||
root: Link, | ||
}), | ||
derives: (child, parent) => { | ||
return ( | ||
and(equalWith(child, parent)) || | ||
and(equal(child.nb.root, parent.nb.root, 'root')) || | ||
ok({}) | ||
) | ||
}, | ||
}), | ||
} | ||
|
||
export const store = { | ||
/** | ||
* Capability can be invoked by a provider to get information an upload shard CID. | ||
*/ | ||
inspect: capability({ | ||
can: 'admin/store/inspect', | ||
with: ProviderDID, | ||
nb: struct({ | ||
link: Link, | ||
}), | ||
derives: (child, parent) => { | ||
return ( | ||
and(equalWith(child, parent)) || | ||
and(equal(child.nb.link, parent.nb.link, 'link')) || | ||
ok({}) | ||
) | ||
}, | ||
}), | ||
} |
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
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,194 @@ | ||
import assert from 'assert' | ||
import { access } from '@ucanto/validator' | ||
import { delegate } from '@ucanto/core' | ||
import { Verifier } from '@ucanto/principal/ed25519' | ||
import * as Admin from '../../src/admin.js' | ||
import { service, alice, readmeCID } from '../helpers/fixtures.js' | ||
|
||
describe('admin/upload/inspect', async function () { | ||
const agent = alice | ||
it('can be invoked by the service on the service', async function () { | ||
const invocation = Admin.upload.inspect.invoke({ | ||
issuer: service, | ||
audience: service, | ||
with: service.did(), | ||
nb: { | ||
root: readmeCID, | ||
}, | ||
}) | ||
const result = await access(await invocation.delegate(), { | ||
capability: Admin.upload.inspect, | ||
principal: Verifier, | ||
authority: service, | ||
}) | ||
if (result.error) { | ||
assert.fail('error in self issue') | ||
} else { | ||
assert.deepEqual(result.ok.audience.did(), service.did()) | ||
assert.equal(result.ok.capability.can, 'admin/upload/inspect') | ||
assert.deepEqual(result.ok.capability.nb, { | ||
root: readmeCID, | ||
}) | ||
} | ||
}) | ||
|
||
it('can be invoked by an agent delegated permissions by the service', async function () { | ||
const auth = Admin.upload.inspect.invoke({ | ||
issuer: agent, | ||
audience: service, | ||
with: service.did(), | ||
nb: { | ||
root: readmeCID, | ||
}, | ||
proofs: [ | ||
await delegate({ | ||
issuer: service, | ||
audience: agent, | ||
capabilities: [{ with: service.did(), can: 'admin/upload/inspect' }], | ||
}), | ||
], | ||
}) | ||
const result = await access(await auth.delegate(), { | ||
capability: Admin.upload.inspect, | ||
principal: Verifier, | ||
authority: service, | ||
}) | ||
if (result.error) { | ||
assert.fail( | ||
`error in self issue: ${JSON.stringify(result.error.message)}` | ||
) | ||
} else { | ||
assert.deepEqual(result.ok.audience.did(), service.did()) | ||
assert.equal(result.ok.capability.can, 'admin/upload/inspect') | ||
assert.deepEqual(result.ok.capability.nb, { | ||
root: readmeCID, | ||
}) | ||
} | ||
}) | ||
|
||
it('fails without a delegation from the service delegation', async function () { | ||
const agent = alice | ||
const auth = Admin.upload.inspect.invoke({ | ||
issuer: agent, | ||
audience: service, | ||
with: service.did(), | ||
nb: { | ||
root: readmeCID, | ||
}, | ||
}) | ||
|
||
const result = await access(await auth.delegate(), { | ||
capability: Admin.upload.inspect, | ||
principal: Verifier, | ||
authority: service, | ||
}) | ||
|
||
assert.equal(result.error?.message.includes('not authorized'), true) | ||
}) | ||
|
||
it('requires nb.root', async function () { | ||
assert.throws(() => { | ||
Admin.upload.inspect.invoke({ | ||
issuer: alice, | ||
audience: service, | ||
with: service.did(), | ||
// @ts-ignore | ||
nb: {}, | ||
}) | ||
}, /Error: Invalid 'nb' - Object contains invalid field "root"/) | ||
}) | ||
}) | ||
|
||
describe('admin/store/inspect', function () { | ||
const agent = alice | ||
it('can be invoked by the service on the service', async function () { | ||
const invocation = Admin.store.inspect.invoke({ | ||
issuer: service, | ||
audience: service, | ||
with: service.did(), | ||
nb: { | ||
link: readmeCID, | ||
}, | ||
}) | ||
const result = await access(await invocation.delegate(), { | ||
capability: Admin.store.inspect, | ||
principal: Verifier, | ||
authority: service, | ||
}) | ||
if (result.error) { | ||
assert.fail('error in self issue') | ||
} else { | ||
assert.deepEqual(result.ok.audience.did(), service.did()) | ||
assert.equal(result.ok.capability.can, 'admin/store/inspect') | ||
assert.deepEqual(result.ok.capability.nb, { | ||
link: readmeCID, | ||
}) | ||
} | ||
}) | ||
|
||
it('can be invoked by an agent delegated permissions by the service', async function () { | ||
const auth = Admin.store.inspect.invoke({ | ||
issuer: agent, | ||
audience: service, | ||
with: service.did(), | ||
nb: { | ||
link: readmeCID, | ||
}, | ||
proofs: [ | ||
await delegate({ | ||
issuer: service, | ||
audience: agent, | ||
capabilities: [{ with: service.did(), can: 'admin/store/inspect' }], | ||
}), | ||
], | ||
}) | ||
const result = await access(await auth.delegate(), { | ||
capability: Admin.store.inspect, | ||
principal: Verifier, | ||
authority: service, | ||
}) | ||
if (result.error) { | ||
assert.fail( | ||
`error in self issue: ${JSON.stringify(result.error.message)}` | ||
) | ||
} else { | ||
assert.deepEqual(result.ok.audience.did(), service.did()) | ||
assert.equal(result.ok.capability.can, 'admin/store/inspect') | ||
assert.deepEqual(result.ok.capability.nb, { | ||
link: readmeCID, | ||
}) | ||
} | ||
}) | ||
|
||
it('fails without a delegation from the service delegation', async function () { | ||
const agent = alice | ||
const auth = Admin.store.inspect.invoke({ | ||
issuer: agent, | ||
audience: service, | ||
with: service.did(), | ||
nb: { | ||
link: readmeCID, | ||
}, | ||
}) | ||
|
||
const result = await access(await auth.delegate(), { | ||
capability: Admin.store.inspect, | ||
principal: Verifier, | ||
authority: service, | ||
}) | ||
|
||
assert.equal(result.error?.message.includes('not authorized'), true) | ||
}) | ||
|
||
it('requires nb.shard', async function () { | ||
assert.throws(() => { | ||
Admin.store.inspect.invoke({ | ||
issuer: alice, | ||
audience: service, | ||
with: service.did(), | ||
// @ts-ignore | ||
nb: {}, | ||
}) | ||
}, /Error: Invalid 'nb' - Object contains invalid field "link"/) | ||
}) | ||
}) |
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,16 @@ | ||
import * as Types from './types.js' | ||
import * as StoreInspect from './admin/store/inspect.js' | ||
import * as UploadInspect from './admin/upload/inspect.js' | ||
|
||
/** | ||
* @param {Types.AdminServiceContext} context | ||
*/ | ||
export const createService = (context) => ({ | ||
store: { | ||
inspect: StoreInspect.provide(context), | ||
}, | ||
|
||
upload: { | ||
inspect: UploadInspect.provide(context), | ||
}, | ||
}) |
Oops, something went wrong.