-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
grpc_proxy.go
177 lines (150 loc) · 5.63 KB
/
grpc_proxy.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
Copyright 2021 The Dapr 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 messaging
import (
"context"
"errors"
"fmt"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
"github.com/dapr/dapr/pkg/acl"
grpcProxy "github.com/dapr/dapr/pkg/api/grpc/proxy"
codec "github.com/dapr/dapr/pkg/api/grpc/proxy/codec"
"github.com/dapr/dapr/pkg/config"
"github.com/dapr/dapr/pkg/diagnostics"
invokev1 "github.com/dapr/dapr/pkg/messaging/v1"
"github.com/dapr/dapr/pkg/proto/common/v1"
"github.com/dapr/dapr/pkg/resiliency"
"github.com/dapr/dapr/pkg/security"
securityConsts "github.com/dapr/dapr/pkg/security/consts"
)
// Proxy is the interface for a gRPC transparent proxy.
type Proxy interface {
Handler() grpc.StreamHandler
SetRemoteAppFn(func(string) (remoteApp, error))
SetTelemetryFn(func(context.Context) context.Context)
}
type proxy struct {
appID string
appClientFn func() (grpc.ClientConnInterface, error)
connectionFactory messageClientConnection
remoteAppFn func(appID string) (remoteApp, error)
telemetryFn func(context.Context) context.Context
acl *config.AccessControlList
resiliency resiliency.Provider
maxRequestBodySize int
}
// ProxyOpts is the struct with options for NewProxy.
type ProxyOpts struct {
AppClientFn func() (grpc.ClientConnInterface, error)
ConnectionFactory messageClientConnection
AppID string
ACL *config.AccessControlList
Resiliency resiliency.Provider
MaxRequestBodySize int
}
// NewProxy returns a new proxy.
func NewProxy(opts ProxyOpts) Proxy {
return &proxy{
appClientFn: opts.AppClientFn,
appID: opts.AppID,
connectionFactory: opts.ConnectionFactory,
acl: opts.ACL,
resiliency: opts.Resiliency,
maxRequestBodySize: opts.MaxRequestBodySize,
}
}
// Handler returns a Stream Handler for handling requests that arrive for services that are not recognized by the server.
func (p *proxy) Handler() grpc.StreamHandler {
return grpcProxy.TransparentHandler(p.intercept,
func(appID, methodName string) *resiliency.PolicyDefinition {
_, isLocal, err := p.isLocal(appID)
if err == nil && !isLocal {
return p.resiliency.EndpointPolicy(appID, appID+":"+methodName)
}
return resiliency.NoOp{}.EndpointPolicy("", "")
},
grpcProxy.DirectorConnectionFactory(p.connectionFactory),
p.maxRequestBodySize,
)
}
func nopTeardown(destroy bool) {
// Nop
}
func (p *proxy) intercept(ctx context.Context, fullName string) (context.Context, *grpc.ClientConn, *grpcProxy.ProxyTarget, func(destroy bool), error) {
md, _ := metadata.FromIncomingContext(ctx)
v := md[diagnostics.GRPCProxyAppIDKey]
if len(v) == 0 {
return ctx, nil, nil, nopTeardown, fmt.Errorf("failed to proxy request: required metadata %s not found", diagnostics.GRPCProxyAppIDKey)
}
outCtx := metadata.NewOutgoingContext(ctx, md.Copy())
appID := v[0]
if p.remoteAppFn == nil {
return ctx, nil, nil, nopTeardown, errors.New("failed to proxy request: proxy not initialized. daprd startup may be incomplete")
}
target, isLocal, err := p.isLocal(appID)
if err != nil {
return ctx, nil, nil, nopTeardown, err
}
if isLocal {
// proxy locally to the app
if p.acl != nil {
ok, authError := acl.ApplyAccessControlPolicies(ctx, fullName, common.HTTPExtension_NONE, false, p.acl) //nolint:nosnakecase
if !ok {
return ctx, nil, nil, nopTeardown, status.Errorf(codes.PermissionDenied, authError)
}
}
var appClient grpc.ClientConnInterface
appClient, err = p.appClientFn()
if err != nil {
return ctx, nil, nil, nopTeardown, err
}
appMetadataToken := security.GetAppToken()
if appMetadataToken != "" {
outCtx = metadata.AppendToOutgoingContext(outCtx, securityConsts.APITokenHeader, appMetadataToken)
}
return outCtx, appClient.(*grpc.ClientConn), nil, nopTeardown, nil
}
// proxy to a remote daprd
conn, teardown, cErr := p.connectionFactory(outCtx, target.address, target.id, target.namespace,
grpc.WithDefaultCallOptions(grpc.CallContentSubtype((&codec.Proxy{}).Name())),
)
outCtx = p.telemetryFn(outCtx)
outCtx = metadata.AppendToOutgoingContext(outCtx, invokev1.CallerIDHeader, p.appID, invokev1.CalleeIDHeader, target.id)
pt := &grpcProxy.ProxyTarget{
ID: target.id,
Namespace: target.namespace,
Address: target.address,
}
return outCtx, conn, pt, teardown, cErr
}
// SetRemoteAppFn sets a function that helps the proxy resolve an app ID to an actual address.
func (p *proxy) SetRemoteAppFn(remoteAppFn func(appID string) (remoteApp, error)) {
p.remoteAppFn = remoteAppFn
}
// SetTelemetryFn sets a function that enriches the context with telemetry.
func (p *proxy) SetTelemetryFn(spanFn func(context.Context) context.Context) {
p.telemetryFn = spanFn
}
func (p *proxy) isLocal(appID string) (remoteApp, bool, error) {
if p.remoteAppFn == nil {
return remoteApp{}, false, errors.New("failed to proxy request: proxy not initialized; daprd startup may be incomplete")
}
target, err := p.remoteAppFn(appID)
if err != nil {
return remoteApp{}, false, err
}
return target, target.id == p.appID, nil
}