forked from ServiceWeaver/weaver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
remote.go
176 lines (157 loc) · 5.44 KB
/
remote.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
// Copyright 2022 Google LLC
//
// 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 weaver
import (
"context"
"crypto/tls"
"fmt"
"strings"
"sync"
"github.com/ServiceWeaver/weaver/internal/envelope/conn"
"github.com/ServiceWeaver/weaver/internal/net/call"
"github.com/ServiceWeaver/weaver/internal/traceio"
"github.com/ServiceWeaver/weaver/runtime"
"github.com/ServiceWeaver/weaver/runtime/logging"
"github.com/ServiceWeaver/weaver/runtime/protos"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"golang.org/x/exp/slog"
)
// remoteEnv implements the env used for non-single-process Service Weaver applications.
type remoteEnv struct {
sysLogger *slog.Logger
conn *conn.WeaveletConn
logmu sync.Mutex
}
var _ env = &remoteEnv{}
func newRemoteEnv(_ context.Context, bootstrap runtime.Bootstrap, handler conn.WeaveletHandler) (*remoteEnv, error) {
// Create pipe to communicate with the envelope.
toWeavelet, toEnvelope, err := bootstrap.MakePipes()
if err != nil {
return nil, err
}
conn, err := conn.NewWeaveletConn(toWeavelet, toEnvelope, handler)
if err != nil {
return nil, fmt.Errorf("new weavelet conn: %w", err)
}
info := conn.EnvelopeInfo()
env := &remoteEnv{
conn: conn,
}
env.sysLogger = slog.New(&logging.LogHandler{
Opts: logging.Options{
App: info.App,
Deployment: info.DeploymentId,
Component: "weavelet",
Weavelet: info.Id,
Attrs: []string{"serviceweaver/system", ""},
},
Write: env.CreateLogSaver(),
})
return env, nil
}
// EnvelopeInfo implements the Env interface.
func (e *remoteEnv) EnvelopeInfo() *protos.EnvelopeInfo {
return e.conn.EnvelopeInfo()
}
// ServeWeaveletConn implements the Env interface.
func (e *remoteEnv) ServeWeaveletConn() error {
return e.conn.Serve()
}
// ActivateComponent implements the Env interface.
func (e *remoteEnv) ActivateComponent(_ context.Context, component string, routed bool) error {
request := &protos.ActivateComponentRequest{
Component: component,
Routed: routed,
}
return e.conn.ActivateComponentRPC(request)
}
// GetListenerAddress implements the Env interface.
func (e *remoteEnv) GetListenerAddress(_ context.Context, name string) (*protos.GetListenerAddressReply, error) {
request := &protos.GetListenerAddressRequest{Name: name}
return e.conn.GetListenerAddressRPC(request)
}
// ExportListener implements the Env interface.
func (e *remoteEnv) ExportListener(_ context.Context, listener, addr string) (*protos.ExportListenerReply, error) {
request := &protos.ExportListenerRequest{
Listener: listener,
Address: addr,
}
return e.conn.ExportListenerRPC(request)
}
// GetSelfCertificate implements the Env interface.
func (e *remoteEnv) GetSelfCertificate(context.Context) ([]byte, []byte, error) {
request := &protos.GetSelfCertificateRequest{}
reply, err := e.conn.GetSelfCertificateRPC(request)
if err != nil {
return nil, nil, err
}
return reply.Cert, reply.Key, nil
}
// VerifyClientCertificate implements the Env interface.
func (e *remoteEnv) VerifyClientCertificate(_ context.Context, certChain [][]byte) ([]string, error) {
request := &protos.VerifyClientCertificateRequest{CertChain: certChain}
reply, err := e.conn.VerifyClientCertificateRPC(request)
if err != nil {
return nil, err
}
return reply.Components, nil
}
// VerifyServerCertificate implements the Env interface.
func (e *remoteEnv) VerifyServerCertificate(_ context.Context, certChain [][]byte, targetComponent string) error {
request := &protos.VerifyServerCertificateRequest{
CertChain: certChain,
TargetComponent: targetComponent,
}
return e.conn.VerifyServerCertificateRPC(request)
}
// CreateLogSaver implements the Env interface.
func (e *remoteEnv) CreateLogSaver() func(entry *protos.LogEntry) {
return func(entry *protos.LogEntry) {
// Hold lock while creating entry and writing so that records are
// printed in timestamp order, and without their contents getting
// jumbled together.
e.logmu.Lock()
defer e.logmu.Unlock()
e.conn.SendLogEntry(entry) //nolint:errcheck // TODO(mwhittaker): Propagate error.
}
}
// SystemLogger implements the Env interface.
func (e *remoteEnv) SystemLogger() *slog.Logger {
return e.sysLogger
}
// CreateTraceExporter implements the Env interface.
func (e *remoteEnv) CreateTraceExporter() sdktrace.SpanExporter {
return traceio.NewWriter(e.conn.SendTraceSpans)
}
// parseEndpoints parses a list of endpoint addresses into a list of
// call.Endpoints.
func parseEndpoints(addrs []string, config *tls.Config) ([]call.Endpoint, error) {
var endpoints []call.Endpoint
var err error
var ep call.Endpoint
for _, addr := range addrs {
const mtlsPrefix = "mtls://"
if ep, err = call.ParseNetEndpoint(strings.TrimPrefix(addr, mtlsPrefix)); err != nil {
return nil, err
}
if strings.HasPrefix(addr, mtlsPrefix) {
if config == nil {
return nil, fmt.Errorf("mtls protocol requires a non-nil TLS config")
}
ep = call.MTLS(config, ep)
}
endpoints = append(endpoints, ep)
}
return endpoints, nil
}