mirror of
https://github.com/tailscale/tailscale.git
synced 2025-12-04 20:09:03 +00:00
ipn/ipn{auth,server}: update ipnauth.Actor to carry a context
The context carries additional information about the actor, such as the request reason, and is canceled when the actor is done. Additionally, we implement three new ipn.Actor types that wrap other actors to modify their behavior: - WithRequestReason, which adds a request reason to the actor; - WithoutClose, which narrows the actor's interface to prevent it from being closed; - WithPolicyChecks, which adds policy checks to the actor's CheckProfileAccess method. Updates #14823 Signed-off-by: Nick Khyl <nickk@tailscale.com>
This commit is contained in:
@@ -4,9 +4,11 @@
|
||||
package ipnauth
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"tailscale.com/client/tailscale/apitype"
|
||||
"tailscale.com/ipn"
|
||||
)
|
||||
|
||||
@@ -32,6 +34,11 @@ type Actor interface {
|
||||
// a connected LocalAPI client. Otherwise, it returns a zero value and false.
|
||||
ClientID() (_ ClientID, ok bool)
|
||||
|
||||
// Context returns the context associated with the actor.
|
||||
// It carries additional information about the actor
|
||||
// and is canceled when the actor is done.
|
||||
Context() context.Context
|
||||
|
||||
// CheckProfileAccess checks whether the actor has the necessary access rights
|
||||
// to perform a given action on the specified Tailscale profile.
|
||||
// It returns an error if access is denied.
|
||||
@@ -102,3 +109,27 @@ func (id ClientID) MarshalJSON() ([]byte, error) {
|
||||
func (id *ClientID) UnmarshalJSON(b []byte) error {
|
||||
return json.Unmarshal(b, &id.v)
|
||||
}
|
||||
|
||||
type actorWithRequestReason struct {
|
||||
Actor
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
// WithRequestReason returns an [Actor] that wraps the given actor and
|
||||
// carries the specified request reason in its context.
|
||||
func WithRequestReason(actor Actor, requestReason string) Actor {
|
||||
ctx := apitype.RequestReasonKey.WithValue(actor.Context(), requestReason)
|
||||
return &actorWithRequestReason{Actor: actor, ctx: ctx}
|
||||
}
|
||||
|
||||
// Context implements [Actor].
|
||||
func (a *actorWithRequestReason) Context() context.Context { return a.ctx }
|
||||
|
||||
type withoutCloseActor struct{ Actor }
|
||||
|
||||
// WithoutClose returns an [Actor] that does not expose the [ActorCloser] interface.
|
||||
// In other words, _, ok := WithoutClose(actor).(ActorCloser) will always be false,
|
||||
// even if the original actor implements [ActorCloser].
|
||||
func WithoutClose(actor Actor) Actor {
|
||||
return withoutCloseActor{actor}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user