This repository has been archived by the owner on Apr 7, 2020. It is now read-only.
forked from istio/istio
-
Notifications
You must be signed in to change notification settings - Fork 152
/
server_sink.go
99 lines (86 loc) · 3.27 KB
/
server_sink.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright 2019 Istio Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package sink
import (
"errors"
"io"
"sync/atomic"
"time"
"golang.org/x/time/rate"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/peer"
"google.golang.org/grpc/status"
mcp "istio.io/api/mcp/v1alpha1"
)
// AuthChecker is used to check the transport auth info that is associated with each stream. If the function
// returns nil, then the connection will be allowed. If the function returns an error, then it will be
// percolated up to the gRPC stack.
//
// Note that it is possible that this method can be called with nil authInfo. This can happen either if there
// is no peer info, or if the underlying gRPC stream is insecure. The implementations should be resilient in
// this case and apply appropriate policy.
type AuthChecker interface {
Check(authInfo credentials.AuthInfo) error
}
// Server implements the server for the MCP sink service. The server is the sink and receives configuration
// from the client.
type Server struct {
authCheck AuthChecker
newConnectionLimiter *rate.Limiter
connections int64
sink *Sink
}
var _ mcp.ResourceSinkServer = &Server{}
// ServerOptions contains source server specific options
type ServerOptions struct {
NewConnectionFreq time.Duration
NewConnectionBurstSize int
AuthChecker AuthChecker
}
// NewServer creates a new instance of a MCP sink server.
func NewServer(srcOptions *Options, serverOptions *ServerOptions) *Server {
limiter := rate.NewLimiter(rate.Every(serverOptions.NewConnectionFreq), serverOptions.NewConnectionBurstSize)
s := &Server{
sink: New(srcOptions),
newConnectionLimiter: limiter,
authCheck: serverOptions.AuthChecker,
}
return s
}
// EstablishResourceStream implements the ResourceSinkServer interface.
func (s *Server) EstablishResourceStream(stream mcp.ResourceSink_EstablishResourceStreamServer) error {
// TODO support receiving configuration from multiple sources?
// TODO MVP - limit to one connection at a time?
if !atomic.CompareAndSwapInt64(&s.connections, 0, 1) {
return errors.New("TODO limited to one connection at a time")
}
defer atomic.AddInt64(&s.connections, -1)
// TODO - rate limit new connections?
var authInfo credentials.AuthInfo
if peerInfo, ok := peer.FromContext(stream.Context()); ok {
authInfo = peerInfo.AuthInfo
} else {
scope.Warnf("No peer info found on the incoming stream.")
}
if err := s.authCheck.Check(authInfo); err != nil {
return status.Errorf(codes.Unauthenticated, "Authentication failure: %v", err)
}
err := s.sink.processStream(stream)
code := status.Code(err)
if code == codes.OK || code == codes.Canceled || err == io.EOF {
return nil
}
return err
}