Skip to content

Commit

Permalink
etcdmain, proxy: handle authed watch in grpcproxy
Browse files Browse the repository at this point in the history
This commit lets grpcproxy handle authed watch. The main changes are:
1. forwrading a token of a new broadcast client
2. checking permission of a new client that participates to an
   existing broadcast
  • Loading branch information
mitake committed Aug 24, 2017
1 parent ed030b2 commit 10f783e
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
2 changes: 2 additions & 0 deletions etcdmain/grpc_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ func mustNewClient() *clientv3.Client {
}
cfg.DialOptions = append(cfg.DialOptions,
grpc.WithUnaryInterceptor(grpcproxy.AuthUnaryClientInterceptor))
cfg.DialOptions = append(cfg.DialOptions,
grpc.WithStreamInterceptor(grpcproxy.AuthStreamClientInterceptor))
client, err := clientv3.New(*cfg)
if err != nil {
fmt.Fprintln(os.Stderr, err)
Expand Down
9 changes: 9 additions & 0 deletions proxy/grpcproxy/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,12 @@ func AuthUnaryClientInterceptor(ctx context.Context, method string, req, reply i
}
return invoker(ctx, method, req, reply, cc, opts...)
}

func AuthStreamClientInterceptor(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string, streamer grpc.Streamer, opts ...grpc.CallOption) (grpc.ClientStream, error) {
tokenif := ctx.Value("token")
if tokenif != nil {
tokenCred := &proxyTokenCredential{tokenif.(string)}
opts = append(opts, grpc.PerRPCCredentials(tokenCred))
}
return streamer(ctx, desc, cc, method, opts...)
}
23 changes: 23 additions & 0 deletions proxy/grpcproxy/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ type watchProxy struct {

// wg waits until all outstanding watch servers quit.
wg sync.WaitGroup

// kv is used for permission checking
kv clientv3.KV
}

func NewWatchProxy(c *clientv3.Client) (pb.WatchServer, <-chan struct{}) {
Expand All @@ -48,6 +51,8 @@ func NewWatchProxy(c *clientv3.Client) (pb.WatchServer, <-chan struct{}) {
cw: c.Watcher,
ctx: cctx,
leader: newLeader(c.Ctx(), c.Watcher),

kv: c.KV, // for permission checking
}
wp.ranges = newWatchRanges(wp)
ch := make(chan struct{})
Expand Down Expand Up @@ -92,6 +97,7 @@ func (wp *watchProxy) Watch(stream pb.Watch_WatchServer) (err error) {
watchCh: make(chan *pb.WatchResponse, 1024),
ctx: ctx,
cancel: cancel,
kv: wp.kv,
}

var lostLeaderC <-chan struct{}
Expand Down Expand Up @@ -171,6 +177,9 @@ type watchProxyStream struct {

ctx context.Context
cancel context.CancelFunc

// kv is used for permission checking
kv clientv3.KV
}

func (wps *watchProxyStream) close() {
Expand All @@ -192,6 +201,15 @@ func (wps *watchProxyStream) close() {
close(wps.watchCh)
}

func (wps *watchProxyStream) checkPermissionForWatch(key, rangeEnd []byte) error {
req := &pb.RangeRequest{
Key: key,
RangeEnd: rangeEnd,
}
_, err := wps.kv.Do(wps.ctx, RangeRequestToOp(req))
return err
}

func (wps *watchProxyStream) recvLoop() error {
for {
req, err := wps.stream.Recv()
Expand All @@ -201,6 +219,11 @@ func (wps *watchProxyStream) recvLoop() error {
switch uv := req.RequestUnion.(type) {
case *pb.WatchRequest_CreateRequest:
cr := uv.CreateRequest

if err = wps.checkPermissionForWatch(cr.Key, cr.RangeEnd); err != nil {
return err
}

w := &watcher{
wr: watchRange{string(cr.Key), string(cr.RangeEnd)},
id: wps.nextWatcherID,
Expand Down
6 changes: 6 additions & 0 deletions proxy/grpcproxy/watch_broadcast.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ func newWatchBroadcast(wp *watchProxy, w *watcher, update func(*watchBroadcast))
clientv3.WithCreatedNotify(),
}

// Forward a token from client to server.
token := getAuthTokenFromClient(w.wps.stream.Context())
if token != "" {
cctx = context.WithValue(cctx, "token", token)
}

wch := wp.cw.Watch(cctx, w.wr.key, opts...)

for wr := range wch {
Expand Down

0 comments on commit 10f783e

Please sign in to comment.