Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: define sync capability per namespace #319

Merged
merged 1 commit into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 60 additions & 6 deletions src/capabilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const BLOCKED_ROLE_ID = '9e6d29263cba36c9'
* @property {string} name
* @property {Record<import('@mapeo/schema').MapeoDoc['schemaName'], DocCapability>} docs
* @property {RoleId[]} roleAssignment
* @property {'allowed' | 'blocked'} sync
* @property {Record<import('./core-manager/core-index.js').Namespace, 'allowed' | 'blocked'>} sync
*/

/**
Expand All @@ -43,7 +43,41 @@ export const CREATOR_CAPABILITIES = {
]
}),
roleAssignment: [COORDINATOR_ROLE_ID, MEMBER_ROLE_ID, BLOCKED_ROLE_ID],
sync: 'allowed',
sync: {
auth: 'allowed',
config: 'allowed',
data: 'allowed',
blobIndex: 'allowed',
blob: 'allowed',
},
}

/**
* These are the capabilities assumed for a device when no capability record can
* be found. This can happen when an invited device did not manage to sync with
* the device that invited them, and they then try to sync with someone else. We
* want them to be able to sync the auth and config store, because that way they
* may be able to receive their role record, and they can get the project config
* so that they can start collecting data.
*
* @type {Capability}
*/
export const NO_ROLE_CAPABILITIES = {
name: 'No Role',
docs: mapObject(currentSchemaVersions, (key) => {
return [
key,
{ readOwn: true, writeOwn: true, readOthers: false, writeOthers: false },
]
}),
roleAssignment: [],
sync: {
auth: 'allowed',
config: 'allowed',
data: 'blocked',
blobIndex: 'blocked',
blob: 'blocked',
},
}

/** @type {Record<RoleId, Capability>} */
Expand All @@ -57,7 +91,13 @@ export const DEFAULT_CAPABILITIES = {
]
}),
roleAssignment: [],
sync: 'allowed',
sync: {
auth: 'allowed',
config: 'allowed',
data: 'allowed',
blobIndex: 'allowed',
blob: 'allowed',
},
},
[COORDINATOR_ROLE_ID]: {
name: 'Coordinator',
Expand All @@ -68,7 +108,13 @@ export const DEFAULT_CAPABILITIES = {
]
}),
roleAssignment: [COORDINATOR_ROLE_ID, MEMBER_ROLE_ID, BLOCKED_ROLE_ID],
sync: 'allowed',
sync: {
auth: 'allowed',
config: 'allowed',
data: 'allowed',
blobIndex: 'allowed',
blob: 'allowed',
},
},
[BLOCKED_ROLE_ID]: {
name: 'Blocked',
Expand All @@ -84,7 +130,13 @@ export const DEFAULT_CAPABILITIES = {
]
}),
roleAssignment: [],
sync: 'blocked',
sync: {
auth: 'blocked',
config: 'blocked',
data: 'blocked',
blobIndex: 'blocked',
blob: 'blocked',
},
},
}

Expand Down Expand Up @@ -135,7 +187,9 @@ export class Capabilities {
if (authCoreId === this.#projectCreatorAuthCoreId) {
return CREATOR_CAPABILITIES
} else {
return DEFAULT_CAPABILITIES[BLOCKED_ROLE_ID]
// When no role assignment exists, e.g. a newly added device which has
// not yet synced role records.
return NO_ROLE_CAPABILITIES
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looking at this just a few lines above:

const authCoreId = await this.#coreOwnership.getCoreId(deviceId, 'auth')

do you have to wrap that in a try/catch, and return this no role capabilities in the case of the catch? with #317, that call may throw an error if the document isn't found.

Basically, should this block look like this?

      try {
        const authCoreId = await this.#coreOwnership.getCoreId(deviceId, 'auth')
        if (authCoreId === this.#projectCreatorAuthCoreId) {
          return CREATOR_CAPABILITIES
        } else {
          // When no role assignment exists, e.g. a newly added device which has
          // not yet synced role records.
          return NO_ROLE_CAPABILITIES
        }
      } catch (err) {
        // When no role assignment exists, e.g. a newly added device which has
        // not yet synced role records.
        return NO_ROLE_CAPABILITIES
      }

}
}
if (!isKnownRoleId(roleId)) {
Expand Down
13 changes: 9 additions & 4 deletions test-e2e/capabilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
DEFAULT_CAPABILITIES,
CREATOR_CAPABILITIES,
MEMBER_ROLE_ID,
BLOCKED_ROLE_ID,
} from '../src/capabilities.js'
import { randomBytes } from 'crypto'

Expand Down Expand Up @@ -58,9 +57,15 @@ test('New device without capabilities', async (t) => {
const ownCapabilities = await project.$getOwnCapabilities()

t.alike(
ownCapabilities,
DEFAULT_CAPABILITIES[BLOCKED_ROLE_ID],
'A new device before sync is blocked'
ownCapabilities.sync,
{
auth: 'allowed',
config: 'allowed',
data: 'blocked',
blobIndex: 'blocked',
blob: 'blocked',
},
'A new device before sync can sync auth and config namespaces, but not other namespaces'
)
await t.exception(async () => {
const deviceId = randomBytes(32).toString('hex')
Expand Down