From a73320a46205df5ec9a280eba229212ed79592cf Mon Sep 17 00:00:00 2001 From: Jeff Davey Date: Thu, 23 Jan 2025 13:52:47 -0700 Subject: [PATCH 1/3] Add a new field, transportSpecific to ServerContext Motivation: Currently there's no way to plumb through details from the transport level to a request handler. Adding this field allows transports, such as the nio transport, to add the peer certificate to the server context when using mTLS. From there there it's easy for an interceptor to take this data and propogate it forward to a request handler. Modifications: This PR adds a single field to the ServerContext that transports can use Result: A new field will be accessible to transports and consumers of the API --- Sources/GRPCCore/Call/Server/ServerContext.swift | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Sources/GRPCCore/Call/Server/ServerContext.swift b/Sources/GRPCCore/Call/Server/ServerContext.swift index d2518adf9..eea48633f 100644 --- a/Sources/GRPCCore/Call/Server/ServerContext.swift +++ b/Sources/GRPCCore/Call/Server/ServerContext.swift @@ -45,6 +45,15 @@ public struct ServerContext: Sendable { /// - "in-process:27182". public var localPeer: String + /// An optional field for transports to store specific data + /// + /// Refer to the transport documentation to understand what type of + /// value this field will contain, if any. + /// + /// An example of what this field can be used for, would be to store + /// things like a peerCertificate from a mTLS connection + public var transportSpecific: (any Sendable)? + /// A handle for checking the cancellation status of an RPC. public var cancellation: RPCCancellationHandle From 3ff823e5e368cef63d908a1b25fb103ad934fab6 Mon Sep 17 00:00:00 2001 From: Jeff Davey <86268978+jtdavey@users.noreply.github.com> Date: Thu, 1 May 2025 10:49:27 -0600 Subject: [PATCH 2/3] Update Sources/GRPCCore/Call/Server/ServerContext.swift Co-authored-by: George Barnett --- Sources/GRPCCore/Call/Server/ServerContext.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/GRPCCore/Call/Server/ServerContext.swift b/Sources/GRPCCore/Call/Server/ServerContext.swift index eea48633f..ac72870b4 100644 --- a/Sources/GRPCCore/Call/Server/ServerContext.swift +++ b/Sources/GRPCCore/Call/Server/ServerContext.swift @@ -51,7 +51,7 @@ public struct ServerContext: Sendable { /// value this field will contain, if any. /// /// An example of what this field can be used for, would be to store - /// things like a peerCertificate from a mTLS connection + /// things like a peer certificate from a mTLS connection public var transportSpecific: (any Sendable)? /// A handle for checking the cancellation status of an RPC. From 188a912a3624026ed2052fba0b6c45477676f2d0 Mon Sep 17 00:00:00 2001 From: Jeff Davey Date: Thu, 1 May 2025 10:52:18 -0600 Subject: [PATCH 3/3] As per review, add a protocol to define the transport field --- Sources/GRPCCore/Call/Server/ServerContext.swift | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Sources/GRPCCore/Call/Server/ServerContext.swift b/Sources/GRPCCore/Call/Server/ServerContext.swift index ac72870b4..f027ad058 100644 --- a/Sources/GRPCCore/Call/Server/ServerContext.swift +++ b/Sources/GRPCCore/Call/Server/ServerContext.swift @@ -16,6 +16,10 @@ /// Additional information about an RPC handled by a server. public struct ServerContext: Sendable { + + /// Protocol used to help identify transport specific context fields + public protocol TransportSpecific: Sendable {} + /// A description of the method being called. public var descriptor: MethodDescriptor @@ -52,7 +56,7 @@ public struct ServerContext: Sendable { /// /// An example of what this field can be used for, would be to store /// things like a peer certificate from a mTLS connection - public var transportSpecific: (any Sendable)? + public var transportSpecific: (any TransportSpecific)? /// A handle for checking the cancellation status of an RPC. public var cancellation: RPCCancellationHandle