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

Implement connection props. #3193

Merged
merged 5 commits into from
Dec 11, 2024
Merged

Implement connection props. #3193

merged 5 commits into from
Dec 11, 2024

Conversation

kentonv
Copy link
Member

@kentonv kentonv commented Nov 29, 2024

This adds ctx.props to the ctx object given to WorkerEntrypoints. The property receives metadata about the particular service binding over which the entrypoint was invoked.

class MyEntrypoint extends WorkerEntrypoint {
  foo() {
    console.log("called by: " + this.ctx.props.caller);
  }
}

Service binding declarations in the workerd config may specify what metadata to pass:

bindings = [
  ( name = "FOO",
    service = (
      name = "my-service",
      entrypoint = "MyEntrypoint",
      props = (
        json = `{"caller": "my-calling-service"}
      )
    )
  )
]

Note that "caller" is just an example. The props can contain anything. Use cases include:

  • Authentication of the caller's identity.
  • Authorization / permissions (independent of caller identity).
  • Specifying a particular resource. For example, if the WorkerEntrypoint represents a chat room, props.roomId could be the ID of the specific chat room to access.

This allows service bindings to implement a deeper capability-based security model, where bindings point to specific resources with specific permissions, instead of general APIs.

On Cloudflare, only users who have permission to modify your worker will have permission to create a binding containing arbitrary metadata. Meanwhile we will be creating a mechanism by which you can grant a service binding to your worker to someone, but where you specify the metadata. Thus, you can use the metadata to authenticate requests, without the need for any cryptography.

@kentonv
Copy link
Member Author

kentonv commented Nov 29, 2024

I'm not entirely happy that every CustomEvent implementation had to change here. I might go back and try a different approach to reduce the noise a bit.

@kentonv kentonv force-pushed the kenton/connection-meta branch from 20274fa to bb2b4f6 Compare December 6, 2024 16:30
@kentonv kentonv changed the title [WIP] Implement connection metadata. Implement connection props. Dec 6, 2024
@kentonv kentonv marked this pull request as ready for review December 6, 2024 16:33
@kentonv kentonv requested review from a team as code owners December 6, 2024 16:33
@kentonv kentonv requested review from harrishancock, vickykont and jp4a50 and removed request for harrishancock December 6, 2024 16:33
@kentonv kentonv force-pushed the kenton/connection-meta branch from bb2b4f6 to a03109c Compare December 6, 2024 16:47
@kentonv
Copy link
Member Author

kentonv commented Dec 6, 2024

OK this is ready for review!

It was auto-assigned to @harrishancock but he already owes me other reviews so I removed him and added @jp4a50 instead.

@kentonv kentonv force-pushed the kenton/connection-meta branch 2 times, most recently from b9c6609 to dc08c4f Compare December 6, 2024 19:20
@kentonv
Copy link
Member Author

kentonv commented Dec 6, 2024

Last push was fixup: b9c6609

@kentonv kentonv requested a review from a team as a code owner December 6, 2024 21:19

void visitForGc(jsg::GcVisitor& visitor) {
visitor.visit(props);
}
Copy link
Member

Choose a reason for hiding this comment

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

Non-blocking nit: this should likely implement visitForMemoryInfo(...) also

Copy link
Collaborator

@jp4a50 jp4a50 left a comment

Choose a reason for hiding this comment

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

LGTM

# be an object. Each property in this list must have a `name`. They will be added as properties
# of the object.
#
# If a property in the list concflicts with a property that already exists in the base value,
Copy link
Collaborator

Choose a reason for hiding this comment

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

[typo] concflicts

src/workerd/io/frankenvalue.h Show resolved Hide resolved
JSG_RESOURCE_TYPE(ExecutionContext, CompatibilityFlags::Reader flags) {
JSG_METHOD(waitUntil);
JSG_METHOD(passThroughOnException);
JSG_LAZY_INSTANCE_PROPERTY(props, getProps);
Copy link
Collaborator

Choose a reason for hiding this comment

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

What's the rationale for this being read-write instead of read-only?

Copy link
Member Author

Choose a reason for hiding this comment

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

Performance: A lazy instance property becomes a regular JS property after the first access, which can then be optimized better if it is accessed repeatedly. A regular property would have to call back to C++ for every access.

Copy link
Member

Choose a reason for hiding this comment

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

I think the question was more: Why JSG_LAZY_INSTANCE_PROPERTY and not JSG_LAZY_READYONLY_INSTANCE_PROPERTY... you'd get the same lazy replacement either way. Tho, marking the field read only does have an ever so slight performance impact.

src/workerd/io/worker-entrypoint.c++ Show resolved Hide resolved
@kentonv kentonv force-pushed the kenton/connection-meta branch from 7f6d4cf to 1744eac Compare December 10, 2024 22:17
@kentonv
Copy link
Member Author

kentonv commented Dec 10, 2024

Fixups:

Currently, `ServiceDesignator` can only resolve to one of a fixed set of objects that already exist, but we want to make it a little bit more dynamic in later commits.
This will allow us to extend it to support connection metadata, which might be different for each binding pointing to the same entrypoint.
This adds `ctx.props` to the `ctx` object given to `WorkerEntrypoint`s. The property receives metadata about the particular service binding over which the entrypoint was invoked.

```
class MyEntrypoint extends WorkerEntrypoint {
  foo() {
    console.log("called by: " + this.ctx.props.caller);
  }
}
```

Service binding declarations in the workerd config may specify what metadata to pass:

```
bindings = [
  ( name = "FOO",
    service = (
      name = "my-service",
      entrypoint = "MyEntrypoint",
      props = (
        json = `{"caller": "my-calling-service"}
      )
    )
  )
]
```

Note that "caller" is just an example. The props can contain anything. Use cases include:
* Authentication of the caller's identity.
* Authorization / permissions (independent of caller identity).
* Specifying a particular resource. For example, if the `WorkerEntrypoint` represents a chat room, `props.roomId` could be the ID of the specific chat room to access.

This allows service bindings to implement a deeper capability-based security model, where bindings point to specific resources with specific permissions, instead of general APIs.

On Cloudflare, only users who have permission to modify your worker will have permission to create a binding containing arbitrary metadata. Meanwhile we will be creating a mechanism by which you can grant a service binding to your worker to someone, but where you specify the metadata. Thus, you can use the metadata to authenticate requests, without the need for any cryptography.
@kentonv kentonv force-pushed the kenton/connection-meta branch from 1744eac to c2cce70 Compare December 10, 2024 22:18
//
// This is called `set` because the new property will override any existing property with the
// same name, but note that this strictly appends content. The replacement happens only when the
// Frankenvalue is finally converted to JS.
Copy link
Member

Choose a reason for hiding this comment

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

Non-blocking nit: It's likely worth documenting that toJs(...) will throw if there are properties added but the realized JS value is not actually an object.

Comment on lines +55 to +60
struct Json {
kj::String json;
};
struct V8Serialized {
kj::Array<byte> data;
};
Copy link
Member

Choose a reason for hiding this comment

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

Non-blocking nit: Since these are just single valued, structs this might be a bit cleaner:

using Json = kj::String;
using V8Serialized = kj::Array<kj::byte>;

@kentonv kentonv merged commit 7518bb8 into main Dec 11, 2024
15 checks passed
@kentonv kentonv deleted the kenton/connection-meta branch December 11, 2024 00:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants